"""Done fns. These can be used either with termination or truncation."""fromtypingimportAny,Dict,List,Optionalimportnumpyasnpfromcambrian.agentsimportMjCambrianAgentfromcambrian.envsimportMjCambrianEnvfromcambrian.utilsimportagent_selected# ======================
[docs]defdone_if_exceeds_max_episode_steps(env:MjCambrianEnv,agent:MjCambrianAgent,info:Dict[str,Any])->bool:"""Done if episode step exceeds max episode steps."""returnenv.episode_step>=env.max_episode_steps-1
[docs]defdone_if_low_reward(env:MjCambrianEnv,agent:MjCambrianAgent,info:Dict[str,Any],*,threshold:float,disable:bool=False,)->bool:"""Done if agent has low reward."""ifdisable:returnFalsereturnenv.cumulative_reward<threshold
[docs]defdone_if_has_contacts(env:MjCambrianEnv,agent:MjCambrianAgent,info:Dict[str,Any],*,for_agents:Optional[List[str]]=None,disable:bool=False,)->bool:"""Done if agent has contacts."""ifnotagent_selected(agent,for_agents)ordisable:returnFalsereturninfo["has_contacts"]
[docs]defdone_if_close_to_agents(env:MjCambrianEnv,agent:MjCambrianAgent,info:Dict[str,Any],*,to_agents:Optional[List[str]]=None,for_agents:Optional[List[str]]=None,distance_threshold:float,disable:bool=False,)->bool:"""Done if agent is close to another agent."""# Early exit if the agent is not in the for_agents listifnotagent_selected(agent,for_agents)ordisable:returnFalseforother_agentinenv.agents.values():ifnotagent_selected(other_agent,to_agents)orother_agent.name==agent.name:continueifnp.linalg.norm(other_agent.pos-agent.pos)<distance_threshold:returnTruereturnFalse