def visualize_paths(self, results_dir, exp_module_name, agent_name): world_config = self.get_world_config(results_dir, exp_module_name) # delete log folder we just created by re-importing the world config shutil.rmtree(world_config.world_logger.curr_exp_log_dir) worlds = self.get_world_images(results_dir) poss = self.get_init_poss(results_dir) actionss = self.get_actionss(results_dir, agent_name) _dir = os.path.join(results_dir.split("/")[-1], agent_name) if not os.path.exists(self.RESULTS_DIR + _dir): os.makedirs(self.RESULTS_DIR + _dir) # print 100 paths for i in range(0, len(actionss), len(actionss) / 100): world = worlds[i] pos = poss[i] actions = actionss[i] name = os.path.join(_dir, "epoch" + str(i)) # calc start of view because mid is given start_pos = pos - Point(world_config.view_size.w / 2, world_config.view_size.h / 2) self.visualizer.visualize_course_of_action( world, start_pos.x, start_pos.y, world_config.view_size.w, world_config.view_size.h, actions, image_name=name)
def update_view_image(self): # if view position is out of bounds we can't generate the next view image if self.is_oob(): self.view_image = None else: # find start of view because view_pos indicates center of view view_start = self.view_pos - Point(self.view_size.w / 2, self.view_size.h / 2) self.view_image = Rectangle( view_start, self.view_size).crop_matrix(self.world_image)
def _get_init_pos_from_file(self, path): poses = {} _file = open(path) _file.next() # skip header line for line in _file: vals = line.split("\t") epoch = int(vals[0]) pos = Point(int(vals[1]), int(vals[2])) poses[epoch] = pos return poses
def _get_init_poses(self): poses = {} init_pos_path = os.path.join(self.worlds_path, self.INIT_STATES_FILE_NAME) _file = open(init_pos_path) _file.next() # skip header line for line in _file: vals = line.split("\t") epoch = int(vals[0]) pos = Point(int(vals[1]), int(vals[2])) poses[epoch] = pos return poses
def get_init_poss(self, results_dir): poss = {} init_pos_path = os.path.join(results_dir, self.WORLD_DIR, self.WORLD_INIT_DIR, self.WORLD_INIT_NAME) _file = open(init_pos_path) _file.next() # skip header line for line in _file: vals = line.split("\t") epoch = int(vals[0]) pos = Point(int(vals[1]), int(vals[2])) poss[epoch] = pos return poss
def sample_step_from(self, view_size, bbox, prev_node, allow_opp_dir=False): direction = -1 while direction == -1 or (not allow_opp_dir and direction == Actions.get_opposite(prev_node.prev_dir)): direction = sample_int_uniform(4) step = Point(0, 0) step_size_min = max( min(view_size.tuple()) / 2., 2) if self.step_size_min == -1 else self.step_size_min if direction == Actions.UP: step_size_max = prev_node.pos.y - bbox.y step.y = -1 elif direction == Actions.DOWN: step_size_max = bbox.y + bbox.h - prev_node.pos.y step.y = 1 elif direction == Actions.LEFT: step_size_max = prev_node.pos.x - bbox.x step.x = -1 else: # Actions.RIGHT step_size_max = bbox.x + bbox.w - prev_node.pos.x step.x = 1 if self.step_size_max != -1: step_size_max = min(step_size_max, self.step_size_max) step_size_diff = step_size_max - step_size_min # check if step not possible if step_size_diff <= 1: return None, None step_size = sample_int_normal_bounded(step_size_min, step_size_max) step *= step_size return direction, prev_node.pos + step
def update_view(self, action): x = 0 y = 0 if action == Actions.UP: y = -1 elif action == Actions.DOWN: y = 1 elif action == Actions.LEFT: x = -1 elif action == Actions.RIGHT: x = 1 self.view_pos += Point(x, y) self.update_view_image() return self.get_current_view()
def _get_sample_bbox(self, bbox, target_pos): """returns a new bbox from which to sample afterwards based on the current bounds and the target position""" # sampling box is doubled to cover the whole world because we sample around target h_spl_box = self._get_side_length(self.min_sample_size.h, bbox.h) * 2 w_spl_box = self._get_side_length(self.min_sample_size.w, bbox.w) * 2 # log h and w self.logger.log_parameter("expansive_w_h", [w_spl_box, h_spl_box]) x = target_pos.x - int(w_spl_box * 0.5) y = target_pos.y - int(h_spl_box * 0.5) pt_start_spl_box = Point(x, y) spl_box_size = Size(w_spl_box, h_spl_box) spl_box = Rectangle(pt_start_spl_box, spl_box_size) inter_box = spl_box.intersection(bbox) self.logger.log_parameter( "expansive_rectangles", [str(bbox), str(spl_box), str(inter_box)]) return inter_box
def is_intersecting(self, e1, e2): v1, v2 = e1 v3, v4 = e2 p1 = v1.pos p2 = v2.pos p3 = v3.pos p4 = v4.pos # Before anything else check if lines have a mutual abcisses interval_1 = [min(p1.x, p2.x), max(p1.x, p2.x)] interval_2 = [min(p3.x, p4.x), max(p3.x, p4.x)] interval = [ min(interval_1[1], interval_2[1]), max(interval_1[0], interval_2[0]) ] if interval_1[1] < interval_2[0]: return False # no mutual abcisses # Try to compute interception def line(p1, p2): A = (p1.y - p2.y) B = (p2.x - p1.x) C = (p1.x * p2.y - p2.x * p1.y) return A, B, -C L1 = line(p1, p2) L2 = line(p3, p4) D = L1[0] * L2[1] - L1[1] * L2[0] Dx = L1[2] * L2[1] - L1[1] * L2[2] Dy = L1[0] * L2[2] - L1[2] * L2[0] if D != 0: x = Dx / D y = Dy / D p = Point(x, y) if p.x < interval[1] or p.x > interval[0]: return False # out of bounds else: # it's an intersection if new edge does not originate from previous one return not (p == p1 == p3 or p == p1 == p4) else: return False # not intersecting
def _get_target_pos(self, world): y = int(np.mean(np.where(world == self.TARGET_VALUE)[0])) x = int(np.mean(np.where(world == self.TARGET_VALUE)[1])) return Point(x, y)