Skip to content

Commit 02e8073

Browse files
committed
added ScaleFactor enum
1 parent 8074051 commit 02e8073

File tree

4 files changed

+39
-46
lines changed

4 files changed

+39
-46
lines changed

NoraGrace/NoraGrace.Engine/Evaluation/EvalResults.cs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public class EvalResults
1818
public int Material { get; private set; }
1919
public PhasedScore PcSq { get; set; }
2020
public PhasedScore Pawns { get; private set; }
21-
public int StageStartWeight { get; private set; }
22-
public int ScaleWhite { get; private set; }
23-
public int ScaleBlack { get; private set; }
21+
public ScaleFactor StageStartWeight { get; private set; }
22+
public ScaleFactor ScaleWhite { get; private set; }
23+
public ScaleFactor ScaleBlack { get; private set; }
2424
public Bitboard PassedPawns { get; private set; } //not a score but information
2525
public Bitboard CandidatePawns { get; private set; } //not a score but information
2626

@@ -39,9 +39,9 @@ public void Reset()
3939
Pawns = 0;
4040
PawnsPassed = 0;
4141
ShelterStorm = 0;
42-
StageStartWeight = 0;
43-
ScaleWhite = 100;
44-
ScaleBlack = 100;
42+
StageStartWeight = ScaleFactor.FULL;
43+
ScaleWhite = ScaleFactor.FULL;
44+
ScaleBlack = ScaleFactor.FULL;
4545
DrawScore = 0;
4646
PassedPawns = Bitboard.Empty;
4747
CandidatePawns = Bitboard.Empty;
@@ -95,10 +95,6 @@ public int LazyLow
9595
return Score - margin;
9696
}
9797
}
98-
public int StageEndWeight
99-
{
100-
get { return 100 - StageStartWeight; }
101-
}
10298

10399
public int Score
104100
{
@@ -108,20 +104,18 @@ public int Score
108104
.Add(Pawns)
109105
.Add(PawnsPassed)
110106
.Add(ShelterStorm)
111-
.Add(this.Attacks[0].Mobility.Subtract(this.Attacks[1].Mobility)).ApplyWeights(StageStartWeight) + Material;
107+
.Add(this.Attacks[0].Mobility.Subtract(this.Attacks[1].Mobility)).ApplyScaleFactor(StageStartWeight) + Material;
112108

113109
nonScaled += this.Attacks[0].KingAttackerScore;
114110
nonScaled -= this.Attacks[1].KingAttackerScore;
115111

116-
if (nonScaled > DrawScore && ScaleWhite < 100)
112+
if (nonScaled > DrawScore && ScaleWhite < ScaleFactor.FULL)
117113
{
118-
int scaled = (((nonScaled - DrawScore) * ScaleWhite) / 100) + DrawScore;
119-
return scaled;
114+
return ScaleWhite.ScaleValue(nonScaled - DrawScore) + DrawScore;
120115
}
121-
else if (nonScaled < DrawScore && ScaleBlack < 100)
116+
else if (nonScaled < DrawScore && ScaleBlack < ScaleFactor.FULL)
122117
{
123-
int scaled = (((nonScaled - DrawScore) * ScaleBlack) / 100) + DrawScore;
124-
return scaled;
118+
return ScaleBlack.ScaleValue(nonScaled - DrawScore) + DrawScore;
125119
}
126120
return nonScaled;
127121
}
@@ -139,30 +133,30 @@ public int PcSqPhased
139133
{
140134
get
141135
{
142-
return PcSq.ApplyWeights(StageStartWeight);
136+
return PcSq.ApplyScaleFactor(StageStartWeight);
143137
}
144138
}
145139

