Source code for smartgrid.rewards.reward_collection
"""The RewardCollection is responsible for computing rewards from reward functions."""fromtypingimportList,Dictfromsmartgrid.agentsimportAgentfrom.rewardimportReward
[docs]classRewardCollection:""" The RewardCollection holds all desired reward functions, and computes the rewards. This class allows for multi-objective reinforcement learning, by holding several reward functions, and returning dicts of rewards (names -> values), instead of using a single reward function. The multiple reward functions can be aggregated (scalarized) to adapt to single-objective learning algorithms, by using a :py:class:`~smartgrid.wrappers.reward_aggregator.RewardAggregator` wrapper over the environment. """
[docs]def__init__(self,rewards:List[Reward]):""" Create a RewardCollection based on a list of "reward functions". :param rewards: The list of "reward functions" (actually instances of the :py:class:`~smartgrid.rewards.reward.Reward` class). This list must contain at least 1 element. """assertlen(rewards)>0,"You need to specify at least one Reward."self.rewards=rewards
[docs]defcompute(self,world:'World',agent:Agent)->Dict[str,float]:""" Compute the list of :py:class:`.Reward` for the Agent. :param world: reference on the world for global information. :param agent: reference on the agent for local information. :return: A dictionary mapping the rewards' name to their value, for each reward function in this Reward Collection. """to_return={}forrewardinself.rewards:ifreward.is_activated(world,agent):to_return[reward.name]=reward.calculate(world,agent)returnto_return
[docs]defreset(self):""" Reset the reward functions. """forrewardinself.rewards:reward.reset()