def simulate(self): """ A function that simulates an entire episode. The gen_agents are updated with simultaneous threads running their update() functions and updating the robot with commands from the external joystick process. """ # initialize pre-simulation metadata self.init_sim_data() # keep track of wall-time in the simulator start_time = time.time() # get initial state current_state = self.save_state() # initialize robot update thread r_t = self.init_robot_listener_thread(current_state) # start iteration iteration = 0 self.print_sim_progress(iteration) # run simulation while self.sim_t <= self.episode_params.max_time and self.loop_condition( ): wall_t = time.time() # update the time for all agents Agent.set_sim_t(self.sim_t) # initiate thread operations self.pedestrians_update(current_state) if self.robot is not None: # calls a single iteration of the robot update self.robot.update() # update simulator time self.sim_t += self.dt # capture time after all the gen_agents have updated # Takes screenshot of the new simulation state current_state = self.save_state(wall_t - start_time) if self.robot: self.robot.update_world(current_state) # update iteration count iteration += 1 # print simulation progress self.print_sim_progress(iteration) # synchronize time with real-world if running in asynchronous mode self.synchronize(wall_t) # finish the simulate self.conclude_simulation(start_time, iteration, r_t)
def init_sim_data(self, verbose: bool = True): self.total_agents = len(self.agents) + len(self.backstage_prerecs) # Create pre-simulation metadata if verbose: print("Running simulation on", self.total_agents, "agents") # scale the simulator time self.dt = self.params.delta_t_scale * self.params.dt # update the baseline agents' simulation refresh rate Agent.set_sim_dt(self.dt) Agent.set_sim_t(self.sim_t) # add the first (when t=0) agents to the self.prerecs dict self.init_prerec_agent_threads(current_state=None) # save initial state before the simulator is spawned self.sim_t = 0.0 if self.dt < self.params.dt: print( "%sSimulation dt is too small; either lower the gen_agents' dt's" % color_red, self.params.dt, "or increase simulation delta_t%s" % color_reset) exit(1)