Exemplo n.º 1
0
    def get_current_optimization_metrics(self,
                                         agents,
                                         isoelastic_eta=0.23,
                                         labor_exponent=2.0,
                                         labor_coefficient=0.1):
        """
        Compute optimization metrics based on the current state. Used to compute reward.

        Returns:
            curr_optimization_metric (dict): A dictionary of {agent.idx: metric}
                with an entry for each agent (including the planner) in the env.
        """
        curr_optimization_metric = {}

        coin_endowments = np.array(
            [agent.total_endowment("Coin") for agent in agents])

        pretax_incomes = np.array(
            [agent.state["production"] for agent in agents])

        # Optimization metric for agents:
        for agent in agents:
            if self.agent_reward_type == "isoelastic_coin_minus_labor":
                assert 0.0 <= isoelastic_eta <= 1.0
                curr_optimization_metric[
                    agent.idx] = rewards.isoelastic_coin_minus_labor(
                        coin_endowment=agent.total_endowment("Coin"),
                        total_labor=agent.state["endogenous"]["Labor"],
                        isoelastic_eta=isoelastic_eta,
                        labor_coefficient=labor_coefficient,
                    )
            elif self.agent_reward_type == "coin_minus_labor_cost":
                assert labor_exponent > 1.0
                curr_optimization_metric[
                    agent.idx] = rewards.coin_minus_labor_cost(
                        coin_endowment=agent.total_endowment("Coin"),
                        total_labor=agent.state["endogenous"]["Labor"],
                        labor_exponent=labor_exponent,
                        labor_coefficient=labor_coefficient,
                    )
        # Optimization metric for the planner:
        if self.planner_reward_type == "coin_eq_times_productivity":
            curr_optimization_metric[
                self.world.planner.idx] = rewards.coin_eq_times_productivity(
                    coin_endowments=coin_endowments,
                    equality_weight=1 - self.mixing_weight_gini_vs_coin,
                )
        elif self.planner_reward_type == "inv_income_weighted_utility":
            curr_optimization_metric[
                self.world.planner.idx] = rewards.inv_income_weighted_utility(
                    coin_endowments=pretax_incomes,  # coin_endowments,
                    utilities=np.array([
                        curr_optimization_metric[agent.idx] for agent in agents
                    ]),
                )
        else:
            print("No valid planner reward selected!")
            raise NotImplementedError
        return curr_optimization_metric
    def get_current_optimization_metrics(self):
        curr_optimization_metric = {}
        # (for agents)
        agent_reward = rewards.coin_eq_times_productivity(
            coin_endowments=np.array([agent.total_endowment("Coin") for agent in self.world.agents]),
            equality_weight=1 - self.mixing_weight_gini_vs_coin,
        )
        for agent in self.world.agents:
            # scale reward to be close to original (utility rewards)
            curr_optimization_metric[agent.idx] = agent_reward / 10.

        # (for the planner)
        if self.planner_reward_type == "coin_eq_times_productivity":
            curr_optimization_metric[
                self.world.planner.idx
            ] = rewards.coin_eq_times_productivity(
                coin_endowments=np.array(
                    [agent.total_endowment("Coin") for agent in self.world.agents]
                ),
                equality_weight=1 - self.mixing_weight_gini_vs_coin,
            )
        elif self.planner_reward_type == "inv_income_weighted_coin_endowments":
            curr_optimization_metric[
                self.world.planner.idx
            ] = rewards.inv_income_weighted_coin_endowments(
                coin_endowments=np.array(
                    [agent.total_endowment("Coin") for agent in self.world.agents]
                ),
            )
        elif self.planner_reward_type == "inv_income_weighted_utility":
            curr_optimization_metric[
                self.world.planner.idx
            ] = rewards.inv_income_weighted_utility(
                coin_endowments=np.array(
                    [agent.total_endowment("Coin") for agent in self.world.agents]
                ),
                utilities=np.array(
                    [curr_optimization_metric[agent.idx] for agent in self.world.agents]
                ),
            )
        else:
            print("No valid planner reward selected!")
            raise NotImplementedError
        return curr_optimization_metric
Exemplo n.º 3
0
    def scenario_metrics(self):
        """
        Allows the scenario to generate metrics (collected along with component metrics
        in the 'metrics' property).

        To have the scenario add metrics, this function needs to return a dictionary of
        {metric_key: value} where 'value' is a scalar (no nesting or lists!)

        Here, summarize social metrics, endowments, utilities, and labor cost annealing.
        """
        metrics = dict()

        coin_endowments = np.array(
            [agent.total_endowment("Coin") for agent in self.world.agents]
        )
        metrics["social/productivity"] = social_metrics.get_productivity(
            coin_endowments
        )
        metrics["social/equality"] = social_metrics.get_equality(coin_endowments)

        utilities = np.array(
            [self.curr_optimization_metric[agent.idx] for agent in self.world.agents]
        )
        metrics[
            "social_welfare/coin_eq_times_productivity"
        ] = rewards.coin_eq_times_productivity(
            coin_endowments=coin_endowments, equality_weight=1.0
        )
        metrics[
            "social_welfare/inv_income_weighted_coin_endow"
        ] = rewards.inv_income_weighted_coin_endowments(coin_endowments=coin_endowments)
        metrics[
            "social_welfare/inv_income_weighted_utility"
        ] = rewards.inv_income_weighted_utility(
            coin_endowments=coin_endowments, utilities=utilities
        )

        for agent in self.all_agents:
            for resource, quantity in agent.inventory.items():
                metrics[
                    "endow/{}/{}".format(agent.idx, resource)
                ] = agent.total_endowment(resource)

            if agent.endogenous is not None:
                for resource, quantity in agent.endogenous.items():
                    metrics["endogenous/{}/{}".format(agent.idx, resource)] = quantity

            metrics["util/{}".format(agent.idx)] = self.curr_optimization_metric[
                agent.idx
            ]

        # Labor weight
        metrics["labor/weighted_cost"] = self.energy_cost * self.energy_weight
        metrics["labor/warmup_integrator"] = int(self._auto_warmup_integrator)

        return metrics
