def get_agent(self, name, AgentClass, x_init,*args,**kwargs): if not self.initialized: raise Exception("{} need to be initialized first".format(type(self).__name__)) if self.agents.get(name) is not None: return self.agents.get(name) agent = AgentClass(name=name,*args,**kwargs) x_init = np.squeeze(x_init) if (x_init.shape[0] != agent.dim): raise ValueError(x_init) agent.set_init(x_init,self.times[-1]) if self.keep_global_state : if len(self.states)>0: x = list(np.squeeze(self.states[-1])) x.extend(x_init) self.states[-1] = np.squeeze(x) else: self.states.append(x_init) self.agents[name] = agent self.n_dim = sum([self.agents[key].dim for key in self.agents]) #np.squeeze(self.states[-1]).shape[0] return agent
def phy_equation(self,t,x): f_x= [] self.update_state_for_all_agents(x,t,save=False) for _,agent in self.agents.items(): dx = agent(z=x,t=t) f_x.extend(np.squeeze(dx)) return f_x
def update_state(self,x,t,save=True): x = np.squeeze(x) if x.shape[0] != self.dim or len(x.shape) != 1: raise ValueError(x) self.state = x self.t = t if self.save_history and save: self.history["t"].append(t) self.history["x"].append(x)
def __call__(self,z=None,t=None,*args,**kwargs): if self.controller is None: u = z else: u = self.controller(x=z, t=t, ego_agent=self,*args,**kwargs) u = np.squeeze(u) if u.shape[0] != self.dim_u or len(u.shape)!=1: raise ValueError(u) return self.dynamics(u,t,*args,**kwargs)
def update_state_for_all_agents(self,state,t,save=True): state = np.squeeze(state) for agent,x in zip(self.agents,np.split(state,len(self.agents))): self.agents[agent].update_state(x,t,save=save)