forked from lichess-org/mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.dart
More file actions
287 lines (243 loc) · 7.98 KB
/
eval.dart
File metadata and controls
287 lines (243 loc) · 7.98 KB
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
import 'dart:math' as math;
import 'package:chessground/chessground.dart';
import 'package:collection/collection.dart';
import 'package:dartchess/dartchess.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:lichess_mobile/src/model/common/chess.dart';
part 'eval.freezed.dart';
part 'eval.g.dart';
/// Base class for evals.
sealed class Eval {
String get evalString;
/// The winning chances for the given [Side].
///
/// 1 = infinitely winning
/// -1 = infinitely losing
double winningChances(Side side);
}
/// The eval from the client side, either from the cloud or the local engine.
sealed class ClientEval extends Eval {
Position get position;
IList<PvData> get pvs;
}
/// The eval coming from other Lichess clients, served from the network.
@freezed
class CloudEval with _$CloudEval implements ClientEval {
CloudEval._();
factory CloudEval({required int depth, required Position position, required IList<PvData> pvs}) =
_CloudEval;
@override
String get evalString => _evalString(cp, mate);
@override
double winningChances(Side side) => _toPov(side, _toWhiteWinningChances(cp, mate));
int? get cp => pvs[0].cp;
int? get mate => pvs[0].mate;
}
/// The eval from the local engine.
@freezed
class LocalEval with _$LocalEval implements ClientEval {
const LocalEval._();
const factory LocalEval({
required Position position,
required int depth,
required int nodes,
required IList<PvData> pvs,
required int millis,
required Duration searchTime,
int? cp,
int? mate,
}) = _LocalEval;
double get knps => nodes / millis;
Move? get bestMove {
final uci = pvs.firstOrNull?.moves.firstOrNull;
if (uci == null) return null;
return Move.parse(uci);
}
IList<MoveWithWinningChances> get bestMoves {
return pvs
.where((e) => e.moves.isNotEmpty)
.map((e) => e._firstMoveWithWinningChances(position.turn))
.nonNulls
.sorted((a, b) => b.winningChances.compareTo(a.winningChances))
.toIList();
}
@override
String get evalString => _evalString(cp, mate);
@override
double winningChances(Side side) => _toPov(side, _whiteWinningChances);
double get _whiteWinningChances {
return _toWhiteWinningChances(cp, mate);
}
}
/// The eval from an external engine, typically Lichess server side Stockfish.
@Freezed(fromJson: true, toJson: true)
class ExternalEval with _$ExternalEval implements Eval {
const ExternalEval._();
const factory ExternalEval({
required int? cp,
required int? mate,
int? depth,
UCIMove? bestMove,
String? variation,
({String name, String comment})? judgment,
}) = _ExternalEval;
factory ExternalEval.fromPgnEval(PgnEvaluation eval) {
return ExternalEval(
cp: eval.pawns != null ? cpFromPawns(eval.pawns!) : null,
mate: eval.mate,
depth: eval.depth,
);
}
factory ExternalEval.fromJson(Map<String, dynamic> json) => _$ExternalEvalFromJson(json);
@override
String get evalString => _evalString(cp, mate);
@override
double winningChances(Side side) => _toPov(side, _whiteWinningChances);
double get _whiteWinningChances {
if (mate != null) {
return mateWinningChances(mate!);
} else if (cp != null) {
return cpWinningChances(cp!);
} else {
return 0;
}
}
}
double _toWhiteWinningChances(int? cp, int? mate) {
if (mate != null) {
return mateWinningChances(mate);
} else if (cp != null) {
return cpWinningChances(cp);
} else {
return 0;
}
}
@freezed
class PvData with _$PvData {
const PvData._();
const factory PvData({required IList<UCIMove> moves, int? mate, int? cp}) = _PvData;
String get evalString => _evalString(cp, mate);
Side? get winningSide {
if (mate != null) {
return mate! > 0 ? Side.white : Side.black;
} else if (cp != null) {
return cp! > 0 ? Side.white : Side.black;
} else {
return null;
}
}
List<String> sanMoves(Position fromPosition) {
Position pos = fromPosition;
final List<String> res = [];
for (final uciMove in moves.sublist(0, math.min(12, moves.length))) {
// assume uciMove string is valid as it comes from stockfish
final move = Move.parse(uciMove)!;
if (pos.isLegal(move)) {
final (newPos, san) = pos.makeSanUnchecked(move);
res.add(san);
pos = newPos;
} else {
break;
}
}
return res;
}
MoveWithWinningChances? _firstMoveWithWinningChances(Side sideToMove) {
final uciMove = (moves.isNotEmpty) ? Move.parse(moves.first) : null;
return (uciMove != null)
? (move: uciMove, winningChances: _toPov(sideToMove, _toWhiteWinningChances(cp, mate)))
: null;
}
}
typedef MoveWithWinningChances = ({Move move, double winningChances});
ISet<Shape> computeBestMoveShapes(
IList<MoveWithWinningChances> moves,
Side sideToMove,
PieceAssets pieceAssets,
Color boardPrefsColor,
) {
// Scale down all moves with index > 0 based on how much worse their winning chances are compared to the best move
// (assume moves are ordered by their winning chances, so index==0 is the best move)
double scaleArrowAgainstBestMove(int index) {
const minScale = 0.15;
const maxScale = 1.0;
const winningDiffScaleFactor = 2.5;
final bestMove = moves[0];
final winningDiffComparedToBestMove = bestMove.winningChances - moves[index].winningChances;
// Force minimum scale if the best move is significantly better than this move
if (winningDiffComparedToBestMove > 0.3) {
return minScale;
}
return clampDouble(
math.max(minScale, maxScale - winningDiffScaleFactor * winningDiffComparedToBestMove),
0,
1,
);
}
return ISet(
moves
.mapIndexed((i, m) {
final move = m.move;
// Same colors as in the Web UI with a slightly different opacity
// The best move has a different color than the other moves
final color = (i == 0) ? boardPrefsColor : const Color(0x664A4A4A);
switch (move) {
case NormalMove(from: _, to: _, promotion: final promRole):
return [
Arrow(
color: color,
orig: move.from,
dest: move.to,
scale: scaleArrowAgainstBestMove(i),
),
if (promRole != null)
PieceShape(
color: color,
orig: move.to,
pieceAssets: pieceAssets,
piece: Piece(color: sideToMove, role: promRole),
),
];
case DropMove(role: final role, to: _):
return [
PieceShape(
color: color,
orig: move.to,
pieceAssets: pieceAssets,
opacity: 0.5,
piece: Piece(color: sideToMove, role: role),
),
];
}
})
.expand((e) => e),
);
}
double cpToPawns(int cp) => cp / 100;
int cpFromPawns(double pawns) => (pawns * 100).round();
double cpWinningChances(int cp) => _rawWinningChances(math.min(math.max(-1000, cp), 1000));
double mateWinningChances(int mate) {
final cp = (21 - math.min(10, mate.abs())) * 100;
final signed = cp * (mate > 0 ? 1 : -1);
return _rawWinningChances(signed);
}
double _toPov(Side side, double diff) => side == Side.white ? diff : -diff;
// https://github.com/lichess-org/lila/pull/11148
double _rawWinningChances(num cp) {
// https://github.com/lichess-org/lila/pull/11148
const multiplier = -0.00368208;
return 2 / (1 + math.exp(multiplier * cp)) - 1;
}
String _evalString(int? cp, int? mate) {
if (cp != null) {
final e = cpToPawns(cp);
return e > 0 ? '+${e.toStringAsFixed(1)}' : e.toStringAsFixed(1);
} else if (mate != null) {
return '#$mate';
} else {
return '-';
}
}