Last active
March 21, 2016 01:05
-
-
Save Jdsleppy/6f84a195b4e7717840c1 to your computer and use it in GitHub Desktop.
2D peak finding inspired by binary search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_search_grid(center_point, side_length): | |
# Return a 3x3 grid of points, centered at center_point, | |
# making a square side_length tall and wide. | |
return grid_of_points | |
def stop_condition(value): | |
# Decide if you're at a good enough place to end the search. | |
# Tweak this for your own application. | |
return should_stop_searching | |
def measure_here(): | |
# Query the function you are trying to optimize. | |
# We want to do this as few times as possible! | |
return value | |
search_size = # Side length of approximately the area you wish to search. | |
# The peak should be in here somewhere. | |
start_point = # The center point of the area to search. | |
resolution = # Some size below which you will stop searching. | |
# Beyond this point, you just aren't going to find anything better. | |
best_point_and_value = (start_point, 0) | |
while search_size > resolution: | |
for point in create_search_grid(best_point_and_value[0], search_size): | |
# Go to each point and measure | |
move_to(point) | |
measuredValue = measure_here() | |
# If that's good enough, stop | |
if stop_condition(measuredValue): | |
break | |
# If it's the best point so far, remember it | |
if measuredValue > best_point_and_value[1]: | |
best_point_and_value = (point, measuredValue) | |
# Search a smaller grid, centered on the best point in the current grid. | |
search_size /= 2 | |
else: | |
# End the search with a final scan of size = resolution | |
for point in create_search_grid(best_point_and_value[0], resolution): | |
move_to(point) | |
measuredValue = measure_here() | |
if stop_condition(measuredValue): | |
break | |
if measuredValue > best_point_and_value[1]: | |
best_point_and_value = (point, measuredValue) | |
# You found the best spot. Go there and get on with your life. | |
MoveTo(best_point_and_value[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment