clientAI_usehistory.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #include "clientAI.h"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <vector>
  6. #include <string>
  7. #include <cstring>
  8. #include <cmath>
  9. using namespace std;
  10. class simplex {
  11. private:
  12. static const int maxsize = 12;
  13. static const double inf = 1e70;
  14. static const double eps = 1e-13;
  15. double a[maxsize][maxsize], a2[maxsize][maxsize];
  16. double b[maxsize], b2[maxsize], q[maxsize], c[maxsize];
  17. int n, nn[maxsize], bb[maxsize];
  18. double u, min;
  19. private:
  20. int getpos()
  21. {
  22. int ret = 0;
  23. double max = 0;
  24. for (int i = 1; i <= n; ++i)
  25. if (c[i] > max) {
  26. max = c[i];
  27. ret = i;
  28. }
  29. return ret;
  30. }
  31. void pivot(int e, int l)
  32. {
  33. for (int i = 1; i <= n; ++i)
  34. b2[i] = b[i];
  35. b2[l] = b[l] / a[l][e];
  36. for (int i = 1; i <= n; ++i)
  37. for (int j = 1; j <= n; ++j)
  38. a2[i][j] = a[i][j];
  39. for (int j = 1; j <= n; ++j)
  40. if (j != e)
  41. a2[l][j] = a[l][j] / a[l][e];
  42. a2[l][e] = 1 / a[l][e];
  43. for (int i = 1; i <= n; ++i)
  44. if (i != l) {
  45. b2[i] = b[i] - a[i][e] * b2[l];
  46. for (int j = 1; j <= n; ++j)
  47. if (j != e)
  48. a2[i][j] = a[i][j] - a[i][e] * a2[l][j];
  49. a2[i][e] = -a[i][e] / a[l][e];
  50. }
  51. u += c[e] * b2[l];
  52. swap(nn[e], bb[l]);
  53. for (int j = 1; j <= n; ++j)
  54. if (j != e)
  55. c[j] -= c[e] * a2[l][j];
  56. c[e] = -c[e] / a[l][e];
  57. for (int i = 1; i <= n; ++i) {
  58. b[i] = b2[i];
  59. for (int j = 1; j <= n; ++j)
  60. a[i][j] = a2[i][j];
  61. }
  62. }
  63. public:
  64. simplex(vector< vector<double> > p)
  65. {
  66. memset(a, 0, sizeof(a));
  67. memset(a2, 0, sizeof(a2));
  68. memset(b, 0, sizeof(b));
  69. memset(b2, 0, sizeof(b2));
  70. memset(q, 0, sizeof(q));
  71. memset(c, 0, sizeof(c));
  72. memset(nn, 0, sizeof(nn));
  73. memset(bb, 0, sizeof(bb));
  74. u = min = 0;
  75. n = p.size();
  76. for (int i = 1; i <= n; ++i)
  77. for (int j = 1; j <= n; ++j)
  78. a[i][j] = -p[j - 1][i - 1];
  79. }
  80. vector<double> calc()
  81. {
  82. min = -1;
  83. for (int i = 1; i <= n; ++i)
  84. for (int j = 1; j <= n; ++j)
  85. if (a[i][j] - 1 < min)
  86. min = a[i][j] - 1;
  87. for (int i = 1; i <= n; ++i)
  88. for (int j = 1; j <= n; ++j)
  89. a[i][j] -= min;
  90. for (int i = 1; i <= n; ++i) {
  91. bb[i] = n + i;
  92. b[i] = 1;
  93. }
  94. for (int i = 1; i <= n; ++i) {
  95. nn[i] = i;
  96. c[i] = 1;
  97. }
  98. int e = getpos();
  99. double k, t;
  100. while (e > 0) {
  101. k = inf;
  102. int l = 0;
  103. for (int i = 1; i <= n; ++i)
  104. if (a[i][e] > eps) {
  105. t = b[i] / a[i][e];
  106. if (t < k) {
  107. k = t;
  108. l = i;
  109. }
  110. }
  111. //assert(l != 0);
  112. pivot(e, l);
  113. e = getpos();
  114. }
  115. vector<double> ret(n, 0);
  116. for (int i = 1; i <= n; ++i)
  117. if (bb[i] <= n)
  118. ret[bb[i] - 1] = b[i] / u;
  119. ret.push_back(-(min + 1 / u));
  120. return ret;
  121. }
  122. };
  123. const double p[100][8] = {
  124. {0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,1.000000000},
  125. {0.326214077,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.673785923},
  126. {0.360759516,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.639240484},
  127. {0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  128. {1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  129. {0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,1.000000000},
  130. {0.000000000,0.234699425,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.765300575},
  131. {0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  132. {0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  133. {0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  134. {0.000000000,0.000000000,0.000000000,0.000000000,0.430972608,0.000000000,0.000000000,0.569027392},
  135. {0.186779202,0.000000000,0.000000000,0.000000000,0.518296112,0.000000000,0.000000000,0.294924685},
  136. {0.247984354,0.000000000,0.000000000,0.000000000,0.526573867,0.000000000,0.000000000,0.225441779},
  137. {0.195828207,0.000000000,0.212775315,0.000000000,0.485008821,0.000000000,0.000000000,0.106387657},
  138. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  139. {0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.508864848,0.000000000,0.491135152},
  140. {0.000000000,0.170392972,0.000000000,0.000000000,0.000000000,0.560620915,0.000000000,0.268986113},
  141. {0.000000000,0.160958296,0.268899318,0.000000000,0.000000000,0.475152201,0.000000000,0.094990184},
  142. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  143. {0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  144. {0.000000000,0.000000000,0.000000000,0.000000000,0.360759516,0.000000000,0.000000000,0.639240484},
  145. {0.188429176,0.000000000,0.000000000,0.000000000,0.558420581,0.000000000,0.000000000,0.253150243},
  146. {0.000000000,0.212766115,0.000000000,0.000000000,0.426795483,0.000000000,0.000000000,0.360438402},
  147. {0.040984904,0.153643979,0.127244302,0.000000000,0.501479928,0.000000000,0.000000000,0.176646887},
  148. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  149. {0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.471944192,0.000000000,0.528055808},
  150. {0.000000000,0.208009580,0.000000000,0.000000000,0.000000000,0.485924068,0.000000000,0.306066352},
  151. {0.000000000,0.220388626,0.130209468,0.000000000,0.000000000,0.496664921,0.000000000,0.152736986},
  152. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  153. {0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  154. {0.000000000,0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000},
  155. {0.121611094,0.000000000,0.000000000,0.000000000,0.291573724,0.243222189,0.000000000,0.343592993},
  156. {0.247887510,0.002852454,0.000000000,0.000000000,0.132786242,0.319611965,0.000000000,0.296861829},
  157. {0.000000000,0.000000000,0.351552849,0.000000000,0.000000000,0.351552849,0.000000000,0.296894302},
  158. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  159. {0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000},
  160. {0.000000000,0.313924586,0.000000000,0.000000000,0.000000000,0.338534720,0.000000000,0.347540695},
  161. {0.000000000,0.000000000,0.406570036,0.000000000,0.000000000,0.316845991,0.000000000,0.276583973},
  162. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  163. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  164. {1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  165. {0.000000000,0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000},
  166. {0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  167. {0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000},
  168. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  169. {1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  170. {0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000},
  171. {0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  172. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  173. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  174. {0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,1.000000000},
  175. {0.353637024,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.646362976},
  176. {0.471944192,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.528055808},
  177. {0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,1.000000000},
  178. {1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  179. {0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,1.000000000},
  180. {0.000000000,0.346113151,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.653886849},
  181. {0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,1.000000000},
  182. {0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  183. {0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  184. {0.000000000,0.000000000,0.000000000,0.000000000,0.234699425,0.000000000,0.000000000,0.765300575},
  185. {0.199880553,0.000000000,0.000000000,0.000000000,0.477441742,0.000000000,0.000000000,0.322677705},
  186. {0.276066172,0.000000000,0.000000000,0.000000000,0.547303276,0.000000000,0.000000000,0.176630552},
  187. {0.000000000,0.000000000,0.160665079,0.000000000,0.525410335,0.000000000,0.000000000,0.313924586},
  188. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  189. {0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.346113151,0.000000000,0.653886849},
  190. {0.000000000,0.221542882,0.000000000,0.000000000,0.000000000,0.470683420,0.000000000,0.307773698},
  191. {0.000000000,0.249140538,0.000000000,0.000000000,0.000000000,0.529316580,0.000000000,0.221542882},
  192. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  193. {0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  194. {0.000000000,0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000},
  195. {0.094990184,0.000000000,0.000000000,0.000000000,0.234699425,0.306676138,0.000000000,0.363634253},
  196. {0.199915278,0.000000000,0.000000000,0.000000000,0.228805623,0.308153870,0.000000000,0.263125229},
  197. {0.000000000,0.000000000,0.300801436,0.000000000,0.000000000,0.370814689,0.000000000,0.328383875},
  198. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  199. {0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000},
  200. {0.000000000,0.264658290,0.000000000,0.000000000,0.000000000,0.437714865,0.000000000,0.297626844},
  201. {0.000000000,0.000000000,0.333333333,0.000000000,0.000000000,0.333333333,0.000000000,0.333333333},
  202. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  203. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  204. {1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  205. {0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000},
  206. {0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000},
  207. {0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000},
  208. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  209. {1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  210. {0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000},
  211. {0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000},
  212. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  213. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  214. {1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  215. {1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  216. {1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  217. {1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  218. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  219. {1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  220. {1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  221. {1.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  222. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000},
  223. {0.000000000,0.000000000,0.000000000,1.000000000,0.000000000,0.000000000,0.000000000,0.000000000}
  224. };
  225. const double f[100] = {
  226. 0.500000000,0.784513696,0.922261265,1.000000000,1.000000000,
  227. 0.690258965,0.927303957,1.000000000,1.000000000,1.000000000,
  228. 0.215486304,0.500000000,0.680185803,0.878388906,1.000000000,
  229. 0.351248523,0.690258965,0.905009816,1.000000000,1.000000000,
  230. 0.077738735,0.319814197,0.500000000,0.749260036,1.000000000,
  231. 0.165769701,0.543423024,0.800084722,1.000000000,1.000000000,
  232. 0.000000000,0.121611094,0.250739964,0.500000000,1.000000000,
  233. 0.000000000,0.313924586,0.597484603,1.000000000,1.000000000,
  234. 0.000000000,0.000000000,0.000000000,0.000000000,0.500000000,
  235. 0.000000000,0.000000000,0.000000000,0.690258965,0.927303957,
  236. 0.309741035,0.648751477,0.834230299,1.000000000,1.000000000,
  237. 0.500000000,0.826943425,1.000000000,1.000000000,1.000000000,
  238. 0.072696043,0.309741035,0.456576976,0.686075414,1.000000000,
  239. 0.173056575,0.500000000,0.735341710,1.000000000,1.000000000,
  240. 0.000000000,0.094990184,0.199915278,0.402515397,1.000000000,
  241. 0.000000000,0.264658290,0.500000000,1.000000000,1.000000000,
  242. 0.000000000,0.000000000,0.000000000,0.000000000,0.309741035,
  243. 0.000000000,0.000000000,0.000000000,0.500000000,0.826943425,
  244. 0.000000000,0.000000000,0.000000000,0.000000000,0.072696043,
  245. 0.000000000,0.000000000,0.000000000,0.173056575,0.500000000
  246. };
  247. const int stateNum = 100;
  248. const int actionNum = 8;
  249. const int Dmp[2][actionNum] = {{-1, -2, -3, -4, 0, -1, -2, 1},
  250. {-5, -1, -2, -3, -5, 0, -5, 1}};
  251. int cntRand, randNum[100000];
  252. int e[stateNum][actionNum][actionNum];
  253. int win[stateNum], lose[stateNum];
  254. vector<int> history;
  255. int Set, cntlose;
  256. bool useHistory;
  257. int calc(int mymp, int mylv, int opmp, int oplv, ACTION myact, ACTION opact)
  258. {
  259. bool myerr = mymp + Dmp[mylv][myact] < 0;
  260. bool operr = opmp + Dmp[oplv][opact] < 0;
  261. if (myerr && operr) return -3;
  262. if (myerr) return -4;
  263. if (operr) return -5;
  264. if (myact < 4 && opact < 4) {
  265. if (myact > opact) return -1;
  266. if (myact < opact) return -2;
  267. }else {
  268. if (myact < 2 && opact >= 6) return -1;
  269. if (myact == ATTACK_3 && opact != DEFEND_2) return -1;
  270. if (myact == ATTACK_4) return -1;
  271. if (opact < 2 && myact >= 6) return -2;
  272. if (opact == ATTACK_3 && myact != DEFEND_2) return -2;
  273. if (opact == ATTACK_4) return -2;
  274. }
  275. mymp += Dmp[mylv][myact];
  276. if (myact == RAMPAGE) ++mylv;
  277. opmp += Dmp[oplv][opact];
  278. if (opact == RAMPAGE) ++oplv;
  279. if (mymp > 4) mymp = 4;
  280. if (opmp > 4) opmp = 4;
  281. return mymp + mylv * 5 + opmp * 10 + oplv * 50;
  282. }
  283. void init(int totalSet) {
  284. srand(((time(NULL)) + 5) % 10000);
  285. cntRand = 0;
  286. for (int i = 0; i < 100000; i ++)
  287. randNum[i] = (rand() + rand() + rand() + rand()) % 10000;
  288. useHistory = false;
  289. cntlose = 0;
  290. memset(win, 0, sizeof(win));
  291. memset(lose, 0, sizeof(lose));
  292. for (int i = 0; i < stateNum; ++i)
  293. for (int j = 0; j < actionNum; ++j)
  294. for (int k = 0; k < actionNum; ++k)
  295. e[i][j][k] = calc(i % 5, i % 10 / 5, i / 10 % 5, i / 50, (enum ACTION)j, (enum ACTION)k);
  296. }
  297. void beginSet(int cntSet) {
  298. Set = cntSet;
  299. history.clear();
  300. }
  301. void endSet(int result) {
  302. if (result == 2) {
  303. ++cntlose;
  304. if (cntlose == 2) {
  305. useHistory = !useHistory;
  306. cntlose = 0;
  307. }
  308. }else
  309. cntlose = 0;
  310. for (int i = 0; i < history.size(); ++i) {
  311. if (result == 0 || result == 1)
  312. ++win[history[i]];
  313. if (result == 0 || result == 2)
  314. ++lose[history[i]];
  315. }
  316. }
  317. ACTION getAction(int cntRound, int myMP, int myState, int opMP, int opState, ACTION opAction)
  318. {
  319. if (myMP > 4) myMP = 4;
  320. if (opMP > 4) opMP = 4;
  321. int S = myMP + myState * 5 + opMP * 10 + opState * 50;
  322. history.push_back(S);
  323. double P = randNum[cntRand++] * 10000 + randNum[cntRand++] + 1;
  324. //probs: when to use the study strayge
  325. if (useHistory) {
  326. vector< vector<double> > c(actionNum, vector<double>(actionNum, 0));
  327. for (int i = 0; i < actionNum; ++i)
  328. for (int j = 0; j < actionNum; ++j) {
  329. int k = e[S][i][j];
  330. if (k >= 0)
  331. if (win[k] > 0 && lose[k] > 0)
  332. c[i][j] = sqrt(f[k] * win[k] / (win[k] + lose[k]));
  333. else
  334. c[i][j] = f[k];
  335. else
  336. if (k == -1 || k == -5)
  337. c[i][j] = 1;
  338. else
  339. c[i][j] = 0;
  340. }
  341. vector<double> ret = simplex(c).calc();
  342. for (int i = 0; i < actionNum; ++i) {
  343. P -= 1e8 * ret[i];
  344. if (P <= 0 && myMP + Dmp[myState][i] >= 0) return (enum ACTION)(i);
  345. }
  346. }else
  347. for (int i = 0; i < 8; ++i) {
  348. P -= 1e8 * p[S][i];
  349. if (P <= 0 && myMP + Dmp[myState][i] >= 0) return (enum ACTION)(i);
  350. }
  351. return RESTORE;
  352. }