I am trying to constrain my problem by making the absolute difference of two LpVariables more than 0. Is there anything obvious i am missing in my implementation?
This is an implementation i found on http://lpsolve.sourceforge.net/5.1/absolute.htm which ive implemented it in the code below. I tried changing the value for M but it does not make a difference.
When i enter the actual result into the formula (such that q1x-q2x = 0) i find that this does not fulfill the constraints. However pulp says the solution is optimal.
prob = LpProblem("myProblem", LpMinimize)
q1x = LpVariable("q1x",1,8)
q2x = LpVariable("q2x",1,8)
B12 = LpVariable("B12",0,1)
M=8
prob += (q1x-q2x) + M * B12 >= 1
prob += -(q1x-q2x) + M * (1-B12) >= 1
prob += q1x+q1y
Expected :
q1x = 1; q2x = 2 or "infeasible"
Actual:
q1x = 1; q2x = 1 "optimal"
The very first "constraint" that is added to the LpProblem
class will always be considered the objective function, as described in this test problem.
Also i am assuming you want your decision variables q1x
and q2x
to be integer which you have to be explicit about when defining your variables
Try this:
prob = LpProblem('myProblem', LpMinimize)
q1x = LpVariable("q1x",lowBound = 1, upBound = 8, cat = "Integer")
q2x = LpVariable("q2x",lowBound = 1, upBound = 8, cat = "Integer")
B12 = LpVariable("B12", lowBound = 0, upBound = 1, cat = "Integer")
# B12 can be also be defined by:
#B12 = LpVariable("B12", cat = "Binary")
M = 8
prob += q1x + q2x
prob += (q1x-q2x) + M * B12 >= 1
prob += -(q1x-q2x) + M * (1-B12) >= 1
prob.solve()
for v in prob.variables():
print(v.name, " = ", v.varValue)
print("Status:", LpStatus[prob.status])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.