Exemplo n.º 1
0
def add_possible_orders_to_saved_game(saved_game):
    """ Adds possible_orders for each phase of the saved game """
    if saved_game['map'].startswith('standard'):
        for phase in saved_game['phases']:
            game = Game(map_name=saved_game['map'], rules=saved_game['rules'])
            game.set_state(phase['state'])
            phase['possible_orders'] = game.get_all_possible_orders()
    return saved_game
Exemplo n.º 2
0
def main():
    try:
        (options, arguments) = getopt.getopt(sys.argv[1:], 'h')
    except getopt.error:
        sys.exit("Unknown input parameter.")
    if [] == arguments:
        arguments = ["shiftLeft"]
    if not os.path.exists(arguments[0] + ".map"):
        sys.exit("%s.map could not be opened" % (arguments[0], ))
    game = Game(map_name=arguments[0])
    while not game.is_game_done:

        # Getting the list of possible orders for all locations
        possible_orders = game.get_all_possible_orders()

        # For each power, randomly sampling a valid order
        for power_name, power in game.powers.items():
            #        power_orders = [random.choice(possible_orders[loc]) for loc in game.get_orderable_locations(power_name)
            #                        if possible_orders[loc]]
            power_orders = []
            for loc in game.get_orderable_locations(power_name):
                if '/' == loc[-1]:
                    loc = loc[:-1]
                if possible_orders[loc]:
                    power_orders.append(random.choice(possible_orders[loc]))
            game.set_orders(power_name, power_orders)

        # Messages can be sent locally with game.add_message
        # e.g. game.add_message(Message(sender='FRANCE',
        #                               recipient='ENGLAND',
        #                               message='This is a message',
        #                               phase=self.get_current_phase(),
        #                               time_sent=int(time.time())))

        # Processing the game to move to the next phase
        game.process()


# to_saved_game_format(game, output_path='collected_autoplay_games.json')
# Exporting the game to disk to visualize (game is appended)
    with open('collected_autoplay_games.json', 'a') as outp:
        outp.write(to_saved_game_format(game))
