-
Notifications
You must be signed in to change notification settings - Fork 0
/
puzzle_solver.py
62 lines (53 loc) · 1.96 KB
/
puzzle_solver.py
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
import csv
import chess
import random
import game_logic
import time
import numpy as np
def play_puzzle(fen, moves):
board = chess.Board(fen)
move_number = 0
moves_made = 0
misses = 0
# Player settings
player = game_logic.ChessPlayer(board.turn, game_logic.ALPHA_BETA_MOVE, game_logic.MATERIAL_EVAL)
# player = game_logic.ChessPlayer(board.turn, game_logic.GREEDY_MOVE, game_logic.STOCKFISH_EVAL)
# player = game_logic.ChessPlayer(board.turn, game_logic.RANDOM_MOVE, game_logic.MATERIAL_EVAL)
while move_number < len(moves):
next_move = chess.Move.from_uci(moves[move_number])
board.push(next_move)
move_number = move_number + 1
right_move = chess.Move.from_uci(moves[move_number])
move = player.get_player_move(board)
if move != right_move and not board.is_checkmate():
misses = misses + 1
moves_made = moves_made + 1
board.push(right_move)
move_number = move_number + 1
return 1 - misses/moves_made
FEN = 1
MOVES = 2
# Number of path plannings used in the Monte Carlo analysis
num_iterations = 1
# num_iterations = 10
# num_iterations = 100 # Monte Carlo
with open('puzzles/puzzles.csv', mode='r') as db:
csv_reader = csv.reader(db, delimiter=',')
rows = list(csv_reader)
random.seed(10)
score = np.zeros((num_iterations, 1))
times = np.zeros((num_iterations, 1))
for i in range(num_iterations):
puzzle = rows[random.randint(0, len(rows))]
fen = puzzle[FEN]
moves = list(puzzle[MOVES].split(' '))
tic = time.time()
score[i] = play_puzzle(fen, moves)
toc = time.time()
times[i] = toc - tic
print(r'Iteration: {0}, Score: {1}, Time: {2}'.format(i + 1, float(score[i]), float(times[i])))
db.close()
# Print statistics
print('\n')
print(r'Compute time: mean: {0}, std: {1}'.format(np.mean(times), np.std(times)))
print(r'Score: mean: {0}, std: {1}'.format(np.mean(score), np.std(score)))