Exemplo n.º 1
0
    def holdings_history(self):
        """
        Compute the history of holdings.

        :return: a matrix of shape (nb_transactions, nb_agents, nb_goods). i=0 is the initial endowment matrix.
        """
        nb_transactions = len(self.game.transactions)
        nb_agents = self.game.configuration.nb_agents
        nb_goods = self.game.configuration.nb_goods
        result = np.zeros((nb_transactions + 1, nb_agents, nb_goods),
                          dtype=np.int32)

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

        # initial holdings
        result[0, :] = np.asarray(temp_game.initialization.endowments,
                                  dtype=np.int32)

        # 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)
            result[idx + 1, :] = np.asarray(temp_game.get_holdings_matrix(),
                                            dtype=np.int32)

        return result
Exemplo n.º 2
0
    def eq_vs_mean_price(self) -> Tuple[List[str], np.ndarray]:
        """
        Compute the mean price of each good and display it together with the equilibrium price.

        :return: a matrix of shape (2, nb_goods), where every column i contains the prices of the good.
        """
        nb_transactions = len(self.game.transactions)
        eq_prices = self.game.initialization.eq_prices
        nb_goods = len(eq_prices)

        result = np.zeros((2, nb_goods), dtype=np.float32)
        result[0, :] = np.asarray(eq_prices, dtype=np.float32)

        prices_by_transactions = np.zeros((nb_transactions + 1, nb_goods),
                                          dtype=np.float32)

        # initial prices
        prices_by_transactions[0, :] = np.asarray(0, dtype=np.float32)

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

        for idx, tx in enumerate(self.game.transactions):
            temp_game.settle_transaction(tx)
            prices_by_transactions[idx + 1, :] = np.asarray(
                temp_game.get_prices(), dtype=np.float32)

        denominator = (prices_by_transactions != 0).sum(0)
        result[1, :] = np.true_divide(prices_by_transactions.sum(0),
                                      denominator)
        result[1, denominator == 0] = 0

        result = np.transpose(result)

        return self.game.configuration.good_names, result
Exemplo n.º 3
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
Exemplo n.º 4
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)
Exemplo n.º 5
0
    def get_initial_scores(self) -> Dict[str, float]:
        """
        Get the initial score for all agents.

        :return: dictionary mapping agent name to initial score.
        """
        temp_game = Game(self.game.configuration, self.game.initialization)
        scores_dict = {
            self.game.configuration.agent_pbk_to_name[agent_pbk]: score
            for agent_pbk, score in temp_game.get_scores().items()
        }
        return scores_dict
Exemplo n.º 6
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
Exemplo n.º 7
0
    def test_generate_game(self):
        """Test the game generation algorithm."""
        version_id = "1"
        nb_agents = 3
        nb_goods = 3
        money_endowment = 20
        tx_fee = 2.5
        base_amount = 2
        lower_bound_factor = 1
        upper_bound_factor = 3
        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",
        }

        _ = Game.generate_game(
            version_id,
            nb_agents,
            nb_goods,
            money_endowment,
            tx_fee,
            base_amount,
            lower_bound_factor,
            upper_bound_factor,
            agent_pbk_to_name,
            good_pbk_to_name,
        )
Exemplo n.º 8
0
    def tx_counts(self) -> Dict[str, Dict[str, int]]:
        """Get the tx counts."""
        agent_pbk_to_name = self.game.configuration.agent_pbk_to_name
        result = {agent_name: 0 for agent_name in agent_pbk_to_name.values()}
        results = {"seller": result.copy(), "buyer": result.copy()}

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

        # 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)
            results["seller"][agent_pbk_to_name[tx.seller_pbk]] += 1
            results["buyer"][agent_pbk_to_name[tx.buyer_pbk]] += 1

        return results
