-
Notifications
You must be signed in to change notification settings - Fork 476
/
util.py
78 lines (63 loc) · 2.36 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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 = 0.75 * 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