Esempio n. 1
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
    def generate_observations(self):
        """
        Generate observations associated with this scenario.

        A scenario does not need to produce observations and can provide observations
        for only some agent types; however, for a given agent type, it should either
        always or never yield an observation. If it does yield an observation,
        that observation should always have the same structure/sizes!

        Returns:
            obs (dict): A dictionary of {agent.idx: agent_obs_dict}. In words,
                return a dictionary with an entry for each agent (which can including
                the planner) for which this scenario provides an observation. For each
                entry, the key specifies the index of the agent and the value contains
                its associated observation dictionary.

        Here, non-planner agents receive spatial observations (depending on the env
        config) as well as the contents of their inventory and endogenous quantities.
        The planner also receives spatial observations (again, depending on the env
        config) as well as the inventory of each of the mobile agents.
        """
        obs_dict = dict()
        for agent in self.world.agents:
            obs_dict[str(agent.idx)] = {}

        coin_endowments = np.array(
            [agent.total_endowment("Coin") for agent in self.world.agents])
        equality = social_metrics.get_equality(coin_endowments)
        productivity = social_metrics.get_productivity(coin_endowments)
        normalized_per_capita_productivity = productivity / self.num_agents / 1000
        obs_dict[self.world.planner.idx] = {
            "normalized_per_capita_productivity":
            normalized_per_capita_productivity,
            "equality": equality,
        }

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