We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d91381c commit 2c9966aCopy full SHA for 2c9966a
README.md
@@ -40,7 +40,7 @@ given = np.array([
40
41
42
# Variables
43
-puzzle = IntVar(1,9, shape=given.shape)
+puzzle = IntVar(1,9, shape=given.shape, name="puzzle")
44
45
constraints = []
46
# Constraints on rows and columns
@@ -81,7 +81,7 @@ from cpmpy import *
81
import numpy as np
82
83
# Construct the model
84
-s,e,n,d,m,o,r,y = IntVar(0,9, 8)
+s,e,n,d,m,o,r,y = IntVar(0,9, shape=8)
85
86
constraint = []
87
constraint += [ alldifferent([s,e,n,d,m,o,r,y]) ]
@@ -109,7 +109,7 @@ demands = [8, 10, 7, 12, 4, 4]
109
slots = len(demands)
110
111
# variables
112
-x = IntVar(0,sum(demands), slots)
+x = IntVar(0,sum(demands), shape=slots, name="x")
113
114
constraint = [x[i] + x[i+1] >= demands[i] for i in range(0,slots-1)]
115
constraint += [x[-1] + x[0] == demands[-1]] # 'around the clock' constraint
examples/bibd.py
@@ -25,7 +25,7 @@
25
l = 1
26
27
# Variables, incidence matrix
28
-block = BoolVar(shape=(v,b))
+block = BoolVar(shape=(v,b), name="block")
29
30
# Constraints on incidence matrix
31
m = Model([
examples/bus_schedule.py
@@ -14,7 +14,7 @@
14
15
16
17
18
19
20
examples/knapsack.py
@@ -15,7 +15,7 @@
capacity = np.random.randint(sum(weights)*.2, sum(weights)*.5)
# Construct the model.
-x = BoolVar(n)
+x = BoolVar(shape=n, name="x")
constraint = [ sum(x*weights) <= capacity ]
21
objective = sum(x*values)
examples/mario.py
@@ -24,7 +24,7 @@
24
arc_fuel = data['conso'] # arc_fuel[a,b] = fuel from a to b
# s[i] is the house succeeding to the ith house (s[i]=i if not part of the route)
-s = IntVar(0,nHouses-1, shape=nHouses)
+s = IntVar(0,nHouses-1, shape=nHouses, name="s")
cons = []
# s should be a path, mimic (sub)circuit by connecting end-point back to start
examples/npuzzle.py
@@ -57,17 +57,17 @@ def pos(r,c):
57
valid_table.append([pos(r,c), pos(r+rr,c+cc)])
58
59
60
-x = IntVar(0,n-1, shape=(num_sols,n))
+x = IntVar(0,n-1, shape=(num_sols,n), name="x")
61
62
# the moves, index in puzzle
63
-move_from = IntVar(0,n-1, shape=(num_sols)) # is move_to[i-1]...
64
-move_to = IntVar(0,n-1, shape=(num_sols))
+move_from = IntVar(0,n-1, shape=(num_sols), name="move_from") # is move_to[i-1]
+move_to = IntVar(0,n-1, shape=(num_sols), name="move_to")
65
66
# is this row the solution?
67
-check = BoolVar(shape=num_sols)
+check = BoolVar(shape=num_sols, name="check")
68
69
# index of first solution
70
-check_ix = IntVar(0,num_sols)
+check_ix = IntVar(0,num_sols, name="check_ix")
71
72
73
m = Model(minimize=check_ix)
examples/nqueens.py
@@ -17,13 +17,13 @@
N = 8
# Variables (one per row)
-queens = IntVar(1,N, shape=N)
+queens = IntVar(1,N, shape=N, name="queens")
22
# Constraints on columns and left/right diagonal
23
alldifferent(queens),
- alldifferent([queens[i] - i for i in range(N)]),
alldifferent([queens[i] + i for i in range(N)]),
+ alldifferent([queens[i] - i for i in range(N)]),
])
if m.solve():
examples/quickstart_sudoku.ipynb
@@ -77,7 +77,7 @@
77
"outputs": [],
78
"source": [
79
"# Variables\n",
80
- "puzzle = IntVar(1, 9, shape=given.shape)"
+ "puzzle = IntVar(1, 9, shape=given.shape, name=\"puzzle\")"
]
},
{
examples/sudoku.py
@@ -23,7 +23,7 @@
examples/who_killed_agatha.py
@@ -16,14 +16,14 @@
# Who killed agatha?
victim = agatha
-killer = IntVar(0,2)
+killer = IntVar(0,2, name="killer")
# A killer always hates, and is no richer than his victim.
-hates = BoolVar((n,n))
+hates = BoolVar((n,n), name="hates")
constraint += [ hates[killer, victim] == 1 ]
-richer = BoolVar((n,n))
+richer = BoolVar((n,n), name="richer")
constraint += [ richer[killer, victim] == 0 ]
# implied richness: no one richer than himself, and anti-reflexive
tests/test_variables.py
@@ -39,3 +39,13 @@ def test_array_IntVar(self):
39
iv = cp.IntVar(0, i, shape=(i, j))
self.assertEqual(iv.shape, (i,j), "Shape should be equal size")
self.assertIsInstance(iv, cp.NDVarArray, f"Instance not {cp.NDVarArray} got {type(iv)}")
+
+ def test_namevar(self):
+ a = cp.BoolVar(name="a")
+ self.assertEqual(str(a), "a")
47
+ b = cp.BoolVar(shape=(3,), name="b")
48
+ self.assertEqual(str(b), "[b[0] b[1] b[2]]")
49
50
+ c = cp.BoolVar(shape=(2,3), name="c")
51
+ self.assertEqual(str(c), "[[c[0,0] c[0,1] c[0,2]]\n [c[1,0] c[1,1] c[1,2]]]")
0 commit comments