backgammon/bot.py

48 lines
1.5 KiB
Python
Raw Normal View History

from cup import Cup
2018-03-04 16:35:36 +00:00
from network import Network
2018-02-13 13:38:49 +00:00
from board import Board
2018-03-14 13:02:19 +00:00
import tensorflow as tf
import numpy as np
import random
class Bot:
2018-03-14 19:42:09 +00:00
def __init__(self, sym, config = None, name = "unnamed"):
2018-03-08 15:27:16 +00:00
self.config = config
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-14 19:42:09 +00:00
self.network = Network(self.session, config, name)
self.network.restore_model()
2018-03-14 19:42:09 +00:00
variables_names = [v.name for v in tf.trainable_variables()]
values = self.session.run(variables_names)
for k, v in zip(variables_names, values):
print("Variable: ", k)
print("Shape: ", v.shape)
print(v)
def restore_model(self):
with self.graph.as_default():
self.network.restore_model()
2018-03-04 16:35:36 +00:00
def get_session(self):
return self.session
def get_sym(self):
return self.sym
2018-03-04 16:35:36 +00:00
def get_network(self):
return self.network
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