20-add-negamax-eval-function (#31)
All checks were successful
Python tests (make) / test (push) Successful in 12s
All checks were successful
Python tests (make) / test (push) Successful in 12s
Reviewed-on: #31 Co-authored-by: Josh <josh@joshuaschuett.com> Co-committed-by: Josh <josh@joshuaschuett.com>
This commit is contained in:
71
scripts/evaluation.py
Normal file
71
scripts/evaluation.py
Normal file
@@ -0,0 +1,71 @@
|
||||
import random
|
||||
from binding.python_c_ffi import ChessFFI
|
||||
from binding.python_c_ffi import Move
|
||||
|
||||
|
||||
class BaseEvaluation:
|
||||
NAME = ""
|
||||
|
||||
|
||||
def __init__(self, chess_ffi=None):
|
||||
if chess_ffi is None:
|
||||
chess_ffi = ChessFFI()
|
||||
self.chess_ffi = chess_ffi
|
||||
|
||||
|
||||
def get_best_move(self, board, legal_moves):
|
||||
pass
|
||||
|
||||
|
||||
def get_params(self):
|
||||
pass
|
||||
|
||||
|
||||
"""
|
||||
We will use a random move evaluation as our base AI. This
|
||||
is expected to be the worst performing strategy. We can
|
||||
play our other AI evaluation methods against this one to
|
||||
confirm if our other strategies are at least better than
|
||||
a simplistic approach.
|
||||
"""
|
||||
class RandomEval(BaseEvaluation):
|
||||
NAME = "random"
|
||||
|
||||
|
||||
def __init__(self, chess_ffi=None):
|
||||
super().__init__(chess_ffi)
|
||||
|
||||
|
||||
def get_best_move(self, board, legal_moves):
|
||||
return random.choice(legal_moves)
|
||||
|
||||
|
||||
class NegaMaxEval(BaseEvaluation):
|
||||
NAME = "negamax"
|
||||
|
||||
|
||||
def __init__(self, depth=6, cp_window=20, chess_ffi=None):
|
||||
super().__init__(chess_ffi)
|
||||
self.depth = depth
|
||||
self.window = cp_window
|
||||
|
||||
|
||||
def get_best_move(self, board, legal_moves):
|
||||
best = Move()
|
||||
ok = self.chess_ffi._c_ai_find_best_move_with_window(
|
||||
board,
|
||||
self.depth,
|
||||
self.window,
|
||||
best
|
||||
)
|
||||
|
||||
if ok:
|
||||
return best
|
||||
return legal_moves[0]
|
||||
|
||||
|
||||
def get_params(self):
|
||||
return {
|
||||
"depth": self.depth,
|
||||
"window": self.window,
|
||||
}
|
||||
Reference in New Issue
Block a user