Skip to content

Commit

Permalink
Merge pull request #1295 from arcondello/fix/constraint-move
Browse files Browse the repository at this point in the history
Fix the ``move`` kwarg in ``CQM.add_constraint_from_model()``
  • Loading branch information
arcondello authored Nov 25, 2022
2 parents 0491d32 + 91c7d11 commit f3d3f9e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
9 changes: 7 additions & 2 deletions dimod/constrained/cyconstrained.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ cdef class cyConstrainedQuadraticModel:

def add_constraint_from_model(self, cyQMBase model, sense, bias_type rhs, label, bint copy, weight, penalty):
# get a mapping from the model's variables to ours
cdef vector[Py_ssize_t] mapping
cdef vector[index_type] mapping
mapping.reserve(model.num_variables())
cdef Py_ssize_t vi
for vi in range(model.num_variables()):
Expand Down Expand Up @@ -205,7 +205,12 @@ cdef class cyConstrainedQuadraticModel:
upper_bound=model.base.upper_bound(vi),
)

self.cppcqm.add_constraint(deref(model.base), cppsense(sense), rhs, mapping)
if copy:
self.cppcqm.add_constraint(deref(model.base), cppsense(sense), rhs, mapping)
else:
self.cppcqm.add_constraint(move(deref(model.base)), cppsense(sense), rhs, mapping)
model.clear()

label = self.constraint_labels._append(label)
assert(self.cppcqm.num_constraints() == self.constraint_labels.size())

Expand Down
4 changes: 3 additions & 1 deletion dimod/include/dimod/abc.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#pragma once

#include <algorithm>
#include <cassert>
#include <iostream>
#include <limits>
#include <memory>
#include <utility>
#include <vector>
Expand All @@ -42,7 +44,7 @@ struct TwoVarTerm {
index_type v;
bias_type bias;

explicit TwoVarTerm(index_type u) : u(u), v(-1), bias(NAN) {}
explicit TwoVarTerm(index_type u) : u(u), v(-1), bias(std::numeric_limits<double>::signaling_NaN()) {}

TwoVarTerm(index_type u, index_type v, bias_type bias): u(u), v(v), bias(bias) {}

Expand Down
1 change: 1 addition & 0 deletions dimod/libcpp/constrained_quadratic_model.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ cdef extern from "dimod/constrained_quadratic_model.h" namespace "dimod" nogil:
index_type add_constraint()
index_type add_constraint(Constraint[bias_type, index_type]) except+
index_type add_constraint[B, I, T](QuadraticModelBase[B, I]&, Sense, bias_type, vector[T])
index_type add_constraint(QuadraticModelBase[bias_type, index_type]&, Sense, bias_type, vector[index_type])
index_type add_constraints(index_type)
index_type add_variable(Vartype)
index_type add_variable(Vartype, bias_type, bias_type)
Expand Down
6 changes: 6 additions & 0 deletions releasenotes/notes/constraint-move-befce0fefaec3d95.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Fix the ``copy`` keyword argument of ``ConstrainedQuadraticModel.add_constraint_from_model()``.
Previously it would always make a copy.
- Add missing ``#include`` in ``dimod/include/dimod/abc.h``.
11 changes: 11 additions & 0 deletions tests/test_constrained.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ def test_bqm(self):
cqm.add_constraint(bqm, '<=')
cqm.add_constraint(bqm, '>=') # add it again

def test_copy(self):
cqm = CQM()

bqm = BQM({'a': -1}, {'ab': 1}, 1.5, 'SPIN')
cqm.add_constraint(bqm, '<=', 1, label=1)
self.assertTrue(bqm.is_equal(cqm.constraints[1].lhs))
cqm.add_constraint(bqm, '>=', 2, label=2, copy=False)
self.assertEqual(bqm.num_variables, 0)
self.assertEqual(bqm.variables, [])
self.assertTrue(cqm.constraints[1].lhs.is_equal(cqm.constraints[2].lhs))

def test_duplicate(self):
cqm = CQM()

Expand Down

0 comments on commit f3d3f9e

Please sign in to comment.