def extract_phase_history_proto(game, nb_previous_phases=NB_PREV_ORDERS_HISTORY): """ Extracts the phase_history_proto from a diplomacy.Game object :param game: The diplomacy.Game object :param nb_previous_phases: Integer. If set, only the last x phases will be returned. If None, the full history since the beginning of the game is returned. :return: A list of `.proto.game.PhaseHistory` proto. :type game: diplomacy.Game """ from_phase = None if nb_previous_phases is None else -1 * nb_previous_phases phase_history = Game.get_phase_history(game, from_phase=from_phase) return [ dict_to_proto(hist.to_dict(), PhaseHistoryProto) for hist in phase_history ]
def main(sl_model, other_agent): """ Plays a local game with 7 bots """ # player1 = RandomPlayer() # Use main player here x1 player1 = sl_model # (Use when get_orders is ready) player2 = other_agent # Use other player here x6 game = Game() reward_class = Reward(game) supply_centers_dist = game.get_centers() # For randomly choosing the power of the special player powers = list(game.powers) random.shuffle(powers) powers1 = powers[0] powers2 = powers[1:7] # Playing game while not game.is_game_done: if reward_class.get_terminal_reward(powers1) == 0: return "defeated" orders1, action_prob = player1.get_orders(game, [powers1]) # orders1 = {power_name: player1.get_orders(game, power_name) for power_name in powers1} orders2 = yield { power_name: player2.get_orders(game, power_name) for power_name in powers2 } # for power_name, power_orders in orders1.items(): # for power_name, power_orders in orders1.items(): if reward_class.get_terminal_reward(powers1) != 0: game.set_orders(powers1, orders1[0]) for power_name, power_orders in orders2.items(): game.set_orders(power_name, power_orders) game.process() print(reward_class.get_local_reward_all_powers()) # input() print(reward_class.get_terminal_reward_all_powers()) print(game.outcome) # Calculating support phase_history = game.get_phase_history() support_count, x_support_count, eff_x_support_count = 0, 0, 0 for phase in phase_history: for order_index in range(len(phase.orders[powers1])): order_split = phase.orders[powers1][order_index].split() if 'S' in order_split: support_count += 1 s_loc = order_split.index('S') supported = order_split[s_loc + 1] + " " + order_split[s_loc + 2] if supported not in phase.state['units'][powers1]: x_support_count += 1 supporter = order_split[s_loc - 2] + " " + order_split[s_loc - 1] if phase.results[supporter] == []: eff_x_support_count += 1 print("X-Support Ratio: " + str(x_support_count / support_count)) print("Eff-X-Support Ratio: " + str(eff_x_support_count / x_support_count)) # Saving to disk with open('game.json', 'w') as file: file.write(json.dumps(to_saved_game_format(game))) sc_dict = reward_class.get_terminal_reward_all_powers() if len(game.outcome) == 2 and game.outcome[-1] == powers1: return "won" elif len(game.outcome) == 2 and game.outcome[-1] != powers1: return "defeated" elif len(game.outcome) != 2 and [ (k, sc_dict[k]) for k in sorted(sc_dict, key=sc_dict.get, reverse=True) ][0][0] == powers1: return "most_sc" elif len(game.outcome) != 2 and [ (k, sc_dict[k]) for k in sorted(sc_dict, key=sc_dict.get, reverse=True) ][0][0] != powers1: return "survived" # won = len(game.outcome) == 2 and game.outcome[-1] == powers1 # defeated = len(game.outcome) == 2 and game.outcome[-1] != powers1 # most_sc = len(game.outcome) != 2 and [(k, sc_dict[k]) for k in sorted(sc_dict, key=sc_dict.get, reverse=True)][0][0] == powers1 # survived = len(game.outcome) != 2 and [(k, sc_dict[k]) for k in sorted(sc_dict, key=sc_dict.get, reverse=True)][0][0] != powers1 return { "sl_model": powers1, "Game outcome": game.outcome, "get_terminal_reward_all_powers": reward_class.get_terminal_reward_all_powers(), "x-support": x_support_count / support_count }