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

Works for python 3 and is updated for Tensorflow API changes #5

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
12 changes: 6 additions & 6 deletions backgammon/agents/human_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ def __init__(self, player):

def get_action(self, moves, game=None):
if not moves:
raw_input("No moves for you...(hit enter)")
input("No moves for you...(hit enter)")
return None

while True:
while True:
mv1 = raw_input('Please enter a move "<location start>,<location end>" ("%s" for on the board, "%s" for off the board): ' % (Game.ON, Game.OFF))
mv1 = input('Please enter a move "<location start>,<location end>" ("%s" for on the board, "%s" for off the board): ' % (Game.ON, Game.OFF))
mv1 = self.get_formatted_move(mv1)
if not mv1:
print 'Bad format enter e.g. "3,4"'
print ('Bad format enter e.g. "3,4"')
else:
break

while True:
mv2 = raw_input('Please enter a second move (enter to skip): ')
mv2 = input('Please enter a second move (enter to skip): ')
if mv2 == '':
mv2 = None
break
mv2 = self.get_formatted_move(mv2)
if not mv2:
print 'Bad format enter e.g. "3,4"'
print ('Bad format enter e.g. "3,4"')
else:
break

Expand All @@ -41,7 +41,7 @@ def get_action(self, moves, game=None):
move = move[::-1]
return move
else:
print "You can't play that move"
print ("You can't play that move")

return None

Expand Down
32 changes: 16 additions & 16 deletions backgammon/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,37 +315,37 @@ def is_valid_move(self, start, end, token):
return False

def draw_col(self,i,col):
print "|",
print ("|", end = "")
if i==-2:
if col<10:
print "",
print str(col),
print ("", end = "")
print (str(col), end = "")
elif i==-1:
print "--",
print ("--", end = "")
elif len(self.grid[col])>i:
print " "+self.grid[col][i],
print (" "+self.grid[col][i], end = "")
else:
print " ",
print (" ", end = "")

def draw(self):
os.system('clear')
largest = max([len(self.grid[i]) for i in range(len(self.grid)/2,len(self.grid))])
largest = max([len(self.grid[i]) for i in range(int(len(self.grid)/2),int(len(self.grid)))])
for i in range(-2,largest):
for col in range(len(self.grid)/2,len(self.grid)):
for col in range(int(len(self.grid)/2),int(len(self.grid))):
self.draw_col(i,col)
print "|"
print ("|")
print
print
largest = max([len(self.grid[i]) for i in range(len(self.grid)/2)])
largest = max([len(self.grid[i]) for i in range(int(len(self.grid)/2))])
for i in range(largest-1,-3,-1):
for col in range(len(self.grid)/2-1,-1,-1):
for col in range(int(len(self.grid)/2-1),-1,-1):
self.draw_col(i,col)
print "|"
print ("|")
for t in self.players:
print "<Player %s> Off Board : "%(t),
print ("<Player %s> Off Board : "%(t), end = "")
for piece in self.off_pieces[t]:
print t+'',
print " Bar : ",
print (t+'', end = "")
print (" Bar : ", end = "")
for piece in self.bar_pieces[t]:
print t+'',
print (t+'', end = "")
print
30 changes: 15 additions & 15 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def __init__(self, sess, model_path, summary_path, checkpoint_path, restore=Fals
alpha = tf.maximum(0.01, tf.train.exponential_decay(0.1, self.global_step, \
40000, 0.96, staircase=True), name='alpha')

tf.scalar_summary('lambda', lamda)
tf.scalar_summary('alpha', alpha)
tf.summary.scalar('lambda', lamda)
tf.summary.scalar('alpha', alpha)

# describe network size
layer_size_input = 294
Expand All @@ -57,8 +57,8 @@ def __init__(self, sess, model_path, summary_path, checkpoint_path, restore=Fals
self.V = dense_layer(prev_y, [layer_size_hidden, layer_size_output], tf.sigmoid, name='layer2')

# watch the individual value predictions over time
tf.scalar_summary('V_next', tf.reduce_sum(self.V_next))
tf.scalar_summary('V', tf.reduce_sum(self.V))
tf.summary.scalar('V_next', tf.reduce_sum(self.V_next))
tf.summary.scalar('V', tf.reduce_sum(self.V))

# delta = V_next - V
delta_op = tf.reduce_sum(self.V_next - self.V, name='delta')
Expand Down Expand Up @@ -94,12 +94,12 @@ def __init__(self, sess, model_path, summary_path, checkpoint_path, restore=Fals
delta_avg_ema_op = delta_avg_ema.apply([delta_avg_op])
accuracy_avg_ema_op = accuracy_avg_ema.apply([accuracy_avg_op])

tf.scalar_summary('game/loss_avg', loss_avg_op)
tf.scalar_summary('game/delta_avg', delta_avg_op)
tf.scalar_summary('game/accuracy_avg', accuracy_avg_op)
tf.scalar_summary('game/loss_avg_ema', loss_avg_ema.average(loss_avg_op))
tf.scalar_summary('game/delta_avg_ema', delta_avg_ema.average(delta_avg_op))
tf.scalar_summary('game/accuracy_avg_ema', accuracy_avg_ema.average(accuracy_avg_op))
tf.summary.scalar('game/loss_avg', loss_avg_op)
tf.summary.scalar('game/delta_avg', delta_avg_op)
tf.summary.scalar('game/accuracy_avg', accuracy_avg_op)
tf.summary.scalar('game/loss_avg_ema', loss_avg_ema.average(loss_avg_op))
tf.summary.scalar('game/delta_avg_ema', delta_avg_ema.average(delta_avg_op))
tf.summary.scalar('game/accuracy_avg_ema', accuracy_avg_ema.average(accuracy_avg_op))

# reset per-game monitoring variables
game_step_reset_op = game_step.assign(0.0)
Expand All @@ -115,8 +115,8 @@ def __init__(self, sess, model_path, summary_path, checkpoint_path, restore=Fals

# watch the weight and gradient distributions
for grad, var in zip(grads, tvars):
tf.histogram_summary(var.name, var)
tf.histogram_summary(var.name + '/gradients/grad', grad)
tf.summary.histogram(var.name, var)
tf.summary.histogram(var.name + '/gradients/grad', grad)

# for each variable, define operations to update the var with delta,
# taking into account the gradient as part of the eligibility trace
Expand All @@ -127,11 +127,11 @@ def __init__(self, sess, model_path, summary_path, checkpoint_path, restore=Fals
# e-> = lambda * e-> + <grad of output w.r.t weights>
trace = tf.Variable(tf.zeros(grad.get_shape()), trainable=False, name='trace')
trace_op = trace.assign((lamda * trace) + grad)
tf.histogram_summary(var.name + '/traces', trace)
tf.summary.histogram(var.name + '/traces', trace)

# grad with trace = alpha * delta * e
grad_trace = alpha * delta_op * trace_op
tf.histogram_summary(var.name + '/gradients/trace', grad_trace)
tf.summary.histogram(var.name + '/gradients/trace', grad_trace)

grad_apply = var.assign_add(grad_trace)
apply_gradients.append(grad_apply)
Expand All @@ -151,7 +151,7 @@ def __init__(self, sess, model_path, summary_path, checkpoint_path, restore=Fals
self.train_op = tf.group(*apply_gradients, name='train')

# merge summaries for TensorBoard
self.summaries_op = tf.merge_all_summaries()
self.summaries_op = tf.summary.merge_all()

# create a saver for periodic checkpoints
self.saver = tf.train.Saver(max_to_keep=1)
Expand Down