Skip to content

Commit

Permalink
named and cleaned up heuristics
Browse files Browse the repository at this point in the history
  • Loading branch information
mbluemer committed Apr 24, 2017
1 parent ae13be0 commit 901501b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 62 deletions.
12 changes: 6 additions & 6 deletions include/Heuristic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
92 changes: 36 additions & 56 deletions src/Heuristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -52,104 +49,87 @@ 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)
for(int i=0; i<7; i++)
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);
}


0 comments on commit 901501b

Please sign in to comment.