Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --random_selfplay option to play 1 game with random agents, which #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion backgammon/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Game:
OFF = 'off'
ON = 'on'
TOKENS = ['x', 'o']
SLEEP = 1

def __init__(self, layout=LAYOUT, grid=None, off_pieces=None, bar_pieces=None, num_pieces=None, players=None):
"""
Expand Down Expand Up @@ -80,11 +81,14 @@ def next_step(self, player, player_num, draw=False):
def take_turn(self, player, roll, draw=False):
if draw:
print("Player %s rolled <%d, %d>." % (player.player, roll[0], roll[1]))
time.sleep(1)
time.sleep(self.SLEEP)

moves = self.get_actions(roll, player.player, nodups=True)
move = player.get_action(moves, self) if moves else None

if draw:
print("Player %s played %s" % (player.player, str(move)))

if move:
self.take_action(move, player.player)

Expand Down
3 changes: 3 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

flags.DEFINE_boolean('test', False, 'If true, test against a random strategy.')
flags.DEFINE_boolean('play', False, 'If true, play against a trained TD-Gammon strategy.')
flags.DEFINE_boolean('random_selfplay', False, 'Watch 2 random agents play')
flags.DEFINE_boolean('restore', False, 'If true, restore a checkpoint before training.')

model_path = os.environ.get('MODEL_PATH', 'models/')
Expand All @@ -32,5 +33,7 @@
model.test(episodes=1000)
elif FLAGS.play:
model.play()
elif FLAGS.random_selfplay:
model.random_selfplay()
else:
model.train()
6 changes: 6 additions & 0 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ def test(self, episodes=100, draw=False):
winners[0], winners[1], winners_total, \
(winners[0] / winners_total) * 100.0))

def random_selfplay(self):
players = [RandomAgent(Game.TOKENS[0]), RandomAgent(Game.TOKENS[1])]
game = Game.new()
game.SLEEP = 0
winner = game.play(players, draw=True)

def train(self):
tf.train.write_graph(self.sess.graph_def, self.model_path, 'td_gammon.pb', as_text=False)
summary_writer = tf.train.SummaryWriter('{0}{1}'.format(self.summary_path, int(time.time()), self.sess.graph_def))
Expand Down