update the policy
This commit is contained in:
commit
715f7be6a8
1
AlphaGo/.gitignore
vendored
1
AlphaGo/.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
data
|
||||
checkpoints
|
||||
checkpoints_origin
|
||||
|
@ -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":
|
||||
|
131
AlphaGo/play.py
131
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()
|
||||
# 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, result)
|
||||
print("COPY BLACK")
|
||||
match = re.search(pattern, move)
|
||||
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()
|
||||
#print "match : " + str(match.group())
|
||||
play_or_pass = match.group()
|
||||
pass_flag[turn] = False
|
||||
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
|
||||
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."
|
||||
|
37
AlphaGo/player.py
Normal file
37
AlphaGo/player.py
Normal file
@ -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
|
||||
|
@ -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()
|
@ -46,9 +46,9 @@ Tianshou(天授) is a reinforcement learning platform. The following image illus
|
||||
|
||||
Please follow [google python coding style](https://google.github.io/styleguide/pyguide.html)
|
||||
|
||||
Files should all be named with lower case letters and underline.
|
||||
All files/folders should be named with lower case letters and underline (except specified names such as `AlphaGo`).
|
||||
|
||||
Try to use full names. Don't use too many abbrevations for class/function/variable names except common abbrevations (such as `num` for number, `dim` for dimension, `env` for environment, `op` for operation). For now we use `pi` to refer to the policy in examples/ppo_example.py.
|
||||
Try to use full names. Don't use abbrevations for class/function/variable names except common abbrevations (such as `num` for number, `dim` for dimension, `env` for environment, `op` for operation). For now we use `pi` to refer to the policy in examples/ppo_example.py.
|
||||
|
||||
The """xxx""" comment should be written right after class/function. Also comment the part that's not intuitive during the code. We must comment, but for now we don't need to polish them.
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import tensorflow as tf, numpy as np
|
||||
import tensorflow as tf
|
||||
import numpy as np
|
||||
import time
|
||||
import gym
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user