Skip to content

Commit

Permalink
Partial work on neural net feature cleanup and new ladder features
Browse files Browse the repository at this point in the history
  • Loading branch information
lightvector committed Sep 3, 2018
1 parent 2bb4eae commit 4935e65
Show file tree
Hide file tree
Showing 12 changed files with 402 additions and 91 deletions.
4 changes: 2 additions & 2 deletions cpp/game/board.cpp
Expand Up @@ -1356,7 +1356,7 @@ void Board::calculateArea(Color* result, bool nonPassAliveStones, bool safeBigTe
}

//This marks pass-alive stones, pass-alive territory always.
//If safeBorderedBigTerritories, marks empty regions bordered by pla stones and no opp stones, where all pla stones are pass-alive.
//If safeBigTerritories, marks empty regions bordered by pla stones and no opp stones, where all pla stones are pass-alive.
//If unsafeBigTerritories, marks empty regions bordered by pla stones and no opp stones, regardless.
void Board::calculateAreaForPla(Player pla, bool safeBigTerritories, bool unsafeBigTerritories, bool isMultiStoneSuicideLegal, Color* result) const {
Color opp = getOpp(pla);
Expand Down Expand Up @@ -1893,7 +1893,7 @@ void Board::printBoard(ostream& out, const Board& board, Loc markLoc, const vect
}
}
}

if(x < board.x_size-1 && !histMarked)
out << ' ';
}
Expand Down
8 changes: 4 additions & 4 deletions cpp/game/board.h
Expand Up @@ -125,7 +125,7 @@ struct Board
};

//Constructors---------------------------------
Board(); //Create Board of size (19,19), multi-stone-suicide illegal
Board(); //Create Board of size (19,19)
Board(int x, int y); //Create Fastboard of size (x,y)
Board(const Board& other);

Expand Down Expand Up @@ -185,9 +185,9 @@ struct Board
bool searchIsLadderCaptured(Loc loc, bool defenderFirst, vector<Loc>& buf);
bool searchIsLadderCapturedAttackerFirst2Libs(Loc loc, vector<Loc>& buf, vector<Loc>& workingMoves);

