def connect_intersections(self, a: 'Intersection', b: 'Intersection', intersections: Set['Intersection']): a_b_separation = mag(a - b) self[a][b].dist = self[b][a].dist = a_b_separation self[a][b].next_step = b self[b][a].next_step = a # Partition all other intersections based on distances to a and b a_set = set() b_set = set() for inter in intersections: if self[inter][a].dist < self[inter][b].dist: a_set.add(inter) else: b_set.add(inter) for a_inter in a_set - {b}: for b_inter in b_set: newly_offered_separation = self[a_inter][ a].dist + a_b_separation + self[b][b_inter].dist if newly_offered_separation < self[a_inter][b_inter].dist: self[a_inter][b_inter].dist = self[b_inter][ a_inter].dist = newly_offered_separation self[a_inter][b_inter].next_step = self[a_inter][ a].next_step if a_inter != a else b self[b_inter][a_inter].next_step = self[b_inter][ b].next_step if b_inter != b else a
def step(self, actions): super().step(actions) for agent_state, action in zip(self.agent_states, actions): agent_state.action_update(action, self) if self.steps == self.tiles_shape[0] + self.tiles_shape[1]: self.done = True return None, None, self.done, self.agent_debugs if self.goal.verb.type == Goal.Verb.Type.REACH: rews = [(mag(state.initial_pos - state.goal.noun.adjective) - mag(state.pos - state.goal.noun.adjective)) / self.cache.normalized_diagonal for state in self.agent_states] for state, rew in zip(self.agent_states, rews): state.health = rew return self.agent_states, rews, self.done, self.agent_debugs else: raise AssertionError('Unknown goal verb type: ' + self.goal.verb.type)
def display_hotspot(self, hotspot: Hotspot): hp = hotspot.pos center = self.window_point(hp) speed = int(255 * min(mag(hotspot.vel), 10) / 10) cv2.circle(self.img, center, int(self.scale * np.sqrt(hotspot.mass)), (255 - speed, 0, speed), -1) cv2.line(self.img, center, self.window_point(hp + 10 * hotspot.force), (0, 255, 0)) ci = hotspot.closest_intersection.obj # cv2.putText(self.img, str(hotspot.closest_intersection.separation), self.window_point((hp + ci) / 2), # cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 0)) self.display_edge(Edge(hp, ci), (255, 0, 255))
def update(self, window_width, tiles_shape): self.diagonal = mag(tiles_shape) self.cell_size = window_width / tiles_shape[0] self.corner_offset = np.array([1 / tiles_shape[0], 1 / tiles_shape[1]])