Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 19054

Two different optimal values for negated constraints

$
0
0

When I run this program in PuLP that is supposed to minimize the unconstrained gamma variable, I get infeasible. Notice the constraint signs are mixed ≤ and ≥. The gamma variable in the dictionaries represents a weight placed on the importance of that goal. But when I negate the constraints to make them all ≤, which should be the same thing, I get a solution. What am I missing here about how PuLP works?

import pulp as lp# Define the problemprob = lp.LpProblem("Annual_usage", lp.LpMinimize)# Decision variables# x_i is the project typex = {'X1': lp.LpVariable('x_1', lowBound=0, cat='Binary'),'X2': lp.LpVariable('x_2', lowBound=0, cat='Binary'),'X3': lp.LpVariable('x_3', lowBound=0, cat='Binary'),'X4': lp.LpVariable('x_4', lowBound=0, cat='Binary'),'X5': lp.LpVariable('x_5', lowBound=0, cat='Binary'),'X6': lp.LpVariable('x_6', lowBound=0, cat='Binary'),'X7': lp.LpVariable('x_7', lowBound=0, cat='Binary'),'X8': lp.LpVariable('x_8', lowBound=0, cat='Binary'),'gamma': lp.LpVariable('gamma', lowBound=None, cat='Continuous')}# Usageusage = {'X1': 4.7, 'X2': 12.5, 'X3': 3.2, 'X4': 7.5, 'X5': 41,  'X6': 47,   'X7': 23,  'X8': 16,'gamma': 0}# Costscost = {'X1': 75,  'X2': 180,  'X3': 350, 'X4': 45,  'X5': 120, 'X6': 80,   'X7': 115, 'X8': 210,'gamma': 0}# Acreageacreage = {'X1': 7,   'X2': 12,   'X3': 20,  'X4': 6, 'X5': 3,   'X6': 25,   'X7': 5,   'X8': 8,'gamma': 0}goal1 = { # at least 1 of X3 and X6'X1': 0,   'X2': 0,   'X3': 1,  'X4': 0, 'X5': 0,   'X6':  1,   'X7': 0,   'X8': 0,'gamma': 16/6}goal2 = { # at least 130'X1': 4.7, 'X2': 12.5, 'X3': 3.2, 'X4': 7.5, 'X5': 41,  'X6': 47,   'X7': 23,  'X8': 16,'gamma': 16/3}goal3 = { #sum is at most 7'X1': 3,   'X2': 2,    'X3': 1,   'X4': 3, 'X5': 2,   'X6':  1,    'X7': 2,   'X8': 3,'gamma': 16/2}goal4 = { #at least 495)'X1': 75,  'X2': 180,  'X3': 350, 'X4': 45,  'X5': 120, 'X6': 80,   'X7': 115, 'X8': 210,'gamma': 16}goal5 = { # sum is at least 5'X1': 1,   'X2': 1,   'X3': 1,  'X4': 1, 'X5': 1,   'X6':  1,   'X7': 1,   'X8': 1,'gamma': 4}gamma_objective = { 'X1': 0,   'X2': 0,   'X3': 0,  'X4': 0, 'X5': 0,   'X6':  0,   'X7': 0,   'X8': 0,'gamma': 1}# Objective functionprob += lp.lpSum(gamma_objective[i] * x[i] for i in x)# Constraintsprob += lp.lpSum(x[i] * goal1[i] for i in x) >= 1, "goal 1"prob += lp.lpSum(x[i] * goal2[i] for i in x) >= 130, "goal 2"prob += lp.lpSum(x[i] * goal3[i] for i in x) <= 7,  "goal 3"prob += lp.lpSum(x[i] * goal4[i] for i in x) >= 495, "goal 4"prob += lp.lpSum(x[i] * goal5[i] for i in x) >= 5, "goal 5"prob += lp.lpSum(x[i] * cost[i] for i in x) <=550, "# cost constraint"prob += lp.lpSum(x[i] * acreage[i] for i in x) <=50, "# acreage constraint"# Solve the problemprob.solve()# Print the solutionfor v in prob.variables():    print(v.name, "=", v.varValue)print("Total Cost =", lp.value(prob.objective))status = prob.statusprint("Status:", lp.LpStatus[status])

