def play(self): for i in range(self.G): self.initiate_game_manager() self.current_state = self.game_manager.initial_state() self.root = Node(self.current_state, None) self.simulator = GameSimulator(self.M, self.game_manager) self.starting_player = self.game_manager.current_player print(f"Starting player:\t P{self.game_manager.current_player}") if (self.verbose): print(self.game_manager.get_start_position_as_string()) self.single_game() print("--------------------------------------------------------------") print(f"Total games:\t\t\t\t {self.G}") print(f"Games won by Player 1:\t\t\t {self.p1_winner_count}") print( f"Games where Player 1 had first move:\t {self.p1_starting_count}") print(f"Games won by Player 2:\t\t\t {self.p2_winner_count}") print( f"Games where Player 2 had first move:\t {self.p2_starting_count}") print( f"Percentage of games won by the player who had first move: {100*self.correct_winner_count/self.G} %" ) print("--------------------------------------------------------------")
def clean(self): result = cherrypy.request.json nickname = cherrypy.request.headers['Nickname'] game_simulator = self.games.get_game(nickname) game_simulator.clean() game_simulator = GameSimulator() return result
def sim(depth, conf): print "Trying for depth " + str(depth) + "..." simulator = GameSimulator(Game) winner, conf_arr = simulator.simulate(conf, depth) if (winner == 0): print "Found winning strategy for FIRST player." elif (winner == 1): print "Found winning strategy for SECOND player." else: print "No winning strategy found" print "" print_strategy(conf_arr) return False print "" print_strategy(conf_arr) return True
def nickname(self): nickname = cherrypy.request.json print('nickname') print(nickname) if self.games.exists(nickname): raise cherrypy.HTTPError(message='Invalid nickname') self.games.set_game(nickname, GameSimulator()) return nickname
def sim(depth,conf): print "Trying for depth " + str(depth) + "..." simulator = GameSimulator(Game) winner, conf_arr = simulator.simulate(conf,depth) if(winner == 0): print "Found winning strategy for FIRST player." elif(winner == 1): print "Found winning strategy for SECOND player." else: print "No winning strategy found" print "" print_strategy(conf_arr) return False print "" print_strategy(conf_arr) return True
class Game: def __init__(self): self.conf = json.loads(open("conf.json", "r").read()) self.G = self.conf["G"] self.M = self.conf["M"] self.game_type = self.conf["GameType"] self.verbose = self.conf["Verbose"] self.correct_winner_count = 0 self.p1_winner_count = 0 self.p1_starting_count = 0 self.p2_winner_count = 0 self.p2_starting_count = 0 def initiate_game_manager(self): if (self.game_type == "NIM"): self.game_manager = NIMManager(self.conf["NIM"]["N"], self.conf["NIM"]["K"], self.conf["P"]) elif (self.game_type == "Ledge"): self.game_manager = LedgeManager(self.conf["Ledge"]["B"], self.conf["P"]) else: print(f"Does not support {self.game_type} yet.") def play(self): for i in range(self.G): self.initiate_game_manager() self.current_state = self.game_manager.initial_state() self.root = Node(self.current_state, None) self.simulator = GameSimulator(self.M, self.game_manager) self.starting_player = self.game_manager.current_player print(f"Starting player:\t P{self.game_manager.current_player}") if (self.verbose): print(self.game_manager.get_start_position_as_string()) self.single_game() print("--------------------------------------------------------------") print(f"Total games:\t\t\t\t {self.G}") print(f"Games won by Player 1:\t\t\t {self.p1_winner_count}") print( f"Games where Player 1 had first move:\t {self.p1_starting_count}") print(f"Games won by Player 2:\t\t\t {self.p2_winner_count}") print( f"Games where Player 2 had first move:\t {self.p2_starting_count}") print( f"Percentage of games won by the player who had first move: {100*self.correct_winner_count/self.G} %" ) print("--------------------------------------------------------------") def single_game(self): while not self.game_manager.is_finished(self.root.game_state): self.simulator.run_simulations(self.root) old_game_state = self.root.game_state self.root = self.root.best_traverse( ) # self.root.most_visited_child() self.root.set_as_root() new_game_state = self.root.game_state if (self.verbose): print( self.game_manager.get_move_as_string( old_game_state, new_game_state)) winner = self.game_manager.get_winner(self.root.game_state) self.update_game_stats(winner) print(f"Winner:\t\t\t P{winner}") print("-----------\n") def update_game_stats(self, winner): if (self.starting_player == 1): self.p1_starting_count += 1 else: self.p2_starting_count += 1 if (winner == 1): self.p1_winner_count += 1 else: self.p2_winner_count += 1 if (winner == self.starting_player): self.correct_winner_count += 1
for idx,var in enumerate(tfVars[0:total_vars//2]): op_holder.append(tfVars[idx+total_vars//2].assign((var.value()*tau) + ((1-tau)*tfVars[idx+total_vars//2].value()))) return op_holder def updateTarget(op_holder,sess): for op in op_holder: sess.run(op) def saveScore(score): my_file = open(reward_savefile, 'a') # Name and path of the reward text file my_file.write("%s\n" % score) my_file.close() ########################################### game = GameSimulator() game.initialize() ACTION_COUNT = game.get_action_size() gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.33) SESSION = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) if LOAD_MODEL: EPSILON_MAX = 0.25 # restart after 20+ epoch agent = Agent(memory_cap = MEMORY_CAP, batch_size = BATCH_SIZE, resolution = RESOLUTION, action_count = ACTION_COUNT, session = SESSION, lr = LEARNING_RATE, gamma = GAMMA, epsilon_min = EPSILON_MIN, trace_length=TRACE_LENGTH, epsilon_decay_steps = EPSILON_DECAY_STEPS, epsilon_max=EPSILON_MAX, hidden_size=HIDDEN_SIZE)
def __init__(self): self.games = GameSession() self.game_simulator = GameSimulator()
bob = BigBob() bob.credits = 10000 carl = CarefulCarl() carl.credits = 10000 adam = AlloutAdam() adam.credits = 10000 melanie = MiddleMelanie() melanie.credits = 10000 # Play a bunch of rounds for game in range(1, 10000): # Generate a new set of payouts payouts = GameSimulator.generate_payouts() # From those payouts generate weighted_power values weighted_powers = GameSimulator.get_weighted_powers(payouts) # Let each player place their bets. bob.place_bets(payouts) carl.place_bets(payouts) adam.place_bets(payouts) melanie.place_bets(payouts) # BANG!! And Their Off!! winners = GameSimulator.play(weighted_powers) # Let each player check to see if they won anything. bob.check_win(winners)
def updateTarget(op_holder, sess): for op in op_holder: sess.run(op) def saveScore(score): my_file = open(reward_savefile, 'a') # Name and path of the reward text file my_file.write("%.1f (±%.1f) min:%.1f max:%.1f\n" % (score.mean(), score.std(), score.min(), score.max())) my_file.close() ########################################### game = GameSimulator() game.initialize() ACTION_COUNT = game.get_action_size() print("game.get_action_size()---------------------------") print(ACTION_COUNT) gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.33) SESSION = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) if LOAD_MODEL: EPSILON_MAX = 0.1 # restart after 20+ epoch agent = Agent(memory_cap=MEMORY_CAP, batch_size=BATCH_SIZE,
def updateTarget(op_holder, sess): for op in op_holder: sess.run(op) def saveScore(score): my_file = open(reward_savefile, 'a') # Name and path of the reward text file my_file.write("%s\n" % score) my_file.close() ########################################### game = GameSimulator() game.initialize() ACTION_COUNT = game.get_action_size() gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.33) SESSION = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) if LOAD_MODEL: EPSILON_MAX = 0.25 # restart after 20+ epoch agent = Agent(memory_cap=MEMORY_CAP, batch_size=BATCH_SIZE, resolution=RESOLUTION, action_count=ACTION_COUNT,
def updateTarget(op_holder, sess): for op in op_holder: sess.run(op) def saveScore(score): my_file = open(reward_savefile, 'a') # Name and path of the reward text file my_file.write("%s\n" % score) my_file.close() ########################################### game = GameSimulator() game.initialize() ACTION_COUNT = game.get_action_size() gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.33) SESSION = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) agent = Agent(memory_cap=MEMORY_CAP, batch_size=BATCH_SIZE, resolution=RESOLUTION, action_count=ACTION_COUNT, session=SESSION, lr=LEARNING_RATE, gamma=GAMMA,