diff --git a/ortools/linear_solver/python/model_builder_test.py b/ortools/linear_solver/python/model_builder_test.py index 6f6b510579c..b473e108639 100644 --- a/ortools/linear_solver/python/model_builder_test.py +++ b/ortools/linear_solver/python/model_builder_test.py @@ -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( @@ -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( @@ -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( @@ -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( @@ -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( diff --git a/ortools/linear_solver/wrappers/model_builder_helper.cc b/ortools/linear_solver/wrappers/model_builder_helper.cc index e3731363ac8..142a8523a58 100644 --- a/ortools/linear_solver/wrappers/model_builder_helper.cc +++ b/ortools/linear_solver/wrappers/model_builder_helper.cc @@ -142,6 +142,7 @@ 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); @@ -149,6 +150,7 @@ void ModelBuilderHelper::AddConstraintTerm(int ct_index, int var_index, 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) {