-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCEvaluationList.cs
180 lines (163 loc) · 4.23 KB
/
CEvaluationList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NSProgram
{
class CElementE
{
public string fen = string.Empty;
public int eval = 0;
public string WriteToStr()
{
return $"{fen} ce {eval}";
}
public bool ReadFromStr(string value)
{
string[] tokens = value.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (tokens.Length < 6)
return false;
List<string> sl = new List<string>(tokens);
List<string> subList = sl.GetRange(0, 6);
fen = String.Join(" ", subList.ToArray()).Trim();
eval = 0;
if (tokens.Length > 7)
if (int.TryParse(tokens[7], out int v))
eval = v;
return true;
}
}
internal class CEvaluationList : List<CElementE>
{
bool loaded = false;
public int index = -1;
public double centyLoss = 0;
public int centyCount = 0;
public int CurIndex
{
get
{
return Count - 1 - index;
}
}
public int Limit
{
get
{
if ((Constants.limit > 0) && (Constants.limit < Count))
return Constants.limit;
return Count;
}
}
public CElementE CurElement
{
get
{
if ((index < 0) || (index >= Count))
return null;
return this[CurIndex];
}
}
public void AddScore(int best, int score)
{
int delta = Math.Abs(best - score);
centyCount++;
centyLoss += delta;
}
public double GetAccuracy()
{
return centyLoss / (centyCount + 1);
}
public void Reset()
{
index = -1;
centyLoss = 0;
centyCount = 0;
Next();
}
public void Fill()
{
for (int n = 0; n < Count; n++)
this[n].eval = 0;
SaveToFile();
}
public void DeleteFen(string fen)
{
for (int n = Count - 1; n >= 0; n--)
if (this[n].fen == fen)
{
RemoveAt(n);
if (index >= Count - 1 - n)
index--;
}
}
public void LoadFromFile()
{
if (loaded)
return;
loaded = true;
string fn = Constants.evalEpd;
Clear();
if (!File.Exists(fn))
return;
using (FileStream fs = File.Open(fn, FileMode.Open, FileAccess.Read, FileShare.Read))
using (StreamReader reader = new StreamReader(fs))
{
string line = String.Empty;
while ((line = reader.ReadLine()) != null)
if (!String.IsNullOrEmpty(line))
{
CElementE e = new CElementE();
e.ReadFromStr( line);
Add(e);
}
}
Console.WriteLine($"info string evaluation on fens {Count:N0} fail {CountFail()}");
}
public void SaveToFile()
{
string lastFen = String.Empty;
using (FileStream fs = File.Open(Constants.evalEpd, FileMode.Create, FileAccess.Write, FileShare.None))
using (StreamWriter sw = new StreamWriter(fs))
{
foreach (CElementE e in this)
{
string l = e.WriteToStr();
string[] tokens = l.Split(' ');
string curFen = $"{tokens[0]} {tokens[1]}";
if (lastFen == curFen)
continue;
lastFen = curFen;
sw.WriteLine(l);
}
}
}
public bool Next()
{
do
{
if (index >= Limit - 1)
return false;
index++;
if (CurElement.fen == String.Empty)
{
RemoveAt(CurIndex);
index--;
SaveToFile();
continue;
}
} while (CurElement.eval == 0);
return true;
}
public int CountFail()
{
int result = 0;
foreach (CElementE e in this)
if (e.eval == 0)
result++;
return result;
}
}
}