GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — dwavebinarycsp/tests/test_constraint.py at master · dwavesystems/dwavebinarycsp

This repository was archived by the owner on Sep 24, 2025. It is now read-only.

Expand file tree

/

test_constraint.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
222 lines (143 loc) · 8.27 KB
/

test_constraint.py

Copy path

File metadata and controls

222 lines (143 loc) · 8.27 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# Copyright 2018 D-Wave Systems Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import operator
import itertools
import dwavebinarycsp
import dwavebinarycsp.testing as dcspt
class TestConstraint(unittest.TestCase):
def test__len__(self):
const = dwavebinarycsp.Constraint.from_func(operator.eq, ['a', 'b'], dwavebinarycsp.SPIN)
dcspt.assert_consistent_constraint(const)
self.assertEqual(len(const), 2)
def test_from_func(self):
const = dwavebinarycsp.Constraint.from_func(operator.eq, ['a', 'b'], dwavebinarycsp.SPIN)
dcspt.assert_consistent_constraint(const)
self.assertEqual(const.configurations, frozenset([(-1, -1), (1, 1)]))
self.assertEqual(('a', 'b'), const.variables)
def test_from_configurations(self):
const = dwavebinarycsp.Constraint.from_configurations([(-1, 1), (1, 1)], ['a', 'b'], dwavebinarycsp.SPIN)
dcspt.assert_consistent_constraint(const)
self.assertEqual(const.configurations, frozenset([(-1, 1), (1, 1)]))
self.assertEqual(const.variables, ('a', 'b'))
def test_fix_variable(self):
const = dwavebinarycsp.Constraint.from_configurations([(-1, 1), (1, 1)], ['a', 'b'], dwavebinarycsp.SPIN)
const.fix_variable('a', -1)
dcspt.assert_consistent_constraint(const)
self.assertEqual(const.configurations, frozenset([(1,)]))
self.assertEqual(const.variables, ('b',))
def test_fix_variable_unsat(self):
const = dwavebinarycsp.Constraint.from_configurations([(-1, 1), (1, 1)], ['a', 'b'], dwavebinarycsp.SPIN)
with self.assertRaises(dwavebinarycsp.exceptions.UnsatError):
const.fix_variable('b', -1)
def test__or__disjoint(self):
eq_a_b = dwavebinarycsp.Constraint.from_func(operator.eq, ['a', 'b'], dwavebinarycsp.SPIN)
ne_c_d = dwavebinarycsp.Constraint.from_func(operator.ne, ['c', 'd'], dwavebinarycsp.SPIN)
const = eq_a_b | ne_c_d
dcspt.assert_consistent_constraint(const)
self.assertEqual(const.variables, ('a', 'b', 'c', 'd'))
self.assertTrue(const.check({'a': 0, 'b': 0, 'c': 0, 'd': 0})) # only eq_a_b is satisfied
self.assertFalse(const.check({'a': 1, 'b': 0, 'c': 0, 'd': 0})) # neither satisified
def test__or__(self):
eq_a_b = dwavebinarycsp.Constraint.from_func(operator.eq, ['a', 'b'], dwavebinarycsp.SPIN)
ne_b_c = dwavebinarycsp.Constraint.from_func(operator.ne, ['c', 'b'], dwavebinarycsp.SPIN)
const = eq_a_b | ne_b_c
dcspt.assert_consistent_constraint(const)
self.assertEqual(const.variables, ('a', 'b', 'c'))
self.assertTrue(const.check({'a': 0, 'b': 0, 'c': 0})) # only eq_a_b is satisfied
self.assertFalse(const.check({'a': 1, 'b': 0, 'c': 0})) # neither satisfied
self.assertTrue(const.check({'a': 0, 'b': 1, 'c': 0})) # only ne_b_c is satisfied
def test__and__disjoint(self):
eq_a_b = dwavebinarycsp.Constraint.from_func(operator.eq, ['a', 'b'], dwavebinarycsp.SPIN)
ne_c_d = dwavebinarycsp.Constraint.from_func(operator.ne, ['c', 'd'], dwavebinarycsp.SPIN)
const = eq_a_b & ne_c_d
dcspt.assert_consistent_constraint(const)
self.assertEqual(const.configurations, frozenset([(-1, -1, -1, 1), (1, 1, -1, 1),
(-1, -1, 1, -1), (1, 1, 1, -1)]))
self.assertEqual(const.variables, ('a', 'b', 'c', 'd'))
def test__and__(self):
eq_a_b = dwavebinarycsp.Constraint.from_func(operator.eq, ['a', 'b'], dwavebinarycsp.SPIN)
ne_b_c = dwavebinarycsp.Constraint.from_func(operator.ne, ['c', 'b'], dwavebinarycsp.SPIN)
const = eq_a_b & ne_b_c
dcspt.assert_consistent_constraint(const)
self.assertEqual(const.configurations, frozenset([(-1, -1, 1), (1, 1, -1)]))
self.assertEqual(const.variables, ('a', 'b', 'c'))
def test_negate_variables_binary(self):
const = dwavebinarycsp.Constraint.from_configurations([(0, 1), (1, 0)], ['a', 'b'], vartype=dwavebinarycsp.BINARY)
const.flip_variable('a')
dcspt.assert_consistent_constraint(const)
self.assertEqual(const.configurations, frozenset([(1, 1), (0, 0)]))
#
const = dwavebinarycsp.Constraint.from_configurations([(0, 1), (1, 0)], ['a', 'b'], vartype=dwavebinarycsp.BINARY)
const.flip_variable('b')
dcspt.assert_consistent_constraint(const)
self.assertEqual(const.configurations, frozenset([(1, 1), (0, 0)]))
#
const = dwavebinarycsp.Constraint.from_configurations([(0, 1, 1), (1, 0, 0)],
['a', 'b', 'c'],
vartype=dwavebinarycsp.BINARY)
const.flip_variable('b')
dcspt.assert_consistent_constraint(const)
self.assertEqual(const.configurations, frozenset([(1, 1, 0), (0, 0, 1)]))
def test_negate_variables_spi(self):
const = dwavebinarycsp.Constraint.from_configurations([(-1, 1), (1, -1)], ['a', 'b'], vartype=dwavebinarycsp.SPIN)
const.flip_variable('a')
dcspt.assert_consistent_constraint(const)
self.assertEqual(const.configurations, frozenset([(1, 1), (-1, -1)]))
#
const = dwavebinarycsp.Constraint.from_configurations([(-1, 1), (1, -1)], ['a', 'b'], vartype=dwavebinarycsp.SPIN)
const.flip_variable('b')
dcspt.assert_consistent_constraint(const)
self.assertEqual(const.configurations, frozenset([(1, 1), (-1, -1)]))
#
const = dwavebinarycsp.Constraint.from_configurations([(-1, 1, 1), (1, -1, -1)],
['a', 'b', 'c'],
vartype=dwavebinarycsp.SPIN)
const.flip_variable('b')
dcspt.assert_consistent_constraint(const)
self.assertEqual(const.configurations, frozenset([(1, 1, -1), (-1, -1, 1)]))
def test_construction_typechecking(self):
# not function
with self.assertRaises(TypeError):
dwavebinarycsp.Constraint(1, {}, [], dwavebinarycsp.BINARY)
def f():
pass
# mismatched lengths
with self.assertRaises(ValueError):
dwavebinarycsp.Constraint(f, {tuple(), (1,)}, [], dwavebinarycsp.BINARY)
with self.assertRaises(ValueError):
dwavebinarycsp.Constraint(f, {(0,), (1,)}, ['a', 'b'], dwavebinarycsp.BINARY)
# bad vartype
with self.assertRaises(ValueError):
dwavebinarycsp.Constraint(f, {(-1,), (0,)}, ['a'], dwavebinarycsp.BINARY)
def test_copy(self):
const = dwavebinarycsp.Constraint.from_func(operator.eq, ['a', 'b'], dwavebinarycsp.SPIN)
new_const = const.copy()
self.assertEqual(const, new_const)
self.assertIsNot(const, new_const)
def test_projection_identity(self):
const = dwavebinarycsp.Constraint.from_func(operator.eq, ['a', 'b'], dwavebinarycsp.SPIN)
proj = const.projection(['a', 'b'])
self.assertEqual(const, proj)
self.assertIsNot(const, proj)
def test_projection_unknown_variables(self):
const = dwavebinarycsp.Constraint.from_func(operator.eq, ['a', 'b'], dwavebinarycsp.SPIN)
with self.assertRaises(ValueError):
proj = const.projection(['a', 'c'])
def test_projection_reducible(self):
const = dwavebinarycsp.Constraint.from_configurations([(0, 0), (0, 1)], ['a', 'b'], dwavebinarycsp.BINARY)
a = dwavebinarycsp.Constraint.from_configurations([(0,)], ['a'], dwavebinarycsp.BINARY)
b = dwavebinarycsp.Constraint.from_configurations([(0,), (1,)], ['b'], dwavebinarycsp.BINARY)
self.assertEqual(const.projection(['b']), b)
self.assertEqual(const.projection(['a']), a)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.