Newer
Older
algo_dropdown = widgets.Dropdown(description="Search algorithm: ",
options=sorted(list(algorithm.keys())),
value="Breadth First Tree Search")
display(algo_dropdown)
elif algorithm is None:
print("No algorithm to run.")
return 0
def slider_callback(iteration):
# don't show graph for the first time running the cell calling this function
try:
show_map(graph_data, node_colors=all_node_colors[iteration])
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
except:
pass
def visualize_callback(Visualize):
if Visualize is True:
button.value = False
problem = GraphProblem(start_dropdown.value, end_dropdown.value, romania_map)
global all_node_colors
user_algorithm = algorithm[algo_dropdown.value]
iterations, all_node_colors, node = user_algorithm(problem)
solution = node.solution()
all_node_colors.append(final_path_colors(all_node_colors[0], problem, solution))
slider.max = len(all_node_colors) - 1
for i in range(slider.max + 1):
slider.value = i
#time.sleep(.5)
start_dropdown = widgets.Dropdown(description="Start city: ",
options=sorted(list(node_colors.keys())), value="Arad")
display(start_dropdown)
end_dropdown = widgets.Dropdown(description="Goal city: ",
options=sorted(list(node_colors.keys())), value="Fagaras")
display(end_dropdown)
button = widgets.ToggleButton(value=False)
button_visual = widgets.interactive(visualize_callback, Visualize=button)
display(button_visual)
slider = widgets.IntSlider(min=0, max=1, step=1, value=0)
slider_visual = widgets.interactive(slider_callback, iteration=slider)
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
display(slider_visual)
# Function to plot NQueensCSP in csp.py and NQueensProblem in search.py
def plot_NQueens(solution):
n = len(solution)
board = np.array([2 * int((i + j) % 2) for j in range(n) for i in range(n)]).reshape((n, n))
im = Image.open('images/queen_s.png')
height = im.size[1]
im = np.array(im).astype(np.float) / 255
fig = plt.figure(figsize=(7, 7))
ax = fig.add_subplot(111)
ax.set_title('{} Queens'.format(n))
plt.imshow(board, cmap='binary', interpolation='nearest')
# NQueensCSP gives a solution as a dictionary
if isinstance(solution, dict):
for (k, v) in solution.items():
newax = fig.add_axes([0.064 + (k * 0.112), 0.062 + ((7 - v) * 0.112), 0.1, 0.1], zorder=1)
newax.imshow(im)
newax.axis('off')
# NQueensProblem gives a solution as a list
elif isinstance(solution, list):
for (k, v) in enumerate(solution):
newax = fig.add_axes([0.064 + (k * 0.112), 0.062 + ((7 - v) * 0.112), 0.1, 0.1], zorder=1)
newax.imshow(im)
newax.axis('off')
fig.tight_layout()
plt.show()
# Function to plot a heatmap, given a grid
def heatmap(grid, cmap='binary', interpolation='nearest'):
fig = plt.figure(figsize=(7, 7))
ax = fig.add_subplot(111)
ax.set_title('Heatmap')
plt.imshow(grid, cmap=cmap, interpolation=interpolation)
fig.tight_layout()
plt.show()
# Generates a gaussian kernel
def gaussian_kernel(l=5, sig=1.0):
ax = np.arange(-l // 2 + 1., l // 2 + 1.)
xx, yy = np.meshgrid(ax, ax)
kernel = np.exp(-(xx**2 + yy**2) / (2. * sig**2))
return kernel
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
# Plots utility function for a POMDP
def plot_pomdp_utility(utility):
save = utility['0'][0]
delete = utility['1'][0]
ask_save = utility['2'][0]
ask_delete = utility['2'][-1]
left = (save[0] - ask_save[0]) / (save[0] - ask_save[0] + ask_save[1] - save[1])
right = (delete[0] - ask_delete[0]) / (delete[0] - ask_delete[0] + ask_delete[1] - delete[1])
colors = ['g', 'b', 'k']
for action in utility:
for value in utility[action]:
plt.plot(value, color=colors[int(action)])
plt.vlines([left, right], -20, 10, linestyles='dashed', colors='c')
plt.ylim(-20, 13)
plt.xlim(0, 1)
plt.text(left/2 - 0.05, 10, 'Save')
plt.text((right + left)/2 - 0.02, 10, 'Ask')
plt.text((right + 1)/2 - 0.07, 10, 'Delete')
plt.show()