Exemplo n.º 9
0
    def price_history(self) -> np.ndarray:
        """Get the price history."""
        nb_transactions = len(self.game.transactions)
        nb_goods = self.game.configuration.nb_goods
        result = np.zeros((nb_transactions + 1, nb_goods), dtype=np.float32)

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

        # initial prices
        result[0, :] = np.asarray(0, dtype=np.float32)

        for idx, tx in enumerate(self.game.transactions):
            temp_game.settle_transaction(tx)
            result[idx + 1, :] = np.asarray(temp_game.get_prices(),
                                            dtype=np.float32)

        return result
Exemplo n.º 10
0
    def tx_prices(self) -> Dict[str, List[float]]:
        """Get the tx counts."""
        agent_pbk_to_name = self.game.configuration.agent_pbk_to_name
        results = {
            agent_name: []
            for agent_name in agent_pbk_to_name.values()
        }  # type: Dict[str, List[float]]

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

        # 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)
            results[agent_pbk_to_name[tx.seller_pbk]].append(tx.amount)

        return results
Exemplo n.º 11
0
    def score_history(self) -> Tuple[List[str], np.ndarray]:
        """
        Compute the history of the scores for every agent.

        To do so, we need to simulate the game again, by settling transactions one by one
        and get the scores after every transaction.

        :return: a matrix of shape (nb_transactions + 1, nb_agents), where every row i contains the scores
                 after transaction i (i=0 is a row with the initial scores.)
        """
        nb_transactions = len(self.game.transactions)
        nb_agents = self.game.configuration.nb_agents
        result = np.zeros((nb_transactions + 1, nb_agents))

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

        # initial scores
        scores_dict = temp_game.get_scores()
        result[0, :] = list(scores_dict.values())
        keys = list(scores_dict.keys())

        # 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()
            result[idx + 1, :] = list(scores_dict.values())

        return keys, result
Exemplo n.º 12
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)
        game_stats = GameStats(game)
        return ControllerDashboard(game_stats, env_name=env_name)
Exemplo n.º 13
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
Exemplo n.º 14
0
    def _create_game(self) -> Game:
        """
        Create a TAC game.

        :return: a Game instance.
        """
        nb_agents = len(self.registered_agents)

        game = Game.generate_game(self.tac_parameters.version_id, nb_agents,
                                  self.tac_parameters.nb_goods,
                                  self.tac_parameters.tx_fee,
                                  self.tac_parameters.money_endowment,
                                  self.tac_parameters.base_good_endowment,
                                  self.tac_parameters.lower_bound_factor,
                                  self.tac_parameters.upper_bound_factor,
                                  self.agent_pbk_to_name,
                                  self.good_pbk_to_name)

        return game
Exemplo n.º 15
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")
            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
Exemplo n.º 16
0
    def test_baseline_agent_score_does_not_decrease(self):
        """Test that all the baseline agent scores do not decrease after each transaction."""
        finished_game = self.tac_controller.game_handler.current_game
        game_configuration = finished_game.configuration
        game_initialization = finished_game.initialization
        game = Game(game_configuration, game_initialization)

        scores_dict = game.get_scores()
        current_score = np.asarray(list(scores_dict.values()))
        next_scores = None
        for tx in finished_game.transactions:
            game.settle_transaction(tx)
            scores_dict = game.get_scores()
            next_scores = np.asarray(list(scores_dict.values()))
            assert not (next_scores < current_score).any()
            current_score = next_scores
Exemplo n.º 17
0
    def balance_history(self) -> Tuple[List[str], np.ndarray]:
        """Get the balance history."""
        nb_transactions = len(self.game.transactions)
        nb_agents = self.game.configuration.nb_agents
        result = np.zeros((nb_transactions + 1, nb_agents), dtype=np.int32)

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

        # initial balances
        balances_dict = temp_game.get_balances()
        result[0, :] = np.asarray(list(balances_dict.values()), dtype=np.int32)
        keys = list(balances_dict.keys())

        # 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)
            balances_dict = temp_game.get_balances()
            result[idx + 1, :] = np.asarray(list(balances_dict.values()),
                                            dtype=np.int32)

        return keys, result
Exemplo n.º 18
0
 def from_json(cls, d: Dict[str, Any]):
     """Read from json."""
     game = Game.from_dict(d)
     return GameStats(game)