-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMSList.cs
552 lines (498 loc) · 14.4 KB
/
MSList.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
using NSChess;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Windows.Forms.VisualStyles;
namespace NSProgram
{
class MSRec
{
public string move;
public int score;
public MSRec(MSRec rec)
{
move = rec.move;
score = rec.score;
}
public MSRec(string m, int s)
{
move = m;
score = s;
}
}
internal class MSLine : List<MSRec>
{
public string fen = String.Empty;
public int depth = 0;
public double loss = 100;
public bool Valid()
{
return loss < 100;
}
public void Assign(MSLine line)
{
Clear();
fen = line.fen;
depth = line.depth;
loss = line.loss;
foreach (MSRec rec in line)
Add(new MSRec(rec));
}
public void DeleteMove(string move)
{
for (int n = Count - 1; n >= 0; n--)
if (this[n].move == move)
RemoveAt(n);
}
public void AddRec(MSRec rec)
{
DeleteMove(rec.move);
Add(rec);
SortScore();
}
public void SortScore()
{
Sort(delegate (MSRec r1, MSRec r2)
{
return r2.score - r1.score;
});
}
public string ShortFen()
{
return fen.Split(' ')[0];
}
public MSRec First()
{
if (Count > 0)
return this[0];
return null;
}
public MSRec Last()
{
if (Count > 0)
return this[Count - 1];
return null;
}
public static double WiningChances(int centipawns)
{
return 50 + 50 * (2 / (1 + Math.Exp(-0.00368208 * centipawns)) - 1);
}
public static double GetAccuracy(double winPercentBefore, double winPercentAfter)
{
return 103.1668 * Math.Exp(-0.04354 * (winPercentBefore - winPercentAfter)) - 3.1669;
}
public static double GetAccuracy(int scoreBefore, int scoreAfter)
{
double winPercentBefore = WiningChances(scoreBefore);
double winPercentAfter = WiningChances(scoreAfter);
return GetAccuracy(winPercentBefore, winPercentAfter);
}
public static double GetLoss(int before, int after)
{
double winPercentBefore = WiningChances(before);
double winPercentAfter = WiningChances(after);
return winPercentBefore - winPercentAfter;
}
public double GetAccuracy()
{
MSRec f = First();
MSRec l = Last();
if ((f == null) || (l == null))
return 0;
return GetAccuracy(First().score, Last().score);
}
public double GetLoss()
{
if (Count < 2)
return 0;
return GetLoss(First().score, Last().score);
}
public bool Fail()
{
if (Count < 3)
return true;
if (Math.Abs(First().score) > 30000)
return true;
if (Math.Abs(Last().score) > 30000)
return true;
return GetLoss() < Constants.blunder;
}
public bool MoveExists(string move)
{
foreach (MSRec rec in this)
if (rec.move == move)
return true;
return false;
}
void Reset()
{
fen = String.Empty;
depth = 0;
loss = 0;
Clear();
}
string GetMoves()
{
string moves = String.Empty;
foreach (MSRec rec in this)
{
moves = $"{moves} bm {rec.move} ce {rec.score}";
}
return moves.Trim();
}
public int GetScore(string move)
{
foreach (MSRec rec in this)
if (rec.move == move)
return rec.score;
return Last().score;
}
public string SaveToStr()
{
string moves = GetMoves();
string l = Convert.ToString(loss, CultureInfo.InvariantCulture.NumberFormat);
return $"{fen} acd {depth} loss {l} {moves}".Trim();
}
public bool LoadFromStr(string line)
{
Reset();
string[] tokens = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (tokens.Length < 6)
return false;
List<string> sl = new List<string>(tokens);
string last = String.Empty;
string move = String.Empty;
for (int n = 0; n < sl.Count; n++)
{
string s = sl[n];
if ((s == "bm") || (s == "ce") || (s == "acd") || (s == "loss"))
{
last = s;
continue;
}
if (n < 6)
{
fen += ' ' + s;
continue;
}
switch (last)
{
case "acd":
depth = Convert.ToInt32(s);
break;
case "loss":
double.TryParse(s, NumberStyles.Number, CultureInfo.InvariantCulture.NumberFormat, out loss);
break;
case "bm":
move = s;
break;
case "ce":
if (!String.IsNullOrEmpty(move))
{
MSRec rec = new MSRec(move, Convert.ToInt32(s));
AddRec(rec);
}
break;
}
}
fen = fen.Trim();
return true;
}
}
internal class MSList : List<MSLine>
{
public bool valid = true;
public string fileName = Constants.accuracyEpd;
public int Check()
{
if (Count == 0)
return 0;
SortFen();
MSLine last = this[0];
CChess chess = new CChess();
int result = 0;
for (int n = Count - 1; n >= 0; n--)
{
MSLine msl = this[n];
if (chess.SetFen(msl.fen))
{
string fen = chess.GetFen();
if (msl.fen != fen)
result++;
msl.fen = fen;
}
else
{
result++;
RemoveAt(n);
}
if ((msl.ShortFen() == last.ShortFen()) && (Count > 1))
{
result++;
if (msl.depth < last.depth)
RemoveAt(n);
else
RemoveAt(n + 1);
}
last = msl;
}
return result;
}
public void GetDepth(out int min, out int max)
{
min = int.MaxValue;
max = 0;
foreach (MSLine msl in this)
{
if (min > msl.depth)
min = msl.depth;
if (max < msl.depth)
max = msl.depth;
}
if (min > max)
min = 0;
}
public void SetDepth(int depth)
{
foreach (MSLine msl in this)
if (msl.depth > depth)
msl.depth = depth;
}
public void GetMoves(out int min, out int max)
{
min = int.MaxValue;
max = 0;
foreach (MSLine msl in this)
{
if (min > msl.Count)
min = msl.Count;
if (max < msl.Count)
max = msl.Count;
}
if (min > max)
min = 0;
}
public int CountMovesMin(out int min)
{
int result = 0;
min = int.MaxValue;
foreach (MSLine msl in this)
{
if (min > msl.Count)
{
min = msl.Count;
result = 1;
}
else if (min == msl.Count)
result++;
}
return result;
}
public int CountMovesMax(out int max)
{
int result = 0;
max = 0;
foreach (MSLine msl in this)
{
if (max < msl.Count)
{
max = msl.Count;
result = 1;
}
else if (max == msl.Count)
result++;
}
return result;
}
public int CountFail()
{
int result = 0;
foreach (MSLine msl in this)
if (msl.Fail())
result++;
return result;
}
public int DeleteFail()
{
int result = 0;
for (int n = Count - 1; n >= 0; n--)
{
MSLine msl = this[n];
if (msl.Fail())
{
result++;
RemoveAt(n);
}
}
if (result > 0)
SaveToEpd();
return result;
}
public void DeleteFen(string fen)
{
for (int n = Count - 1; n >= 0; n--)
if (this[n].fen == fen)
RemoveAt(n);
}
public int DeleteMoves(int moves)
{
int result = 0;
for (int n = Count - 1; n >= 0; n--)
if (this[n].Count == moves)
{
result++;
RemoveAt(n);
}
if (result > 0)
SaveToEpd();
return result;
}
public void SaveToEpd()
{
SortFen();
using (FileStream fs = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
using (StreamWriter sw = new StreamWriter(fs))
{
foreach (MSLine msl in this)
{
string l = msl.SaveToStr();
sw.WriteLine(l);
}
}
}
public bool LoadFromEpd()
{
valid = true;
Clear();
if (!File.Exists(fileName))
return false;
using (FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
using (StreamReader reader = new StreamReader(fs))
{
string line = String.Empty;
while ((line = reader.ReadLine()) != null)
{
MSLine msl = new MSLine();
if (msl.LoadFromStr(line))
Add(msl);
if (!msl.Valid())
{
msl.Valid();
valid = false;
}
}
}
return Count > 0;
}
void SortFen()
{
Sort(delegate (MSLine l1, MSLine l2)
{
return String.Compare(l1.fen, l2.fen, StringComparison.Ordinal);
});
}
public void SortDepth()
{
Sort(delegate (MSLine l1, MSLine l2)
{
int d1 = l1.depth;
int d2 = l2.depth;
return d1 - d2;
});
}
public void SortLoss()
{
Sort(delegate (MSLine l1, MSLine l2)
{
double d1 = l1.loss;
double d2 = l2.loss;
if (d1 == d2) return 0;
return d1 < d2 ? 1 : -1;
});
}
public void SortRandom()
{
for (int n = 0; n < Count; n++)
{
int r = CChess.rnd.Next(Count);
(this[n], this[r]) = (this[r], this[n]);
}
}
public int GetFenIndex(string fen)
{
for (int n = 0; n < Count; n++)
if (this[n].fen == fen)
return n;
return -1;
}
public bool AddLine(MSLine line)
{
int index = GetFenIndex(line.fen);
if (index < 0)
Add(line);
return index < 0;
}
public bool AddLine(string line)
{
MSLine msl = new MSLine();
if (msl.LoadFromStr(line))
return AddLine(msl);
return false;
}
public void ReplaceLine(MSLine line)
{
int index = GetFenIndex(line.fen);
if (index >= 0)
this[index].Assign(line);
else
Add(line);
}
public int CountShallowLine()
{
int result = 0;
foreach (MSLine line in this)
if (line.depth < Constants.minDepth)
result++;
return result;
}
public MSLine GetShallowLine()
{
if (Count == 0)
return null;
MSLine bst = this[0];
foreach (MSLine line in this)
if (bst.depth > line.depth)
bst = line;
return bst.depth < Constants.minDepth ? bst : null;
}
public MSLine GetLineFail()
{
foreach (MSLine line in this)
if (line.Fail())
return line;
return null;
}
public MSLine GetRandomLine()
{
if (Count == 0)
return null;
int index = CChess.rnd.Next(Count);
return this[index];
}
public void ResetLoss()
{
foreach (MSLine line in this)
line.loss = 100;
SaveToEpd();
valid = false;
}
public bool GetValid()
{
foreach (MSLine msl in this)
if (msl.loss == 100)
return false;
return true;
}
}
}