Skip to content

Commit 34a3473

Browse files
committed
added searchdata and lazyeval classes
1 parent 0661d4b commit 34a3473

File tree

8 files changed

+203
-160
lines changed

8 files changed

+203
-160
lines changed

NoraGrace/NoraGrace.Engine.Tests/EvalTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ public void EvalTest1()
143143

144144
EvalResults e1 = new EvalResults();
145145
EvalResults e2 = new EvalResults();
146-
eval.EvalLazy(board, e1, null, int.MinValue, int.MaxValue);
147-
eval.EvalLazy(boardRev, e2, null, int.MinValue, int.MaxValue);
146+
eval.Eval(board, e1);
147+
eval.Eval(boardRev, e2);
148148

149149
if (e1.Score != -e2.Score)
150150
{

NoraGrace/NoraGrace.Engine/Evaluation/EvalResults.cs

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -171,85 +171,4 @@ public int KingSafetyPhased
171171

172172
}
173173

174-
public class ChessEvalInfoStack
175-
{
176-
177-
public readonly Evaluator _eval;
178-
179-
List<EvalResults> _plyInfoList = new List<EvalResults>();
180-
181-
public ChessEvalInfoStack(Evaluator eval, int plyCapacity = 50)
182-
{
183-
_eval = eval;
184-
while (_plyInfoList.Count < plyCapacity)
185-
{
186-
_plyInfoList.Add(new EvalResults());
187-
}
188-
}
189-
190-
public EvalResults this[int ply]
191-
{
192-
get { return _plyInfoList[ply]; }
193-
}
194-
195-
public Evaluator Evaluator
196-
{
197-
get { return _eval; }
198-
}
199-
200-
public int EvalFor(int ply, Board board, Player player, out EvalResults info, int alpha, int beta)
201-
{
202-
System.Diagnostics.Debug.Assert(alpha >= Evaluator.MinValue);
203-
System.Diagnostics.Debug.Assert(beta <= Evaluator.MaxValue);
204-
205-
if (player == Player.White)
206-
{
207-
return Eval(ply, board, out info, alpha, beta);
208-
}
209-
else
210-
{
211-
return -Eval(ply, board, out info, -beta, -alpha);
212-
}
213-
}
214-
215-
public int Eval(int ply, Board board, out EvalResults info, int alpha, int beta)
216-
{
217-
System.Diagnostics.Debug.Assert(alpha >= Evaluator.MinValue);
218-
System.Diagnostics.Debug.Assert(beta <= Evaluator.MaxValue);
219-
220-
info = _plyInfoList[ply];
221-
222-
223-
//check to see if we already have evaluated.
224-
if(board.ZobristBoard == info.Zobrist)
225-
{
226-
if (info.LazyAge == 0) { return info.Score; }
227-
if (info.LazyHigh < alpha)
228-
{
229-
return info.LazyHigh;
230-
}
231-
else if (info.LazyLow > beta)
232-
{
233-
return info.LazyLow;
234-
}
235-
}
236-
237-
238-
EvalResults prev = null;
239-
if (ply > 0)
240-
{
241-
prev = _plyInfoList[ply - 1];
242-
if (prev.Zobrist != board.ZobristPrevious)
243-
{
244-
prev = null;
245-
}
246-
}
247-
248-
return _eval.EvalLazy(board, info, prev, alpha, beta);
249-
250-
}
251-
252-
253-
254-
}
255174
}

NoraGrace/NoraGrace.Engine/Evaluation/Evaluator.cs

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Evaluator: IChessEval
1818
public const int MaxValue = int.MaxValue - 100;
1919
public const int MinValue = -MaxValue;
2020

21-
protected readonly PawnEvaluator _evalPawns;
21+
public readonly PawnEvaluator _evalPawns;
2222
public readonly MaterialEvaluator _evalMaterial;
2323
private readonly PcSqEvaluator _evalPcSq;
2424

@@ -176,51 +176,17 @@ public int EvalFor(Board board, Player who)
176176
}
177177

178178
public EvalResults _evalInfo = new EvalResults();
179-
public virtual int Eval(Board board)
179+
public virtual int Eval(Board board, EvalResults results = null)
180180
{
181-
return EvalLazy(board, _evalInfo, null, Evaluator.MinValue, Evaluator.MaxValue);
182-
}
183-
184-
public int EvalLazy(Board board, EvalResults evalInfo, EvalResults prevEvalInfo, int alpha, int beta)
185-
{
186-
System.Diagnostics.Debug.Assert(alpha >= MinValue);
187-
System.Diagnostics.Debug.Assert(beta <= MaxValue);
188-
189-
evalInfo.Reset();
190-
191-
192-
//material
193-
MaterialResults material = _evalMaterial.EvalMaterialHash(board);
194-
195-
//pawns
196-
PawnResults pawns = this._evalPawns.PawnEval(board);
197-
System.Diagnostics.Debug.Assert(pawns.WhitePawns == (board[PieceType.Pawn] & board[Player.White]));
198-
System.Diagnostics.Debug.Assert(pawns.BlackPawns == (board[PieceType.Pawn] & board[Player.Black]));
199-
200-
evalInfo.MaterialPawnsApply(board, material, pawns, this.DrawScore);
201-
202-
if (prevEvalInfo != null && evalInfo.PassedPawns == prevEvalInfo.PassedPawns)
203-
{
204-
evalInfo.ApplyPreviousEval(board, prevEvalInfo);
205-
System.Diagnostics.Debug.Assert(evalInfo.LazyAge > 0);
181+
var material = _evalMaterial.EvalMaterialHash(board);
182+
var pawns = _evalPawns.PawnEval(board);
183+
if (results == null) { results = _evalInfo; }
206184

207-
int fuzzyLazyScore = evalInfo.Score;
208-
int margin = evalInfo.LazyAge * 50;
209-
if (fuzzyLazyScore + margin < alpha)
210-
{
211-
return alpha;
212-
}
213-
if (fuzzyLazyScore - margin > beta)
214-
{
215-
return beta;
216-
}
217-
}
218-
219-
EvalAdvanced(board, evalInfo, material, pawns);
220-
221-
return evalInfo.Score;
185+
EvalAdvanced(board, _evalInfo, material, pawns);
186+
return _evalInfo.Score;
222187
}
223188

