def _go_to_max(self): """Return to the location of the largest focus value seen so far and perform a local search to find the exact location of the peak.""" current_pos = self.camera.last_position() maximum_pos = max(self.camera.visited_positions, key=(lambda pos : self.camera.get_fvalue(pos))) if maximum_pos < current_pos: direction = Direction("left") elif maximum_pos > current_pos: direction = Direction("right") elif current_pos < self.camera.visited_positions[-2]: direction = Direction("left") else: direction = Direction("right") rev_direction = direction.reverse() # Take as many coarse steps as needed to go back to the maximum # without going over it. distance = abs(current_pos - maximum_pos) coarse_steps = distance / 8 self.camera.move_coarse(direction, coarse_steps) # Keep going in fine steps to see if we can find a higher position. start_pos = self.camera.last_position() self._do_local_search(direction, rev_direction) # If we didn't move further, we might want to look in the other # direction too. if start_pos == self.camera.last_position(): self._do_local_search(rev_direction, direction) self.status = "foundmax"
def search_standard(scenes, scene_to_print): print ("Perform a standard hill-climbing search, where coarse steps are\n" "taken until some stopping condition occurs, at which point the\n" "movement is reversed, at which point fine steps are taken to\n" "maximize the focus value. This is the method described in\n" "[He2003] and [Li2005].\n\n" "To visualize the steps taken for simulation of a specific scene,\n" "use the command-line argument --scene-to-print=something.txt") step_size = 8 data_rows = [("filename", "success %", "steps")] # Redirect stdout to a file for printing R script. orig_stdout = sys.stdout file_to_print = open("comparison.R", "w+") sys.stdout = file_to_print total_success = 0 for scene in scenes: success_count = 0 total_step_count = 0 initial_positions = range(0, scene.step_count - step_size) for initial_position in initial_positions: camera = CameraModel(scene, initial_position, simulate_backlash=simulate_backlash, simulate_noise=simulate_noise) first_measure = camera.last_fmeasure() camera.move_coarse(Direction("right")) # Determine whether to start moving left or right. if camera.last_fmeasure() < first_measure: direction = Direction("left") else: direction = Direction("right") # If the first step decreases focus value, switch direction. # This is a simple backtracking, basically. first_measure = camera.last_fmeasure() camera.move_coarse(direction) if camera.last_fmeasure() < first_measure: direction = direction.reverse() # Sweep max_value = camera.last_fmeasure() while not camera.will_hit_edge(direction): camera.move_coarse(direction) max_value = max(max_value, camera.last_fmeasure()) # Have we found a peak? if camera.last_fmeasure() < max_value * 0.9: # Stop searching break # Hillclimb until we're back at the peak. while not camera.will_hit_edge(direction.reverse()): prev_measure = camera.last_fmeasure() camera.move_fine(direction.reverse()) if prev_measure > camera.last_fmeasure(): camera.move_fine(direction) break # Record if we succeeded. if scene.distance_to_closest_peak(camera.last_position()) <= 1: success_count += 1 evaluation = "succeeded" else: evaluation = "failed" if scene.filename == scene_to_print: camera.print_script(evaluation) total_step_count += camera.steps_taken success = float(success_count) / len(initial_positions) * 100 line = (scene.name, "%.1f" % success, "%.1f" % (float(total_step_count) / len(initial_positions))) data_rows.append(line) total_success += success # Restore original stdout sys.stdout = orig_stdout file_to_print.close() print_aligned_data_rows(data_rows) print "average success : %.1f" % (total_success / len(scenes))