Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 651cf66

Browse files
ad71norvig
authored andcommitted
Changed plotting function for NQueensCSP (aimacode#847)
* Updated README.md * Added function to plot NQueensProblem * Added queen image * Changed plotting function for NQueensCSP * Replaced f'{}' with .format() notation * Added Pillow to travis.yml
1 parent 11cc2cc commit 651cf66

File tree

5 files changed

+45
-49
lines changed

5 files changed

+45
-49
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ install:
1414
- pip install matplotlib
1515
- pip install networkx
1616
- pip install ipywidgets
17+
- pip install Pillow
1718

1819
script:
1920
- py.test

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Here is a table of algorithms, the figure, name of the algorithm in the book and
9090
| 6 | CSP | `CSP` | [`csp.py`][csp] | Done | Included |
9191
| 6.3 | AC-3 | `AC3` | [`csp.py`][csp] | Done | |
9292
| 6.5 | Backtracking-Search | `backtracking_search` | [`csp.py`][csp] | Done | Included |
93-
| 6.8 | Min-Conflicts | `min_conflicts` | [`csp.py`][csp] | Done | |
93+
| 6.8 | Min-Conflicts | `min_conflicts` | [`csp.py`][csp] | Done | Included |
9494
| 6.11 | Tree-CSP-Solver | `tree_csp_solver` | [`csp.py`][csp] | Done | Included |
9595
| 7 | KB | `KB` | [`logic.py`][logic] | Done | Included |
9696
| 7.1 | KB-Agent | `KB_Agent` | [`logic.py`][logic] | Done | |

csp.ipynb

Lines changed: 14 additions & 47 deletions
Large diffs are not rendered by default.

images/queen_s.png

14.1 KB
Loading

notebook.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import matplotlib.pyplot as plt
1111
import numpy as np
12+
from PIL import Image
1213

1314
import os, struct
1415
import array
@@ -1041,4 +1042,31 @@ def visualize_callback(Visualize):
10411042

10421043
slider = widgets.IntSlider(min=0, max=1, step=1, value=0)
10431044
slider_visual = widgets.interactive(slider_callback, iteration=slider)
1044-
display(slider_visual)
1045+
display(slider_visual)
1046+
1047+
1048+
# Function to plot NQueensCSP in csp.py and NQueensProblem in search.py
1049+
def plot_NQueens(solution):
1050+
n = len(solution)
1051+
board = np.array([2 * int((i + j) % 2) for j in range(n) for i in range(n)]).reshape((n, n))
1052+
im = Image.open('images/queen_s.png')
1053+
height = im.size[1]
1054+
im = np.array(im).astype(np.float) / 255
1055+
fig = plt.figure(figsize=(7, 7))
1056+
ax = fig.add_subplot(111)
1057+
ax.set_title('{} Queens'.format(n))
1058+
plt.imshow(board, cmap='binary', interpolation='nearest')
1059+
# NQueensCSP gives a solution as a dictionary
1060+
if isinstance(solution, dict):
1061+
for (k, v) in solution.items():
1062+
newax = fig.add_axes([0.064 + (k * 0.112), 0.062 + ((7 - v) * 0.112), 0.1, 0.1], zorder=1)
1063+
newax.imshow(im)
1064+
newax.axis('off')
1065+
# NQueensProblem gives a solution as a list
1066+
elif isinstance(solution, list):
1067+
for (k, v) in enumerate(solution):
1068+
newax = fig.add_axes([0.064 + (k * 0.112), 0.062 + ((7 - v) * 0.112), 0.1, 0.1], zorder=1)
1069+
newax.imshow(im)
1070+
newax.axis('off')
1071+
fig.tight_layout()
1072+
plt.show()

0 commit comments

Comments
 (0)