Skip to content

Instantly share code, notes, and snippets.

@andrewcsmith
Created August 9, 2012 01:08
Show Gist options
  • Save andrewcsmith/3300144 to your computer and use it in GitHub Desktop.
Save andrewcsmith/3300144 to your computer and use it in GitHub Desktop.
Search algorithm that slows way, way down after around 40,000 iterations
max_iterations.times do |iter|
begin
# puts "Iteration #{iter}"
# puts "Now #{current_cost} away at #{current_point.to_a}"
print "\rIteration #{iter}: #{current_cost} away at #{current_point.to_a}"
cost = NMath.sqrt(((tuneable_data[true,1,true,true] - goal_vector) ** 2).sum(0))
# If this is the first run-through, keep the interval index the same
if initial_run
interval_index = 0
end
begin
ind_x, ind_y = sort_by_cost(cost, interval_index)
best_interval = tuneable_data[true,0,ind_x,ind_y]
path << change_inner_interval(current_point, ind_y, HD.r(*best_interval))
while banned_points.has_key? HD.narray_to_string path[-1]
interval_index += 1
if interval_index >= cost.size
break
end
ind_x, ind_y = sort_by_cost(cost, interval_index)
best_interval = tuneable_data[true,0,ind_x,ind_y]
path[-1] = change_inner_interval(current_point, ind_y, HD.r(*best_interval))
end
rescue IndexError => er
# puts "\nIndexError: #{er.message}"
# print er.backtrace.join("\n")
# puts
banned_points[HD.narray_to_string path[-1]] = path[-1]
path.pop
current_point = path[-1]
initial_run = true
current_cost = NMath.sqrt(((get_angle(path[-1], start_vector, hd_config)[0..1] - goal_vector) ** 2).sum)
next
end
# print "Trying interval #{HD.r(*best_interval)} at #{ind_y}"
if NMath.sqrt(((get_angle(path[-1], start_vector, hd_config)[0..1] - goal_vector) ** 2).sum) < current_cost
current_cost = NMath.sqrt(((get_angle(path[-1], start_vector, hd_config)[0..1] - goal_vector) ** 2).sum)
else
banned_points[HD.narray_to_string path[-1]] = path[-1]
path.pop
initial_run = false
# puts "banning #{banned_points[-1].to_a}"
next
end
if current_cost < epsilon
current_point = path[-1]
break
else
# Update the data table
tuneable_data[true,1,true,true] += get_angle(current_point, start_vector, hd_config)[0..1]
current_point = path[-1]
if current_cost < best_cost_so_far
best_point_so_far = current_point
best_cost_so_far = current_cost
end
initial_run = true
end
# puts "Now #{current_cost} away at #{current_point.to_a}"
rescue RangeError => er
# puts "\nRangeError -- skipping this one"
# print er.backtrace.join("\n")
banned_points[HD.narray_to_string path[-1]] = path[-1]
path.pop
initial_run = false
next
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment