how to add new coloumns into the left side of constraints?
Answeredfor example, i design original constraints like:
c1: a1*x1+a2*x2=1
c2: a3*x1+a4*x2=1
c3: x1+x2 ≤ 5
while new constraints will be
c1: a1*x1+a2*x2 + a5*x3 + a6*x4=1
c2: a3*x1+a4*x2+ a7*x3 + a8*x4=1
c3: x1+x2 + x3 + x4 ≤ 5
maybe add more xi
I WANT to extract the left side these constrainst and add new elements to it to generate new constrainsts, but Gurobi mentions that "no lhs of LinExpr" or no"Expr", how can i achive the purpose to add new columns to the model?
old_expr = cons[0].lhs
old_expr = cons[0].getAttr('Expr')
-
You can get the LHS of a linear constraint by calling the getRow() method, which will return a LinExpr object.
However, what you need in your case is the Column object, which you fill with the respective coefficients and constraints via the addTerms method and then you can call the addVar method and pass this Column object to add the variable to the constraint of interest with pre-defined coefficients.
In your particular example, this would be something like
colx3 = gp.Column()
colx4 = gp.Column()
colx3.addTerms([a5, a7, 1.0], [c1, c2, c3])
colx4.addTerms([a6, a8, 1.0], [c1, c2, c3])
x3 = model.addVar(..., column = colx3)
x4 = model.addVar(..., column = colx4)Please note that the Column object works only with linear constraints.
Best regards,
Jaromił0
Please sign in to leave a comment.
Comments
1 comment