//If a point is a pass-alive stone or pass-alive territory for a color, mark it that color
//If nonPassAliveStones, also marks non-pass-alive stones
//If safeBorderedBigTerritories, also marks for each pla empty regions bordered by pla stones and no opp stones, where all pla stones are pass-alive.
//If a point is a pass-alive stone or pass-alive territory for a color, mark it that color.
//If nonPassAliveStones, also marks non-pass-alive stones that are not part of the opposing pass-alive territory.
//If safeBigTerritories, also marks for each pla empty regions bordered by pla stones and no opp stones, where all pla stones are pass-alive.
//If unsafeBigTerritories, also marks for each pla empty regions bordered by pla stones and no opp stones, regardless.
//All other points are marked as C_EMPTY.
//[result] must be a buffer of size MAX_ARR_SIZE and will get filled with the result
Expand Down
29 changes: 28 additions & 1 deletion cpp/game/boardhistory.cpp
Expand Up @@ -19,6 +19,8 @@ BoardHistory::BoardHistory()
:rules(),
moveHistory(),koHashHistory(),
koHistoryLastClearedBeginningMoveIdx(0),
recentBoards(),
currentRecentBoardIdx(0),
consecutiveEndingPasses(0),
hashesAfterBlackPass(),hashesAfterWhitePass(),
encorePhase(0),koProhibitHash(),
Expand All @@ -40,6 +42,8 @@ BoardHistory::BoardHistory(const Board& board, Player pla, const Rules& r)
:rules(r),
moveHistory(),koHashHistory(),
koHistoryLastClearedBeginningMoveIdx(0),
recentBoards(),
currentRecentBoardIdx(0),
consecutiveEndingPasses(0),
hashesAfterBlackPass(),hashesAfterWhitePass(),
encorePhase(0),koProhibitHash(),
Expand All @@ -60,6 +64,8 @@ BoardHistory::BoardHistory(const BoardHistory& other)
:rules(other.rules),
moveHistory(other.moveHistory),koHashHistory(other.koHashHistory),
koHistoryLastClearedBeginningMoveIdx(other.koHistoryLastClearedBeginningMoveIdx),
recentBoards(other.recentBoards),
currentRecentBoardIdx(other.currentRecentBoardIdx),
consecutiveEndingPasses(other.consecutiveEndingPasses),
hashesAfterBlackPass(other.hashesAfterBlackPass),hashesAfterWhitePass(other.hashesAfterWhitePass),
encorePhase(other.encorePhase),koProhibitHash(other.koProhibitHash),
Expand All @@ -83,6 +89,8 @@ BoardHistory& BoardHistory::operator=(const BoardHistory& other)
moveHistory = other.moveHistory;
koHashHistory = other.koHashHistory;
koHistoryLastClearedBeginningMoveIdx = other.koHistoryLastClearedBeginningMoveIdx;
std::copy(other.recentBoards, other.recentBoards+NUM_RECENT_BOARDS, recentBoards);
currentRecentBoardIdx = other.currentRecentBoardIdx;
std::copy(other.wasEverOccupiedOrPlayed, other.wasEverOccupiedOrPlayed+Board::MAX_ARR_SIZE, wasEverOccupiedOrPlayed);
std::copy(other.superKoBanned, other.superKoBanned+Board::MAX_ARR_SIZE, superKoBanned);
consecutiveEndingPasses = other.consecutiveEndingPasses;
Expand All @@ -107,6 +115,8 @@ BoardHistory::BoardHistory(BoardHistory&& other) noexcept
:rules(other.rules),
moveHistory(std::move(other.moveHistory)),koHashHistory(std::move(other.koHashHistory)),
koHistoryLastClearedBeginningMoveIdx(other.koHistoryLastClearedBeginningMoveIdx),
recentBoards(other.recentBoards),
currentRecentBoardIdx(other.currentRecentBoardIdx),
consecutiveEndingPasses(other.consecutiveEndingPasses),
hashesAfterBlackPass(std::move(other.hashesAfterBlackPass)),hashesAfterWhitePass(std::move(other.hashesAfterWhitePass)),
encorePhase(other.encorePhase),koProhibitHash(other.koProhibitHash),
Expand All @@ -127,6 +137,8 @@ BoardHistory& BoardHistory::operator=(BoardHistory&& other) noexcept
moveHistory = std::move(other.moveHistory);
koHashHistory = std::move(other.koHashHistory);
koHistoryLastClearedBeginningMoveIdx = other.koHistoryLastClearedBeginningMoveIdx;
std::copy(other.recentBoards, other.recentBoards+NUM_RECENT_BOARDS, recentBoards);
currentRecentBoardIdx = other.currentRecentBoardIdx;
std::copy(other.wasEverOccupiedOrPlayed, other.wasEverOccupiedOrPlayed+Board::MAX_ARR_SIZE, wasEverOccupiedOrPlayed);
std::copy(other.superKoBanned, other.superKoBanned+Board::MAX_ARR_SIZE, superKoBanned);
consecutiveEndingPasses = other.consecutiveEndingPasses;
Expand All @@ -153,6 +165,10 @@ void BoardHistory::clear(const Board& board, Player pla, const Rules& r) {
koHashHistory.clear();
koHistoryLastClearedBeginningMoveIdx = 0;

for(int i = 0; i<NUM_RECENT_BOARDS; i++)
recentBoards[i] = board;
currentRecentBoardIdx = 0;

for(int y = 0; y<board.y_size; y++) {
for(int x = 0; x<board.x_size; x++) {
Loc loc = Location::getLoc(x,y,board.x_size);
Expand Down Expand Up @@ -197,6 +213,13 @@ void BoardHistory::clear(const Board& board, Player pla, const Rules& r) {
koHashHistory.push_back(getKoHash(rules,board,pla,encorePhase,koProhibitHash));
}

const Board& BoardHistory::getRecentBoard(int numMovesAgo) const {
assert(numMoveAgo >= 0 && numMovesAgo < NUM_RECENT_BOARDS);
int idx = (currentRecentBoardIdx + numMovesAgo) % NUM_RECENT_BOARDS;
return recentBoards[idx];
}


void BoardHistory::setKomi(float newKomi) {
rules.komi = newKomi;
isGameFinished = false;
Expand Down Expand Up @@ -386,6 +409,10 @@ void BoardHistory::makeBoardMoveAssumeLegal(Board& board, Loc moveLoc, Player mo
}
}

//Update recent boards
currentRecentBoardIdx = (currentRecentBoardIdx + 1) % NUM_RECENT_BOARDS;
recentBoards[currentRecentBoardIdx] = board;

//Passes clear ko history in the main phase with spight ko rules and in the encore
//This lifts bans in spight ko rules and lifts 3-fold-repetition checking in the encore for no-resultifying infinite cycles
//They also clear in simple ko rules for the purpose of no-resulting long cycles, long cycles with passes do not no-result.
Expand Down Expand Up @@ -524,7 +551,7 @@ void BoardHistory::makeBoardMoveAssumeLegal(Board& board, Loc moveLoc, Player mo
std::fill(whiteKoProhibited, whiteKoProhibited+Board::MAX_ARR_SIZE, false);
koProhibitHash = Hash128();
koCapturesInEncore.clear();

koHashHistory.clear();
koHistoryLastClearedBeginningMoveIdx = moveHistory.size();
koHashHistory.push_back(getKoHash(rules,board,getOpp(movePla),encorePhase,koProhibitHash));
Expand Down
8 changes: 8 additions & 0 deletions cpp/game/boardhistory.h
Expand Up @@ -19,6 +19,10 @@ struct BoardHistory {
vector<Hash128> koHashHistory;
int koHistoryLastClearedBeginningMoveIdx;

static const int NUM_RECENT_BOARDS = 3;
Board recentBoards[NUM_RECENT_BOARDS];
int currentRecentBoardIdx;

//Did this board location ever have a stone there before, or was it ever played?
//(Also includes locations of suicides)
bool wasEverOccupiedOrPlayed[Board::MAX_ARR_SIZE];
Expand Down Expand Up @@ -74,6 +78,10 @@ struct BoardHistory {

float currentSelfKomi(Player pla) const;

//Returns a reference a recent board state, where 0 is the current board, 1 is 1 move ago, etc.
//Requires that numMovesAgo < NUM_RECENT_BOARDS
const Board& getRecentBoard(int numMovesAgo) const;

//Check if a move on the board is legal, taking into account the full game state and superko
bool isLegal(const Board& board, Loc moveLoc, Player movePla) const;

Expand Down

0 comments on commit 4935e65

Please sign in to comment.