Beispiel #1
0
def test_gameboard(game_schema_file): # we use this function for testing various things. Make sure to leave it commented, since otherwise, circular import dependencies will be introduced.

    from monopoly_simulator import initialize_game_elements
    from monopoly_simulator import background_agent_v3
    from monopoly_simulator import simple_decision_agent_1
    from monopoly_simulator.agent import Agent

    player_decision_agents = dict()
    player_decision_agents['player_1'] = Agent(**background_agent_v3.decision_agent_methods)
    player_decision_agents['player_2'] = Agent(**background_agent_v3.decision_agent_methods)
    player_decision_agents['player_3'] = Agent(**background_agent_v3.decision_agent_methods)
    player_decision_agents['player_4'] = Agent(**background_agent_v3.decision_agent_methods)

    game_schema = json.load(open(game_schema_file, 'r'))
    game_elements_orig = initialize_game_elements.initialize_board(game_schema, player_decision_agents)
    # pprint.pprint(game_elements_orig,indent=4)

    player_decision_agents2 = dict()
    player_decision_agents2['player_1'] = simple_decision_agent_1.decision_agent_methods
    player_decision_agents2['player_2'] = simple_decision_agent_1.decision_agent_methods
    player_decision_agents2['player_3'] = simple_decision_agent_1.decision_agent_methods
    player_decision_agents2['player_4'] = background_agent_v3.decision_agent_methods

    hypothetical_gameboard = initialize_hypothetical_universe(game_elements_orig,player_decision_agents2)

    print(hypothetical_gameboard['history'])
    game_elements_copy = copy.deepcopy(hypothetical_gameboard)
Beispiel #2
0
def play_game_in_tournament(game_seed, inject_novelty_function=None):
    logger.debug('seed used: ' + str(game_seed))
    player_decision_agents = dict()
    # for p in ['player_1','player_3']:
    #     player_decision_agents[p] = simple_decision_agent_1.decision_agent_methods
    player_decision_agents['player_1'] = Agent(
        **background_agent_v3_1.decision_agent_methods)
    player_decision_agents['player_2'] = Agent(
        **background_agent_v3_1.decision_agent_methods)
    player_decision_agents['player_3'] = Agent(
        **background_agent_v3_1.decision_agent_methods)
    player_decision_agents['player_4'] = Agent(
        **background_agent_v3_1.decision_agent_methods)

    game_elements = set_up_board('../monopoly_game_schema_v1-2.json',
                                 player_decision_agents)

    #Comment out the above line and uncomment the piece of code to read the gameboard state from an existing json file so that
    #the game starts from a particular game state instead of initializing the gameboard with default start values.
    #Note that the novelties introduced in that particular game which was saved to file will be loaded into this game board as well.
    '''
    logger.debug("Loading gameboard from an existing game state that was saved to file.")
    infile = '../current_gameboard_state.json'
    game_elements = read_write_current_state.read_in_current_state_from_file(infile, player_decision_agents)
    '''

    if inject_novelty_function:
        inject_novelty_function(game_elements)

    if player_decision_agents['player_1'].startup(game_elements) == flag_config_dict['failure_code'] or \
            player_decision_agents['player_2'].startup(game_elements) == flag_config_dict['failure_code'] or \
            player_decision_agents['player_3'].startup(game_elements) == flag_config_dict['failure_code'] or \
            player_decision_agents['player_4'].startup(game_elements) == flag_config_dict['failure_code']:
        logger.error("Error in initializing agents. Cannot play the game.")
        return None
    else:
        logger.debug("Sucessfully initialized all player agents.")
        winner = simulate_game_instance(game_elements,
                                        history_log_file=None,
                                        np_seed=game_seed)
        if player_decision_agents['player_1'].shutdown() == flag_config_dict['failure_code'] or \
                player_decision_agents['player_2'].shutdown() == flag_config_dict['failure_code'] or \
                player_decision_agents['player_3'].shutdown() == flag_config_dict['failure_code'] or \
                player_decision_agents['player_4'].shutdown() == flag_config_dict['failure_code']:
            logger.error("Error in agent shutdown.")
            return None
        else:
            logger.debug("All player agents have been shutdown. ")
            logger.debug("GAME OVER")
            return winner
