2018-03-08 15:27:16 +00:00
|
|
|
import argparse
|
2018-03-08 16:13:25 +00:00
|
|
|
import sys
|
2018-03-09 20:05:38 +00:00
|
|
|
import time
|
2018-03-08 15:27:16 +00:00
|
|
|
|
2018-03-09 20:05:38 +00:00
|
|
|
def print_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),
|
2018-03-09 20:05:38 +00:00
|
|
|
'mean': sum(train_outcome) / len(train_outcome),
|
|
|
|
'time': int(time.time())
|
|
|
|
}
|
|
|
|
print("train;{time};{trained_eps};{count};{sum};{mean}".format(**format_vars))
|
2018-03-08 15:27:16 +00:00
|
|
|
|
2018-03-09 20:05:38 +00:00
|
|
|
def print_eval_outcomes(outcomes, trained_eps = 0):
|
2018-03-08 16:51:32 +00:00
|
|
|
for outcome in outcomes:
|
2018-03-08 15:27:16 +00:00
|
|
|
scores = outcome[1]
|
2018-03-09 20:05:38 +00:00
|
|
|
format_vars = { 'trained_eps': trained_eps,
|
2018-03-08 15:27:16 +00:00
|
|
|
'method': outcome[0],
|
|
|
|
'count': len(scores),
|
|
|
|
'sum': sum(scores),
|
2018-03-09 20:05:38 +00:00
|
|
|
'mean': sum(scores) / len(scores),
|
|
|
|
'time': int(time.time())
|
2018-03-08 15:27:16 +00:00
|
|
|
}
|
2018-03-09 20:05:38 +00:00
|
|
|
print("eval;{time};{method};{trained_eps};{count};{sum};{mean}".format(**format_vars))
|
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('--play', action='store_true',
|
|
|
|
help='whether to play with the neural network')
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
#print("-"*30)
|
|
|
|
#print(type(args.eval_methods))
|
|
|
|
#print(args.eval_methods)
|
|
|
|
#print("-"*30)
|
|
|
|
|
|
|
|
import game
|
|
|
|
g = game.Game(config = config)
|
|
|
|
g.set_up_bots()
|
|
|
|
|
|
|
|
episode_count = args.episode_count
|
|
|
|
|
|
|
|
if args.train:
|
|
|
|
eps = 0
|
|
|
|
while True:
|
2018-03-09 20:05:38 +00:00
|
|
|
train_outcome = g.train_model(episodes = episode_count, trained_eps = eps)
|
2018-03-08 15:27:16 +00:00
|
|
|
eps += episode_count
|
2018-03-09 20:05:38 +00:00
|
|
|
print_train_outcome(train_outcome, trained_eps = eps)
|
|
|
|
if args.eval:
|
|
|
|
eval_outcomes = g.eval(trained_eps = eps)
|
|
|
|
print_eval_outcomes(eval_outcomes, trained_eps = eps)
|
2018-03-08 16:13:25 +00:00
|
|
|
sys.stdout.flush()
|
2018-03-08 15:27:16 +00:00
|
|
|
elif args.eval:
|
|
|
|
outcomes = g.eval()
|
2018-03-09 20:05:38 +00:00
|
|
|
print_eval_outcomes(outcomes, trained_eps = 0)
|
2018-03-08 15:27:16 +00:00
|
|
|
#elif args.play:
|
|
|
|
# g.play(episodes = episode_count)
|
|
|
|
|