Skip to content

Commit

Permalink
chore: Upload files from local computer
Browse files Browse the repository at this point in the history
  • Loading branch information
ntrang086 committed Oct 9, 2017
0 parents commit f80f02d
Show file tree
Hide file tree
Showing 8 changed files with 2,242 additions and 0 deletions.
565 changes: 565 additions & 0 deletions AIND-Constraint_Satisfaction.ipynb

Large diffs are not rendered by default.

718 changes: 718 additions & 0 deletions AIND-Constraint_Satisfaction_Print_Statements.ipynb

Large diffs are not rendered by default.

Binary file added EightQueens.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions IMAGE_LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
("Chess qdt45.svg")[https://commons.wikimedia.org/wiki/File:Chess_qdt45.svg] by [Colin M.L. Burnett](https://en.wikipedia.org/wiki/User:Cburnett) is licensed under [CC Attribution-Share Alike 3.0 Unported](https://creativecommons.org/licenses/by-sa/3.0/deed.en).
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
In this exercise you will explore Constraint Satisfaction Problems by implementing the N-Queens problem using symbolic constraints in a Jupyter notebook, and solving it using the Backtracking Search algorithm from AIMA.

To launch the notebook, run the following command from a terminal with anaconda3 installed and on the application path:

jupyter notebook AIND-Constraint_Satisfaction.ipynb
875 changes: 875 additions & 0 deletions Sympy_Intro.ipynb

Large diffs are not rendered by default.

Binary file added queen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
78 changes: 78 additions & 0 deletions util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

from sympy import *


def constraint(name, expr):
"""Augment the sympy Function class by attaching a symbolic expression
and making the function evaluatable by patching the .subs() method.
Parameters
----------
name : str
A string defining the name of the function
expr : sympy.Expr
A symbolic expression that can be evaluated for a particular
assignment to determine whether the constraint is True, False,
or undetermined
Returns
-------
sympy.Function or sympy.Expr
If the expression still has unassigned free variables, then a
Function is returned with an attached Expr object; otherwise
the expression is returned unchanged
"""
if not len(expr.free_symbols):
return expr
func = Function(name)(*expr.free_symbols)
setattr(func, "expr", expr)
setattr(func, "subs", lambda *a, **b: constraint(name, expr.subs(*a, **b)))
setattr(func, "_subs", lambda *a, **b: expr.subs(*a, **b))
return func


def displayBoard(locations, shape):
"""Draw a chessboard with queens placed at each position specified
by the assignment.
Parameters
----------
locations : list
The locations list should contain one element for each queen
of the chessboard containing a tuple (r, c) indicating the
row and column coordinates of a queen to draw on the board.
shape : integer
The number of cells in each dimension of the board (e.g.,
shape=3 indicates a 3x3 board)
Returns
-------
matplotlib.figure.Figure
The handle to the figure containing the board and queens
"""
r = c = shape
cmap = mpl.colors.ListedColormap(['#f5ecce', '#614532'])
img = mpl.image.imread('queen.png').astype(np.float)
boxprops = {"facecolor": "none", "edgecolor": "none"}

x, y = np.meshgrid(range(c), range(r))
plt.matshow(x % 2 ^ y % 2, cmap=cmap)
plt.axis("off") # eliminate borders from plot

fig = plt.gcf()
fig.set_size_inches([r, c])
scale = fig.get_dpi() / max(img.shape)
ax = plt.gca()
for y, x in set(locations):
box = mpl.offsetbox.OffsetImage(img, zoom=scale)
ab = mpl.offsetbox.AnnotationBbox(box, (y, x), bboxprops=boxprops)
ax.add_artist(ab)

plt.show()
return fig

0 comments on commit f80f02d

Please sign in to comment.