The Moon Rover Problem¶
In [1]:
pylab inline
In [2]:
import matplotlib.pyplot as plt
import numpy as np
import itertools
from scipy.spatial.distance import pdist, squareform
from time import process_time
In [3]:
# Lookup Table for the original alphanumeric coordinates
site_name = ["D6","H1","E2","B3","G3","H4","G6","I7","A8","G8","D10"]
# Lookup Table for the Science Points
site_value = [0,2,4,3,5,1,4,1,3,2,5]
# Lookup Table for the XY coordinates
site_loc = np.array([[4,6],[8,1],[5,2],[2,3],[7,3],[8,4],[7,6],[9,7],[1,8],[7,8],[4,10]])
print(site_loc)
In [4]:
# Compute the Euclidean distance between all rows of loc.
site2site_distance = squareform(pdist(site_loc, 'euclidean'))
print(site2site_distance)
In [5]:
best_path_value = 0 # Start with zero Science Points
t=process_time()
# BRUTE FORCE SEARCH
# Consider every possible permutation of the 10 target sites
for path in itertools.permutations([1,2,3,4,5,6,7,8,9,10]):
current_site = 0
total_path_length = 0.0
path_value = 0
for next_site in path:
distance=site2site_distance[current_site][next_site]
if (total_path_length+distance>25.0):
break
else:
total_path_length+=distance
current_site=next_site
path_value+=site_value[current_site]
# print(path, total_path_length, current_site, path_value)
if (path_value>best_path_value):
best_path=path
best_path_value=path_value
last_best_site=current_site
elapsed_time=process_time()-t
print("Best Path: ",best_path, total_path_length, last_best_site, best_path_value)
print("Elapsed Time: ", elapsed_time)
In [6]:
x = []; y = []
x.append(site_loc[0][0])
y.append(site_loc[0][1])
# Display details about the optimal solution
print("Visit these sites: ",end=" ")
for i in best_path:
x.append(site_loc[i][0])
y.append(site_loc[i][1])
if i==last_best_site:
print(site_name[i])
break
print(site_name[i],end=", ")
print("Total Path Length: ", round(total_path_length,1))
print("Total Science Points: ", best_path_value)
In [7]:
# A simple plot of the path taken by the rover
plt.xlim(0, 10.5)
plt.ylim(0, 10.5)
plt.plot(x, y, 'co')
for i in range(0,len(x)-1):
plt.arrow(x[i], y[i], (x[i+1] - x[i]), (y[i+1] - y[i]), head_width = .3,
color = 'r', length_includes_head = True)