Esempio n. 1
0
    def _load(self) -> List[GameStats]:
        """Load all game statistics from iterated TAC output."""
        result = []  # type: List[GameStats]

        game_dirs = sorted(os.listdir(self.competition_directory))
        for game_dir in game_dirs:
            game_data_json_filepath = os.path.join(self.competition_directory,
                                                   game_dir, "game.json")
            game_data = json.load(open(game_data_json_filepath))
            game = Game.from_dict(game_data)
            game_stats = GameStats(game)
            result.append(game_stats)

        return result
Esempio n. 2
0
    def from_datadir(datadir: str, env_name: str) -> 'ControllerDashboard':
        """
        Return a ControllerDashboard from a data directory.

        :param datadir: the data directory
        :param env_name: the environment name
        :return: controller dashboard
        """
        game_data_json_filepath = os.path.join(datadir, "game.json")
        print("Loading data from {}".format(game_data_json_filepath))
        game_data = json.load(open(game_data_json_filepath))
        game = Game.from_dict(game_data,
                              Crypto())  # any crypto object will do here
        game_stats = GameStats(game)
        return ControllerDashboard(game_stats, env_name=env_name)
Esempio n. 3
0
    def test_from_dict(self):
        """Test that conversion from dict works as expected."""
        nb_agents = 3
        nb_goods = 3
        tx_fee = 1.0
        agent_pbk_to_name = {
            'tac_agent_0_pbk': 'tac_agent_0',
            'tac_agent_1_pbk': 'tac_agent_1',
            'tac_agent_2_pbk': 'tac_agent_2'
        }
        good_pbk_to_name = {
            'tac_good_0_pbk': 'tac_good_0',
            'tac_good_1_pbk': 'tac_good_1',
            'tac_good_2_pbk': 'tac_good_2'
        }
        money_amounts = [20, 20, 20]
        endowments = [[1, 1, 1], [2, 1, 1], [1, 1, 2]]
        utility_params = [[20.0, 40.0, 40.0], [10.0, 50.0, 40.0],
                          [40.0, 30.0, 30.0]]
        eq_prices = [1.0, 1.0, 4.0]
        eq_good_holdings = [[1.0, 1.0, 4.0], [1.0, 5.0, 1.0], [6.0, 1.0, 2.0]]
        eq_money_holdings = [20.0, 20.0, 20.0]

        game_configuration = GameConfiguration(nb_agents, nb_goods, tx_fee,
                                               agent_pbk_to_name,
                                               good_pbk_to_name)
        game_initialization = GameInitialization(money_amounts, endowments,
                                                 utility_params, eq_prices,
                                                 eq_good_holdings,
                                                 eq_money_holdings)

        expected_game = Game(game_configuration, game_initialization)

        tx_id = 'some_tx_id'
        sender_pbk = 'tac_agent_0_pbk'
        counterparty_pbk = 'tac_agent_1_pbk'
        transaction_1 = Transaction(tx_id, True, counterparty_pbk,
                                    10, {'tac_good_0_pbk': 1}, sender_pbk,
                                    Crypto())
        transaction_2 = Transaction(tx_id, False, counterparty_pbk,
                                    10, {'tac_good_0_pbk': 1}, sender_pbk,
                                    Crypto())
        expected_game.settle_transaction(transaction_1)
        expected_game.settle_transaction(transaction_2)

        actual_game = Game.from_dict(expected_game.to_dict(), Crypto())

        assert actual_game == expected_game
Esempio n. 4
0
 def from_json(cls, d: Dict[str, Any]):
     """Read from json."""
     game = Game.from_dict(d, Crypto())  # any crypto object will do here
     return GameStats(game)