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))