-
Notifications
You must be signed in to change notification settings - Fork 84
Addressing issue #1364: option for a variable check in add_discrete method #1365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Addressing issue #1364: option for a variable check in add_discrete method #1365
Conversation
…crete method (issue dwavesystems#1364)
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1365 +/- ##
=======================================
Coverage 94.86% 94.86%
=======================================
Files 96 96
Lines 10324 10324
=======================================
Hits 9794 9794
Misses 530 530 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does need a release note. Probably something like
Add <kwarg name> keyword argument to `ConstrainedQuadraticModel.add_discrete()`, see `#1364 <https://github.com/dwavesystems/dimod/issues/1364>`_.
Also, ideally we would add tests to TestAddDiscrete
def test_overlap_check(self):
cqm.add_discrete("abc")
i = cqm.add_variable("INTEGER", "i")
with self.assertRaises(ValueError):
cqm.add_discrete("bcd", check_overlaps=True)
cqm.add_discrete("bcd", check_overlap=False) # allows overlapping
with self.assertRaises(ValueError):
cqm.add_discrete(i, check_overlap=False) # still checks vartype
dimod/constrained/constrained.py
Outdated
| label: Optional[Hashable] = None, | ||
| copy: bool = True) -> Hashable: | ||
| copy: bool = True, | ||
| do_var_check: bool = True) -> Hashable: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think check_valid or check_overlaps would be a better name, if we want to keep the "direction" of having it default to True.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check_overlaps is a great name! I'll stick with that
dimod/constrained/constrained.py
Outdated
| the model can cause issues. | ||
|
|
||
| do_var_check: If `True` we perform a variable check. In particular, if | ||
| the variables already exist, we make sure they're not used. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| the variables already exist, we make sure they're not used. | |
| the variables already exist, we make sure they're not used in another discrete constraint. |
here and elsewhere.
dimod/constrained/constrained.py
Outdated
| if do_var_check and v in self.variables: | ||
| # it already exists, let's make sure it's not already used | ||
| if any(v in self.constraints[label].lhs.variables for label in self.discrete): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if do_var_check and v in self.variables: | |
| # it already exists, let's make sure it's not already used | |
| if any(v in self.constraints[label].lhs.variables for label in self.discrete): | |
| if v in self.variables: | |
| # it already exists, let's make sure it's not already used | |
| if do_var_check and any(v in self.constraints[label].lhs.variables for label in self.discrete): |
we still want to check that existing variables are binary, we just want to skip the check for overlapping constraints.
|
FWIW, I think this keyword arg is sufficiently advanced that we don't need to add a docstring example for it. |
As discussed in the issue #1364, the variable check in
ConstrainedQuadraticModel.add_discretemethod can be skipped in some cases and the user can decide if this is truly necessary.This is implemented by adding a
do_var_checkboolean variable (default:True), which enables the check if it's true, in the methodsConstrainedQuadraticModel.add_discrete_from_model,ConstrainedQuadraticModel.add_discrete_from_comparison, andConstrainedQuadraticModel.add_discrete_from_iterable.