def simulate(self, history): if util.is_terminal(history): return self.handle_terminal_state(history) player = util.player(history) if self.out_of_tree[player]: return self.rollout(history) player_history = util.information_function(history, player) player_tree = get_tree(player) if player_history in player_tree and player_tree[player_history].children: action = self.select(history) else: expand(player_one_tree, history, 1) expand(player_two_tree, history, -1) action = random.choice(util.get_available_actions(history)) if player != 0: self.out_of_tree[1] = True self.out_of_tree[-1] = True new_history = history + action running_reward = evaluator.calculate_reward_full_info(history) + self.discount_factor * self.simulate(new_history) update_player_tree(history, action, 1, running_reward) update_player_tree(history, action, -1, running_reward) return running_reward
def get_best_action_ucb(self, history, player, tree): player_history = util.information_function(history, player) best_value = float('-inf') best_action = None for action in util.get_available_actions(history): node_val = self.calculate_next_node_value(tree, player_history, action, player) if node_val > best_value: best_action = action best_value = node_val return best_action
def expand(tree, history, player): player_history = util.information_function(history, player) if player_history not in tree: tree[player_history] = potree.PoNode() for action in util.get_available_actions(player_history, player=player): new_history = player_history + action if new_history not in tree: tree[new_history] = potree.PoNode() tree[player_history].children.add(new_history)
def get_game_state(self): return { "p1_card": self.p1_card + ".svg", "p2_card": self.get_player_two_display_card(), "textbox": self.display_text, "pot": evaluator.get_pot(self.history), "public": self.pub_card, "actions": util.get_available_actions(self.history), "winnings": self.total_winnings }
def select(self, history): player = util.player(history) player_history = util.information_function(history, player) if player in {-1, 1}: tree = get_tree(player) eta_sub_expression = math.pow(1 + (.1 * math.sqrt(tree[player_history].visitation_count)), -1) eta = max((GAMMA, .9 * eta_sub_expression)) z = random.uniform(0, 1) if z < eta: return self.get_best_action_ucb(history, player, tree) else: return self.get_best_action_avg_strategy(player_history, tree) else: return random.choice(util.get_available_actions(history))
def build_tree(history, tree): if history not in tree: tree[history] = potree.PoNode() if not util.is_terminal(history): actions = util.get_available_actions(history) for action in actions: child = history + action tree[child] = potree.PoNode() tree[child].parent = history tree[history].children.add(child) build_tree(child, tree) return tree
def rollout(self, history): action = random.choice(util.get_available_actions(history)) new_history = history + action return self.simulate(new_history)
def main() -> int: """ Main execution flow :return : 0 on success, 1 otherwise """ # TODO: add meaningful description parser = ArgumentParser(description="""\r ***YOUR_HELP_MESSAGE_HERE***.\r """) parser.add_argument( '--do', dest='do', choices=["%s" % action for action in util.get_available_actions()], help='Specify action.', required=True) parser.add_argument('--environment', '-e', dest='environment', help='Specify environment. Default: dev', default='dev') parser.add_argument('--project-dir', '-d', dest='project_dir', help='Specify project directory.', default='./') parser.add_argument('--migration-id', '-m', dest='migration_id', help='Specify migration ID to work with.', default=None) parser.add_argument( '--log-level', dest='log_level', choices=["%s" % level for level in logger.Levels.__members__.keys()], help='Specify logging level', default='DEBUG') args = parser.parse_args() # TODO: I have no idea at this point about best practices of adding logging to python application app_logger = logger.Logger(level=logger.Levels[args.log_level]) os_env = os.environ # TODO: will be great to have immutable config config = util.load_config( os.path.join(os.pardir, args.project_dir + '/pymigrate.conf'), app_logger) # Note that config dict should't have any values of None type config['MIGRATION_ID'] = str( args.migration_id) if args.migration_id else 'None' config['ENVIRONMENT'] = str(args.environment) config['PROJECT_DIR'] = os.path.abspath(args.project_dir) if 'MIGRATIONS_DIR' not in config: config['MIGRATIONS_DIR'] = args.project_dir + 'migrations' final_config = os_env.copy() final_config.update(config) # app_logger.log_plain('Starting with env:\n{0}'.format(util.get_formatted_env_vars()), logger.Levels.DEBUG) # app_logger.log_plain('Got config:\n{0}'.format(str(config)), logger.Levels.DEBUG) res = getattr(cli_commands, args.do)(final_config, app_logger) return 0 if res else 1