diff --git a/AlphaGo/.gitignore b/AlphaGo/.gitignore index 9ba06a7..9c2fe16 100644 --- a/AlphaGo/.gitignore +++ b/AlphaGo/.gitignore @@ -1,2 +1,3 @@ data checkpoints +checkpoints_origin diff --git a/AlphaGo/engine.py b/AlphaGo/engine.py index 97f625b..716d40b 100644 --- a/AlphaGo/engine.py +++ b/AlphaGo/engine.py @@ -186,8 +186,7 @@ class GTPEngine(): return self._game.executor.get_score(), None def cmd_show_board(self, args, **kwargs): - self._game.show_board() - return None, None + return self._game.board, True if __name__ == "main": diff --git a/AlphaGo/play.py b/AlphaGo/play.py index 1d7f69f..18ce869 100644 --- a/AlphaGo/play.py +++ b/AlphaGo/play.py @@ -1,89 +1,70 @@ import subprocess import sys import re +import Pyro4 import time + +#start a name server to find the remote object +kill_old_server = subprocess.Popen(['killall', 'pyro4-ns']) +print "kill old server, the return code is : " + str(kill_old_server.wait()) +time.sleep(1) +start_new_server = subprocess.Popen(['pyro4-ns', '&']) +print "Start Name Sever : " + str(start_new_server.pid)# + str(start_new_server.wait()) +time.sleep(1) +agent_v0 = subprocess.Popen(['python', '-u', 'player.py', '--role=black'], + stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) +time.sleep(3) +print "Start Player 0 at : " + str(agent_v0.pid) +agent_v1 = subprocess.Popen(['python', '-u', 'player.py', '--role=white', '--checkpoint_path=./checkpoints_origin/'], + stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) +time.sleep(3) +print "Start Player 1 at : " + str(agent_v1.pid) + +player = [None] * 2 +player[0] = Pyro4.Proxy("PYRONAME:black") +player[1] = Pyro4.Proxy("PYRONAME:white") + +role = ["BLACK", "WHITE"] +color = ['b', 'w'] + pattern = "[A-Z]{1}[0-9]{1}" size = 9 -agent_v1 = subprocess.Popen(['python', '-u', 'test.py'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) -agent_v0 = subprocess.Popen(['python', '-u', 'test.py', '--checkpoint_path=./checkpoints_origin/'], stdin=subprocess.PIPE, - stdout=subprocess.PIPE, stderr=subprocess.STDOUT) +show = ['.', 'X', 'O'] - -num = 0 game_num = 0 -black_pass = False -white_pass = False - - -while game_num < 10: +while game_num < 1: + num = 0 + pass_flag = [False, False] print("Start game {}".format(game_num)) - while not (black_pass and white_pass) and num < size ** 2 * 2: - print(num) - if num % 2 == 0: - print('BLACK TURN') - agent_v1.stdin.write(str(num) + ' genmove b\n') - agent_v1.stdin.flush() - result = agent_v1.stdout.readline() - sys.stdout.write(result) - sys.stdout.flush() - num += 1 - match = re.search(pattern, result) - print("COPY BLACK") - if match is not None: - agent_v0.stdin.write(str(num) + ' play b ' + match.group() + '\n') - agent_v0.stdin.flush() - result = agent_v0.stdout.readline() - sys.stdout.flush() - else: - agent_v0.stdin.write(str(num) + ' play b PASS\n') - agent_v0.stdin.flush() - result = agent_v0.stdout.readline() - sys.stdout.flush() - if re.search("pass", result) is not None: - black_pass = True - else: - black_pass = False + # end the game if both palyer chose to pass, or play too much turns + while not (pass_flag[0] and pass_flag[1]) and num < size ** 2 * 2: + turn = num % 2 + move = player[turn].run_cmd(str(num) + ' genmove ' + color[turn] + '\n') + print role[turn] + " : " + str(move), + num += 1 + match = re.search(pattern, move) + if match is not None: + #print "match : " + str(match.group()) + play_or_pass = match.group() + pass_flag[turn] = False else: - print('WHITE TURN') - agent_v0.stdin.write(str(num) + ' genmove w\n') - agent_v0.stdin.flush() - result = agent_v0.stdout.readline() - sys.stdout.write(result) - sys.stdout.flush() - num += 1 - match = re.search(pattern, result) - print("COPY WHITE") - if match is not None: - agent_v1.stdin.write(str(num) + ' play w ' + match.group() + '\n') - agent_v1.stdin.flush() - result = agent_v1.stdout.readline() - sys.stdout.flush() - else: - agent_v1.stdin.write(str(num) + ' play w PASS\n') - agent_v1.stdin.flush() - result = agent_v1.stdout.readline() - sys.stdout.flush() - if re.search("pass", result) is not None: - black_pass = True - else: - black_pass = False + #print "no match" + play_or_pass = ' PASS' + pass_flag[turn] = True + result = player[1 - turn].run_cmd(str(num) + ' play ' + color[turn] + ' ' + play_or_pass + '\n') + board = player[turn].run_cmd(str(num) + ' show_board') + board = eval(board[board.index('['):board.index(']') + 1]) + for i in range(size): + for j in range(size): + print show[board[i * size + j]] + " ", + print "\n", - print("Finished") - print("\n") - - agent_v1.stdin.write('clear_board\n') - agent_v1.stdin.flush() - result = agent_v1.stdout.readline() - sys.stdout.flush() - - agent_v0.stdin.write('clear_board\n') - agent_v0.stdin.flush() - result = agent_v0.stdout.readline() - sys.stdout.flush() - - agent_v1.stdin.write('get_score\n') - agent_v1.stdin.flush() - result = agent_v1.stdout.readline() - sys.stdout.write(result) - sys.stdout.flush() + score = player[turn].run_cmd(str(num) + ' get_score') + print "Finished : ", score.split(" ")[1] + player[0].run_cmd(str(num) + ' clear_board') + player[1].run_cmd(str(num) + ' clear_board') game_num += 1 + +subprocess.call(["kill", "-9", str(agent_v0.pid)]) +subprocess.call(["kill", "-9", str(agent_v1.pid)]) +print "Kill all player, finish all game." diff --git a/AlphaGo/player.py b/AlphaGo/player.py new file mode 100644 index 0000000..36965a9 --- /dev/null +++ b/AlphaGo/player.py @@ -0,0 +1,37 @@ +import argparse +import time +import sys +import Pyro4 + +from game import Game +from engine import GTPEngine + +@Pyro4.expose +class Player(object): + def __init__(self, **kwargs): + self.role = kwargs['role'] + self.engine = kwargs['engine'] + + def run_cmd(self, command): + #return "inside the Player of player.py" + return self.engine.run_cmd(command) + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument("--checkpoint_path", type=str, default="./checkpoints/") + parser.add_argument("--role", type=str, default="unknown") + args = parser.parse_args() + + game = Game(checkpoint_path=args.checkpoint_path) + engine = GTPEngine(game_obj=game, name='tianshou', version=0) + + daemon = Pyro4.Daemon() # make a Pyro daemon + ns = Pyro4.locateNS() # find the name server + player = Player(role = args.role, engine = engine) + print "Init " + args.role + " player finished" + uri = daemon.register(player) # register the greeting maker as a Pyro object + print "Start on name " + args.role + ns.register(args.role, uri) # register the object with a name in the name server + print "Start Request Loop " + str(uri) + daemon.requestLoop() # start the event loop of the server to wait for calls + diff --git a/AlphaGo/test.py b/AlphaGo/test.py deleted file mode 100644 index 16230b6..0000000 --- a/AlphaGo/test.py +++ /dev/null @@ -1,19 +0,0 @@ -import sys -from game import Game -from engine import GTPEngine -# import utils -import argparse -import time - -parser = argparse.ArgumentParser() -parser.add_argument("--checkpoint_path", type=str, default="./checkpoints/") -args = parser.parse_args() - -game = Game(checkpoint_path=args.checkpoint_path) -engine = GTPEngine(game_obj=game, name='tianshou', version=0) - -while not engine.disconnect: - command = sys.stdin.readline() - result = engine.run_cmd(command) - sys.stdout.write(result) - sys.stdout.flush()