Skip to content

Bug Report: Incorrect Sum Constraint Generation in PuLP v3.0 #794

@hanapirath

Description

@hanapirath

Details for the issue

When using lpSum to define a sum constraint and applying both Min and Max constraints to the resulting variable, PuLP generates the constraints incorrectly. Specifically, the Min_Constraint is overwritten or altered when the Max_Constraint is added. This issue occurs when multiple constraints are applied sequentially to the sum of variables. See example below.

import sys
import pulp

print(f"Python version: {sys.version}")
print(f"pulp version: {pulp.__version__}")

# Create a minimization problem
model = pulp.LpProblem("Sum_Constraint_Example", pulp.LpMinimize)

# Define individual variables x[i] for i in range(3)
x_vars = {i: pulp.LpVariable(f"x{i}", lowBound=0, cat=pulp.LpContinuous) for i in range(3)}

# Define x as the sum of all x[i]
x = pulp.lpSum(x_vars[i] for i in range(3))

# Add min and max constraints on x
model += (x >= 2, "Min_Constraint")
model += (x <= 5, "Max_Constraint")  

# Print model
print(model)

What Pulp generated

Python version: 3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0]
pulp version: 3.0.0
Sum_Constraint_Example:
MINIMIZE
None
SUBJECT TO
Min_Constraint: x0 + x1 + x2 >= 7

Max_Constraint: x0 + x1 + x2 <= 7

VARIABLES
x0 Continuous
x1 Continuous
x2 Continuous

Useful extra information

The info below often helps, please fill it out if you're able to. :)

What operating system are you using?

  • Windows: ( version: ___ )
  • Linux: ( distro: ___ )
  • Mac OS: ( version: ___ )
  • Other: ___

I'm using python version:

  • 3.7
  • 3.8
  • 3.9
  • 3.10
  • 3.11
  • Other: ___

I installed PuLP via:

Did you also

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions