diff --git a/app.py b/app.py new file mode 100644 index 0000000..1cabd49 --- /dev/null +++ b/app.py @@ -0,0 +1,188 @@ +from flask import Flask, request, jsonify +from flask_json import FlaskJSON, as_json_p +from flask_cors import CORS +from board import Board +import tensorflow as tf +from eval import Eval +import argparse +import main +import random +from network import Network + +parser = argparse.ArgumentParser(description="Backgammon games") + +parser.add_argument('--model', action='store', dest='model', + default='player_testings', + help='name of Tensorflow model to use') + +parser.add_argument('--board-rep', action='store', dest='board_rep', + default='tesauro', + help='name of board representation to use as input to neural network') + + +args = parser.parse_args() + + +app = Flask(__name__) + + +app.config['JSON_ADD_STATUS'] = False +app.config['JSON_JSONP_OPTIONAL'] = False + +json = FlaskJSON(app) +CORS(app) + +config = main.config.copy() +config['model'] = args.model +config['board_representation'] = args.board_rep + +network = Network(config, config['model']) + +sess = tf.Session() +network.restore_model(sess) +sess.run(tf.global_variables_initializer()) + + + +def calc_move_sets(from_board, roll, player): + board = from_board + sets = [] + total = 0 + for r in roll: + # print("Value of r:", r) + sets.append([Board.calculate_legal_states(board, player, [r, 0]), r]) + total += r + sets.append([Board.calculate_legal_states(board, player, roll), total]) + return sets + + +def tmp_name(from_board, to_board, roll, player, total_moves, is_quad=False): + sets = calc_move_sets(from_board, roll, player) + return_board = from_board + print("To board:\n",to_board) + print("All sets:\n",sets) + for idx, board_set in enumerate(sets): + board_set[0] = list(board_set[0]) + # print(to_board) + # print(board_set) + if to_board in board_set[0]: + # print("To board:", to_board) + # print(board_set[0]) + # print(board_set[1]) + total_moves -= board_set[1] + # if it's not the sum of the moves + if idx < (4 if is_quad else 2): + roll[idx] = 0 + else: + roll = [0, 0] + return_board = to_board + break + + # print("Return board!:\n",return_board) + return total_moves, roll, return_board + +def calc_move_stuff(from_board, to_board, roll, player, total_roll, is_quad): + + total_moves, roll, board = tmp_name(from_board, to_board, list(roll), player, total_roll, is_quad) + return board, total_moves, roll + + +@app.route('/get_board', methods=['GET']) +@as_json_p +def get_board(): + return {'board':'0, 2, 0, 0, 0, 0, -5, 0, -3, 0, 0, 0, 5, -5, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, -2, 0'} + + + +def check_move(prev, curr): + + # TODO: Decide on player system and implement roll properly + legal_states = Board.calculate_legal_states(tuple(prev), -1, [1,2]) + + truth_list = [list(curr) == list(ele) for ele in legal_states] + + return any(truth_list) + + +@app.route('/pubeval_move', methods=['POST']) +def pubeval_move(): + + data = request.get_json(force=True) + board = [int(x) for x in data['board'].split(',')] + player = int(data['player']) + + roll = (random.randrange(1,7), random.randrange(1,7)) + + board, value = Eval.make_pubeval_move(tuple(board), player, roll) + print("Doing pubeval move") + return ",".join([str(x) for x in list(board)]) + + +@app.route('/network_move', methods=['POST']) +def network_move(): + + data = request.get_json(force=True) + board = [int(x) for x in data['board'].split(',')] + player = int(data['player']) + + roll = (random.randrange(1,7), random.randrange(1,7)) + + board, value = network.make_move(sess, tuple(board), roll, player) + + print("Doing network move") + return ",".join([str(x) for x in list(board)]) + + +@app.route('/bot_move', methods=['POST']) +def bot_move(): + data = request.get_json(force=True) + + board = [int(x) for x in data['board'].split(',')] + use_pubeval = bool(data['pubeval']) + + roll = (random.randrange(1, 7), random.randrange(1, 7)) + + if use_pubeval: + board, value = Eval.make_pubeval_move(tuple(board), 1, roll) + else: + board, _ = network.make_move(sess, tuple(board), roll, 1) + + # print("Board!:",board) + + return ",".join([str(x) for x in list(board)]) + + + +@app.route('/post_board', methods=['POST']) +def post_board(): + data = request.get_json(force=True) + + # TODO: Fix hardcoded player + player = -1 + + board = [int(x) for x in data['board'].split(',')] + prev_board = [int(x) for x in data['prevBoard'].split(',')] + print(data['roll']) + roll = [int(x) for x in data['roll'].split(',')] + print(roll) + quad = data['quad'] == "true" + + + # print(board) + + total_roll = int(data['totalRoll']) + print("total roll is:", total_roll) + return_board, total_moves, roll = calc_move_stuff(tuple(prev_board), tuple(board), tuple(roll), player, total_roll, quad) + + str_board = ",".join([str(x) for x in return_board]) + str_roll = ",".join([str(x) for x in roll]) + + + return_string = str_board + "#" + str(total_moves) + "#" + str_roll + + print(return_string) + + return return_string + +if __name__ == '__main__': + app.run(host = '0.0.0.0', port=35270) diff --git a/board.py b/board.py index f1e4c68..07ac630 100644 --- a/board.py +++ b/board.py @@ -97,7 +97,7 @@ class Board: board_rep += bar_trans(board, player) board_rep += (15 - Board.num_of_checkers_for_player(board, player),) - board_rep += ([1,0] if cur_player == 1 else [0,1]) + board_rep += ([1,0] if cur_player == 1 else [1,0]) return np.array(board_rep).reshape(1,198) diff --git a/network.py b/network.py index 84802e3..03c2020 100644 --- a/network.py +++ b/network.py @@ -139,22 +139,6 @@ class Network: if os.path.isfile(episode_count_path): with open(episode_count_path, 'r') as f: self.config['start_episode'] = int(f.read()) - elif self.config['use_baseline'] and glob.glob(os.path.join(os.path.join(self.config['model_storage_path'], "baseline_model"), 'model.ckpt*.index')): - checkpoint_path = os.path.join(self.config['model_storage_path'], "baseline_model") - latest_checkpoint = tf.train.latest_checkpoint(checkpoint_path) - print("[NETWK] ({name}) Restoring model from:".format(name=self.name), - str(latest_checkpoint)) - self.saver.restore(sess, latest_checkpoint) - - variables_names = [v.name for v in tf.trainable_variables()] - values = sess.run(variables_names) - for k, v in zip(variables_names, values): - print("Variable: ", k) - print("Shape: ", v.shape) - print(v) - elif not self.config['force_creation']: - print("You need to have baseline_model inside models") - exit() def make_move(self, sess, board, roll, player):