示例#1
0
    def __init__(self, configuration: GameConfiguration,
                 initialization: GameInitialization):
        """
        Initialize a game.

        :param configuration: the game configuration.
        :param initialization: the game initialization.
        """
        self._configuration = configuration  # type GameConfiguration
        self._initialization = initialization  # type: GameInitialization
        self.transactions = []  # type: List[Transaction]

        self._initial_agent_states = dict(
            (agent_pbk,
             AgentState(initialization.initial_money_amounts[i],
                        initialization.endowments[i],
                        initialization.utility_params[i]))
            for agent_pbk, i in zip(
                configuration.agent_pbks, range(
                    configuration.nb_agents)))  # type: Dict[str, AgentState]

        self.agent_states = dict(
            (agent_pbk,
             AgentState(initialization.initial_money_amounts[i],
                        initialization.endowments[i],
                        initialization.utility_params[i]))
            for agent_pbk, i in zip(
                configuration.agent_pbks, range(
                    configuration.nb_agents)))  # type: Dict[str, AgentState]

        self.good_states = dict(
            (good_pbk, GoodState(DEFAULT_PRICE)) for good_pbk in
            configuration.good_pbks)  # type: Dict[str, GoodState]
示例#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,
            )
示例#3
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
示例#4
0
    def adjusted_score(self) -> Tuple[List[str], np.ndarray]:
        """
        Compute the adjusted score of each agent.

        :return: a matrix of shape (1, nb_agents), where every column i contains the score of the agent.
        """
        nb_agents = self.game.configuration.nb_agents
        current_scores = np.zeros((1, nb_agents), dtype=np.float32)

        eq_agent_states = dict((
            agent_pbk,
            AgentState(
                self.game.initialization.eq_money_holdings[i],
                [int(h) for h in self.game.initialization.eq_good_holdings[i]],
                self.game.initialization.utility_params[i],
            ),
        ) for agent_pbk, i in zip(
            self.game.configuration.agent_pbks,
            range(self.game.configuration.nb_agents),
        ))  # type: Dict[str, AgentState]

        result = np.zeros((1, nb_agents), dtype=np.float32)

        eq_scores = np.zeros((1, nb_agents), dtype=np.float32)
        eq_scores[0, :] = [
            eq_agent_state.get_score()
            for eq_agent_state in eq_agent_states.values()
        ]

        temp_game = Game(self.game.configuration, self.game.initialization)

        # initial scores
        initial_scores = np.zeros((1, nb_agents), dtype=np.float32)
        scores_dict = temp_game.get_scores()
        initial_scores[0, :] = list(scores_dict.values())
        keys = list(scores_dict.keys())
        current_scores = np.zeros((1, nb_agents), dtype=np.float32)
        current_scores[0, :] = initial_scores[0, :]

        # compute the partial scores for every agent after every transaction
        # (remember that indexes of the transaction start from one, because index 0 is reserved for the initial scores)
        for idx, tx in enumerate(self.game.transactions):
            temp_game.settle_transaction(tx)
            scores_dict = temp_game.get_scores()
            current_scores[0, :] = list(scores_dict.values())

        result[0, :] = np.divide(
            np.subtract(current_scores, initial_scores),
            np.subtract(eq_scores, initial_scores),
        )
        result = np.transpose(result)

        return keys, result
示例#5
0
    def get_eq_scores(self) -> Dict[str, float]:
        """
        Get the equilibrium score for all agents.

        :return: dictionary mapping agent name to equilibrium score.
        """
        eq_agent_states = dict((
            agent_pbk,
            AgentState(
                self.game.initialization.eq_money_holdings[i],
                [int(h) for h in self.game.initialization.eq_good_holdings[i]],
                self.game.initialization.utility_params[i],
            ),
        ) for agent_pbk, i in zip(
            self.game.configuration.agent_pbks,
            range(self.game.configuration.nb_agents),
        ))  # type: Dict[str, AgentState]

        result = {
            self.game.configuration.agent_pbk_to_name[agent_pbk]:
            eq_agent_state.get_score()
            for agent_pbk, eq_agent_state in eq_agent_states.items()
        }
        return result