189+
224190
public void EvalAdvanced(Board board, EvalResults evalInfo, MaterialResults material, PawnResults pawns)
225191
{
226192

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace NoraGrace.Engine.Evaluation
8+
{
9+
public class Lazy
10+
{
11+
public static int EvalFor(SearchData sdata, int ply, Board board, Player player, out EvalResults info, int alpha, int beta)
12+
{
13+
System.Diagnostics.Debug.Assert(alpha >= Evaluator.MinValue);
14+
System.Diagnostics.Debug.Assert(beta <= Evaluator.MaxValue);
15+
16+
if (player == Player.White)
17+
{
18+
return Eval(sdata, ply, board, out info, alpha, beta);
19+
}
20+
else
21+
{
22+
return -Eval(sdata, ply, board, out info, -beta, -alpha);
23+
}
24+
}
25+
26+
public static int Eval(SearchData sdata, int ply, Board board, out EvalResults info, int alpha, int beta)
27+
{
28+
System.Diagnostics.Debug.Assert(alpha >= Evaluator.MinValue);
29+
System.Diagnostics.Debug.Assert(beta <= Evaluator.MaxValue);
30+
31+
info = sdata[ply].EvalResults;
32+
33+
34+
//check to see if we already have evaluated.
35+
if (board.ZobristBoard == info.Zobrist)
36+
{
37+
if (info.LazyAge == 0) { return info.Score; }
38+
if (info.LazyHigh < alpha)
39+
{
40+
return info.LazyHigh;
41+
}
42+
else if (info.LazyLow > beta)
43+
{
44+
return info.LazyLow;
45+
}
46+
}
47+
48+
49+
EvalResults prev = null;
50+
if (ply > 0)
51+
{
52+
prev = sdata[ply - 1].EvalResults;
53+
if (prev.Zobrist != board.ZobristPrevious)
54+
{
55+
prev = null;
56+
}
57+
}
58+
59+
return EvalLazy(sdata.Evaluator, board, info, prev, alpha, beta);
60+
61+
}
62+
63+
private static int EvalLazy(Evaluator evaluator, Board board, EvalResults evalInfo, EvalResults prevEvalInfo, int alpha, int beta)
64+
{
65+
System.Diagnostics.Debug.Assert(alpha >= Evaluator.MinValue);
66+
System.Diagnostics.Debug.Assert(beta <= Evaluator.MaxValue);
67+
68+
evalInfo.Reset();
69+
70+
71+
//material
72+
MaterialResults material = evaluator._evalMaterial.EvalMaterialHash(board);
73+
74+
//pawns
75+
PawnResults pawns = evaluator._evalPawns.PawnEval(board);
76+
System.Diagnostics.Debug.Assert(pawns.WhitePawns == (board[PieceType.Pawn] & board[Player.White]));
77+
System.Diagnostics.Debug.Assert(pawns.BlackPawns == (board[PieceType.Pawn] & board[Player.Black]));
78+
79+
80+
evalInfo.MaterialPawnsApply(board, material, pawns, evaluator.DrawScore);
81+
82+
if (prevEvalInfo != null && evalInfo.PassedPawns == prevEvalInfo.PassedPawns)
83+
{
84+
evalInfo.ApplyPreviousEval(board, prevEvalInfo);
85+
System.Diagnostics.Debug.Assert(evalInfo.LazyAge > 0);
86+
87+
int fuzzyLazyScore = evalInfo.Score;
88+
int margin = evalInfo.LazyAge * 50;
89+
if (fuzzyLazyScore + margin < alpha)
90+
{
91+
return alpha;
92+
}
93+
if (fuzzyLazyScore - margin > beta)
94+
{
95+
return beta;
96+
}
97+
}
98+
99+
evaluator.EvalAdvanced(board, evalInfo, material, pawns);
100+
101+
return evalInfo.Score;
102+
}
103+
104+
105+
}
106+
}

NoraGrace/NoraGrace.Engine/NoraGrace.Engine.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<Compile Include="Evaluation\EvalResults.cs" />
6161
<Compile Include="Evaluation\Helpers\Mobility.cs" />
6262
<Compile Include="Evaluation\Helpers\QuadraticBezier.cs" />
63+
<Compile Include="Evaluation\Lazy.cs" />
6364
<Compile Include="Evaluation\MaterialEvaluator.cs" />
6465
<Compile Include="Evaluation\PawnEvaluator.cs" />
6566
<Compile Include="Evaluation\PcSqEvaluator.cs" />
@@ -76,6 +77,7 @@
7677
<Compile Include="Move.Generate.cs" />
7778
<Compile Include="Move.Text.cs" />
7879
<Compile Include="MovePicker.cs" />
80+
<Compile Include="SearchData.cs" />
7981
<Compile Include="SearchDepth.cs" />
8082
<Compile Include="StaticExchange.cs" />
8183
<Compile Include="Opening.cs" />

0 commit comments

Comments
 (0)