2018-02-05 22:50:31 +00:00
|
|
|
from cup import Cup
|
2018-03-04 16:35:36 +00:00
|
|
|
import tensorflow as tf
|
|
|
|
from network import Network
|
|
|
|
import numpy as np
|
2018-02-13 13:38:49 +00:00
|
|
|
from board import Board
|
2018-03-11 19:00:24 +00:00
|
|
|
import subprocess
|
2018-02-05 22:50:31 +00:00
|
|
|
import random
|
2018-03-11 19:00:24 +00:00
|
|
|
import sys
|
2018-02-05 22:50:31 +00:00
|
|
|
|
|
|
|
class Bot:
|
|
|
|
|
2018-03-08 15:27:16 +00:00
|
|
|
def __init__(self, sym, config = None):
|
|
|
|
self.config = config
|
2018-02-05 22:50:31 +00:00
|
|
|
self.cup = Cup()
|
|
|
|
self.sym = sym
|
2018-03-04 16:35:36 +00:00
|
|
|
self.graph = tf.Graph()
|
|
|
|
with self.graph.as_default():
|
|
|
|
self.session = tf.Session()
|
2018-03-08 15:27:16 +00:00
|
|
|
self.network = Network(self.session, config)
|
2018-03-09 23:22:20 +00:00
|
|
|
self.network.restore_model()
|
2018-03-04 16:35:36 +00:00
|
|
|
|
2018-02-05 22:50:31 +00:00
|
|
|
|
|
|
|
def roll(self):
|
|
|
|
print("{} rolled: ".format(self.sym))
|
|
|
|
roll = self.cup.roll()
|
2018-03-06 15:23:08 +00:00
|
|
|
# print(roll)
|
2018-02-05 22:50:31 +00:00
|
|
|
return roll
|
|
|
|
|
|
|
|
|
|
|
|
def switch(self,cur):
|
2018-02-06 22:29:51 +00:00
|
|
|
return -1 if cur == 1 else 1
|
2018-02-05 22:50:31 +00:00
|
|
|
|
2018-03-07 13:44:17 +00:00
|
|
|
def restore_model(self):
|
|
|
|
with self.graph.as_default():
|
|
|
|
self.network.restore_model()
|
2018-03-08 12:32:40 +00:00
|
|
|
|
2018-03-04 16:35:36 +00:00
|
|
|
def get_session(self):
|
|
|
|
return self.session
|
|
|
|
|
2018-02-06 22:29:51 +00:00
|
|
|
def get_sym(self):
|
|
|
|
return self.sym
|
2018-02-05 22:50:31 +00:00
|
|
|
|
2018-03-04 16:35:36 +00:00
|
|
|
def get_network(self):
|
|
|
|
return self.network
|
|
|
|
|
2018-03-08 12:32:40 +00:00
|
|
|
def make_random_move(self, board, sym, roll):
|
|
|
|
legal_moves = Board.calculate_legal_states(board, sym, roll)
|
|
|
|
return random.choice(list(legal_moves))
|
2018-03-11 19:00:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
# TODO: Test this, the score results are deterministic
|
|
|
|
def make_pubeval_move(self, board, sym, roll):
|
|
|
|
legal_moves = Board.calculate_legal_states(board, sym, roll)
|
|
|
|
moves_and_scores = []
|
|
|
|
for board in legal_moves:
|
2018-03-11 23:09:33 +00:00
|
|
|
call_argument = ["./pubeval/pubeval"]
|
2018-03-11 19:00:24 +00:00
|
|
|
for x in Board.board_features_to_pubeval(board, sym):
|
|
|
|
call_argument.append(str(x))
|
|
|
|
data = subprocess.check_output(call_argument)
|
2018-03-11 23:09:33 +00:00
|
|
|
moves_and_scores.append([board, float(bytes.decode(data))])
|
2018-03-11 19:00:24 +00:00
|
|
|
scores = [ x[1] for x in moves_and_scores ]
|
|
|
|
best_move_pair = moves_and_scores[np.array(scores).argmax()]
|
|
|
|
return best_move_pair
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-08 12:32:40 +00:00
|
|
|
|
2018-02-22 13:01:28 +00:00
|
|
|
def make_move(self, board, sym, roll):
|
|
|
|
# print(Board.pretty(board))
|
|
|
|
legal_moves = Board.calculate_legal_states(board, sym, roll)
|
2018-03-06 12:04:47 +00:00
|
|
|
moves_and_scores = [ (move, self.network.eval_state(np.array(move).reshape(1,26))) for move in legal_moves ]
|
|
|
|
scores = [ x[1] for x in moves_and_scores ]
|
2018-03-06 12:08:01 +00:00
|
|
|
best_move_pair = moves_and_scores[np.array(scores).argmax()]
|
2018-03-06 12:04:47 +00:00
|
|
|
#print("Found the best state, being:", np.array(move_scores).argmax())
|
2018-03-06 12:08:01 +00:00
|
|
|
return best_move_pair
|
2018-02-05 22:50:31 +00:00
|
|
|
|
2018-03-08 12:32:40 +00:00
|
|
|
|