Search code examples
python-3.xpulp

Specify variables bounds in pulp


I'm working on linear programming using pulp and what I ask for is how can I specify each items bounds in my list

mylist=["x","y","z"]

I've created this:

vars = LpVariable.dicts("vars", mylist, lowBound=0, cat='Continuous')

but it creates a global bounds for all of the items inside my list and what I want is for each item in my list

I try this but it didn't work:

x = LpVariable("x", lowBound=5, upBound=10, cat='Continuous')

THANKS!!


Solution

  • You just need to create individual constraints for the low/upper bounds if you want them to be different in pulp. It's all the same to the solver.

    Example:

    import pulp as plp
    
    products = ['rice', 'veggies', 'fruit']
    
    low_bounds = {  'rice': 5,
                    'veggies': 7,
                    'fruit': 2}
    
    prob = plp.LpProblem('example')
    
    x = plp.LpVariable.dicts('products', products, cat='Continuous')
    
    for p in products:
        prob += x[p] >= low_bounds[p]
    
    print(prob)
    

    Yields:

    MINIMIZE
    None
    SUBJECT TO
    _C1: products_rice >= 5
    
    _C2: products_veggies >= 7
    
    _C3: products_fruit >= 2
    
    VARIABLES
    products_fruit free Continuous
    products_rice free Continuous
    products_veggies free Continuous