-
Notifications
You must be signed in to change notification settings - Fork 0
/
uncertaintyAgent.py
157 lines (113 loc) · 6.25 KB
/
uncertaintyAgent.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
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
#!/usr/bin/env python3
"""
Instructions.
Run modified_play_game.py uncertaintyAgent uncertaintyAgent
"""
import random
import chess
from player import Player
import torch
from fen_string_convert import process_sense, convert_fen_string, get_row_col_from_num, create_blank_emission_matrix, convert_truncated_to_truth
from uncertainty_rnn import BoardGuesserNetOnline
import numpy as np
class Random(Player):
def handle_game_start(self, color, board, white):
"""
This function is called at the start of the game.
:param color: chess.BLACK or chess.WHITE -- your color assignment for the game
:param board: chess.Board -- initial board state
"""
self.white = white
self.emission_matrix = create_blank_emission_matrix(self.white)
self.network = BoardGuesserNetOnline() # neural network for inferring truth board
if white:
self.network.load_state_dict(torch.load("white_rnn_model"))
else:
self.network.load_state_dict(torch.load("black_rnn_model"))
self.board = board
self.pred_board = None # where e are saving the truthboard
self.hidden = None # where we are saving the hidden states
def handle_opponent_move_result(self, captured_piece, captured_square):
"""
This function is called at the start of your turn and gives you the chance to update your board.
:param captured_piece: bool - true if your opponents captured your piece with their last move
:param captured_square: chess.Square - position where your piece was captured
"""
if captured_piece:
row, col = get_row_col_from_num(captured_square)
self.emission_matrix[12, row, col] = 1
def choose_sense(self, possible_sense, possible_moves, seconds_left):
"""
This function is called to choose a square to perform a sense on.
:param possible_sense: List(chess.SQUARES) -- list of squares to sense around
:param possible_moves: List(chess.Moves) -- list of acceptable moves based on current board
:param seconds_left: float -- seconds left in the game
:return: chess.SQUARE -- the center of 3x3 section of the board you want to sense
:example: choice = chess.A1
"""
return random.choice(possible_sense)
def handle_sense_result(self, sense_result):
"""
This is a function called after your picked your 3x3 square to sense and gives you the chance to update your
board.
:param sense_result: A list of tuples, where each tuple contains a :class:`Square` in the sense, and if there
was a piece on the square, then the corresponding :class:`chess.Piece`, otherwise `None`.
:example:
[
(A8, Piece(ROOK, BLACK)), (B8, Piece(KNIGHT, BLACK)), (C8, Piece(BISHOP, BLACK)),
(A7, Piece(PAWN, BLACK)), (B7, Piece(PAWN, BLACK)), (C7, Piece(PAWN, BLACK)),
(A6, None), (B6, None), (C8, None)
]
"""
process_sense(sense_result, self.emission_matrix) # adds sensing information to emission matrix
pass
def choose_move(self, possible_moves, seconds_left):
"""
Choose a move to enact from a list of possible moves.
:param possible_moves: List(chess.Moves) -- list of acceptable moves based only on pieces
:param seconds_left: float -- seconds left to make a move
:return: chess.Move -- object that includes the square you're moving from to the square you're moving to
:example: choice = chess.Move(chess.F2, chess.F4)
:condition: If you intend to move a pawn for promotion other than Queen, please specify the promotion parameter
:example: choice = chess.Move(chess.G7, chess.G8, promotion=chess.KNIGHT) *default is Queen
"""
# Use rnn to figure out state
self.softmax_out, self.hidden = self.network(torch.Tensor([self.emission_matrix]), self.hidden)
first_pred_label = self.softmax_out.detach().cpu().numpy()
# take an argmax to get the most probable board
max_pred = np.zeros(first_pred_label.shape)
max_pred[np.arange(first_pred_label.shape[0]), np.argmax(first_pred_label, axis=1)] = 1
# convert it into standard truth board format
self.pred_board = convert_truncated_to_truth(max_pred)
# TODO manually fill in in other channels
self.emission_matrix = create_blank_emission_matrix(self.white) # clear it here
return random.choice(possible_moves)
def handle_move_result(self, requested_move, taken_move, reason, captured_piece, captured_square):
"""
This is a function called at the end of your turn/after yourg move was made and gives you the chance to update
your board.
:param requested_move: chess.Move -- the move you intended to make
:param taken_move: chess.Move -- the move that was actually made
:param reason: String -- description of the result from trying to make requested_move
:param captured_piece: bool -- true if you captured your opponents piece
:param captured_square: chess.Square -- position where you captured the piece
"""
if requested_move != None:
from_row, from_col = get_row_col_from_num(requested_move.from_square)
self.emission_matrix[13, from_row, from_col] = 1
to_row, to_col = get_row_col_from_num(requested_move.to_square)
self.emission_matrix[14, from_row, from_col] = 1
if taken_move != None: # what was the move you actually took
from_row, from_col = get_row_col_from_num(taken_move.from_square)
self.emission_matrix[15, from_row, from_col] = 1
to_row, to_col = get_row_col_from_num(taken_move.to_square)
self.emission_matrix[16, from_row, from_col] = 1
if captured_piece: # did you capture a piece
self.emission_matrix[17,:, :] = 1
def handle_game_end(self, winner_color, win_reason): # possible GameHistory object...
"""
This function is called at the end of the game to declare a winner.
:param winner_color: Chess.BLACK/chess.WHITE -- the winning color
:param win_reason: String -- the reason for the game ending
"""
pass