R Gurobi - how to add quadratic constraint with linear terms into the model?
AnsweredHi,
I am trying to solve an MIP model with quadratic constraints involving linear term (R interface):
x^2+y^2+xy+y < 3 (for example)
From the tutorial example, it shows how to add a quadratic constraint, but it does not have a linear term:
#################
qc1 <- list()
#this matrix represents the second order term, not the first order term
qc1$Qc <- spMatrix(3, 3, c(1, 2, 3), c(1, 2, 3), c(1.0, 1.0, -1.0))
qc1$rhs <- 0.0
model$quadcon <- list(qc1)
#################
So I am wondering how to add a quadratic constraint with linear term? (I know customizing the constraints should work, but is there a more convenient way?)
Thank you!
-
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 why not try our AI Gurobot?. -
You can add linear terms to quadratic constraints by specifying the \( \texttt{q} \) component (see the R documentation). E.g., if you have two variables \( x \) and \( y \), you can add the constraint \( x^2 + y^2 + xy + y \leq 3 \) with:
qc1 <- list()
qc1$Qc <- matrix(c(1,1,0,1), nrow=2)
qc1$rhs <- 3
qc1$q <- c(0,1)
model$quadcon <- list(qc1)0 -
Got it, thank you Eli!
0
Post is closed for comments.
Comments
3 comments