Skip to content

Commit

Permalink
Fix discrete constraint marking in ConstrainedQuadraticModel::fix_var…
Browse files Browse the repository at this point in the history
…iables()
  • Loading branch information
arcondello committed Apr 17, 2023
1 parent 92396b0 commit fdf924d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dimod/include/dimod/constrained_quadratic_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ ConstrainedQuadraticModel<bias_type, index_type>::fix_variables(VarIter first, V
new_constraint.set_weight(old_constraint_ptr->weight());
new_constraint.set_penalty(old_constraint_ptr->penalty());
new_constraint.mark_discrete(old_constraint_ptr->marked_discrete() &&
old_constraint_ptr->is_onehot());
new_constraint.is_onehot());

cqm.add_constraint(std::move(new_constraint));
}
Expand Down
29 changes: 29 additions & 0 deletions testscpp/tests/test_constrained_quadratic_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,35 @@ SCENARIO("ConstrainedQuadraticModel tests") {
}
}

GIVEN("A discrete constraint") {
auto cqm = ConstrainedQuadraticModel<double>();
cqm.add_variables(Vartype::BINARY, 5);
auto c0 = cqm.add_linear_constraint({0, 1, 2, 3, 4}, {1, 1, 1, 1, 1}, Sense::EQ, 1);
cqm.constraint_ref(c0).mark_discrete();

WHEN("we fix a variable to 0 while making a new cqm") {
auto sub_cqm = cqm.fix_variables({2}, {0});

THEN("the constraint is still discrete") {
CHECK(sub_cqm.num_variables() == 4);
REQUIRE(sub_cqm.num_constraints() == 1);
CHECK(sub_cqm.constraint_ref(0).marked_discrete());
CHECK(sub_cqm.constraint_ref(0).is_onehot());
}
}

WHEN("we fix a variable to 1 while making a new cqm") {
auto sub_cqm = cqm.fix_variables({2}, {1});

THEN("the constraint is no longer discrete") {
CHECK(sub_cqm.num_variables() == 4);
REQUIRE(sub_cqm.num_constraints() == 1);
CHECK(!sub_cqm.constraint_ref(0).marked_discrete());
CHECK(!sub_cqm.constraint_ref(0).is_onehot());
}
}
}

GIVEN("A constraint with one-hot constraints") {
auto cqm = ConstrainedQuadraticModel<double>();
cqm.add_variables(Vartype::BINARY, 10);
Expand Down

0 comments on commit fdf924d

Please sign in to comment.