146140
public int MobilityPhased
147141
{
148142
get
149143
{
150-
return Attacks[0].Mobility.Subtract(Attacks[1].Mobility).ApplyWeights(StageStartWeight);
144+
return Attacks[0].Mobility.Subtract(Attacks[1].Mobility).ApplyScaleFactor(StageStartWeight);
151145
}
152146
}
153147
public int PawnsPhased
154148
{
155149
get
156150
{
157-
return Pawns.ApplyWeights(StageStartWeight);
151+
return Pawns.ApplyScaleFactor(StageStartWeight);
158152
}
159153
}
160154

161155
public int PawnsPassedPhased
162156
{
163157
get
164158
{
165-
return PawnsPassed.ApplyWeights(StageStartWeight);
159+
return PawnsPassed.ApplyScaleFactor(StageStartWeight);
166160
}
167161
}
168162

NoraGrace/NoraGrace.Engine/Evaluation/MaterialEvaluator.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ public MaterialSettingsPhase()
5959
public class MaterialResults
6060
{
6161
public readonly Int64 ZobristMaterial;
62-
public readonly int StartWeight;
62+
6363
public readonly int Score;
64+
public readonly ScaleFactor StartWeight;
65+
public readonly ScaleFactor ScaleWhite;
66+
public readonly ScaleFactor ScaleBlack;
6467

65-
public readonly int ScaleWhite;
66-
public readonly int ScaleBlack;
6768
//public readonly int Wp;
6869
//public readonly int Wn;
6970
//public readonly int Wb;
@@ -75,7 +76,7 @@ public class MaterialResults
7576
//public readonly int Br;
7677
//public readonly int Bq;
7778

78-
public MaterialResults(Int64 zobristMaterial, int startWeight, int score, int scaleWhite, int scaleBlack /*, int wp, int wn, int wb, int wr, int wq, int bp, int bn, int bb, int br, int bq*/)
79+
public MaterialResults(Int64 zobristMaterial, ScaleFactor startWeight, int score, ScaleFactor scaleWhite, ScaleFactor scaleBlack /*, int wp, int wn, int wb, int wr, int wq, int bp, int bn, int bb, int br, int bq*/)
7980
{
8081
ZobristMaterial = zobristMaterial;
8182
StartWeight = startWeight;
@@ -97,7 +98,7 @@ public MaterialResults(Int64 zobristMaterial, int startWeight, int score, int sc
9798

9899
public bool DoShelter
99100
{
100-
get { return this.StartWeight > 40; }
101+
get { return this.StartWeight.ToDouble() > .4; }
101102
}
102103

103104
}
@@ -157,30 +158,29 @@ public MaterialResults EvalMaterial(Int64 zob, int wp, int wn, int wb, int wr, i
157158

158159

159160

160-
int startWeight = CalcStartWeight(wp, wn, wb, wr, wq, bp, bn, bb, br, bq);
161-
double startWeightPct = (double)startWeight / 100f;
161+
ScaleFactor startWeight = CalcStartWeight(wp, wn, wb, wr, wq, bp, bn, bb, br, bq);
162162

163-
163+
164164
double pctPawns = (double)(wp + bp) / 16f;
165165
double pctMinors = (double)(wn + bn + wb + bb) / 8;
166-
double totalPct = (startWeightPct + pctPawns) / 2f;
166+
double totalPct = (startWeight.ToDouble() + pctPawns) / 2f;
167167

168168
double white = MyMaterial(totalPct, pctPawns, pctMinors, wp, wn, wb, wr, wq, bp, bn, bb, br, bq);
169169
double black = MyMaterial(totalPct, pctPawns, pctMinors, bp, bn, bb, br, bq, wp, wn, wb, wr, wq);
170170

171171
double score = white - black;
172172

173-
double scaleWhite = ScaleFactor(wp, wn, wb, wr, wq, bp, bn, bb, br, bq);
174-
double scaleBlack = ScaleFactor(bp, bn, bb, br, bq, wp, wn, wb, wr, wq);
173+
ScaleFactor scaleWhite = ScaleFactorUtil.FromDouble(ScaleFactorPlayer(wp, wn, wb, wr, wq, bp, bn, bb, br, bq));
174+
ScaleFactor scaleBlack = ScaleFactorUtil.FromDouble(ScaleFactorPlayer(bp, bn, bb, br, bq, wp, wn, wb, wr, wq));
175175

176176

177-
return new MaterialResults(zob, startWeight, (int)score, (int)(scaleWhite * 100), (int)(scaleBlack * 100));
177+
return new MaterialResults(zob, startWeight, (int)score, scaleWhite, scaleBlack);
178178

179179
}
180180

181181

182182

183-
protected int CalcStartWeight(int wp, int wn, int wb, int wr, int wq, int bp, int bn, int bb, int br, int bq)
183+
protected ScaleFactor CalcStartWeight(int wp, int wn, int wb, int wr, int wq, int bp, int bn, int bb, int br, int bq)
184184
{
185185
int basicMaterialCount =
186186
(wn * 3) + (bn * 3)
@@ -191,26 +191,25 @@ protected int CalcStartWeight(int wp, int wn, int wb, int wr, int wq, int bp, in
191191
//full material would be 62
192192
if (basicMaterialCount >= 56)
193193
{
194-
return 100;
194+
return Evaluation.ScaleFactor.FULL;
195195
}
196196
else if (basicMaterialCount <= 10)
197197
{
198-
return 0;
198+
return Evaluation.ScaleFactor.NONE;
199199
}
200200
else
201201
{
202202
int rem = basicMaterialCount - 10;
203203
float retval = (float)rem / 46f;
204-
int retval2 = (int)Math.Round((retval * 100));
205-
return retval2;
204+
return Evaluation.ScaleFactorUtil.FromDouble(retval);
206205
}
207206
}
208207

209208
/// <summary>
210209
/// Functionally equivilent to Fruit2.1, although surprised adding it did not improve much.
211210
/// </summary>
212211
/// <returns></returns>
213-
private double ScaleFactor(int myP, int myN, int myB, int myR, int myQ, int hisP, int hisN, int hisB, int hisR, int hisQ)
212+
private double ScaleFactorPlayer(int myP, int myN, int myB, int myR, int myQ, int hisP, int hisN, int hisB, int hisR, int hisQ)
214213
{
215214
if (myP == 0)
216215
{ // white has no pawns

NoraGrace/NoraGrace.Engine/Evaluation/PhasedScore.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ public static PhasedScore Multiply(this PhasedScore phasedScore, int multiplier)
7272
return (PhasedScore)((long)phasedScore * multiplier);
7373
}
7474

75-
public static int ApplyWeights(this PhasedScore phasedScore, int StageStartWeight)
75+
public static int ApplyScaleFactor(this PhasedScore phasedScore, ScaleFactor startWeight)
7676
{
77-
return (
78-
(phasedScore.Opening() * StageStartWeight)
79-
+ (phasedScore.Endgame() * (100 - StageStartWeight)
80-
)) / 100;
77+
ScaleFactor endWeight = (ScaleFactor)((int)ScaleFactor.FULL - (int)startWeight);
78+
79+
return startWeight.ScaleValue(phasedScore.Opening()) + endWeight.ScaleValue(phasedScore.Endgame());
80+
8181
}
8282

8383

NoraGrace/NoraGrace.Engine/Evaluation/ScaleFactor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace NoraGrace.Engine.Evaluation
1010
public enum ScaleFactor
1111
{
1212
NONE = 0,
13-
FULL = 255
13+
FULL = 256
1414
}
1515

1616
public static class ScaleFactorUtil
@@ -28,7 +28,7 @@ public static double ToDouble(this ScaleFactor sf)
2828

2929
public static string Description(this ScaleFactor sf)
3030
{
31-
return "Scale:" + sf.ToDouble().ToString("D3");
31+
return "Scale:" + sf.ToDouble().ToString("F3");
3232
}
3333

3434
public static int ScaleValue(this ScaleFactor sf, int value)

0 commit comments

Comments
 (0)