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
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
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
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
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
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
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
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
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
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