def move(self, car_state, action, *args, **kwargs): """ Moves object to the next point according to object's state. If object crosses the wall, move is rejected and object's position remains unchanged. :param car_state: state of car, of class CarState :param action: car action, of class Action :return: tuple(CarState with object's next position, boolean indication whether the collision happened) """ position = car_state.position velocity = car_state.velocity acceleration = rotate(car_state.heading, action.steering * pi / 2) * action.acceleration new_position = position + velocity * self.timedelta + acceleration * ( self.timedelta**2) / 2 collision = self.is_out_of_map(new_position) if collision: return CarState(position, -0.5 * velocity, -car_state.heading), collision else: new_velocity = velocity + acceleration * self.timedelta heading = new_position - position if abs(heading) > 1e-5: heading /= abs(heading) else: heading = car_state.heading return CarState(new_position, new_velocity, heading), collision
def set_agents(self, agents=1, agent_class=None): """ Поместить в мир агентов :param agents: int или список Agent, если int -- то обязателен параметр agent_class, так как в мир присвоятся agents агентов класса agent_class; если список, то в мир попадут все агенты из списка :param agent_class: класс создаваемых агентов, если agents - это int """ pos = (self.map[0][0] + self.map[0][1]) / 2 vel = 0 heading = rect(-0.3, 1) if type(agents) is int: self.agents = [agent_class() for _ in range(agents)] elif type(agents) is list: self.agents = agents else: raise ValueError( "Parameter agent should be int or list of agents instead of %s" % type(agents)) self.agent_states = { a: CarState(pos, vel, heading) for a in self.agents } self.circles = {a: 0 for a in self.agents} self._agent_surfaces = [] self._agent_images = []
def reset_agents(self): pos = (self.map[0][0] + self.map[0][1]) / 2 vel = 0 heading = rect(-0.3, 1) self.agent_states = {a: CarState(pos, vel, heading) for a in self.agents} self.circles = {a: 0 for a in self.agents}