def play_game_in_tournament(game_seed, inject_novelty_function=None):
    logger.debug('seed used: ' + str(game_seed))
    player_decision_agents = dict()
    # for p in ['player_1','player_3']:
    #     player_decision_agents[p] = simple_decision_agent_1.decision_agent_methods
    # player_decision_agents['player_1'] = Agent(**RL_agent_v1.decision_agent_methods)
    player_decision_agents['player_1'] = Agent(**background_agent_v3.decision_agent_methods)
    player_decision_agents['player_2'] = Agent(**background_agent_v3.decision_agent_methods)
    player_decision_agents['player_3'] = Agent(**background_agent_v3.decision_agent_methods)
    player_decision_agents['player_4'] = Agent(**background_agent_v3.decision_agent_methods)

    game_elements = set_up_board('../monopoly_game_schema_v1-2.json',
                                 player_decision_agents)
    
    #Comment out the above line and uncomment the piece of code to read the gameboard state from an existing json file so that
    #the game starts from a particular game state instead of initializing the gameboard with default start values.
    #Note that the novelties introduced in that particular game which was saved to file will be loaded into this game board as well.

    # logger.debug("Loading gameboard from an existing game state that was saved to file.")
    # infile = '../current_gameboard_state.json'
    # game_elements = read_write_current_state.read_in_current_state_from_file(infile, player_decision_agents)


    if inject_novelty_function:
        inject_novelty_function(game_elements)
    # print(game_elements.keys())
    # gameboard_keys = ['bank', 'jail_position', 'railroad_positions', 'utility_positions', 'go_position', 'go_increment',
    #                   'location_objects', 'location_sequence', 'color_assets', 'dies', 'chance_cards',
    #                   'community_chest_cards', 'chance_card_objects', 'community_chest_card_objects', 'players', 'type']
    # for key in gameboard_keys:
    #     print(game_elements[key])
    # print(game_elements['picked_chance_cards'])
    # print(game_elements['history'])
    if player_decision_agents['player_1'].startup(game_elements) == -1 or player_decision_agents['player_2'].startup(game_elements) == -1 or \
            player_decision_agents['player_3'].startup(game_elements) == -1 or player_decision_agents['player_4'].startup(game_elements) == -1:
        logger.error("Error in initializing agents. Cannot play the game.")
        return None
    else:
        logger.debug("Sucessfully initialized all player agents.")
        winner = simulate_game_instance(game_elements, history_log_file=None, np_seed=game_seed)
        if player_decision_agents['player_1'].shutdown() == -1 or player_decision_agents['player_2'].shutdown() == -1 or \
            player_decision_agents['player_3'].shutdown() == -1 or player_decision_agents['player_4'].shutdown() == -1:
            logger.error("Error in agent shutdown.")
            return None
        else:
            logger.debug("All player agents have been shutdown. ")
            logger.debug("GAME OVER")
            return winner
Beispiel #4
0
def play_game_in_tournament(game_seed, inject_novelty_function=None):
    logger.debug('seed used: ' + str(game_seed))
    player_decision_agents = dict()
    # for p in ['player_1','player_3']:
    #     player_decision_agents[p] = simple_decision_agent_1.decision_agent_methods
    player_decision_agents['player_1'] = Agent(**background_agent_v1.decision_agent_methods)
    player_decision_agents['player_2'] = Agent(**background_agent_v1.decision_agent_methods)
    player_decision_agents['player_3'] = Agent(**background_agent_v1.decision_agent_methods)
    player_decision_agents['player_4'] = Agent(**background_agent_v1.decision_agent_methods)
    game_elements = set_up_board('../monopoly_game_schema_v1-2.json',
                                 player_decision_agents)
    if inject_novelty_function:
        inject_novelty_function(game_elements)

    winner = simulate_game_instance(game_elements, np_seed=game_seed)
    logger.debug("GAME OVER")
    return winner
