Skip to content

Commit e3b0255

Browse files
committed
Cleaning NumberNode axis-wise bounds
1 parent 15f7b67 commit e3b0255

3 files changed

Lines changed: 10 additions & 12 deletions

File tree

dwave/optimization/model.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ def binary(self, shape: None | _ShapeLike = None,
251251
See Also:
252252
:class:`~dwave.optimization.symbols.numbers.BinaryVariable`: equivalent symbol.
253253
254+
.. versionchanged:: 0.6.7
255+
Beginning in version 0.6.7, user-defined index-wise bounds are
256+
supported.
257+
254258
.. versionchanged:: 0.6.12
255259
Beginning in version 0.6.12, user-defined axis-wise bounds are
256260
supported.

dwave/optimization/symbols/numbers.pyx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import json
1818

19-
import collections.abc
2019
import numpy as np
2120

2221
from cython.operator cimport typeid
@@ -59,6 +58,8 @@ cdef vector[NumberNode.BoundAxisInfo] _convert_python_bound_axes(
5958
cdef double[:] mem
6059

6160
for bound_axis_data in bound_axes_data:
61+
# We allow lists and tuples because the _from_zipfile method yields
62+
# a list of lists not a list of tuples.
6263
if not isinstance(bound_axis_data, (tuple, list)) or len(bound_axis_data) != 3:
6364
raise TypeError("Each bound axis entry must be a tuple or list with"
6465
" three elements: axis, operator(s), bound(s)")
@@ -199,6 +200,7 @@ cdef class BinaryVariable(ArraySymbol):
199200
subject_to = None
200201
else:
201202
with zf.open(info, "r") as f:
203+
# Note that import is a list of lists, not a list of tuples
202204
subject_to = json.load(f)
203205

204206
return BinaryVariable(model,
@@ -231,6 +233,7 @@ cdef class BinaryVariable(ArraySymbol):
231233

232234
subject_to = self.axis_wise_bounds()
233235
if len(subject_to) > 0:
236+
# Using json here converts the tuples to lists
234237
zf.writestr(directory + "subject_to.json", encoder.encode(subject_to))
235238

236239
def axis_wise_bounds(self):
@@ -404,6 +407,7 @@ cdef class IntegerVariable(ArraySymbol):
404407
subject_to = None
405408
else:
406409
with zf.open(info, "r") as f:
410+
# Note that import is a list of lists, not a list of tuples
407411
subject_to = json.load(f)
408412

409413
return IntegerVariable(model,
@@ -442,6 +446,7 @@ cdef class IntegerVariable(ArraySymbol):
442446

443447
subject_to = self.axis_wise_bounds()
444448
if len(subject_to) > 0:
449+
# Using json here converts the tuples to lists
445450
zf.writestr(directory + "subject_to.json", encoder.encode(subject_to))
446451

447452
def axis_wise_bounds(self):

tests/test_symbols.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,17 +2069,6 @@ def test_set_state(self):
20692069
x.set_state(0, [-0.5, -0.75, -0.5, -1.0, -0.1])
20702070
np.testing.assert_array_equal(x.state(), [0, 0, 0, -1, 0])
20712071

2072-
# with self.subTest("Axis-wise bounds"):
2073-
# model = Model()
2074-
# model.states.resize(1)
2075-
# x = model.integer([2, 3], lower_bound=0, upper_bound=2,
2076-
# subject_to=[(0, "==", 0)])
2077-
# x.set_state(0, [0, 0, 1, 0, 1, 0])
2078-
# # with np.testing.assert_raises(ValueError):
2079-
# # x.set_state(0, 2)
2080-
# # with np.testing.assert_raises(ValueError):
2081-
# # x.set_state(0, -2)
2082-
20832072

20842073
class TestIsIn(utils.SymbolTests):
20852074
def generate_symbols(self):

0 commit comments

Comments
 (0)