This is the code that gives me a solution when I negative the 1st, 2nd, 4th, and 5th goal constraints.

import pulp as lp# Define the problemprob = lp.LpProblem("Annual_usage", lp.LpMinimize)# Decision variables# x_i is the project typex = {'X1': lp.LpVariable('x_1', lowBound=0, cat='Binary'),'X2': lp.LpVariable('x_2', lowBound=0, cat='Binary'),'X3': lp.LpVariable('x_3', lowBound=0, cat='Binary'),'X4': lp.LpVariable('x_4', lowBound=0, cat='Binary'),'X5': lp.LpVariable('x_5', lowBound=0, cat='Binary'),'X6': lp.LpVariable('x_6', lowBound=0, cat='Binary'),'X7': lp.LpVariable('x_7', lowBound=0, cat='Binary'),'X8': lp.LpVariable('x_8', lowBound=0, cat='Binary'),'gamma': lp.LpVariable('gamma', lowBound=None, cat='Continuous')}# Usageusage = {'X1': 4.7, 'X2': 12.5, 'X3': 3.2, 'X4': 7.5, 'X5': 41,  'X6': 47,   'X7': 23,  'X8': 16,'gamma': 0}# Costscost = {'X1': 75,  'X2': 180,  'X3': 350, 'X4': 45,  'X5': 120, 'X6': 80,   'X7': 115, 'X8': 210,'gamma': 0}# Acreageacreage = {'X1': 7,   'X2': 12,   'X3': 20,  'X4': 6, 'X5': 3,   'X6': 25,   'X7': 5,   'X8': 8,'gamma': 0}goal1 = { # at least X3 and X6'X1': 0,   'X2': 0,   'X3': -1,  'X4': 0, 'X5': 0,   'X6':  -1,   'X7': 0,   'X8': 0,'gamma': 1-6/6}goal2 = { # at least 130'X1': -4.7, 'X2': -12.5, 'X3': -3.2, 'X4': -7.5, 'X5': -41,  'X6': -47,   'X7': -23,  'X8': -16,'gamma': -16/3}goal3 = { #sum is at most 7'X1': -3,   'X2': -2,    'X3': -1,   'X4': -3, 'X5': -2,   'X6':  -1,    'X7': -2,   'X8':-3,'gamma': -16/2}goal4 = { # At  least 495k'X1': -75,  'X2': -180,  'X3': -350, 'X4': -45,  'X5': -120, 'X6': -80,   'X7': -115, 'X8': -210,'gamma': -16}goal5 = { # sum is at least 5'X1': -1,   'X2': -1,   'X3': -1,  'X4': -1, 'X5': -1,   'X6':  -1,   'X7': -1,   'X8': -1,'gamma': -4}gamma_objective = { 'X1': 0,   'X2': 0,   'X3': 0,  'X4': 0, 'X5': 0,   'X6':  0,   'X7': 0,   'X8': 0,'gamma': 1}# Objective functionprob += lp.lpSum(gamma_objective[i] * x[i] for i in x)# Constraintsprob += lp.lpSum(x[i] * goal1[i] for i in x) <= -1, "goal 1"prob += lp.lpSum(x[i] * goal2[i] for i in x) <= -130, "goal 2"prob += lp.lpSum(x[i] * goal3[i] for i in x) <= 7,  "goal 3"prob += lp.lpSum(x[i] * goal4[i] for i in x) <= -495, "goal 4"prob += lp.lpSum(x[i] * goal5[i] for i in x) <= -5, "goal 5"prob += lp.lpSum(x[i] * cost[i] for i in x) <=550, "# cost constraint"prob += lp.lpSum(x[i] * acreage[i] for i in x) <=50, "# acreage constraint"# Solve the problemprob.solve()# Print the solutionfor v in prob.variables():    print(v.name, "=", v.varValue)print("Total Cost =", lp.value(prob.objective))status = prob.statusprint("Status:", lp.LpStatus[status])

Viewing all articles
Browse latest Browse all 19054

Trending Articles