What is the difference between quadratic expression and quadratic constraint for gurobi?
AnsweredHi Gurobi Community.
What is the difference between quadratic expression and quadratic constraint for gurobi?
For quadratic constraint, I need to set the nonconvex parameter to 2 for gurobi to solve.
If I use the quadratic expression, gurobi solves it without setting the nonconvex parameters to 2.
I believe both are solved by reformulating the bilinear terms as mentioned in slide 8: https://www.gurobi.com/wp-content/uploads/2020-01-14_Non-Convex-Quadratic-Optimization-in-Gurobi-9.0-Webinar.pdf?x58432
For example, by linearizing the product of binary and continuous variables or by replacing the product of two continuous variables using the McCormick envelope.
Below is an illustrative example of formulating a problem using quadratic constraint and quadratic expression
P= m.addVars(n_b, n_t, vtype='C', lb=-GRB.INFINITY, ub=GRB.INFINITY)
j_on = m.addVars(n_j,n_t,vtype=GRB.BINARY)
pj = m.addVars(n_j,n_t, vtype='C', lb=pj_min , ub=pj_max)
# quadratic constraint:
m.addConstr(P[i,t]==gp.quicksum(pj[j,t]*j_on[j,t] for j in j_at_bus) )
# quadratic expression:
P[i,t]=gp.quicksum(pj[j,t]*j_on[j,t] for j in j_at_bus)
Thanks,
Hussein
-
Hi Hussein,
A quadratic constraint consists of quadratic expressions, i.e., a quadratic expression is only a term involving products of variables, not a complete constraint.
In your first case, where you add a quadratic constraint, the resulting model will be non-convex since quadratic equality constraints are always non-convex (except for a few special cases).
In your second case, you do NOT add any quadratic constraints to the model, you just overwrite the Gurobi variable P[i,t] with some quadratic expression, which is not added to the model.
To check what is actually included in your model, you could export it to a file with m.write("model.lp") and open the resulting plain text file.
Best regards,
Mario1 -
Thank you so much! Mario Ruthmair
You are right. In my second case, the constraint was not added to the model.
Best,
Hussein
0
Please sign in to leave a comment.
Comments
2 comments