From 30e6aa705fcc992e48998ecd64e3842bbe707672 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Wed, 24 Nov 2021 08:12:58 +0100 Subject: [PATCH] AI play book: only decent moves --- .../fishnet/src/main/FishnetOpeningBook.scala | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/modules/fishnet/src/main/FishnetOpeningBook.scala b/modules/fishnet/src/main/FishnetOpeningBook.scala index 4bfbe6082f..13efcf8590 100644 --- a/modules/fishnet/src/main/FishnetOpeningBook.scala +++ b/modules/fishnet/src/main/FishnetOpeningBook.scala @@ -2,7 +2,7 @@ package lila.fishnet import chess.format.Forsyth import chess.format.Uci -import chess.Speed +import chess.{ Color, Speed } import com.softwaremill.tagging._ import play.api.libs.json._ import play.api.libs.ws.JsonBodyReadables._ @@ -45,7 +45,7 @@ final private class FishnetOpeningBook( for { data <- res.body[JsValue].validate[Response](responseReader).asOpt _ = if (data.moves.isEmpty) outOfBook.put(game.id) - move <- data.randomPonderedMove + move <- data randomPonderedMove game.turnColor } yield move.uci } .monTry { res => @@ -66,11 +66,13 @@ object FishnetOpeningBook { trait Depth case class Response(moves: List[Move]) { - def randomPonderedMove: Option[Move] = { - val sum = moves.map(_.nb).sum - val novelty = 1 - val rng = ThreadLocalRandom.nextInt(sum + novelty) - moves + + def randomPonderedMove(turn: Color): Option[Move] = { + val decentMoves = moves.filter(_.lossRatioFor(turn) < 0.66) + val sum = decentMoves.map(_.nb).sum + val novelty = 1 + val rng = ThreadLocalRandom.nextInt(sum + novelty) + decentMoves .foldLeft((none[Move], 0)) { case ((found, it), next) => val nextIt = it + next.nb (found orElse (nextIt > rng).option(next), nextIt) @@ -80,7 +82,8 @@ object FishnetOpeningBook { } case class Move(uci: Uci, white: Int, draws: Int, black: Int) { - def nb = white + draws + black + def nb = white + draws + black + def lossRatioFor(color: Color): Float = (nb > 0) ?? color.fold(black, white).toFloat / nb } implicit val moveReader = Json.reads[Move]