GurobiError: Invalid argument to addLConstr
Awaiting user inputDear,
I am trying to implement a model in Gurobi (see below). The issue I'm having is that one of my constraints should be an equality constraint, but it keeps changing to an inequality constraint in the implementation with Gurobi. The constraint is read as a QuadExpr, which apparently does not solve equality constraints. I have read online that 'model.Params.NonConvex = 2' should solve this, but using this hasn't helped my case. Is there another way of going around this or would I have to remodel the constraint somehow?
Thanks in advance!
I am trying to implement the following pricing problem in Gurobi:

The objective of the model is to find a permutation of an item set that can improve the solution found by the master problem. Variable z are the dual prices found in the master problem.
Constraint (7.7), however, is interpreted as a QuadExpr. As I have read online, these are always read as inequality constraints. As you can see from the model, constraint (7.7) should be read as an equality constraint. I have already tried using the 'model.Params.NonConvex = 2' method, but this did not help my case. The code I used to implement this formulation was the following:
class Subproblem:
def __init__(self, permutations, duals, items, bins, n, m):
self.permutations = permutations
self.duals = duals
self.model = gp.Model("Subproblem")
self.items = items
self.bins = bins
#self.model.Params.LogToConsole = 0
self.n = n
self.m = m
def buildModel(self, binprobabilities, sizes, total, capacities, patterns):
# Build the IP model
self.model.Params.NonConvex = 2
self.generateVariables(total, binprobabilities)
self.generateConstraints(sizes, total, capacities)
self.generateObjective()
self.model.update()
print(self.model.display())
self.model.optimize()
#print("Model optmimum: ", self.model.getObjective().getValue())
pattern = 0
return pattern
def generateVariables(self, total, binprobs):
items = list(self.items.keys())
bins = list(self.bins.keys())
bins.insert(0,-1)
self.packed = self.model.addVars(items, bins, lb=0, ub=1, vtype=GRB.BINARY, name='x')
self.delta = self.model.addVars(items, items, lb=0, ub=1, vtype=GRB.BINARY, name='delta')
self.quantity = self.model.addVars(items, lb=0, ub=total, vtype=GRB.CONTINUOUS,name='q')
self.probability = self.model.addVars(bins, lb=0, ub=1, vtype=GRB.CONTINUOUS, name='P')
self.logarithm = self.model.addVars(items, lb=0, ub=1, vtype=GRB.CONTINUOUS, name='L')
return
def generateConstraints(self, sizes, total, capacities):
capacities.sort(reverse=True)
probabilities, volumes = list(), list()
print("PRINT", capacities)
for bin in capacities:
probabilities.append(bin[1])
volumes.append(bin[0])
print(probabilities, volumes)
for probability in range (len(probabilities)):
self.model.addLConstr(self.probability[probability] == gp.quicksum(prob for prob in probabilities[:probability+1]),
name='probability')
for item in range(self.n):
for iitem in range(item+1 ,self.n):
if item != iitem:
self.model.addLConstr(self.delta[item,iitem] + self.delta[iitem, item] == 1, name='order')
for item in range (self.n):
for iitem in range (self.n):
for iiitem in range (self.n):
if item != iitem != iiitem != item:
self.model.addLConstr(self.delta[item, iitem] + self.delta[iitem, iiitem] +
self.delta[iiitem, item] <= 2, name='order')
for item in range (self.n):
self.model.addLConstr(self.quantity[item] == sizes[item] + gp.quicksum((sizes[iitem]*self.delta[iitem,item]
for iitem in range (self.n) if iitem != item)), name='Quantities')
for omega in range (self.m):
for item in range(self.n):
self.model.addLConstr(self.quantity[item] <= volumes[omega] + total*(1-self.packed[item, omega]),
name='MaxQuantities')
for item in range (self.n):
self.model.addQConstr(rhs=self.logarithm[item], sense=GRB.EQUAL, lhs=gp.quicksum(self.probability[omega] *
(self.packed[item, omega] - self.packed[item, omega-1])
for omega in range (self.m)),
name = 'Logarithms')
return
def generateObjective(self):
self.model.setObjective(gp.quicksum(self.duals[item]*self.logarithm[item] for item in range (self.n)))
return
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Simon,
The documentation suggests that quadratic constraints handle equality constraints.
How do you know that the constraints were converted to an inequality form? Did you store the model somehow?
Could you share a minimal reproducible example of the code which produces the inequalities instead of equality constraints?
Best regards
Jonasz0
Post is closed for comments.
Comments
2 comments