fix : accuracy

This commit is contained in:
GuillaumeSD
2024-02-17 22:45:52 +01:00
parent 98d99c0df0
commit 4df0778390
2 changed files with 19 additions and 37 deletions

View File

@@ -70,27 +70,22 @@ export class Stockfish {
await this.sendCommands(["ucinewgame", "isready"], "readyok"); await this.sendCommands(["ucinewgame", "isready"], "readyok");
this.worker.postMessage("position startpos"); this.worker.postMessage("position startpos");
let whiteCpVsBestMove = 0; let whiteCpSum = 0;
let blackCpVsBestMove = 0; let whiteCpMax = 0;
let blackCpSum = 0;
let blackCpMax = 0;
const moves: MoveEval[] = []; const moves: MoveEval[] = [];
for (const fen of fens) { for (const fen of fens) {
console.log(`Evaluating position: ${fen}`); console.log(`Evaluating position: ${fen}`);
const result = await this.evaluatePosition(fen, depth); const result = await this.evaluatePosition(fen, depth);
const bestLine = result.lines[0]; const bestLineEval = result.lines[0].cp ?? 0;
const beforeMoveBestLine: LineEval = moves.at(-1)?.lines[0] ?? { if (this.isWhiteToMove(fen)) {
pv: [], whiteCpMax += bestLineEval;
cp: 0, blackCpSum += bestLineEval;
};
const wasWhiteMove = fen.split(" ")[1] === "b";
const cpVsBestMove = this.calculateCpVsBestMove(
bestLine,
beforeMoveBestLine
);
if (wasWhiteMove) {
whiteCpVsBestMove += cpVsBestMove;
} else { } else {
blackCpVsBestMove += cpVsBestMove; blackCpMax += bestLineEval;
whiteCpSum += bestLineEval;
} }
moves.push(result); moves.push(result);
@@ -99,30 +94,17 @@ export class Stockfish {
this.ready = true; this.ready = true;
console.log("Game evaluated"); console.log("Game evaluated");
console.log(moves); console.log(moves);
const whiteAccuracy = this.calculateAccuracy( const whiteAccuracy = this.calculateAccuracy(whiteCpSum, whiteCpMax);
whiteCpVsBestMove, const blackAccuracy = this.calculateAccuracy(blackCpSum, blackCpMax);
moves.length
);
const blackAccuracy = this.calculateAccuracy(
blackCpVsBestMove,
moves.length
);
return { moves, whiteAccuracy, blackAccuracy }; return { moves, whiteAccuracy, blackAccuracy };
} }
private calculateAccuracy(cpVsBestMove: number, movesNb: number): number { private calculateAccuracy(sum: number, max: number): number {
return 100 - (cpVsBestMove / movesNb) * 100; return (sum / max) * 100;
} }
private calculateCpVsBestMove( private isWhiteToMove(fen: string): boolean {
bestLine: LineEval, return fen.split(" ")[1] === "w";
beforeMoveBestLine: LineEval
): number {
if (bestLine.cp === undefined || beforeMoveBestLine.cp === undefined) {
return 0;
}
return bestLine.cp - beforeMoveBestLine.cp;
} }
public async evaluatePosition(fen: string, depth = 16): Promise<MoveEval> { public async evaluatePosition(fen: string, depth = 16): Promise<MoveEval> {

View File

@@ -16,8 +16,8 @@ export default function ReviewResult() {
Accuracies Accuracies
</h2> </h2>
<div id="accuracies"> <div id="accuracies">
<b id="white-accuracy">{gameEval.whiteAccuracy}%</b> <b id="white-accuracy">{gameEval.whiteAccuracy.toFixed(1)}%</b>
<b id="black-accuracy">{gameEval.blackAccuracy}%</b> <b id="black-accuracy">{gameEval.blackAccuracy.toFixed(1)}%</b>
</div> </div>
<div id="classification-message-container"> <div id="classification-message-container">
@@ -36,7 +36,7 @@ export default function ReviewResult() {
{moveEval?.lines.map((line) => ( {moveEval?.lines.map((line) => (
<div key={line.pv[0]} style={{ color: "white" }}> <div key={line.pv[0]} style={{ color: "white" }}>
<span style={{ marginRight: "2em" }}> <span style={{ marginRight: "2em" }}>
{line.cp ? line.cp / 100 : `Mate in ${line.mate}`} {line.cp ? line.cp / 100 : `Mate in ${Math.abs(line.mate ?? 0)}`}
</span> </span>
<span>{line.pv.slice(0, 7).join(", ")}</span> <span>{line.pv.slice(0, 7).join(", ")}</span>
</div> </div>