clientAI.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 f[100] = {
  124. 0.500000000,0.784513696,0.922261265,1.000000000,1.000000000,
  125. 0.690258965,0.927303957,1.000000000,1.000000000,1.000000000,
  126. 0.215486304,0.500000000,0.680185803,0.878388906,1.000000000,
  127. 0.351248523,0.690258965,0.905009816,1.000000000,1.000000000,
  128. 0.077738735,0.319814197,0.500000000,0.749260036,1.000000000,
  129. 0.165769701,0.543423024,0.800084722,1.000000000,1.000000000,
  130. 0.000000000,0.121611094,0.250739964,0.500000000,1.000000000,
  131. 0.000000000,0.313924586,0.597484603,1.000000000,1.000000000,
  132. 0.000000000,0.000000000,0.000000000,0.000000000,0.500000000,
  133. 0.000000000,0.000000000,0.000000000,0.690258965,0.927303957,
  134. 0.309741035,0.648751477,0.834230299,1.000000000,1.000000000,
  135. 0.500000000,0.826943425,1.000000000,1.000000000,1.000000000,
  136. 0.072696043,0.309741035,0.456576976,0.686075414,1.000000000,
  137. 0.173056575,0.500000000,0.735341710,1.000000000,1.000000000,
  138. 0.000000000,0.094990184,0.199915278,0.402515397,1.000000000,
  139. 0.000000000,0.264658290,0.500000000,1.000000000,1.000000000,
  140. 0.000000000,0.000000000,0.000000000,0.000000000,0.309741035,
  141. 0.000000000,0.000000000,0.000000000,0.500000000,0.826943425,
  142. 0.000000000,0.000000000,0.000000000,0.000000000,0.072696043,
  143. 0.000000000,0.000000000,0.000000000,0.173056575,0.500000000
  144. };
  145. const int stateNum = 100;
  146. const int actionNum = 8;
  147. const int Dmp[2][actionNum] = {{-1, -2, -3, -4, 0, -1, -2, 1},
  148. {-5, -1, -2, -3, -5, 0, -5, 1}};
  149. int cntRand, randNum[100000];
  150. int e[stateNum][actionNum][actionNum];
  151. int win[stateNum][actionNum], lose[stateNum][actionNum];
  152. vector< pair<int, int> > history;
  153. int calc(int mymp, int mylv, int opmp, int oplv, ACTION myact, ACTION opact)
  154. {
  155. bool myerr = mymp + Dmp[mylv][myact] < 0;
  156. bool operr = opmp + Dmp[oplv][opact] < 0;
  157. if (myerr && operr) return -3;
  158. if (myerr) return -4;
  159. if (operr) return -5;
  160. if (myact < 4 && opact < 4) {
  161. if (myact > opact) return -1;
  162. if (myact < opact) return -2;
  163. }else {
  164. if (myact < 2 && opact >= 6) return -1;
  165. if (myact == ATTACK_3 && opact != DEFEND_2) return -1;
  166. if (myact == ATTACK_4) return -1;
  167. if (opact < 2 && myact >= 6) return -2;
  168. if (opact == ATTACK_3 && myact != DEFEND_2) return -2;
  169. if (opact == ATTACK_4) return -2;
  170. }
  171. mymp += Dmp[mylv][myact];
  172. if (myact == RAMPAGE) ++mylv;
  173. opmp += Dmp[oplv][opact];
  174. if (opact == RAMPAGE) ++oplv;
  175. if (mymp > 4) mymp = 4;
  176. if (opmp > 4) opmp = 4;
  177. return mymp + mylv * 5 + opmp * 10 + oplv * 50;
  178. }
  179. void init(int totalSet) {
  180. srand(((time(NULL)) + 5) % 10000);
  181. cntRand = 0;
  182. for (int i = 0; i < 100000; i ++)
  183. randNum[i] = (rand() + rand() + rand() + rand()) % 10000;
  184. memset(win, 0, sizeof(win));
  185. memset(lose, 0, sizeof(lose));
  186. for (int i = 0; i < stateNum; ++i)
  187. for (int j = 0; j < actionNum; ++j)
  188. for (int k = 0; k < actionNum; ++k)
  189. e[i][j][k] = calc(i % 5, i % 10 / 5, i / 10 % 5, i / 50, (enum ACTION)j, (enum ACTION)k);
  190. }
  191. void beginSet(int cntSet) {
  192. history.clear();
  193. }
  194. void endSet(int result) {
  195. for (int i = 0; i < history.size(); ++i) {
  196. if (result == 0 || result == 1)
  197. ++win[history[i].first][history[i].second];
  198. if (result == 0 || result == 2)
  199. ++lose[history[i].first][history[i].second];
  200. }
  201. }
  202. ACTION getAction(int cntRound, int myMP, int myState, int opMP, int opState, ACTION opAction)
  203. {
  204. if (myMP > 4) myMP = 4;
  205. if (opMP > 4) opMP = 4;
  206. int S = myMP + myState * 5 + opMP * 10 + opState * 50;
  207. vector< vector<double> > c(actionNum, vector<double>(actionNum, 0));
  208. for (int i = 0; i < actionNum; ++i)
  209. for (int j = 0; j < actionNum; ++j) {
  210. int k = e[S][i][j];
  211. if (k >= 0)
  212. c[i][j] = f[k];
  213. else
  214. if (k == -1 || k == -5)
  215. c[i][j] = 1;
  216. else
  217. c[i][j] = 0;
  218. if (i == 4)
  219. c[i][j] *= 2;
  220. /*
  221. if (win[S][i] > 5 && lose[S][i] > 5 && f[S] > 0.05)
  222. c[i][j] = c[i][j] * win[S][i] / (win[S][i] + lose[S][i]) / f[S];
  223. */
  224. }
  225. cout << fixed;
  226. cout.precision(7);
  227. for (int i = 0; i < actionNum; ++i) if (i == 0 || i == 4 || i == 7) {
  228. for (int j = 0; j < actionNum; ++j) if (j == 0 || j == 4 || j == 7)
  229. cout << c[i][j] << "\t";
  230. cout << endl;
  231. }
  232. vector<double> ret = simplex(c).calc();
  233. cout << ret[0] << " " << ret[4] << " " << ret[7] << endl;
  234. cout << ret[0] * c[0][0] + ret[4] * c[4][0] + ret[7] * c[7][0] << endl;
  235. cout << ret[0] * c[0][4] + ret[4] * c[4][4] + ret[7] * c[7][4] << endl;
  236. cout << ret[0] * c[0][7] + ret[4] * c[4][7] + ret[7] * c[7][7] << endl;
  237. system("pause");
  238. double P = randNum[cntRand++] * 10000 + randNum[cntRand++] + 1;
  239. for (int i = 0; i < actionNum; ++i) {
  240. P -= 1e8 * ret[i];
  241. if (P <= 0 && myMP + Dmp[myState][i] >= 0) {
  242. history.push_back(make_pair(S, i));
  243. return (enum ACTION)(i);
  244. }
  245. }
  246. history.push_back(make_pair(S, RESTORE));
  247. return RESTORE;
  248. }
  249. int main()
  250. {
  251. init(0);
  252. win[11][DEFEND_1] = 100;
  253. lose[11][DEFEND_1] = 70;
  254. ACTION t = getAction(0, 1, 0, 1, 0, RESTORE);
  255. return 0;
  256. }