def test_from_dict(self): """Test that conversion from dict works as expected.""" version_id = "1" 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.0, 20.0, 20.0] 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(version_id, 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.0, {"tac_good_0_pbk": 1}, sender_pbk) transaction_2 = Transaction(tx_id, False, counterparty_pbk, 10.0, {"tac_good_0_pbk": 1}, sender_pbk) expected_game.settle_transaction(transaction_1) expected_game.settle_transaction(transaction_2) actual_game = Game.from_dict(expected_game.to_dict()) assert actual_game == expected_game
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) game_stats = GameStats(game) return ControllerDashboard(game_stats, env_name=env_name)
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") if not os.path.exists(game_data_json_filepath): continue game_data = json.load(open(game_data_json_filepath)) if game_data == {}: print( "Found incomplete data for game_dir={}!".format(game_dir)) continue game = Game.from_dict(game_data) game_stats = GameStats(game) result.append(game_stats) return result
def from_json(cls, d: Dict[str, Any]): """Read from json.""" game = Game.from_dict(d) return GameStats(game)