Beispiel #5
0
def play_game():
    """
    Use this function if you want to test a single game instance and control lots of things. For experiments, we will directly
    call some of the functions in gameplay from test_harness.py.

    This is where everything begins. Assign decision agents to your players, set up the board and start simulating! You can
    control any number of players you like, and assign the rest to the simple agent. We plan to release a more sophisticated
    but still relatively simple agent soon.
    :return: String. the name of the player who won the game, if there was a winner, otherwise None.
    """

    try:
        os.makedirs('../single_tournament/')
        print('Creating folder and logging gameplay.')
    except:
        print('Logging gameplay.')

    logger = log_file_create('../single_tournament/seed_6.log')
    player_decision_agents = dict()
    # for p in ['player_1','player_3']:
    #     player_decision_agents[p] = simple_decision_agent_1.decision_agent_methods
    player_decision_agents['player_1'] = Agent(**background_agent_v1.decision_agent_methods)
    player_decision_agents['player_2'] = Agent(**background_agent_v1.decision_agent_methods)
    player_decision_agents['player_3'] = Agent(**background_agent_v1.decision_agent_methods)
    player_decision_agents['player_4'] = Agent(**background_agent_v1.decision_agent_methods)
    game_elements = set_up_board('../monopoly_game_schema_v1-2.json',
                                 player_decision_agents)
    inject_novelty(game_elements)

    winner = simulate_game_instance(game_elements)
    logger.debug("GAME OVER")

    handlers_copy = logger.handlers[:]
    for handler in handlers_copy:
            logger.removeHandler(handler)
            handler.close()
            handler.flush()
    return winner
    for item in game_schema['cards']['picked_chance_cards']:
        picked_chance_cards.append(chance_card_objects[item])

    for item in game_schema['cards']['picked_community_chest_cards']:
        picked_community_chest_cards.append(community_chest_card_objects[item])

    current_gameboard['chance_cards'] = chance_cards
    current_gameboard['community_chest_cards'] = community_chest_cards
    current_gameboard['chance_card_objects'] = chance_card_objects
    current_gameboard['community_chest_card_objects'] = community_chest_card_objects
    current_gameboard['picked_chance_cards'] = picked_chance_cards
    current_gameboard['picked_community_chest_cards'] = picked_community_chest_cards


def _initialize_game_history_structs(current_gameboard):
    current_gameboard['history'] = dict()
    current_gameboard['history']['function'] = list()
    current_gameboard['history']['param'] = list()
    current_gameboard['history']['return'] = list()

if __name__ == '__main__':
    from monopoly_simulator.agent import Agent
    from monopoly_simulator import background_agent_v3
    infile = '/media/becky/GNOME-p3/Hypothetical_simulator/game_board/current_gameboard_state33.json'
    player_decision_agents = dict()
    for name_num in range(1,5):
        player_decision_agents['player_' + str(name_num)] = Agent(**background_agent_v3.decision_agent_methods)
    game_element = read_in_current_state_from_file(infile, player_decision_agents)
    for i in range(4):
        print(game_element['players'][i].status, game_element['players'][i].current_position)
        print(game_element['players'][i].current_cash, game_element['players'][i].assets)
