-1

I am having objective function with pre-decided objective value, but want to know the values of decision variable for that objective function.

from pulp import LpMaximize, LpProblem, LpStatus, lpSum, LpVariable, LpConstraint

constraints =['0 <= X1<= 150',
'0 <= X2= 1453',
'0 <= X3<= 12',
'0 <= X4<= 149',
,'X1+X2 <= 14'
,'X3+X4 <=1'
,'X1+ X3 <= 6'
,'X2 +X4 <= 9']

for i in range(4):
  t = f'X{i+1} = LpVariable('X{i+1}' , cat= \'Integer\')'
  exec(t)

model = LpProblem(name = "test" , sense = LpMaximize )

for i in range(0, len(constraints)):
    model += (eval(constraints[i]), 'constraint'+str(i))

#objective function
model += lpsum([eval('X1+X2+X3+X4')]

status = model.solve()
for var in model.varibles():
   print(f"{var.name}: {var.value()}")

The expected output : X1=6,X2=8,X3=0,X4=1 but getting output X1= -6,X2=20,X3=12,X4= -11, even though I have added the range constraint for decision variable.

can anybody help me on this , how can I get the expected output where variable values should not be negative.

SKP
  • 151
  • 16

1 Answers1

1

A couple of things...

First, the code you pasted above does not execute, it has a handful of typos that need to be fixed, so I'm not sure how you are getting a solution out of it.

Second, it isn't clear why you are using strings and exec() approach, which is non-standard. Why aren't you coding the constraints directly rather than trying to process strings?

To my knowledge, pulp cannot process two-sided inequalities. Break each of your constraints into single inequalities and try again. Comment back if stuck! :)

AirSquid
  • 10,214
  • 2
  • 7
  • 31