Minimax.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. #include "Minimax.h"
  2. #include "ChessGame.h"
  3. #include "Search.h"
  4. #include <iostream>
  5. #include <functional>
  6. #include <limits>
  7. #include <chrono>
  8. using namespace chessy;
  9. /*size_t chessy::perft(std::ostream& out, ChessGame& chessGame, int depth)
  10. {
  11. size_t result = 0;
  12. std::function<void(const MoveInfo&, ChessGame&, int, bool)> searcher;
  13. std::function<void(ChessGame&, int, bool)> iterate;
  14. // apply a move and search deeper
  15. searcher = [&result, &iterate] (const MoveInfo& mi, ChessGame& cg,
  16. int depth, bool print)
  17. {
  18. if (depth > 1) {
  19. UndoInfo ui = cg.doMove(mi);
  20. size_t interRes = result;
  21. iterate(cg, depth - 1, false);
  22. if (print)
  23. std::cout << mi.move.asString() << ": " <<
  24. (result - interRes) << std::endl;
  25. cg.undoMove(ui);
  26. }
  27. else {
  28. if (print)
  29. std::cout << mi.move.asString() << ": " <<
  30. 1 << std::endl;
  31. result++;
  32. }
  33. };
  34. // iterate through all positions in the current position
  35. iterate = [&searcher] (ChessGame& cg, int depth, bool print)
  36. {
  37. Search<decltype(searcher)&> search (searcher, cg);
  38. if (cg.getTurn() == WHITE_SIDE)
  39. search.iterateAll<WHITE_SIDE>(cg, depth, print);
  40. else
  41. search.iterateAll<BLACK_SIDE>(cg, depth, print);
  42. };
  43. using namespace std::chrono;
  44. auto begin = high_resolution_clock::now();
  45. iterate(chessGame, depth, true);
  46. auto end = high_resolution_clock::now();
  47. auto millis = duration_cast<milliseconds>(end - begin);
  48. double seconds = millis.count() / 1000.0;
  49. size_t nodesPerSecond = (result * 1000 * 1000 * 1000) /
  50. duration_cast<nanoseconds>(end - begin).count();
  51. out << "Searched " << result << " nodes in " << seconds <<
  52. " seconds. (at " << nodesPerSecond << " nodes per second)" << std::endl;
  53. return result;
  54. }*/
  55. size_t chessy::perft(std::ostream& out, ChessGame& cg, int depth)
  56. {
  57. std::function<size_t(bool, int)> iterate = [&](bool print, int depth) -> size_t {
  58. if (depth <= 0)
  59. return 1;
  60. std::vector<Move> moves;
  61. moves.reserve(50);
  62. if (cg.getTurn() == WHITE_SIDE)
  63. generateAllMoves<WHITE_SIDE, false>(cg, moves);
  64. else
  65. generateAllMoves<BLACK_SIDE, false>(cg, moves);
  66. size_t sum = 0;
  67. for (Move move : moves) {
  68. MoveInfo mi{ move, cg };
  69. UndoInfo ui = cg.doMove(mi);
  70. size_t v = iterate(false, depth - 1);
  71. if (print)
  72. out << mi.move.asString() << ": " << v << std::endl;
  73. sum += v;
  74. cg.undoMove(ui);
  75. }
  76. return sum;
  77. };
  78. using namespace std::chrono;
  79. auto begin = high_resolution_clock::now();
  80. size_t sum = iterate(true, depth);
  81. auto end = high_resolution_clock::now();
  82. auto millis = duration_cast<milliseconds>(end - begin);
  83. double seconds = millis.count() / 1000.0;
  84. size_t nodesPerSecond = (sum * 1000 * 1000 * 1000) /
  85. duration_cast<nanoseconds>(end - begin).count();
  86. out << "Searched " << sum << " nodes in " << seconds <<
  87. " seconds. (at " << nodesPerSecond << " nodes per second)" << std::endl;
  88. return sum;
  89. }
  90. #include <iostream>
  91. using namespace std;
  92. std::pair<Move, MoveValue> chessy::miniMax(ChessGame& cg, int depth)
  93. {
  94. std::vector<Move> moves;
  95. moves.reserve(200);
  96. if (cg.getTurn() == WHITE_SIDE) {
  97. generateAllMoves<WHITE_SIDE, false>(cg, moves);
  98. orderMoves<WHITE_SIDE>(cg, moves);
  99. }
  100. else {
  101. generateAllMoves<BLACK_SIDE, false>(cg, moves);
  102. orderMoves<BLACK_SIDE>(cg, moves);
  103. }
  104. const Board& b = cg.getBoard();
  105. auto isCheck = [&cg, &b] () {
  106. return cg.getTurn() == BLACK_SIDE ? b.isCheck<WHITE_SIDE>() :
  107. b.isCheck<BLACK_SIDE>();
  108. };
  109. Move bestMove{ 0, 0 };
  110. MoveValue alpha = -1e+30;
  111. MoveValue beta = 1e+30;
  112. Move lastValidMove = bestMove;
  113. for (Move move : moves) {
  114. MoveInfo mi{ move, cg };
  115. UndoInfo ui = cg.doMove(mi);
  116. MoveValue val;
  117. if (isCheck()) {
  118. cg.undoMove(ui);
  119. continue;
  120. }
  121. else {
  122. lastValidMove = move;
  123. val = -negamaxImplementation(cg, depth - 1, -beta, -alpha);
  124. }
  125. //cout << move.asString() << ": " << val << endl;
  126. cg.undoMove(ui);
  127. if(val > alpha) {
  128. alpha = val;
  129. bestMove = move;
  130. }
  131. }
  132. if (bestMove.origin == 0 && bestMove.destination == 0) {
  133. bestMove = lastValidMove;
  134. }
  135. return { bestMove, alpha }; //negamaxImplementation(chessGame, 5);
  136. }
  137. MoveValue chessy::negamaxImplementation(ChessGame& cg, int depth,
  138. chessy::MoveValue alpha, chessy::MoveValue beta)
  139. {
  140. if (depth <= 0)
  141. //return quiescence(cg, 2, -beta, -alpha);
  142. return evaluate(cg.getTurn(), cg);
  143. const Board& b = cg.getBoard();
  144. auto isCheck = [&cg, &b] (Side turn) {
  145. return turn == BLACK_SIDE ? b.isCheck<WHITE_SIDE>() :
  146. b.isCheck<BLACK_SIDE>();
  147. };
  148. std::vector<Move> moves;
  149. moves.reserve(200);
  150. if (cg.getTurn() == WHITE_SIDE) {
  151. generateAllMoves<WHITE_SIDE, false>(cg, moves);
  152. orderMoves<WHITE_SIDE>(cg, moves);
  153. }
  154. else {
  155. generateAllMoves<BLACK_SIDE, false>(cg, moves);
  156. orderMoves<BLACK_SIDE>(cg, moves);
  157. }
  158. bool thereIsMove = false;
  159. for (Move move : moves) {
  160. MoveInfo mi{ move, cg };
  161. UndoInfo ui = cg.doMove(mi);
  162. MoveValue val;
  163. if (isCheck(cg.getTurn()))
  164. val = -1e+30;
  165. else {
  166. val = -negamaxImplementation(cg, depth - 1, -beta, -alpha);
  167. thereIsMove = true;
  168. }
  169. cg.undoMove(ui);
  170. if(val >= beta)
  171. return beta;
  172. if(val > alpha)
  173. alpha = val;
  174. }
  175. if (!thereIsMove)
  176. return isCheck(otherSide(cg.getTurn())) ? -1e+30 : 0.0;
  177. return alpha;
  178. }
  179. MoveValue chessy::quiescence(ChessGame& cg, int maxDepth,
  180. MoveValue alpha, MoveValue beta)
  181. {
  182. if (maxDepth <= 0)
  183. return evaluate(cg.getTurn(), cg);
  184. const Board& b = cg.getBoard();
  185. auto isCheck = [&cg, &b] (Side turn) {
  186. return turn == BLACK_SIDE ? b.isCheck<WHITE_SIDE>() :
  187. b.isCheck<BLACK_SIDE>();
  188. };
  189. std::vector<Move> moves;
  190. moves.reserve(50);
  191. if (cg.getTurn() == WHITE_SIDE)
  192. generateAllMoves<WHITE_SIDE, true>(cg, moves);
  193. else
  194. generateAllMoves<BLACK_SIDE, true>(cg, moves);
  195. bool thereIsMove = false;
  196. for (Move move : moves) {
  197. MoveInfo mi{ move, cg };
  198. UndoInfo ui = cg.doMove(mi);
  199. MoveValue val;
  200. if (isCheck(cg.getTurn()))
  201. val = -1e+30;
  202. else {
  203. val = -quiescence(cg, maxDepth - 1, -beta, -alpha);
  204. thereIsMove = true;
  205. }
  206. cg.undoMove(ui);
  207. if(val >= beta)
  208. return beta;
  209. if(val > alpha)
  210. alpha = val;
  211. }
  212. if (!thereIsMove)
  213. return isCheck(otherSide(cg.getTurn())) ? -1e+30 : 0.0;
  214. return alpha;
  215. }
  216. template<Side side>
  217. MoveValue chessy::evaluatePositives(const ChessGame& game)
  218. {
  219. MoveValue piecePoints = 0;
  220. const Board& bd = game.getBoard();
  221. Bitboard p = bd.getPawns<side>();
  222. Bitboard n = bd.getKnights<side>();
  223. Bitboard b = bd.getBishops<side>();
  224. Bitboard r = bd.getRooks<side>();
  225. Bitboard q = bd.getQueens<side>();
  226. Bitboard k = bd.getKing<side>();
  227. piecePoints += 1 * p.popcount();
  228. piecePoints += 3 * n.popcount();
  229. piecePoints += 3 * b.popcount();
  230. piecePoints += 5 * r.popcount();
  231. piecePoints += 9 * q.popcount();
  232. for (auto knight : PositionSet{ n })
  233. piecePoints += KnightMoveGenerator{ knight, bd.get<side>() }.getBitboard().popcount() * 0.05;
  234. for (auto bishop : PositionSet{ b })
  235. piecePoints += PrimitiveBishopMoveGenerator{ bishop, bd.get<otherSide(side)>(), bd.get<side>() }.getBitboard().popcount() * 0.03;
  236. for (auto rook : PositionSet{ r })
  237. piecePoints += PrimitiveRookMoveGenerator{ rook, bd.get<otherSide(side)>(), bd.get<side>() }.getBitboard().popcount() * 0.03;
  238. for (auto queen : PositionSet{ q })
  239. piecePoints += PrimitiveQueenMoveGenerator{ queen, bd.get<otherSide(side)>(), bd.get<side>() }.getBitboard().popcount() * 0.02;
  240. return piecePoints;
  241. /*
  242. constexpr Side other = otherSide(side);
  243. p = bd.getPawns<other>();
  244. n = bd.getKnights<other>();
  245. b = bd.getBishops<other>();
  246. r = bd.getRooks<other>();
  247. q = bd.getQueens<other>();
  248. k = bd.getKing<other>();
  249. piecePoints -= 1 * p.popcount();
  250. piecePoints -= 3 * n.popcount();
  251. piecePoints -= 3 * b.popcount();
  252. piecePoints -= 5 * r.popcount();
  253. piecePoints -= 9 * q.popcount();
  254. //for (auto knight : PositionSet{ n })
  255. // piecePoints -= KnightMoveGenerator{ knight, bd.get<otherSide(side)>() }.getBitboard().popcount() * 0.2;
  256. for (auto bishop : PositionSet{ b })
  257. piecePoints -= PrimitiveBishopMoveGenerator{ bishop, bd.get<side>(), bd.get<otherSide(side)>() }.getBitboard().popcount() * 0.2;
  258. */
  259. }
  260. MoveValue chessy::evaluate(Side side, const ChessGame& game)
  261. {
  262. if (side == WHITE_SIDE)
  263. return evaluatePositives<WHITE_SIDE>(game) -
  264. evaluatePositives<BLACK_SIDE>(game);
  265. else
  266. return evaluatePositives<BLACK_SIDE>(game) -
  267. evaluatePositives<WHITE_SIDE>(game);
  268. }
  269. /*
  270. template MiniMax::BestMove MiniMax::minimax<WHITE_SIDE>(int);
  271. template MiniMax::BestMove MiniMax::minimax<BLACK_SIDE>(int);
  272. Move MiniMax::calculateBest(int depth)
  273. {
  274. if (game.getTurn() == WHITE_SIDE) {
  275. BestMove bm = minimax<WHITE_SIDE>(depth);
  276. return bm.move;
  277. }
  278. else
  279. return minimax<BLACK_SIDE>(depth).move;
  280. }
  281. template<Side side>
  282. float MiniMax::evaluate(void) const
  283. {
  284. int piecePoints = 0;
  285. Board& bd = game.getBoard();
  286. Bitboard p = bd.getPawns<side>();
  287. Bitboard n = bd.getKnights<side>();
  288. Bitboard b = bd.getBishops<side>();
  289. Bitboard r = bd.getRooks<side>();
  290. Bitboard q = bd.getQueens<side>();
  291. Bitboard k = bd.getKing<side>();
  292. piecePoints += 1 * p.popcount();
  293. piecePoints += 3 * n.popcount();
  294. piecePoints += 3 * b.popcount();
  295. piecePoints += 4 * r.popcount();
  296. piecePoints += 6 * q.popcount();
  297. if (k == Bitboard(0ULL))
  298. piecePoints -= 100000;
  299. const Side other = otherSide(side);
  300. p = bd.getPawns<other>();
  301. n = bd.getKnights<other>();
  302. b = bd.getBishops<other>();
  303. r = bd.getRooks<other>();
  304. q = bd.getQueens<other>();
  305. k = bd.getKing<other>();
  306. piecePoints -= 1 * p.popcount();
  307. piecePoints -= 3 * n.popcount();
  308. piecePoints -= 3 * b.popcount();
  309. piecePoints -= 4 * r.popcount();
  310. piecePoints -= 6 * q.popcount();
  311. if (k == Bitboard(0ULL))
  312. piecePoints += 100000;
  313. return piecePoints;
  314. }
  315. template<Side side>
  316. MiniMax::BestMove MiniMax::minimax(int depth)
  317. {
  318. if (depth == 0) {
  319. return { {0, 0}, -evaluate<side>() };
  320. }
  321. BestMove bestMove = { {0, 0}, -std::numeric_limits<float>::infinity() };
  322. Board& board = game.getBoard();
  323. Bitboard friends;
  324. Bitboard enemies;
  325. if (side == WHITE_SIDE) {
  326. friends = board.getWhites();
  327. enemies = board.getBlacks();
  328. }
  329. else {
  330. friends = board.getBlacks();
  331. enemies = board.getWhites();
  332. }
  333. const Board temp = board;
  334. PawnPushGenerator<side> mg{ game };
  335. for (Move push : mg) {
  336. Bitboard& pawns = board.getPawns<side>();
  337. Bitboard pTemp = pawns;
  338. pawns.applyMove(push);
  339. BestMove m = minimax<otherSide(side)>(depth - 1);
  340. m.move = push;
  341. bestMove.overwriteIfBetter(m);
  342. pawns = pTemp;
  343. }
  344. PawnDoublePushGenerator<side> dp{ game };
  345. for (Move push : dp) {
  346. Bitboard& pawns = board.getPawns<side>();
  347. Bitboard pTemp = pawns;
  348. pawns.applyMove(push);
  349. BestMove m = minimax<otherSide(side)>(depth - 1);
  350. m.move = push;
  351. bestMove.overwriteIfBetter(m);
  352. pawns = pTemp;
  353. }
  354. PawnCaptureGenerator<side, LEFT> pl{ game };
  355. for (Move capture : pl) {
  356. Bitboard& pawns = board.getPawns<side>();
  357. board.removeAt(capture.destination);
  358. pawns.applyMove(capture);
  359. BestMove m = minimax<otherSide(side)>(depth - 1);
  360. m.move = capture;
  361. bestMove.overwriteIfBetter(m);
  362. board = temp;
  363. }
  364. PawnCaptureGenerator<side, RIGHT> pr{ game };
  365. for (Move capture : pr) {
  366. Bitboard& pawns = board.getPawns<side>();
  367. board.removeAt(capture.destination);
  368. pawns.applyMove(capture);
  369. BestMove m = minimax<otherSide(side)>(depth - 1);
  370. m.move = capture;
  371. bestMove.overwriteIfBetter(m);
  372. board = temp;
  373. }
  374. PromotionGenerator<side> pg{ game };
  375. for (Move promotion : pg) {
  376. Bitboard& pawns = board.getPawns<side>();
  377. board.removeAt(promotion.destination);
  378. pawns.applyMove(promotion);
  379. BestMove m = minimax<otherSide(side)>(depth - 1);
  380. m.move = promotion;
  381. bestMove.overwriteIfBetter(m);
  382. board = temp;
  383. }
  384. Bitboard& ns = board.getKnights<side>();
  385. PositionSet knights { ns };
  386. for (auto knight : knights) {
  387. for (auto pos : KnightMoveGenerator{ knight, friends }) {
  388. Move move = { knight, pos };
  389. board.removeAt(move.destination);
  390. ns.applyMove(move);
  391. BestMove m = minimax<otherSide(side)>(depth - 1);
  392. m.move = move;
  393. bestMove.overwriteIfBetter(m);
  394. board = temp;
  395. }
  396. }
  397. Bitboard& bs = board.getBishops<side>();
  398. PositionSet bishops { bs };
  399. for (auto bishop : bishops) {
  400. for (auto pos : PrimitiveBishopMoveGenerator{ bishop, enemies, friends }) {
  401. Move move = { bishop, pos };
  402. board.removeAt(move.destination);
  403. bs.applyMove(move);
  404. BestMove m = minimax<otherSide(side)>(depth - 1);
  405. m.move = move;
  406. bestMove.overwriteIfBetter(m);
  407. board = temp;
  408. }
  409. }
  410. Bitboard& rs = board.getRooks<side>();
  411. PositionSet rooks { rs };
  412. for (auto rook : rooks) {
  413. for (auto pos : PrimitiveRookMoveGenerator{ rook, enemies, friends }) {
  414. Move move = { rook, pos };
  415. board.removeAt(move.destination);
  416. rs.applyMove(move);
  417. BestMove m = minimax<otherSide(side)>(depth - 1);
  418. m.move = move;
  419. bestMove.overwriteIfBetter(m);
  420. board = temp;
  421. }
  422. }
  423. Bitboard& qs = board.getQueens<side>();
  424. PositionSet queens { qs };
  425. for (auto queen : queens) {
  426. for (auto pos : PrimitiveQueenMoveGenerator{ queen, enemies, friends }) {
  427. Move move = { queen, pos };
  428. board.removeAt(move.destination);
  429. qs.applyMove(move);
  430. BestMove m = minimax<otherSide(side)>(depth - 1);
  431. m.move = move;
  432. bestMove.overwriteIfBetter(m);
  433. board = temp;
  434. }
  435. }
  436. Bitboard& king = board.getKing<side>();
  437. Index kingIndex = king.getLeastSignificantBit();
  438. for (auto pos : KingMoveGenerator{ king, friends }) {
  439. Move move = { kingIndex, pos };
  440. board.removeAt(pos);
  441. king.applyMove(move);
  442. BestMove m = minimax<otherSide(side)>(depth - 1);
  443. m.move = move;
  444. //if (depth >= 3)
  445. // std::cout << m.move.asString() << " " << bestMove.value << " -> " << m.value << std::endl;
  446. bestMove.overwriteIfBetter(m);
  447. board = temp;
  448. }
  449. if (side == WHITE_SIDE) {
  450. if (game.getCanCastleKingSide(side)) {
  451. if((board.getWhites().bits & 0x6) == 0) {
  452. Move kingMove = {3, 1};
  453. Move rookMove = {0, 2};
  454. king.applyMove(kingMove);
  455. rs.applyMove(rookMove);
  456. board = temp;
  457. }
  458. }
  459. }
  460. for (auto pos : CastlingGenerator<side>{ game }) {
  461. Move move = { kingIndex, pos };
  462. king.applyMove(move);
  463. BestMove m = minimax<otherSide(side)>(depth - 1);
  464. m.move = move;
  465. //if (depth >= 3)
  466. // std::cout << m.move.asString() << " " << bestMove.value << " -> " << m.value << std::endl;
  467. bestMove.overwriteIfBetter(m);
  468. board = temp;
  469. }
  470. float v = evaluate<side>();
  471. bestMove.value *= 0.9f;
  472. bestMove.value += v * 0.2f;
  473. return -bestMove;
  474. }
  475. */