Exemplo n.º 4
0
    def get_current_optimization_metrics(self):
        """
        Compute optimization metrics based on the current state. Used to compute reward.

        Returns:
            curr_optimization_metric (dict): A dictionary of {agent.idx: metric}
                with an entry for each agent (including the planner) in the env.
        """
        curr_optimization_metric = {}
        # (for agents)
        for agent in self.world.agents:
            curr_optimization_metric[
                agent.idx] = rewards.isoelastic_coin_minus_labor(
                    coin_endowment=agent.total_endowment("Coin"),
                    total_labor=agent.state["endogenous"]["Labor"],
                    isoelastic_eta=self.isoelastic_eta,
                    labor_coefficient=self.energy_weight * self.energy_cost,
                )
        # (for the planner)
        if self.planner_reward_type == "coin_eq_times_productivity":
            curr_optimization_metric[
                self.world.planner.idx] = rewards.coin_eq_times_productivity(
                    coin_endowments=np.array([
                        agent.total_endowment("Coin")
                        for agent in self.world.agents
                    ]),
                    equality_weight=1 - self.mixing_weight_gini_vs_coin,
                )
        elif self.planner_reward_type == "inv_income_weighted_coin_endowments":
            curr_optimization_metric[
                self.world.planner.
                idx] = rewards.inv_income_weighted_coin_endowments(
                    coin_endowments=np.array([
                        agent.total_endowment("Coin")
                        for agent in self.world.agents
                    ]))
        elif self.planner_reward_type == "inv_income_weighted_utility":
            curr_optimization_metric[
                self.world.planner.idx] = rewards.inv_income_weighted_utility(
                    coin_endowments=np.array([
                        agent.total_endowment("Coin")
                        for agent in self.world.agents
                    ]),
                    utilities=np.array([
                        curr_optimization_metric[agent.idx]
                        for agent in self.world.agents
                    ]),
                )
        else:
            print("No valid planner reward selected!")
            raise NotImplementedError
        return curr_optimization_metric
Exemplo n.º 5
0
    def scenario_metrics(self):
        """
        Allows the scenario to generate metrics (collected along with component metrics
        in the 'metrics' property).

        To have the scenario add metrics, this function needs to return a dictionary of
        {metric_key: value} where 'value' is a scalar (no nesting or lists!)

        Here, summarize social metrics, endowments, utilities, and labor cost annealing.
        """
        metrics = dict()

        # Log social/economic indicators
        coin_endowments = np.array(
            [agent.total_endowment("Coin") for agent in self.world.agents])
        pretax_incomes = np.array(
            [agent.state["production"] for agent in self.world.agents])
        metrics["social/productivity"] = social_metrics.get_productivity(
            coin_endowments)
        metrics["social/equality"] = social_metrics.get_equality(
            coin_endowments)

        utilities = np.array([
            self.curr_optimization_metrics[agent.idx]
            for agent in self.world.agents
        ])
        metrics[
            "social_welfare/coin_eq_times_productivity"] = rewards.coin_eq_times_productivity(
                coin_endowments=coin_endowments, equality_weight=1.0)
        metrics[
            "social_welfare/inv_income_weighted_utility"] = rewards.inv_income_weighted_utility(
                coin_endowments=pretax_incomes,
                utilities=utilities  # coin_endowments,
            )

        # Log average endowments, endogenous, and utility for agents
        agent_endows = {}
        agent_endogenous = {}
        agent_utilities = []
        for agent in self.world.agents:
            for resource in agent.inventory.keys():
                if resource not in agent_endows:
                    agent_endows[resource] = []
                agent_endows[resource].append(agent.inventory[resource] +
                                              agent.escrow[resource])

            for endogenous, quantity in agent.endogenous.items():
                if endogenous not in agent_endogenous:
                    agent_endogenous[endogenous] = []
                agent_endogenous[endogenous].append(quantity)

            agent_utilities.append(self.curr_optimization_metrics[agent.idx])

        for resource, quantities in agent_endows.items():
            metrics["endow/avg_agent/{}".format(resource)] = np.mean(
                quantities)

        for endogenous, quantities in agent_endogenous.items():
            metrics["endogenous/avg_agent/{}".format(endogenous)] = np.mean(
                quantities)

        metrics["util/avg_agent"] = np.mean(agent_utilities)

        # Log endowments and utility for the planner
        for resource, quantity in self.world.planner.inventory.items():
            metrics["endow/p/{}".format(resource)] = quantity

        metrics["util/p"] = self.curr_optimization_metrics[
            self.world.planner.idx]

        return metrics