backgammon/main.py

104 lines
3.7 KiB
Python
Raw Normal View History

2018-03-08 15:27:16 +00:00
import argparse
2018-03-08 16:13:25 +00:00
import sys
import os
import time
2018-03-08 15:27:16 +00:00
# Define helper functions
def log_train_outcome(outcome, trained_eps = 0):
format_vars = { 'trained_eps': trained_eps,
2018-03-08 15:27:16 +00:00
'count': len(train_outcome),
'sum': sum(train_outcome),
'mean': sum(train_outcome) / len(train_outcome),
'time': int(time.time())
}
with open(os.path.join(config['model_path'], 'logs', "train.log"), 'a+') as f:
f.write("{time};{trained_eps};{count};{sum};{mean}".format(**format_vars) + "\n")
2018-03-08 15:27:16 +00:00
def log_eval_outcomes(outcomes, trained_eps = 0):
2018-03-08 16:51:32 +00:00
for outcome in outcomes:
scores = outcome[1]
format_vars = { 'trained_eps': trained_eps,
'method': outcome[0],
'count': len(scores),
'sum': sum(scores),
'mean': sum(scores) / len(scores),
'time': int(time.time())
}
with open(os.path.join(config['model_path'], 'logs', "eval.log"), 'a+') as f:
f.write("{time};{method};{trained_eps};{count};{sum};{mean}".format(**format_vars) + "\n")
2018-03-08 15:27:16 +00:00
# Parse command line arguments
2018-03-08 15:27:16 +00:00
parser = argparse.ArgumentParser(description="Backgammon games")
parser.add_argument('--episodes', action='store', dest='episode_count',
type=int, default=1000,
help='number of episodes to train')
parser.add_argument('--model-path', action='store', dest='model_path',
default='./model',
help='path to Tensorflow model')
parser.add_argument('--eval-methods', action='store',
default=['random'], nargs='*',
help='specifies evaluation methods')
parser.add_argument('--eval', action='store_true',
help='whether to evaluate the neural network with a random choice bot')
parser.add_argument('--train', action='store_true',
help='whether to train the neural network')
parser.add_argument('--eval-after-train', action='store_true', dest='eval_after_train',
help='whether to evaluate after each training session')
2018-03-08 15:27:16 +00:00
parser.add_argument('--play', action='store_true',
help='whether to play with the neural network')
parser.add_argument('--start-episode', action='store', dest='start_episode',
type=int, default=0,
help='episode count to start at; purely for display purposes')
2018-03-08 15:27:16 +00:00
args = parser.parse_args()
config = {
'model_path': args.model_path,
'episode_count': args.episode_count,
'eval_methods': args.eval_methods,
'train': args.train,
'play': args.play,
'eval': args.eval,
'eval_after_train': args.eval_after_train,
'start_episode': args.start_episode
2018-03-08 15:27:16 +00:00
}
# Make sure directories exist
model_path = os.path.join(config['model_path'])
log_path = os.path.join(model_path, 'logs')
if not os.path.isdir(model_path):
os.mkdir(model_path)
if not os.path.isdir(log_path):
os.mkdir(log_path)
2018-03-08 15:27:16 +00:00
# Set up game
2018-03-08 15:27:16 +00:00
import game
g = game.Game(config = config)
g.set_up_bots()
# Set up variables
episode_count = config['episode_count']
# Do actions specified by command-line
2018-03-08 15:27:16 +00:00
if args.train:
eps = config['start_episode']
2018-03-08 15:27:16 +00:00
while True:
train_outcome = g.train_model(episodes = episode_count, trained_eps = eps)
2018-03-08 15:27:16 +00:00
eps += episode_count
log_train_outcome(train_outcome, trained_eps = eps)
if config['eval_after_train']:
eval_outcomes = g.eval(trained_eps = eps)
log_eval_outcomes(eval_outcomes, trained_eps = eps)
2018-03-08 15:27:16 +00:00
elif args.eval:
eps = config['start_episode']
2018-03-08 15:27:16 +00:00
outcomes = g.eval()
log_eval_outcomes(outcomes, trained_eps = eps)
2018-03-08 15:27:16 +00:00
#elif args.play:
# g.play(episodes = episode_count)