From 901501bb6ce9b0fad30b0046a426d1c7bedd2974 Mon Sep 17 00:00:00 2001 From: Mark Bluemer Date: Mon, 24 Apr 2017 14:46:24 -0400 Subject: [PATCH] named and cleaned up heuristics --- include/Heuristic.hpp | 12 +++--- src/Heuristic.cpp | 92 +++++++++++++++++-------------------------- 2 files changed, 42 insertions(+), 62 deletions(-) diff --git a/include/Heuristic.hpp b/include/Heuristic.hpp index c440fe3..37b43f5 100644 --- a/include/Heuristic.hpp +++ b/include/Heuristic.hpp @@ -13,12 +13,12 @@ class Heuristic double kingCount() const; int safePawns() const; int safeKings() const; - int h5() const; - int h6() const; - int h7() const; - int h8() const; - int h9() const; - int h10() const; + int pawnPromotionDistance() const; + int defenderCount() const; + int attackerCount() const; + int openPromotionCount() const; + int moveablePawns() const; + int moveableKings() const; const int count(uint32_t i) const; diff --git a/src/Heuristic.cpp b/src/Heuristic.cpp index b940baf..5ff06dd 100644 --- a/src/Heuristic.cpp +++ b/src/Heuristic.cpp @@ -25,19 +25,16 @@ int Heuristic::evaluate(bup &bitboard){ coeff[0] * pawnCount() + coeff[1] * kingCount() + coeff[2] * safePawns() + - coeff[3] * safeKings();// + - - // Test game was taking too long, let just do 4 for now... - - // coeff[4]*h5() + - //coeff[5]*h6() + - //coeff[6]*h7() + - //coeff[7]*h8() + - //coeff[8]*h9() + - //coeff[9]*h10() ; + coeff[3] * safeKings() + + coeff[4] * pawnPromotionDistance() + + coeff[5] * defenderCount() + + coeff[6] * attackerCount() + + coeff[7] * openPromotionCount() + + coeff[8] * moveablePawns() + + coeff[9] * moveableKings(); } -// Number of pawns +// Number of pawns int Heuristic::pawnCount() const { return count(b & ~k) - count(w & ~k); @@ -52,17 +49,17 @@ double Heuristic::kingCount() const // Number of safe pawns int Heuristic::safePawns() const { - return count((active & ~k) & BB::edgeLoc); + return (isBlacksTurn ? 1 : -1) * count((active & ~k) & BB::edgeLoc); } // Number of safe kings int Heuristic::safeKings() const { - return 1.5 * count((active & k) & BB::edgeLoc); + return (isBlacksTurn ? 1 : -1) * 1.5 * count((active & k) & BB::edgeLoc); } // Aggregated distance of pawns to promotion line -int Heuristic::h5() const +int Heuristic::pawnPromotionDistance() const { int aggrDistance = 0; if(isBlacksTurn) @@ -70,86 +67,69 @@ int Heuristic::h5() const aggrDistance += (7-i)*count(b & k & BB::rowMask(i)); else for(int i=1; i<8; i++) - aggrDistance += i*count(w & k & BB::rowMask(i)); + aggrDistance -= i*count(w & k & BB::rowMask(i)); return aggrDistance; } // Number of defender pieces, closest two BB::rows on players side -int Heuristic::h6() const +int Heuristic::defenderCount() const { if(isBlacksTurn) return count(b & (BB::rowMask(0) | BB::rowMask(1))); else - return count(w & (BB::rowMask(6) | BB::rowMask(7))); + return -1 * count(w & (BB::rowMask(6) | BB::rowMask(7))); } // Number of attacking pawns, position in upper three most BB::rows, opponents side -int Heuristic::h7() const +int Heuristic::attackerCount() const { if(isBlacksTurn) return count(b & ~k & ( BB::rowMask(7) | BB::rowMask(6) | BB::rowMask(5))); else - return count(w & ~k & ( BB::rowMask(2) | BB::rowMask(1) | BB::rowMask(0))); + return -1 * count(w & ~k & ( BB::rowMask(2) | BB::rowMask(1) | BB::rowMask(0))); } // Number of unoccupied fields on promotion line -int Heuristic::h8() const +int Heuristic::openPromotionCount() const { if(isBlacksTurn) - return count( (b | w) & BB::rowMask(7)); + return count( ~(b | w) & BB::rowMask(7)); else - return count( (b | w) & BB::rowMask(0)); + return -1 * count( ~(b | w) & BB::rowMask(0)); } // Number of moveable pawns -int Heuristic::h9() const +int Heuristic::moveablePawns() const { - uint32_t accumP; + uint32_t accumP = 0; uint32_t emptyPos = ~(b | w); - - // these heuristics are not calculated correctly, TODO if(isBlacksTurn) { - accumP |= board->rotr32(board->rotr32(b & ~k & BB::downLeft, 1) & emptyPos, -1); - accumP |= board->rotl32(board->rotl32(b & ~k & BB::downRight, 1) & emptyPos, -1); - - + accumP |= (board->rotr32(emptyPos, 7) & BB::downRight) & (b & ~k); + accumP |= (board->rotr32(emptyPos, 1) & BB::downLeft) & (b & ~k); } else { - accumP |= board->rotr32(board->rotr32(w & ~k & BB::upRight, -1) & emptyPos, 1); - accumP |= board->rotl32(board->rotl32(w & ~k & BB::upLeft, -1) & emptyPos, 1); + accumP |= (board->rotl32(emptyPos, 7) & BB::upLeft) & (w & ~k); + accumP |= (board->rotl32(emptyPos, 1) & BB::upRight) & (w & ~k); } - return count(accumP); + return (isBlacksTurn ? 1 : -1) * count(accumP); } -// Number of moveable kings -int Heuristic::h10() const +// Number of moveable (not including jumps) kings +int Heuristic::moveableKings() const { - - // these heuristics are not calculated correctly, TODO - - uint32_t accumK; - uint32_t kings; + uint32_t accumK = 0; + uint32_t kings = active & k; uint32_t emptyPos = ~(b | w); - if(isBlacksTurn) - { - kings = b & k; - accumK |= board->rotr32(board->rotr32(kings & BB::downLeft, 1) & emptyPos, -1); - accumK |= board->rotl32(board->rotl32(kings & BB::downRight, 1) & emptyPos, -1); - accumK |= board->rotr32(board->rotr32(kings & BB::upRight, -1) & emptyPos, 1); - accumK |= board->rotl32(board->rotl32(kings & BB::upLeft, -1) & emptyPos, 1); - } - else { - kings = w & k; - accumK |= board->rotr32(board->rotr32(kings & BB::downLeft, 1) & emptyPos, -1); - accumK |= board->rotl32(board->rotl32(kings & BB::downRight, 1) & emptyPos, -1); - accumK |= board->rotr32(board->rotr32(kings & BB::upRight, -1) & emptyPos, 1); - accumK |= board->rotl32(board->rotl32(kings & BB::upLeft, -1) & emptyPos, 1); - } - return count(accumK); + accumK |= (board->rotr32(emptyPos, 7) & BB::downRight) & kings; + accumK |= (board->rotr32(emptyPos, 1) & BB::downLeft) & kings; + accumK |= (board->rotl32(emptyPos, 7) & BB::upLeft) & kings; + accumK |= (board->rotl32(emptyPos, 1) & BB::upRight) & kings; + + return (isBlacksTurn ? 1 : -1) * count(accumK); }