Skip to content

Commit

Permalink
Added transposition table
Browse files Browse the repository at this point in the history
  • Loading branch information
skotz committed Dec 17, 2016
1 parent 0d97dfa commit 7697565
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
30 changes: 27 additions & 3 deletions Chase.Engine/Search.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Chase.Engine.Interfaces;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
Expand All @@ -10,7 +11,9 @@ namespace Chase.Engine
{
public class Search : ISearchAlgorithm
{
private int evaluations;
private long evaluations;

private long hashLookups;

private Stopwatch timer;

Expand All @@ -22,16 +25,21 @@ public class Search : ISearchAlgorithm

private string levelOneNode;

private Dictionary<ulong, SearchResult> hashtable;

public event EventHandler<SearchStatus> OnNewResult;

public SearchResult GetBestMove(Position position, SearchArgs settings)
{
SearchResult result = new SearchResult();

hashtable = new Dictionary<ulong, SearchResult>();

timer = Stopwatch.StartNew();
cutoff = false;
evaluations = 0;
timeLimitMilliseconds = settings.MaxSeconds * 1000;
hashLookups = 0;
timeLimitMilliseconds = settings.MaxSeconds < 0 ? 10000000 : settings.MaxSeconds * 1000;

// Iterative deepening
// Increment depth by 2 since we always want to consider pairs of move (our move and opponent's move)
Expand All @@ -40,14 +48,16 @@ public SearchResult GetBestMove(Position position, SearchArgs settings)
currentDepth = depth;

SearchResult nextDepth = AlphaBetaSearch(position, int.MinValue, int.MaxValue, depth, 1, 1);
nextDepth.Evaluations = evaluations;

if (!cutoff)
{
result = nextDepth;
}
}

result.Evaluations = evaluations;
result.HashLookups = hashLookups;

return result;
}

Expand Down Expand Up @@ -173,6 +183,14 @@ private int EvaluateDevelopment(Position position)

private SearchResult AlphaBetaSearch(Position position, int alpha, int beta, int depth, int reportdepth, int depthUp)
{
// If we've already processed this position, use the saved evaluation
ulong hash = position.GetHash();
if (hashtable.ContainsKey(hash))
{
hashLookups++;
return hashtable[hash];
}

// Evaluate the position
int eval = EvaluatePosition(position);

Expand Down Expand Up @@ -278,11 +296,17 @@ private SearchResult AlphaBetaSearch(Position position, int alpha, int beta, int
CurrentMove = movenum++,
TotalMoves = moves.Count,
Depth = currentDepth,
HashLookups = hashLookups,
CurrentVariation = (depthUp != 1 ? levelOneNode + " " : "") + move.ToString() + " " + child.PrimaryVariation
});
}
}

if (!hashtable.ContainsKey(hash))
{
hashtable.Add(hash, best);
}

return best;
}
}
Expand Down
2 changes: 2 additions & 0 deletions Chase.Engine/SearchResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class SearchResult

public long Evaluations { get; set; }

public long HashLookups { get; set; }

public string PrimaryVariation { get; set; }

public Move BestMove { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions Chase.Engine/SearchStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class SearchStatus

public long SearchedNodes { get; set; }

public long HashLookups { get; set; }

public long ElapsedMilliseconds { get; set; }

public int TotalMoves { get; set; }
Expand Down
1 change: 1 addition & 0 deletions Chase.GUI/GameForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ private void Game_OnSearchProgress(SearchStatus status)
" score: " + status.BestMoveSoFar.Score +
" depth: " + status.Depth +
" nps: " + status.NodesPerSecond.ToString("0") +
" hl: " + status.HashLookups +
" pv: " + status.CurrentVariation;
}
else
Expand Down

0 comments on commit 7697565

Please sign in to comment.