Skip to content

Commit 5957022

Browse files
authored
Merge pull request #23 from boraaros/guess-strategy
iterative search guess strategy
2 parents 722ee96 + 328fa01 commit 5957022

3 files changed

Lines changed: 43 additions & 32 deletions

File tree

Alligator.Benchmark/Program.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ private static IEnumerable<IPosition> EnumerateExamples()
7474
yield return Example3();
7575
yield return Example4();
7676
yield return Example5();
77+
yield return Example6();
7778
}
7879

7980
#region Examles
@@ -158,6 +159,30 @@ private static IPosition Example5()
158159
return position;
159160
}
160161

162+
private static IPosition Example6()
163+
{
164+
IPosition position = new Position();
165+
position.Take(new Placement(0));
166+
position.Take(new Placement(12));
167+
position.Take(new Placement(1));
168+
position.Take(new Placement(7));
169+
position.Take(new Placement(2));
170+
position.Take(new Placement(13));
171+
position.Take(new Placement(11));
172+
position.Take(new Placement(17));
173+
position.Take(new Movement(2, 7, 1));
174+
position.Take(new Placement(8));
175+
position.Take(new Placement(16));
176+
position.Take(new Placement(22));
177+
position.Take(new Movement(7, 12, 2));
178+
position.Take(new Movement(12, 1, 1));
179+
position.Take(new Movement(11, 12, 1));
180+
position.Take(new Movement(12, 1, 1));
181+
position.Take(new Movement(1, 8, 3));
182+
position.Take(new Movement(13, 8, 1));
183+
return position;
184+
}
185+
161186
#endregion
162187
}
163188
}

Alligator.Solver/Algorithms/AlphaBetaSolver.cs

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ internal class AlphaBetaSolver<TPosition, TStep> : ISolver<TStep>
1010
private readonly ISearchManager searchManager;
1111
private readonly Action<string> logger;
1212

13+
private const int maxDepth = 7; // TODO: magic number!
14+
1315
public AlphaBetaSolver(
1416
AlphaBetaPruning<TPosition, TStep> alphaBetaPruning,
1517
IRules<TPosition, TStep> rules,
@@ -31,7 +33,7 @@ public TStep OptimizeNextStep(IList<TStep> history)
3133
var bestStep = default(TStep);
3234
var guess = 0;
3335

34-
for (int i = 2; i < 7; i += 2) // TODO: magic number (7)!
36+
for (int i = 2; i < maxDepth; i += 2)
3537
{
3638
searchManager.DepthLimit = i;
3739
var (OptimalSteps, Value) = BestNodeSearch(position, guess);
@@ -45,17 +47,25 @@ public TStep OptimizeNextStep(IList<TStep> history)
4547

4648
private (ICollection<TStep> OptimalSteps, int Value) BestNodeSearch(TPosition position, int guess)
4749
{
48-
int alpha = -int.MaxValue;
49-
int beta = int.MaxValue;
50+
int alpha = -sbyte.MaxValue - maxDepth;
51+
int beta = sbyte.MaxValue + maxDepth;
5052

5153
IList<TStep> candidates = rules.LegalStepsAt(position).ToList();
5254

55+
var optimalValue = 0;
56+
5357
while (alpha + 1 < beta && candidates.Count > 1)
5458
{
5559
var newCandidates = new List<TStep>();
5660

5761
foreach (var move in candidates)
5862
{
63+
if (newCandidates.Count > 1)
64+
{
65+
newCandidates.Add(move);
66+
continue;
67+
}
68+
5969
int value = NullWindowTest(position, move, guess);
6070

6171
if (value >= guess)
@@ -66,6 +76,7 @@ public TStep OptimizeNextStep(IList<TStep> history)
6676

6777
if (newCandidates.Count > 0)
6878
{
79+
optimalValue = guess;
6980
candidates = newCandidates;
7081
alpha = guess;
7182
}
@@ -74,10 +85,10 @@ public TStep OptimizeNextStep(IList<TStep> history)
7485
beta = guess;
7586
}
7687

77-
guess = NextGuess(alpha, beta, candidates.Count);
88+
guess = newCandidates.Count > 0 ? guess + 1 : guess - 1;
7889
}
7990

80-
return (candidates, guess);
91+
return (candidates, optimalValue);
8192
}
8293

8394
private int NullWindowTest(TPosition position, TStep step, int guess)
@@ -88,31 +99,6 @@ private int NullWindowTest(TPosition position, TStep step, int guess)
8899
return value;
89100
}
90101

91-
private int NextGuess(int alpha, int beta, int count)
92-
{
93-
if (alpha <= 0)
94-
{
95-
beta = Math.Min(beta, int.MaxValue / 2);
96-
}
97-
98-
if (beta >= 0)
99-
{
100-
alpha = Math.Max(alpha, -int.MaxValue / 2);
101-
}
102-
103-
var guess = (int)(alpha + (count - 1.0) / count * (beta - alpha));
104-
105-
if (guess == alpha)
106-
{
107-
return guess + 1;
108-
}
109-
else if (guess == beta)
110-
{
111-
return guess - 1;
112-
}
113-
return guess;
114-
}
115-
116102
private TPosition CreatePosition(IEnumerable<TStep> history)
117103
{
118104
var position = rules.InitialPosition();

Alligator.Solver/Algorithms/CacheTables.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ internal class CacheTables<TPosition, TStep> : ICacheTables<TPosition, TStep>
88

99
public CacheTables()
1010
{
11-
evaluationTable = new Dictionary<ulong, int>();
12-
transpositionTable = new Dictionary<ulong, Transposition<TStep>>();
11+
evaluationTable = new Dictionary<ulong, int>(10000000);
12+
transpositionTable = new Dictionary<ulong, Transposition<TStep>>(10000000);
1313
}
1414

1515
public void AddTransposition(TPosition position, Transposition<TStep> transposition)

0 commit comments

Comments
 (0)