Exemplo n.º 3
0
def create_mask(board_state, phase, locs, board_dict):
    '''
    Given a board_state, produces a mask that only includes valid orders from loc,
    based on their positions in get_order_vocabulary in state_space. Assumes playing on standard map

    Args:
    board_state - 81 x dilbo vector representing current board state as described in Figure 2
    phase - string indicating phase of game (e.g. 'S1901M')
    loc - list of strings representing locations (e.g. ['PAR', ... ])

    Returns:
    List of Masks for zeroing out invalid orders, length is the number of orders total
    '''

    # create instance of Game object based on board_state
    game = Game(map_name='standard')

    game.set_current_phase(phase)
    game.clear_units()

    power_units = {}
    power_centers = {}

    for loc_idx in range(len(board_state)):
        loc_name = ORDERING[loc_idx]
        loc_vec = board_state[loc_idx,:]

        if "unit_type" in board_dict[loc_name].keys():
            unit_type = board_dict[loc_name]["unit_type"]
        else:
            unit_type = None

        if "unit_power" in board_dict[loc_name].keys():
            unit_power = board_dict[loc_name]["unit_power"]
        else:
            unit_power = None

        if "buildable" in board_dict[loc_name].keys():
            buildable = board_dict[loc_name]["buildable"]
        else:
            buildable = None

        if "removable" in board_dict[loc_name].keys():
            removable = board_dict[loc_name]["removable"]
        else:
            removable = None

        if "d_unit_type" in board_dict[loc_name].keys():
            dislodged_unit_type = board_dict[loc_name]["d_unit_type"]
        else:
            dislodged_unit_type = None

        if "d_unit_power" in board_dict[loc_name].keys():
            dislodged_unit_power = board_dict[loc_name]["d_unit_power"]
        else:
            dislodged_unit_power = None

        if "area_type" in board_dict[loc_name].keys():
            area_type = board_dict[loc_name]["area_type"]
        else:
            area_type = None

        if "supply_center_owner" in board_dict[loc_name].keys():
            supply_center_owner = board_dict[loc_name]["supply_center_owner"]
        else:
            supply_center_owner = None

        # # extract one hot vectors from encoding
        # unit_type_one_hot = loc_vec[0:3]
        # unit_power_one_hot = loc_vec[3:11]
        # buildable = loc_vec[11]
        # removable = loc_vec[12]
        # dislodged_unit_type_one_hot = loc_vec[13:16]
        # dislodged_unit_power_one_hot = loc_vec[16:24]
        # area_type_one_hot = loc_vec[24:27]
        # supply_center_owner_one_hot = loc_vec[27:35]
        #
        # # convert one hot vectors into indices, and index into unit types and powers
        # unit_type = INV_UNIT_TYPE[np.argmax(unit_type_one_hot)]
        # unit_power = INV_UNIT_POWER[np.argmax(unit_power_one_hot)]
        # dislodged_unit_type = INV_UNIT_TYPE[np.argmax(dislodged_unit_type_one_hot)]
        # dislodged_unit_power = INV_UNIT_POWER[np.argmax(dislodged_unit_power_one_hot)]
        # supply_center_owner = INV_UNIT_POWER[np.argmax(supply_center_owner_one_hot)]

        # add the unit and/or dislodged unit in this locatino to power_units dict
        # likewise for supply center (if it exists). See set_units() documentation for how units are formatted
        if unit_type != None:
            if unit_power not in power_units:
                power_units[unit_power] = []
            power_units[unit_power].append('{} {}'.format(unit_type, loc_name))
        if dislodged_unit_type != None:
            if dislodged_unit_type not in power_units:
                power_units[dislodged_unit_power] = []
            power_units[dislodged_unit_power].append('*{} {}'.format(dislodged_unit_type, loc_name))
        if supply_center_owner != None:
            if supply_center_owner not in power_centers:
                power_centers[supply_center_owner] = []
            power_centers[supply_center_owner].append(loc_name)

    # Setting units
    game.clear_units()
    for power_name in list(power_units.keys()):
        game.set_units(power_name, power_units[power_name])

    # Setting centers
    game.clear_centers()
    for power_name in list(power_centers.keys()):
        game.set_centers(power_name, power_centers[power_name])

    possible_orders = game.get_all_possible_orders()

    masks = np.full((len(locs), ORDER_VOCABULARY_SIZE),-(10**15))

    for i in range(len(locs)):
        loc = locs[i]
        # Needs to be negative infinity to mask softmax function.
        # Negative infinity gives nan in loss, so using very large negative number instead
        for order in possible_orders[loc]:
            ix = state_space.order_to_ix(order)
            masks[i,ix] = 0
    return np.array(masks)
import random
from diplomacy import Game
from diplomacy.utils.export import to_saved_game_format

# Creating a game
# Alternatively, a map_name can be specified as an argument. e.g. Game(map_name='pure')
game = Game(map_name='standard')
while not game.is_game_done:

    # Getting the list of possible orders for all locations
    possible_orders = game.get_all_possible_orders()

    # For each power, randomly sampling a valid order
    for power_name, power in game.powers.items():
        power_orders = [random.choice(possible_orders[loc]) for loc in game.get_orderable_locations(power_name)
                        if possible_orders[loc]]
        game.set_orders(power_name, power_orders)

    # Messages can be sent locally with game.add_message
    # e.g. game.add_message(Message(sender='FRANCE',
    #                               recipient='ENGLAND',
    #                               message='This is a message',
    #                               phase=self.get_current_phase(),
    #                               time_sent=int(time.time())))

    # Processing the game to move to the next phase
    game.process()

# Exporting the game to disk to visualize (game is appended to file)
# Alternatively, we can do >> file.write(json.dumps(to_saved_game_format(game)))
to_saved_game_format(game, output_path='collected_game_litanies.json')