Beispiel #1
0
    def test_to_dict(self):
        """Test that conversion into 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, 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(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,
        )

        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)
        transaction_2 = Transaction(tx_id, False, counterparty_pbk, 10,
                                    {"tac_good_0_pbk": 1}, sender_pbk)
        game.settle_transaction(transaction_1)
        game.settle_transaction(transaction_2)

        actual_game_dict = game.to_dict()
        expected_game_dict = {
            "configuration": game_configuration.to_dict(),
            "initialization": game_initialization.to_dict(),
            "transactions": [transaction_1.to_dict(),
                             transaction_2.to_dict()],
        }

        assert actual_game_dict == expected_game_dict
Beispiel #2
0
    def init(self, game_data: GameData, agent_pbk: Address) -> None:
        """
        Populate data structures with the game data.

        :param game_data: the game instance data
        :param agent_pbk: the public key of the agent

        :return: None
        """
        # TODO: extend TAC messages to include reference to version id; then replace below with assert
        game_data.version_id = self.expected_version_id
        self._game_configuration = GameConfiguration(
            game_data.version_id,
            game_data.nb_agents,
            game_data.nb_goods,
            game_data.tx_fee,
            game_data.agent_pbk_to_name,
            game_data.good_pbk_to_name,
        )
        self._initial_agent_state = AgentState(game_data.money,
                                               game_data.endowment,
                                               game_data.utility_params)
        self._agent_state = AgentState(game_data.money, game_data.endowment,
                                       game_data.utility_params)
        if self.strategy.is_world_modeling:
            opponent_pbks = self.game_configuration.agent_pbks
            opponent_pbks.remove(agent_pbk)
            self._world_state = WorldState(
                opponent_pbks,
                self.game_configuration.good_pbks,
                self.initial_agent_state,
            )
Beispiel #3
0
    def test_to_dict(self):
        """Test that conversion into dict works as expected."""
        version_id = "1"
        nb_agents = 10
        nb_goods = 10
        tx_fee = 2.5
        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",
            "tac_agent_3_pbk": "tac_agent_3",
            "tac_agent_4_pbk": "tac_agent_4",
            "tac_agent_5_pbk": "tac_agent_5",
            "tac_agent_6_pbk": "tac_agent_6",
            "tac_agent_7_pbk": "tac_agent_7",
            "tac_agent_8_pbk": "tac_agent_8",
            "tac_agent_9_pbk": "tac_agent_9",
        }
        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",
            "tac_good_3_pbk": "tac_good_3",
            "tac_good_4_pbk": "tac_good_4",
            "tac_good_5_pbk": "tac_good_5",
            "tac_good_6_pbk": "tac_good_6",
            "tac_good_7_pbk": "tac_good_7",
            "tac_good_8_pbk": "tac_good_8",
            "tac_good_9_pbk": "tac_good_9",
        }

        game_configuration = GameConfiguration(version_id, nb_agents, nb_goods,
                                               tx_fee, agent_pbk_to_name,
                                               good_pbk_to_name)

        actual_game_configuration_dict = game_configuration.to_dict()
        expected_game_configuration_dict = {
            "version_id": version_id,
            "nb_agents": nb_agents,
            "nb_goods": nb_goods,
            "tx_fee": tx_fee,
            "agent_pbk_to_name": agent_pbk_to_name,
            "good_pbk_to_name": good_pbk_to_name,
        }

        assert actual_game_configuration_dict == expected_game_configuration_dict
Beispiel #4
0
    def from_dict(cls, d: Dict[str, Any]) -> 'Game':
        """Get class instance from dictionary."""
        configuration = GameConfiguration.from_dict(d["configuration"])
        initialization = GameInitialization.from_dict(d["initialization"])

        game = Game(configuration, initialization)
        for tx_dict in d["transactions"]:
            tx = Transaction.from_dict(tx_dict)
            game.settle_transaction(tx)

        return game
Beispiel #5
0
    def test_transaction_invalid_if_seller_does_not_have_enough_quantities(
            self):
        """Test that a transaction is invalid if the seller does not have enough quantities."""
        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("1", 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,
        )

        game = Game(game_configuration, game_initialization)

        tx_id = "some_tx_id"
        sender_pbk = "tac_agent_0_pbk"
        is_sender_buyer = True
        counterparty_pbk = "tac_agent_1_pbk"
        amount = 20
        quantities_by_good = {0: 3, 1: 0, 2: 0}
        invalid_transaction = Transaction(
            tx_id,
            is_sender_buyer,
            counterparty_pbk,
            amount,
            quantities_by_good,
            sender_pbk,
        )

        assert not game.is_transaction_valid(invalid_transaction)
Beispiel #6
0
    def test_from_dict(self):
        """Test that conversion from dict works as expected."""
        version_id = "1"
        nb_agents = 10
        nb_goods = 10
        tx_fee = 2.5
        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",
            "tac_agent_3_pbk": "tac_agent_3",
            "tac_agent_4_pbk": "tac_agent_4",
            "tac_agent_5_pbk": "tac_agent_5",
            "tac_agent_6_pbk": "tac_agent_6",
            "tac_agent_7_pbk": "tac_agent_7",
            "tac_agent_8_pbk": "tac_agent_8",
            "tac_agent_9_pbk": "tac_agent_9",
        }
        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",
            "tac_good_3_pbk": "tac_good_3",
            "tac_good_4_pbk": "tac_good_4",
            "tac_good_5_pbk": "tac_good_5",
            "tac_good_6_pbk": "tac_good_6",
            "tac_good_7_pbk": "tac_good_7",
            "tac_good_8_pbk": "tac_good_8",
            "tac_good_9_pbk": "tac_good_9",
        }

        expected_game_configuration = GameConfiguration(
            version_id, nb_agents, nb_goods, tx_fee, agent_pbk_to_name,
            good_pbk_to_name)
        actual_game_configuration = GameConfiguration.from_dict(
            expected_game_configuration.to_dict())

        assert actual_game_configuration == expected_game_configuration
Beispiel #7
0
    def test_get_game_data_from_agent_label(self):
        """Test that the getter of game states by agent label 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, 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(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,
        )

        game = Game(game_configuration, game_initialization)

        actual_agent_state_0 = game.get_agent_state_from_agent_pbk(
            "tac_agent_0_pbk")
        actual_agent_state_1 = game.get_agent_state_from_agent_pbk(
            "tac_agent_1_pbk")

        expected_agent_state_0 = AgentState(money_amounts[0], endowments[0],
                                            utility_params[0])
        expected_agent_state_1 = AgentState(money_amounts[1], endowments[1],
                                            utility_params[1])

        assert actual_agent_state_0 == expected_agent_state_0
        assert actual_agent_state_1 == expected_agent_state_1
