Skip to content

Commit

Permalink
[ModelBuilder] do not add term 0 * var
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Jul 20, 2023
1 parent b82cd22 commit 271cf03
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 5 additions & 5 deletions ortools/linear_solver/python/model_builder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ class ModelBuilderLinearConstraintsTest(parameterized.TestCase):
constraint_count=1,
lower_bounds=[-math.inf],
upper_bounds=[0],
expression_terms=lambda x, y: [{x[0]: 0}],
expression_terms=lambda x, y: [{}],
expression_offsets=[0],
),
dict(
Expand Down Expand Up @@ -1064,7 +1064,7 @@ class ModelBuilderLinearConstraintsTest(parameterized.TestCase):
constraint_count=1,
lower_bounds=[0],
upper_bounds=[math.inf],
expression_terms=lambda x, y: [{x[0]: 0}],
expression_terms=lambda x, y: [{}],
expression_offsets=[0],
),
dict(
Expand All @@ -1075,7 +1075,7 @@ class ModelBuilderLinearConstraintsTest(parameterized.TestCase):
constraint_count=1,
lower_bounds=[-math.inf],
upper_bounds=[3],
expression_terms=lambda x, y: [{x[0]: 0}],
expression_terms=lambda x, y: [{}],
expression_offsets=[0],
),
dict(
Expand All @@ -1086,7 +1086,7 @@ class ModelBuilderLinearConstraintsTest(parameterized.TestCase):
constraint_count=1,
lower_bounds=[3],
upper_bounds=[3],
expression_terms=lambda x, y: [{x[0]: 0}],
expression_terms=lambda x, y: [{}],
expression_offsets=[0],
),
dict(
Expand All @@ -1097,7 +1097,7 @@ class ModelBuilderLinearConstraintsTest(parameterized.TestCase):
constraint_count=1,
lower_bounds=[3],
upper_bounds=[math.inf],
expression_terms=lambda x, y: [{x[0]: 0}],
expression_terms=lambda x, y: [{}],
expression_offsets=[0],
),
dict(
Expand Down
2 changes: 2 additions & 0 deletions ortools/linear_solver/wrappers/model_builder_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,15 @@ void ModelBuilderHelper::SetConstraintUpperBound(int ct_index, double ub) {

void ModelBuilderHelper::AddConstraintTerm(int ct_index, int var_index,
double coeff) {
if (coeff == 0.0) return;
MPConstraintProto* ct_proto = model_.mutable_constraint(ct_index);
ct_proto->add_var_index(var_index);
ct_proto->add_coefficient(coeff);
}

void ModelBuilderHelper::SafeAddConstraintTerm(int ct_index, int var_index,
double coeff) {
if (coeff == 0.0) return;
MPConstraintProto* ct_proto = model_.mutable_constraint(ct_index);
for (int i = 0; i < ct_proto->var_index_size(); ++i) {
if (ct_proto->var_index(i) == var_index) {
Expand Down

0 comments on commit 271cf03

Please sign in to comment.