Beispiel #7
0
def play_game():
    """
    Use this function if you want to test a single game instance and control lots of things. For experiments, we will directly
    call some of the functions in gameplay from test_harness.py.

    This is where everything begins. Assign decision agents to your players, set up the board and start simulating! You can
    control any number of players you like, and assign the rest to the simple agent. We plan to release a more sophisticated
    but still relatively simple agent soon.
    :return: String. the name of the player who won the game, if there was a winner, otherwise None.
    """

    try:
        os.makedirs('../single_tournament/')
        print('Creating folder and logging gameplay.')
    except:
        print('Logging gameplay.')

    logger = log_file_create('../single_tournament/seed_6.log')
    player_decision_agents = dict()
    # for p in ['player_1','player_3']:
    #     player_decision_agents[p] = simple_decision_agent_1.decision_agent_methods

    player_decision_agents['player_1'] = Agent(
        **background_agent_v3_1.decision_agent_methods)
    player_decision_agents['player_2'] = Agent(
        **background_agent_v3_1.decision_agent_methods)
    player_decision_agents['player_3'] = Agent(
        **background_agent_v3_1.decision_agent_methods)
    player_decision_agents['player_4'] = Agent(
        **background_agent_v3_1.decision_agent_methods)

    game_elements = set_up_board('../monopoly_game_schema_v1-2.json',
                                 player_decision_agents)

    #Comment out the above line and uncomment the piece of code to read the gameboard state from an existing json file so that
    #the game starts from a particular game state instead of initializing the gameboard with default start values.
    #Note that the novelties introduced in that particular game which was saved to file will be loaded into this game board as well.
    '''
    logger.debug("Loading gameboard from an existing game state that was saved to file.")
    infile = '../current_gameboard_state.json'
    game_elements = read_write_current_state.read_in_current_state_from_file(infile, player_decision_agents)
    '''

    inject_novelty(game_elements)

    if player_decision_agents['player_1'].startup(game_elements) == flag_config_dict['failure_code'] or \
            player_decision_agents['player_2'].startup(game_elements) == flag_config_dict['failure_code'] or \
            player_decision_agents['player_3'].startup(game_elements) == flag_config_dict['failure_code'] or \
            player_decision_agents['player_4'].startup(game_elements) == flag_config_dict['failure_code']:
        logger.error("Error in initializing agents. Cannot play the game.")
        return None
    else:
        logger.debug("Sucessfully initialized all player agents.")
        winner = simulate_game_instance(game_elements)
        if player_decision_agents['player_1'].shutdown() == flag_config_dict['failure_code'] or \
            player_decision_agents['player_2'].shutdown() == flag_config_dict['failure_code'] or \
            player_decision_agents['player_3'].shutdown() == flag_config_dict['failure_code'] or \
            player_decision_agents['player_4'].shutdown() == flag_config_dict['failure_code']:
            logger.error("Error in agent shutdown.")
            handlers_copy = logger.handlers[:]
            for handler in handlers_copy:
                logger.removeHandler(handler)
                handler.close()
                handler.flush()
            return None
        else:
            logger.debug("All player agents have been shutdown. ")
            logger.debug("GAME OVER")
            handlers_copy = logger.handlers[:]
            for handler in handlers_copy:
                logger.removeHandler(handler)
                handler.close()
                handler.flush()
            return winner
Beispiel #8
0
    
    granularityNovelty = novelty_generator.GranularityRepresentationNovelty()
    granularityNovelty.granularity_novelty(current_gameboard, current_gameboard['location_objects']['Baltic Avenue'], 6)
    granularityNovelty.granularity_novelty(current_gameboard, current_gameboard['location_objects']['States Avenue'], 20)
    granularityNovelty.granularity_novelty(current_gameboard, current_gameboard['location_objects']['Tennessee Avenue'], 27)

    spatialNovelty = novelty_generator.SpatialRepresentationNovelty()
    spatialNovelty.color_reordering(current_gameboard, ['Boardwalk', 'Park Place'], 'Blue')

    granularityNovelty.granularity_novelty(current_gameboard, current_gameboard['location_objects']['Park Place'], 52)
    '''

try:
    os.makedirs('../single_tournament/')
    print('Creating folder and logging gameplay.')
except:
    print('Logging gameplay.')

##Logs game play in the single_tournament folder
logger = log_file_create('../single_tournament/seed_6.log')
player_decision_agents = dict()
player_decision_agents['player_1'] = Agent(**background_agent_v1.decision_agent_methods)
player_decision_agents['player_2'] = Agent(**background_agent_v2.decision_agent_methods)
player_decision_agents['player_3'] = Agent(**background_agent_v3.decision_agent_methods)
player_decision_agents['player_4'] = Agent(**background_agent_v1.decision_agent_methods)
game_elements = set_up_board('../monopoly_game_schema_v1-2.json',
                             player_decision_agents)
inject_class_novelty_1(game_elements)
MyMainApp(game_elements).run()