[docs]classCustomCallbacks(DefaultCallbacks):""" CustomCallbacks class that extends the DefaultCallbacks class and overrides its methods to customize the behavior of the callbacks during the RL training process. """
[docs]defon_episode_start(self,*,worker,base_env,policies,episode,env_index,**kwargs)->None:""" Method that is called at the beginning of each episode in the training process. It sets some user_data variables to be used later on. Args: worker (Worker): The worker object that is being used in the training process. base_env (BaseEnv): The base environment that is being used in the training process. policies (Dict[str, Policy]): The policies that are being used in the training process. episode (MultiAgentEpisode): The episode object that is being processed. env_index (int): The index of the environment within the worker task. **kwargs: additional arguments that can be passed. """episode.user_data["net_energy_sum"]=0episode.user_data["CO2_footprint_sum"]=0episode.user_data["step_count"]=0episode.user_data["instantaneous_net_energy"]=[]episode.user_data["load_left"]=0episode.user_data["ls_tasks_in_queue"]=0episode.user_data["ls_tasks_dropped"]=0episode.user_data["water_usage"]=0
[docs]defon_episode_step(self,*,worker,base_env,episode,env_index,**kwargs)->None:""" Method that is called at each step of each episode in the training process. It updates some user_data variables to be used later on. Args: worker (Worker): The worker object that is being used in the training process. base_env (BaseEnv): The base environment that is being used in the training process. episode (MultiAgentEpisode): The episode object that is being processed. env_index (int): The index of the environment within the worker task. **kwargs: additional arguments that can be passed. """# print(f'Logging env base with month: {base_env.envs[0].month}')# print(f'Logging env worker with month: {worker.env.month}')net_energy=base_env.envs[0].bat_info["bat_total_energy_with_battery_KWh"]CO2_footprint=base_env.envs[0].bat_info["bat_CO2_footprint"]load_left=base_env.envs[0].ls_info["ls_unasigned_day_load_left"]tasks_in_queue=base_env.envs[0].ls_info["ls_tasks_in_queue"]tasks_dropped=base_env.envs[0].ls_info["ls_tasks_dropped"]water_usage=base_env.envs[0].dc_info["dc_water_usage"]episode.user_data["instantaneous_net_energy"].append(net_energy)episode.user_data["net_energy_sum"]+=net_energyepisode.user_data["CO2_footprint_sum"]+=CO2_footprintepisode.user_data["load_left"]+=load_leftepisode.user_data["ls_tasks_in_queue"]+=tasks_in_queueepisode.user_data["ls_tasks_dropped"]+=tasks_droppedepisode.user_data["water_usage"]+=water_usageepisode.user_data["step_count"]+=1
[docs]defon_episode_end(self,*,worker,base_env,policies,episode,env_index,**kwargs)->None:""" Method that is called at the end of each episode in the training process. It calculates some metrics based on the updated user_data variables. Args: worker (Worker): The worker object that is being used in the training process. base_env (BaseEnv): The base environment that is being used in the training process. policies (Dict[str, Policy]): The policies that are being used in the training process. episode (MultiAgentEpisode): The episode object that is being processed. env_index (int): The index of the environment within the worker task. **kwargs: additional arguments that can be passed. """ifepisode.user_data["step_count"]>0:average_net_energy=episode.user_data["net_energy_sum"]/episode.user_data["step_count"]average_CO2_footprint=episode.user_data["CO2_footprint_sum"]/episode.user_data["step_count"]total_load_left=episode.user_data["load_left"]total_tasks_in_queue=episode.user_data["ls_tasks_in_queue"]total_tasks_dropped=episode.user_data["ls_tasks_dropped"]total_water_usage=episode.user_data["water_usage"]else:average_net_energy=0average_CO2_footprint=0average_bat_actions=0average_ls_actions=0average_dc_actions=0total_load_left=0total_tasks_in_queue=0total_tasks_dropped=0total_water_usage=0episode.custom_metrics["average_total_energy_with_battery"]=average_net_energyepisode.custom_metrics["average_CO2_footprint"]=average_CO2_footprintepisode.custom_metrics["load_left"]=total_load_leftepisode.custom_metrics["total_tasks_in_queue"]=total_tasks_in_queueepisode.custom_metrics["total_tasks_dropped"]=total_tasks_droppedepisode.custom_metrics["total_water_usage"]=total_water_usage