/
constrained.py
1489 lines (1157 loc) · 57.3 KB
/
constrained.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2021 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.
"""
Constrained Quadratic Model class.
"""
import collections.abc as abc
import json
import re
import tempfile
import uuid
import warnings
import zipfile
from numbers import Number
from typing import Hashable, Optional, Union, BinaryIO, ByteString, Iterable, Collection, Dict
from typing import Callable, MutableMapping, Iterator, Tuple, Mapping, Any, NamedTuple
import numpy as np
from dimod.core.bqm import BQM as BQMabc
from dimod.binary.binary_quadratic_model import BinaryQuadraticModel, Binary, Spin, as_bqm
from dimod.discrete.discrete_quadratic_model import DiscreteQuadraticModel
from dimod.quadratic import QuadraticModel
from dimod.sampleset import as_samples
from dimod.sym import Comparison, Eq, Le, Ge, Sense
from dimod.serialization.fileview import SpooledTemporaryFile, _BytesIO
from dimod.serialization.fileview import load, read_header, write_header
from dimod.typing import Bias, Variable
from dimod.utilities import new_label
from dimod.variables import Variables, serialize_variable, deserialize_variable
from dimod.vartypes import Vartype, as_vartype, VartypeLike
from dimod.serialization.lp import make_lp_grammar, get_variables_from_parsed_lp, constraint_symbols, obj_senses
__all__ = ['ConstrainedQuadraticModel', 'CQM', 'cqm_to_bqm']
CQM_MAGIC_PREFIX = b'DIMODCQM'
class ConstraintData(NamedTuple):
label: Hashable
lhs_energy: float
rhs_energy: float
sense: Sense
activity: float
violation: float
class ConstrainedQuadraticModel:
r"""A constrained quadratic model.
Constrained quadratic models are problems of the form:
.. math::
\begin{align}
\text{Minimize an objective:} & \\
& \sum_{i} a_i x_i + \sum_{i<j} b_{ij} x_i x_j + c, \\
\text{Subject to constraints:} & \\
& \sum_i a_i^{(c)} x_i + \sum_{i<j} b_{ij}^{(c)} x_i x_j+ c^{(c)} \le 0,
\quad c=1, \dots, C_{\rm ineq.}, \\
& \sum_i a_i^{(d)} x_i + \sum_{i<j} b_{ij}^{(d)} x_i x_j + c^{(d)} = 0,
\quad d=1, \dots, C_{\rm eq.},
\end{align}
where :math:`\{ x_i\}_{i=1, \dots, N}` can be binary or integer
variables, :math:`a_{i}, b_{ij}, c` are real values and
:math:`C_{\rm ineq.}, C_{\rm eq,}` are the number of inequality and
equality constraints respectively.
The objective and constraints are encoded as either :class:`.QuadraticModel`
or :class:`.BinaryQuadraticModel` depending on the variable types used.
Example:
Solve a simple `bin packing problem <https://w.wiki/3jz4>`_. In this
problem we wish to pack a set of items of different weights into
the smallest number of bins possible.
See :func:`~dimod.generators.bin_packing` for a general function to
generate bin packing problems. We follow the same naming conventions
in this example.
Let's start with four object weights and assume that each bin has a
capacity of 1.
>>> weights = [.9, .7, .2, .1]
>>> capacity = 1
Let :math:`y_j` indicate that we used bin :math:`j`. We know that we
will use four or fewer total bins.
>>> y = [dimod.Binary(f'y_{j}') for j in range(len(weights))]
Let :math:`x_{i,j}` indicate that we put item :math:`i` in bin
:math:`j`.
>>> x = [[dimod.Binary(f'x_{i}_{j}') for j in range(len(weights))]
... for i in range(len(weights))]
Create an empty constrained quadratic model with no objective or
constraints.
>>> cqm = dimod.ConstrainedQuadraticModel()
We wish to minimize the number of bins used. Therefore our objective
is to minimize the value of :math:`\sum_j y_j`.
>>> cqm.set_objective(sum(y))
We also need to enforce the constraint that each item can only go
in one bin. We can express this constraint, for a given item :math:`i`,
with :math:`\sum_j x_{i, j} == 1`. Note that the label of each
constraint is returned so that we can access them in the future if
desired.
>>> for i in range(len(weights)):
... cqm.add_constraint(sum(x[i]) == 1, label=f'item_placing_{i}')
'item_placing_0'
'item_placing_1'
'item_placing_2'
'item_placing_3'
Finally, we need to enforce the limits on each bin. We can express
this constraint, for a given bin :math:`j`, with
:math:`\sum_i x_{i, j} * w_i <= c` where :math:`w_i` is the weight
of item :math:`i` and :math:`c` is the capacity.
>>> for j in range(len(weights)):
... cqm.add_constraint(
... sum(weights[i] * x[i][j] for i in range(len(weights))) - y[j] * capacity <= 0,
... label=f'capacity_bin_{j}')
'capacity_bin_0'
'capacity_bin_1'
'capacity_bin_2'
'capacity_bin_3'
"""
def __init__(self):
# discrete variable tracking, we probably can do this with less memory
# but for now let's keep it simple
self.discrete: Set[Hashable] = set() # collection of discrete constraints
self._discrete: Set[Variable] = set() # collection of all variables used in discrete
self._objective = QuadraticModel()
@property
def constraints(self) -> Dict[Hashable, Comparison]:
"""The constraints as a dictionary.
This dictionary and its contents should not be modified.
"""
try:
return self._constraints
except AttributeError:
pass
self._constraints: Dict[Hashable, Comparison] = {}
return self._constraints
@property
def objective(self) -> QuadraticModel:
"""The objective to be minimized."""
return self._objective
@property
def variables(self) -> Variables:
"""The variables in use over the objective and all constraints."""
try:
return self._variables
except AttributeError:
pass
self._variables = variables = self.objective.variables
# to support backwards compatibility (0.10.0 - 0.10.5), we annotate
# this object with some attributes. All of these will be removed in
# 0.11.0
def vartype(v):
warnings.warn(
"cqm.variables.vartype(v) is deprecated and will be removed in dimod 0.11.0, "
"use cqm.vartype(v) instead.", DeprecationWarning, stacklevel=2)
return self.vartype(v)
variables.vartype = vartype # method
variables.vartypes = _Vartypes(self)
variables.lower_bounds = _LowerBounds(self)
variables.upper_bounds = _UpperBounds(self)
return variables
def _add_variables_from(self, model: Union[BinaryQuadraticModel, QuadraticModel]):
# todo: singledispatchmethod in 3.8+
if isinstance(model, (BinaryQuadraticModel, BQMabc)):
vartype = model.vartype
for v in model.variables:
self.objective.add_variable(vartype, v)
elif isinstance(model, QuadraticModel):
for v in model.variables:
# for spin, binary variables the bounds are ignored anyway
self.objective.add_variable(model.vartype(v), v,
lower_bound=model.lower_bound(v),
upper_bound=model.upper_bound(v))
else:
raise TypeError("model should be a QuadraticModel or a BinaryQuadraticModel")
def add_constraint(self, data, *args, **kwargs) -> Hashable:
"""A convenience wrapper for other methods that add constraints.
Examples:
>>> from dimod import ConstrainedQuadraticModel, Integers
>>> i, j = Integers(['i', 'j'])
>>> cqm = ConstrainedQuadraticModel()
>>> cqm.add_constraint(i + j <= 3, label='Constrained i-j range')
'Constrained i-j range'
See also:
:meth:`~.ConstrainedQuadraticModel.add_constraint_from_model`
:meth:`~.ConstrainedQuadraticModel.add_constraint_from_comparison`
:meth:`~.ConstrainedQuadraticModel.add_constraint_from_iterable`
"""
# in python 3.8+ we can use singledispatchmethod
if isinstance(data, (BinaryQuadraticModel, QuadraticModel, BQMabc)):
return self.add_constraint_from_model(data, *args, **kwargs)
elif isinstance(data, Comparison):
return self.add_constraint_from_comparison(data, *args, **kwargs)
elif isinstance(data, Iterable):
return self.add_constraint_from_iterable(data, *args, **kwargs)
else:
raise TypeError("unexpected data format")
def add_constraint_from_model(self,
qm: Union[BinaryQuadraticModel, QuadraticModel],
sense: Union[Sense, str],
rhs: Bias = 0,
label: Optional[Hashable] = None,
copy: bool = True) -> Hashable:
"""Add a constraint from a quadratic model.
Args:
qm: A quadratic model or binary quadratic model.
sense: One of `<=', '>=', '=='.
rhs: The right hand side of the constraint.
label: A label for the constraint. Must be unique. If no label
is provided, then one is generated using :mod:`uuid`.
copy: If `True`, the BQM is copied. This can be set to `False` to
improve performance, but subsequently mutating the bqm can
cause issues.
Returns:
The label of the added constraint.
Examples:
>>> from dimod import ConstrainedQuadraticModel, Binary
>>> cqm = ConstrainedQuadraticModel()
>>> x = Binary('x')
>>> cqm.add_constraint_from_model(x, '>=', 0, 'Min x')
'Min x'
"""
variables = self.variables
# get sense as an enum
if isinstance(sense, str):
sense = Sense(sense)
if label is None:
# we support up to 100k constraints and :6 gives us 16777216
# possible so pretty safe
label = uuid.uuid4().hex[:6]
while label in self.constraints:
label = uuid.uuid4().hex[:6]
elif label in self.constraints:
raise ValueError("a constraint with that label already exists")
if isinstance(qm, BQMabc):
qm = as_bqm(qm) # handle legacy BQMs
self._add_variables_from(qm)
if copy:
qm = qm.copy()
if sense is Sense.Le:
self.constraints[label] = Le(qm, rhs)
elif sense is Sense.Ge:
self.constraints[label] = Ge(qm, rhs)
elif sense is Sense.Eq:
self.constraints[label] = Eq(qm, rhs)
else:
raise RuntimeError("unexpected sense")
return label
def add_constraint_from_comparison(self,
comp: Comparison,
label: Optional[Hashable] = None,
copy: bool = True) -> Hashable:
"""Add a constraint from a comparison.
Args:
comp: A comparison object.
label: A label for the constraint. Must be unique. If no label
is provided, one is generated using :mod:`uuid`.
copy: If `True`, the model is copied. You can set to `False` to
improve performance, but subsequently mutating the model can
cause issues.
Returns:
Label of the added constraint.
Examples:
>>> from dimod import ConstrainedQuadraticModel, Integer
>>> i = Integer('i')
>>> cqm = ConstrainedQuadraticModel()
>>> cqm.add_constraint_from_comparison(i <= 3, label='Max i')
'Max i'
"""
if not isinstance(comp.rhs, Number):
raise TypeError("comparison should have a numeric rhs")
if isinstance(comp.lhs, (BinaryQuadraticModel, QuadraticModel)):
return self.add_constraint_from_model(comp.lhs, comp.sense, rhs=comp.rhs,
label=label, copy=copy)
else:
raise ValueError("comparison should have a binary quadratic model "
"or quadratic model lhs.")
def add_constraint_from_iterable(self, iterable: Iterable,
sense: Union[Sense, str],
rhs: Bias = 0,
label: Optional[Hashable] = None,
) -> Hashable:
"""Add a constraint from an iterable of tuples.
Args:
iterable: An iterable of terms as tuples. The variables must
have already been added to the object.
sense: One of `<=', '>=', '=='.
rhs: The right hand side of the constraint.
label: A label for the constraint. Must be unique. If no label
is provided, then one is generated using :mod:`uuid`.
Returns:
The label of the added constraint.
Examples:
>>> from dimod import ConstrainedQuadraticModel, Integer, Binary
>>> cqm = ConstrainedQuadraticModel()
>>> cqm.add_variable('i', 'INTEGER') # doctest: +IGNORE_RESULT
>>> cqm.add_variable('j', 'INTEGER') # doctest: +IGNORE_RESULT
>>> cqm.add_variable('x', 'BINARY') # doctest: +IGNORE_RESULT
>>> cqm.add_variable('y', 'BINARY') # doctest: +IGNORE_RESULT
>>> label1 = cqm.add_constraint_from_iterable([('x', 'y', 1), ('i', 2), ('j', 3),
... ('i', 'j', 1)], '<=', rhs=1)
"""
qm = self._iterable_to_qm(iterable)
# use quadratic model in the future
return self.add_constraint_from_model(
qm, sense, rhs=rhs, label=label, copy=False)
def add_discrete(self, variables: Collection[Variable],
label: Optional[Hashable] = None) -> Hashable:
"""Add an iterable of binary variables as a disjoint one-hot constraint.
Adds a special kind of one-hot constraint. These one-hot constraints
must be disjoint, that is they must not have any overlapping variables.
Args:
variables: An iterable of variables.
label: Label for the constraint. Must be unique. If no label
is provided, then one is generated using :mod:`uuid`.
Returns:
Label of the added constraint.
Raises:
ValueError: If any of the given variables have already been added
to the model with any vartype other than `BINARY`.
ValueError: If any of the given variables are already used in
another discrete variable.
"""
if label is not None and label in self.constraints:
raise ValueError("a constraint with that label already exists")
for v in variables:
if v in self._discrete:
# todo: language around discrete variables?
raise ValueError(f"variable {v!r} is already used in a discrete variable")
elif v in self.variables and self.vartype(v) != Vartype.BINARY:
raise ValueError(f"variable {v!r} has already been added but is not BINARY")
# we can! So add them
bqm = BinaryQuadraticModel('BINARY', dtype=np.float32)
bqm.add_variables_from((v, 1) for v in variables)
label = self.add_constraint(bqm == 1, label=label)
self.discrete.add(label)
self._discrete.update(variables)
return label
def add_variable(self, v: Variable, vartype: VartypeLike,
*, lower_bound: int = 0, upper_bound: Optional[int] = None):
"""Add a variable to the model.
Args:
variable: A variable label.
vartype:
Variable type. One of:
* :class:`.Vartype.SPIN`, ``'SPIN'``, ``{-1, 1}``
* :class:`.Vartype.BINARY`, ``'BINARY'``, ``{0, 1}``
* :class:`.Vartype.INTEGER`, ``'INTEGER'``
lower_bound:
A lower bound on the variable. Ignored when the variable is
not :class:`Vartype.INTEGER`.
upper_bound:
An upper bound on the variable. Ignored when the variable is
not :class:`Vartype.INTEGER`.
Examples:
>>> from dimod import ConstrainedQuadraticModel, Integer
>>> cqm = ConstrainedQuadraticModel()
>>> cqm.add_variable('i', 'INTEGER') # doctest: +IGNORE_RESULT
"""
if self.variables.count(v):
if as_vartype(vartype, extended=True) != self.vartype(v):
raise ValueError("given variable has already been added with a different vartype")
else:
return self.objective.add_variable(vartype, v, lower_bound=lower_bound, upper_bound=upper_bound)
def check_feasible(self, sample_like, rtol: float = 1e-6, atol: float = 1e-8) -> bool:
r"""Return the feasibility of the given sample.
A sample is feasible if all constraints are satisfied. A constraint's
satisfaction is tested using the following equation:
.. math::
violation <= (atol + rtol * | rhs\_energy | )
where ``violation`` and ``rhs_energy`` are as returned by :meth:`.iter_constraint_data`.
Args:
sample_like: A sample.
rtol: The relative tolerance.
atol: the absolute tolerance.
Returns:
True if the sample is feasible (given the tolerances).
"""
return all(datum.violation <= atol + rtol*abs(datum.rhs_energy)
for datum in self.iter_constraint_data(sample_like))
@classmethod
def from_bqm(cls, bqm: BinaryQuadraticModel) -> 'ConstrainedQuadraticModel':
"""Alias for :meth:`from_quadratic_model`."""
return cls.from_quadratic_model(bqm)
@classmethod
def from_discrete_quadratic_model(cls, dqm: DiscreteQuadraticModel, *,
relabel_func: Callable[[Variable, int], Variable] = lambda v, c: (v, c),
) -> 'ConstrainedQuadraticModel':
"""Construct a constrained quadratic model from a discrete quadratic model.
Args:
dqm: a discrete quadratic model.
relabel_func (optional): A function that takes two arguments, the
variable label and the case label, and returns a new variable
label to be used in the CQM. By default generates a 2-tuple
`(variable, case)`.
Returns:
A constrained quadratic model.
"""
cqm = cls()
objective = BinaryQuadraticModel(Vartype.BINARY)
seen = set()
for v in dqm.variables:
seen.add(v)
# convert v, case to a flat set of variables
v_vars = list(relabel_func(v, case) for case in dqm.get_cases(v))
# add the one-hot constraint
cqm.add_discrete(v_vars, label=v)
# add to the objective
objective.add_linear_from(zip(v_vars, dqm.get_linear(v)))
for u in dqm.adj[v]:
if u in seen: # only want upper-triangle
continue
u_vars = list(relabel_func(u, case) for case in dqm.get_cases(u))
objective.add_quadratic_from(
(u_vars[cu], v_vars[cv], bias)
for (cu, cv), bias
in dqm.get_quadratic(u, v).items()
)
objective.offset = dqm.offset
cqm.set_objective(objective)
return cqm
from_dqm = from_discrete_quadratic_model
@classmethod
def from_quadratic_model(cls, qm: Union[QuadraticModel, BinaryQuadraticModel]
) -> 'ConstrainedQuadraticModel':
"""Construct a constrained quadratic model from a quadratic model or
binary quadratic model.
The specified model is set as the objective to be minimzed in the constructed
constrained quadratic model (CQM). You can then add constraints that any feasible
solutions should meet.
Args:
qm: Binary quadratic model (BQM) or quadratic model (QM).
Examples:
This example creates a CQM to minimize a triangular problem with the added
constraint that one of the variables must have value 1 in feasible solutions.
>>> from dimod import ConstrainedQuadraticModel, BinaryQuadraticModel
>>> bqm = BinaryQuadraticModel.from_ising({}, {'ab': 1, 'bc': 1, 'ac': 1})
>>> cqm = ConstrainedQuadraticModel().from_bqm(bqm)
>>> cqm.objective.linear
{'a': 0.0, 'b': 0.0, 'c': 0.0}
>>> cqm.objective.quadratic
{('b', 'a'): 1.0, ('c', 'a'): 1.0, ('c', 'b'): 1.0}
>>> label1 = cqm.add_constraint_from_model(BinaryQuadraticModel({'a': 0}, {}, 0, 'SPIN'), '>=', 0)
"""
cqm = cls()
cqm.set_objective(qm)
return cqm
@classmethod
def from_qm(cls, qm: QuadraticModel) -> 'ConstrainedQuadraticModel':
"""Alias for :meth:`from_quadratic_model`."""
return cls.from_quadratic_model(qm)
@classmethod
def from_file(cls, fp: Union[BinaryIO, ByteString]) -> "ConstrainedQuadraticModel":
"""Construct from a file-like object.
The inverse of :meth:`~ConstrainedQuadraticModel.to_file`.
"""
if isinstance(fp, ByteString):
file_like: BinaryIO = _BytesIO(fp) # type: ignore[assignment]
else:
file_like = fp
header_info = read_header(file_like, CQM_MAGIC_PREFIX)
if header_info.version >= (2, 0):
raise ValueError("cannot load a BQM serialized with version "
f"{header_info.version!r}, try upgrading your "
"dimod version")
# we don't actually need the data
cqm = CQM()
with zipfile.ZipFile(file_like, mode='r') as zf:
cqm.set_objective(load(zf.read("objective")))
constraint_labels = set()
for arch in zf.namelist():
# even on windows zip uses /
match = re.match("constraints/([^/]+)/", arch)
if match is not None:
constraint_labels.add(match.group(1))
for constraint in constraint_labels:
lhs = load(zf.read(f"constraints/{constraint}/lhs"))
rhs = np.frombuffer(zf.read(f"constraints/{constraint}/rhs"), np.float64)[0]
sense = zf.read(f"constraints/{constraint}/sense").decode('ascii')
discrete = any(zf.read(f"constraints/{constraint}/discrete"))
label = deserialize_variable(json.loads(constraint))
cqm.add_constraint(lhs, rhs=rhs, sense=sense, label=label)
if discrete:
cqm.discrete.add(label)
return cqm
def iter_constraint_data(self, sample_like) -> Iterator[ConstraintData]:
"""Yield information about the constraints for the given sample.
Args:
sample_like: A sample.
Yields:
A :class:`collections.namedtuple` with ``label``, ``lhs_energy``,
``rhs_energy``, ``sense``, ``activity``, and ``violation`` fields.
``label`` is the constraint label.
``lhs_energy`` is the energy of the left hand side of the constraint.
``rhs_energy`` is the energy of the right hand side of the constraint.
``sense`` is the :class:`dimod.sym.Sense` of the constraint.
``activity`` is ``lhs_energy - rhs_energy``
``violation`` is determined by the type of constraint. If ``violation``
is positive, that means that the constraint has been violated by
that amount. If it is negative, that means that the constraint has
been satisfied by the amount.
"""
sample, labels = as_samples(sample_like)
if sample.shape[0] != 1:
raise ValueError("sample_like should be a single sample, "
f"received {sample.shape[0]} samples")
for label, constraint in self.constraints.items():
lhs = constraint.lhs.energy((sample, labels))
rhs = constraint.rhs
sense = constraint.sense
activity = lhs - rhs
if sense is Sense.Eq:
violation = abs(activity)
elif sense is Sense.Ge:
violation = -activity
elif sense is Sense.Le:
violation = activity
else:
raise RuntimeError("unexpected sense")
yield ConstraintData(
activity=activity,
sense=sense,
violation=violation,
lhs_energy=lhs,
rhs_energy=rhs,
label=label,
)
def iter_violations(self, sample_like, *, skip_satisfied: bool = False, clip: bool = False,
) -> Iterator[Tuple[Hashable, Bias]]:
"""Yield violations for all constraints.
Args:
sample_like: A sample over the CQM variables.
skip_satisfied: If True, does not yield constraints that are satisfied.
clip: If True, negative violations are rounded up to 0.
Yields:
A 2-tuple containing the constraint label and the amount of
constraints violation.
Example:
Construct a constrained quadratic model.
>>> i, j, k = dimod.Binaries(['i', 'j', 'k'])
>>> cqm = dimod.ConstrainedQuadraticModel()
>>> cqm.add_constraint(i + j + k == 10, label='equal')
'equal'
>>> cqm.add_constraint(i + j <= 15, label='less equal')
'less equal'
>>> cqm.add_constraint(j - k >= 0, label='greater equal')
'greater equal'
Check the violations of a sample that satisfies all constraints.
>>> sample = {'i': 3, 'j': 5, 'k': 2}
>>> for label, violation in cqm.iter_violations(sample, clip=True):
... print(label, violation)
equal 0.0
less equal 0.0
greater equal 0.0
Check the violations for a sample that does not satisfy all of the
constraints.
>>> sample = {'i': 3, 'j': 2, 'k': 5}
>>> for label, violation in cqm.iter_violations(sample, clip=True):
... print(label, violation)
equal 0.0
less equal 0.0
greater equal 3.0
>>> sample = {'i': 3, 'j': 2, 'k': 5}
>>> for label, violation in cqm.iter_violations(sample, skip_satisfied=True):
... print(label, violation)
greater equal 3.0
"""
if skip_satisfied:
# clip doesn't matter in this case
# todo: feasibility tolerance?
for datum in self.iter_constraint_data(sample_like):
if datum.violation > 0:
yield datum.label, datum.violation
elif clip:
for datum in self.iter_constraint_data(sample_like):
yield datum.label, max(datum.violation, 0.0)
else:
for datum in self.iter_constraint_data(sample_like):
yield datum.label, datum.violation
def is_almost_equal(self, other: 'ConstrainedQuadraticModel',
places: int = 7) -> bool:
"""Return True if the given model's objective and constraints are almost equal."""
def constraint_eq(c0: Comparison, c1: Comparison) -> bool:
return (c0.sense is c1.sense
and c0.lhs.is_almost_equal(c1.lhs, places=places)
and not round(c0.rhs - c1.rhs, places))
return (self.objective.is_almost_equal(other.objective, places=places)
and self.constraints.keys() == other.constraints.keys()
and all(constraint_eq(constraint, other.constraints[label])
for label, constraint in self.constraints.items()))
def is_equal(self, other: 'ConstrainedQuadraticModel') -> bool:
"""Return True if the given model has the same objective and constraints."""
def constraint_eq(c0: Comparison, c1: Comparison) -> bool:
return (c0.sense is c1.sense
and c0.lhs.is_equal(c1.lhs)
and c0.rhs == c1.rhs)
return (self.objective.is_equal(other.objective)
and self.constraints.keys() == other.constraints.keys()
and all(constraint_eq(constraint, other.constraints[label])
for label, constraint in self.constraints.items()))
def lower_bound(self, v: Variable) -> Bias:
"""Return the lower bound on the specified variable."""
return self.objective.lower_bound(v)
def num_biases(self) -> int:
"""The number of biases accross the objective and constraints."""
num_biases = len(self.objective.linear) + len(self.objective.quadratic)
num_biases += sum(len(const.lhs.linear) + len(const.lhs.quadratic)
for const in self.constraints.values())
return num_biases
def num_quadratic_variables(self) -> int:
"""Return the total number of variables with at least one quadratic
interaction accross all constraints."""
count = 0
for const in self.constraints.values():
lhs = const.lhs
count += sum(lhs.degree(v) > 0 for v in lhs.variables)
return count
def set_objective(self, objective: Union[BinaryQuadraticModel,
QuadraticModel, Iterable]):
"""Set the objective of the constrained quadratic model.
Args:
objective: Binary quadratic model (BQM) or quadratic model (QM) or
an iterable of tuples.
Examples:
>>> from dimod import Integer, ConstrainedQuadraticModel
>>> i = Integer('i')
>>> j = Integer('j')
>>> cqm = ConstrainedQuadraticModel()
>>> cqm.set_objective(2*i - 0.5*i*j + 10)
"""
if isinstance(objective, Iterable):
objective = self._iterable_to_qm(objective)
# clear out current objective, keeping only the variables
self.objective.quadratic.clear() # there may be a more performant way...
for v in self.objective.variables:
self.objective.set_linear(v, 0)
# offset is overwritten later
# now add everything from the new objective
self._add_variables_from(objective)
for v in objective.variables:
self.objective.set_linear(v, objective.get_linear(v))
self.objective.add_quadratic_from(objective.iter_quadratic())
self.objective.offset = objective.offset
def _iterable_to_qm(self, iterable: Iterable) -> QuadraticModel:
qm = QuadraticModel()
def _add_variable(v):
# handles vartype, and bounds
vartype = self.vartype(v)
if vartype is not Vartype.SPIN and vartype is not Vartype.BINARY:
# need to worry about bounds
qm.add_variable(vartype, v,
lower_bound=self.lower_bound(v),
upper_bound=self.upper_bound(v))
else:
qm.add_variable(vartype, v)
for *variables, bias in iterable:
if len(variables) == 0:
qm.offset += bias
elif len(variables) == 1:
v, = variables
_add_variable(v)
qm.add_linear(v, bias)
elif len(variables) == 2:
u, v = variables
_add_variable(u)
_add_variable(v)
qm.add_quadratic(u, v, bias)
else:
raise ValueError("terms must be constant, linear or quadratic")
return qm
def _substitute_self_loops_from_model(self, qm: Union[BinaryQuadraticModel, QuadraticModel],
mapping: MutableMapping[Variable, Variable]):
if isinstance(qm, BinaryQuadraticModel):
# bqms never have self-loops
return
for u in qm.variables:
vartype = qm.vartype(u)
# integer and binary variables never have self-loops
if vartype is Vartype.SPIN or vartype is Vartype.BINARY:
continue
try:
bias = qm.get_quadratic(u, u)
except ValueError:
# no self-loop
continue
lb = qm.lower_bound(u)
ub = qm.upper_bound(u)
if u not in mapping:
# we've never seen this integer before
new: Variable = new_label()
# on the off chance there are conflicts. Luckily self.variables
# is global accross all constraints/objective so we don't need
# to worry about accidentally picking something we'll regret
while new in self.constraints or new in self.variables:
new = new_label()
mapping[u] = new
self.objective.add_variable(vartype, new, lower_bound=lb, upper_bound=ub)
# we don't add the constraint yet because we don't want
# to modify self.constraints
else:
new = mapping[u]
qm.add_variable(vartype, new, lower_bound=lb, upper_bound=ub)
qm.add_quadratic(u, new, bias)
qm.remove_interaction(u, u)
def substitute_self_loops(self) -> Dict[Variable, Variable]:
"""Replace any integer self-loops in the objective or constraints.
Self-loop :math:`i^2` is removed by introducing a new variable
:math:`j` with interaction :math:`i*j` and adding constraint
:math:`j == i`.
Acts on the objective and constraints in-place.
Returns:
Mapping from the integer variable labels to their introduced
counterparts. The constraint enforcing :math:`j == i` uses
the same label.
Examples:
>>> from dimod import Integer, ConstrainedQuadraticModel
>>> i = Integer('i')
>>> cqm = ConstrainedQuadraticModel()
>>> cqm.add_constraint(i*i <=3, label='i squared')
'i squared'
>>> cqm.substitute_self_loops() # doctest: +IGNORE_RESULT
>>> cqm.constraints # doctest: +IGNORE_RESULT
{'i squared': QuadraticModel({'i': 0.0, 'cf651f3d-bdf8-4735-9139-eee0a32e217f': 0.0}, {('cf651f3d-bdf8-4735-9139-eee0a32e217f', 'i'): 1.0}, 0.0, {'i': 'INTEGER', 'cf651f3d-bdf8-4735-9139-eee0a32e217f': 'INTEGER'}, dtype='float64') <= 3,
'cf651f3d-bdf8-4735-9139-eee0a32e217f': QuadraticModel({'i': 1.0, 'cf651f3d-bdf8-4735-9139-eee0a32e217f': -1.0}, {}, 0.0, {'i': 'INTEGER', 'cf651f3d-bdf8-4735-9139-eee0a32e217f': 'INTEGER'}, dtype='float64') == 0}
"""
mapping: Dict[Variable, Variable] = dict()
self._substitute_self_loops_from_model(self.objective, mapping)
for comparison in self.constraints.values():
self._substitute_self_loops_from_model(comparison.lhs, mapping)
# finally add the constraints for the variables
for v, new in mapping.items():
self.add_constraint([(v, 1), (new, -1)], rhs=0, sense='==', label=new)
return mapping
def to_file(self, *, spool_size: int = int(1e9)) -> tempfile.SpooledTemporaryFile:
"""Serialize to a file-like object.
Args:
spool_size: Defines the `max_size` passed to the constructor of
:class:`tempfile.SpooledTemporaryFile`. Determines whether
the returned file-like's contents will be kept on disk or in
memory.
Format Specification (Version 1.1):
This format is inspired by the `NPY format`_
The first 8 bytes are a magic string: exactly "DIMODCQM".
The next 1 byte is an unsigned byte: the major version of the file
format.
The next 1 byte is an unsigned byte: the minor version of the file
format.
The next 4 bytes form a little-endian unsigned int, the length of
the header data HEADER_LEN.
The next HEADER_LEN bytes form the header data. This is a
json-serialized dictionary. The dictionary is exactly:
.. code-block:: python
dict(num_variables=len(cqm.variables),
num_constraints=len(cqm.constraints),
num_biases=cqm.num_biases(),
num_quadratic_variables=cqm.num_quadratic_variables(),
)
it is terminated by a newline character and padded with spaces to
make the entire length of the entire header divisible by 64.
The constraint quadratic model data comes after the header. It is
encoded as a zip file. The zip file will contain one file
named `objective`, containing the objective as encoded as a file
view. It will also contain a directory called `constraints`. The
`constraints` directory will contain one subdirectory for each
constraint, each containing `lhs`, `rhs` and `sense` encoding
the `lhs` as a fileview, the `rhs` as a float and the sense
as a string. Each directory will also contain a `discrete` file,
encoding whether the constraint represents a discrete variable.
Format Specification (Version 1.0):
This format is the same as Version 1.1, except that the data dict
does not have `num_quadratic_variables`.
.. _NPY format: https://numpy.org/doc/stable/reference/generated/numpy.lib.format.html
"""
file = SpooledTemporaryFile(max_size=spool_size)
data = dict(num_variables=len(self.variables),
num_constraints=len(self.constraints),
num_biases=self.num_biases(),
num_quadratic_variables=self.num_quadratic_variables(),
)