Beispiel #8
0
 def test_not_enough_goods_raises_exception(self):
     """Test that if we try to instantiate a game_configuration with not enough goods, we raise an AssertionError."""
     with pytest.raises(AssertionError,
                        match="Must have at least two goods."):
         GameConfiguration(
             "1",
             2,
             1,
             1.0,
             {
                 "tac_agent_0_pbk": "tac_agent_0",
                 "tac_agent_1_pbk": "tac_agent_1"
             },
             {"tac_good_0": "Good 0"},
         )
Beispiel #9
0
 def test_non_unique_agent_names_raises_exception(self):
     """Test that if we try to instantiate a game_configuration with non unique agent names, we raise an AssertionError."""
     with pytest.raises(AssertionError,
                        match="Agents' names must be unique."):
         GameConfiguration(
             "1",
             2,
             2,
             1.0,
             {
                 "tac_agent_0_pbk": "tac_agent_0",
                 "tac_agent_1_pbk": "tac_agent_0"
             },
             {
                 "tac_good_0": "Good 0",
                 "tac_good_1": "Good 1"
             },
         )
Beispiel #10
0
 def test_negative_tx_fee_raises_exception(self):
     """Test that if we try to instantiate a game_configuration with a negative tx_fee, we raise an AssertionError."""
     with pytest.raises(AssertionError,
                        match="Tx fee must be non-negative."):
         GameConfiguration(
             "1",
             2,
             2,
             -1.0,
             {
                 "tac_agent_0_pbk": "tac_agent_0",
                 "tac_agent_1_pbk": "tac_agent_1"
             },
             {
                 "tac_good_0": "Good 0",
                 "tac_good_1": "Good 1"
             },
         )
Beispiel #11
0
    def generate_game(version_id: str, nb_agents: int, nb_goods: int,
                      tx_fee: float, money_endowment: int,
                      base_good_endowment: int, lower_bound_factor: int,
                      upper_bound_factor: int, agent_pbk_to_name: Dict[str,
                                                                       str],
                      good_pbk_to_name: Dict[str, str]) -> 'Game':
        """
        Generate a game, the endowments and the utilites.

        :param version_id: the version of the game.
        :param nb_agents: the number of agents.
        :param nb_goods: the number of goods.
        :param tx_fee: the fee to pay per transaction.
        :param money_endowment: the initial amount of money for every agent.
        :param base_good_endowment: the base amount of instances per good.
        :param lower_bound_factor: the lower bound of a uniform distribution.
        :param upper_bound_factor: the upper bound of a uniform distribution
        :param agent_pbk_to_name: the mapping of the public keys for the agents to their names.
        :param good_pbk_to_name: the mapping of the public keys for the goods to their names.
        :return: a game.
        """
        game_configuration = GameConfiguration(version_id, nb_agents, nb_goods,
                                               tx_fee, agent_pbk_to_name,
                                               good_pbk_to_name)

        scaling_factor = determine_scaling_factor(money_endowment)
        money_endowments = generate_money_endowments(nb_agents,
                                                     money_endowment)
        good_endowments = generate_good_endowments(nb_goods, nb_agents,
                                                   base_good_endowment,
                                                   lower_bound_factor,
                                                   upper_bound_factor)
        utility_params = generate_utility_params(nb_agents, nb_goods,
                                                 scaling_factor)
        eq_prices, eq_good_holdings, eq_money_holdings = generate_equilibrium_prices_and_holdings(
            good_endowments, utility_params, money_endowment, scaling_factor)
        game_initialization = GameInitialization(money_endowments,
                                                 good_endowments,
                                                 utility_params, eq_prices,
                                                 eq_good_holdings,
                                                 eq_money_holdings)

        return Game(game_configuration, game_initialization)
Beispiel #12
0
 def test_agent_nb_and_public_keys_nonmatch_raises_exception(self):
     """Test that if we try to instantiate a game_configuration with a different number of agents from agent public keys, we raise an AssertionError."""
     with pytest.raises(
             AssertionError,
             match="There must be one public key for each agent."):
         GameConfiguration(
             "1",
             3,
             2,
             1.0,
             {
                 "tac_agent_0_pbk": "tac_agent_0",
                 "tac_agent_1_pbk": "tac_agent_1"
             },
             {
                 "tac_good_0": "Good 0",
                 "tac_good_1": "Good 1"
             },
         )