Skip to content

Commit 2c9966a

Browse files
committed
variable naming in examples, tests
1 parent d91381c commit 2c9966a

11 files changed

+29
-19
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ given = np.array([
4040

4141

4242
# Variables
43-
puzzle = IntVar(1,9, shape=given.shape)
43+
puzzle = IntVar(1,9, shape=given.shape, name="puzzle")
4444

4545
constraints = []
4646
# Constraints on rows and columns
@@ -81,7 +81,7 @@ from cpmpy import *
8181
import numpy as np
8282

8383
# Construct the model
84-
s,e,n,d,m,o,r,y = IntVar(0,9, 8)
84+
s,e,n,d,m,o,r,y = IntVar(0,9, shape=8)
8585

8686
constraint = []
8787
constraint += [ alldifferent([s,e,n,d,m,o,r,y]) ]
@@ -109,7 +109,7 @@ demands = [8, 10, 7, 12, 4, 4]
109109
slots = len(demands)
110110

111111
# variables
112-
x = IntVar(0,sum(demands), slots)
112+
x = IntVar(0,sum(demands), shape=slots, name="x")
113113

114114
constraint = [x[i] + x[i+1] >= demands[i] for i in range(0,slots-1)]
115115
constraint += [x[-1] + x[0] == demands[-1]] # 'around the clock' constraint

examples/bibd.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
l = 1
2626

2727
# Variables, incidence matrix
28-
block = BoolVar(shape=(v,b))
28+
block = BoolVar(shape=(v,b), name="block")
2929

3030
# Constraints on incidence matrix
3131
m = Model([

examples/bus_schedule.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
slots = len(demands)
1515

1616
# variables
17-
x = IntVar(0,sum(demands), slots)
17+
x = IntVar(0,sum(demands), shape=slots, name="x")
1818

1919
constraint = [x[i] + x[i+1] >= demands[i] for i in range(0,slots-1)]
2020
constraint += [x[-1] + x[0] == demands[-1]] # 'around the clock' constraint

examples/knapsack.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
capacity = np.random.randint(sum(weights)*.2, sum(weights)*.5)
1616

1717
# Construct the model.
18-
x = BoolVar(n)
18+
x = BoolVar(shape=n, name="x")
1919

2020
constraint = [ sum(x*weights) <= capacity ]
2121
objective = sum(x*values)

examples/mario.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
arc_fuel = data['conso'] # arc_fuel[a,b] = fuel from a to b
2525

2626
# s[i] is the house succeeding to the ith house (s[i]=i if not part of the route)
27-
s = IntVar(0,nHouses-1, shape=nHouses)
27+
s = IntVar(0,nHouses-1, shape=nHouses, name="s")
2828

2929
cons = []
3030
# s should be a path, mimic (sub)circuit by connecting end-point back to start

examples/npuzzle.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ def pos(r,c):
5757
valid_table.append([pos(r,c), pos(r+rr,c+cc)])
5858

5959
# Variables
60-
x = IntVar(0,n-1, shape=(num_sols,n))
60+
x = IntVar(0,n-1, shape=(num_sols,n), name="x")
6161

6262
# 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))
63+
move_from = IntVar(0,n-1, shape=(num_sols), name="move_from") # is move_to[i-1]
64+
move_to = IntVar(0,n-1, shape=(num_sols), name="move_to")
6565

6666
# is this row the solution?
67-
check = BoolVar(shape=num_sols)
67+
check = BoolVar(shape=num_sols, name="check")
6868

6969
# index of first solution
70-
check_ix = IntVar(0,num_sols)
70+
check_ix = IntVar(0,num_sols, name="check_ix")
7171

7272

7373
m = Model(minimize=check_ix)

examples/nqueens.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
N = 8
1818

1919
# Variables (one per row)
20-
queens = IntVar(1,N, shape=N)
20+
queens = IntVar(1,N, shape=N, name="queens")
2121

2222
# Constraints on columns and left/right diagonal
2323
m = Model([
2424
alldifferent(queens),
25-
alldifferent([queens[i] - i for i in range(N)]),
2625
alldifferent([queens[i] + i for i in range(N)]),
26+
alldifferent([queens[i] - i for i in range(N)]),
2727
])
2828

2929
if m.solve():

examples/quickstart_sudoku.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"outputs": [],
7878
"source": [
7979
"# Variables\n",
80-
"puzzle = IntVar(1, 9, shape=given.shape)"
80+
"puzzle = IntVar(1, 9, shape=given.shape, name=\"puzzle\")"
8181
]
8282
},
8383
{

examples/sudoku.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
# Variables
26-
puzzle = IntVar(1,9, shape=given.shape)
26+
puzzle = IntVar(1,9, shape=given.shape, name="puzzle")
2727

2828
constraints = []
2929
# Constraints on rows and columns

examples/who_killed_agatha.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616

1717
# Who killed agatha?
1818
victim = agatha
19-
killer = IntVar(0,2)
19+
killer = IntVar(0,2, name="killer")
2020

2121
constraint = []
2222
# A killer always hates, and is no richer than his victim.
23-
hates = BoolVar((n,n))
23+
hates = BoolVar((n,n), name="hates")
2424
constraint += [ hates[killer, victim] == 1 ]
2525

26-
richer = BoolVar((n,n))
26+
richer = BoolVar((n,n), name="richer")
2727
constraint += [ richer[killer, victim] == 0 ]
2828

2929
# implied richness: no one richer than himself, and anti-reflexive

tests/test_variables.py

+10
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,13 @@ def test_array_IntVar(self):
3939
iv = cp.IntVar(0, i, shape=(i, j))
4040
self.assertEqual(iv.shape, (i,j), "Shape should be equal size")
4141
self.assertIsInstance(iv, cp.NDVarArray, f"Instance not {cp.NDVarArray} got {type(iv)}")
42+
43+
def test_namevar(self):
44+
a = cp.BoolVar(name="a")
45+
self.assertEqual(str(a), "a")
46+
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

Comments
 (0)