1

Let's say I have this dictionary :

cars = ["car1","car2","car3","car4","car5"]

x = LpVariable.dicts("car",cars, cat='Integer', lowBound=0, upBound=800)

Is there any way to add different lowBound and upBounds to each car, please?

Note

The easy code version looks like this :

car1 = LpVariable("car1", 0, 40)   
car2 = LpVariable("car2", 0, 1000) 

Please notice that the car1 upBound is 40 and the car 2 upBound is 1000.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
test1500
  • 65
  • 1
  • 11
  • 1
    second argument in LpVariable.dicts should be stated like a list comprehension so that you have something like this : x = LpVariable.dicts("car", [car for car in cars], cat ='Integer") – Billal Naseem Sep 02 '20 at 11:52
  • Hello Billal,I need to set differents lowBounds . – test1500 Sep 02 '20 at 12:09

1 Answers1

1

Finally, I 've done it, using his great code : How do I generate PuLP variables and constrains without using exec? Thanks a lot, DSM, bro !

prob = LpProblem("problem", LpMaximize)

# Setting LP variables
lpVars =["car1","car2","car3"]
upbounds=[40,80,30]
xs = [LpVariable("car{}".format(i+1), lowBound = 0,  upBound = upbounds[i], cat='Integer'  ) for i in range(len(lpVars))]

 # add objective
    margin = [3,2,3]
    total_prof = sum(x * value for x,value in zip(xs, margin))
    prob += total_prof

# add constraint
    labour = [2,1,4]
    total_labour = sum(x * w for x,w in zip(xs, labour))
    prob += total_labour <= 100

 # Solve the problem
    prob.solve()

The next step is getting the arrays variables from the front end app (upbounds, margin, labour, etc ..) , thank you, bro, peep my github

test1500
  • 65
  • 1
  • 11
  • Looks like the right way to do it is there : https://stackoverflow.com/questions/7728313/python-pulp-using-with-matrices – test1500 Oct 06 '20 at 14:11