Skip to content

Commit 8a96072

Browse files
authored
Fix AttributeError for NQueensProblem in search.ipynb visualization (#1309)
Guard the tree-search visualization helpers (tree_breadth_search_for_vis / tree_depth_search_for_vis) so they work for problems without a graph attribute (e.g. NQueensProblem), fix the tuple unpacking of the visual search results in the NQueens cells, and replace the removed np.float alias with float in plot_NQueens (notebook.py). Fixes #1308.
1 parent 814c3d9 commit 8a96072

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

‎notebook.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ def plot_NQueens(solution):
10611061
board = np.array([2 * int((i + j) % 2) for j in range(n) for i in range(n)]).reshape((n, n))
10621062
im = Image.open('images/queen_s.png')
10631063
height = im.size[1]
1064-
im = np.array(im).astype(np.float) / 255
1064+
im = np.array(im).astype(float) / 255
10651065
fig = plt.figure(figsize=(7, 7))
10661066
ax = fig.add_subplot(111)
10671067
ax.set_title('{} Queens'.format(n))

‎search.ipynb‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@
11201120
},
11211121
{
11221122
"cell_type": "code",
1123-
"execution_count": 14,
1123+
"execution_count": null,
11241124
"metadata": {},
11251125
"outputs": [],
11261126
"source": [
@@ -1132,7 +1132,11 @@
11321132
" # we use these two variables at the time of visualisations\n",
11331133
" iterations = 0\n",
11341134
" all_node_colors = []\n",
1135-
" node_colors = {k : 'white' for k in problem.graph.nodes()}\n",
1135+
" if hasattr(problem, 'graph'):\n",
1136+
" node_colors = {k : 'white' for k in problem.graph.nodes()}\n",
1137+
" else:\n",
1138+
" node_colors = {}\n",
1139+
"\n",
11361140
" \n",
11371141
" #Adding first node to the queue\n",
11381142
" frontier = deque([Node(problem.initial)])\n",
@@ -1208,7 +1212,7 @@
12081212
},
12091213
{
12101214
"cell_type": "code",
1211-
"execution_count": 16,
1215+
"execution_count": null,
12121216
"metadata": {},
12131217
"outputs": [],
12141218
"source": [
@@ -1220,7 +1224,10 @@
12201224
" # we use these two variables at the time of visualisations\n",
12211225
" iterations = 0\n",
12221226
" all_node_colors = []\n",
1223-
" node_colors = {k : 'white' for k in problem.graph.nodes()}\n",
1227+
" if hasattr(problem, 'graph'):\n",
1228+
" node_colors = {k : 'white' for k in problem.graph.nodes()}\n",
1229+
" else:\n",
1230+
" node_colors = {}\n",
12241231
" \n",
12251232
" #Adding first node to the stack\n",
12261233
" frontier = [Node(problem.initial)]\n",
@@ -5305,13 +5312,14 @@
53055312
},
53065313
{
53075314
"cell_type": "code",
5308-
"execution_count": 83,
5315+
"execution_count": null,
53095316
"metadata": {
53105317
"collapsed": true
53115318
},
53125319
"outputs": [],
53135320
"source": [
5314-
"dfts = depth_first_tree_search(nqp).solution()"
5321+
"_, _, goal_node = depth_first_tree_search(nqp) # Unpack the tuple\n",
5322+
"dfts = goal_node.solution()"
53155323
]
53165324
},
53175325
{
@@ -5361,13 +5369,14 @@
53615369
},
53625370
{
53635371
"cell_type": "code",
5364-
"execution_count": 86,
5372+
"execution_count": null,
53655373
"metadata": {
53665374
"collapsed": true
53675375
},
53685376
"outputs": [],
53695377
"source": [
5370-
"bfts = breadth_first_tree_search(nqp).solution()"
5378+
"_, _, goal_node = breadth_first_tree_search(nqp) # Unpack the tuple\n",
5379+
"bfts = goal_node.solution()"
53715380
]
53725381
},
53735382
{

0 commit comments

Comments
 (0)