How to multiply two linear expressions in java
AnsweredHello expert,
Please, I need your help for this problem :
I have this objectif function : min (K + x + y)^2
Where K is a constant, x and y are variables
I want to write like that :
create a linear expression : q -> K + x + y and after that use a quadratic expression :
GRBLinExpr q = new GRBLinExpr ();
q.addTerm(1.0, xvar);
q.addTerm(1.0, yvar);
q.addConstant(K);
GRBQuadExpr f = new GRBQuadExpr();
f.addTerm(1.0, q, q);
Unfortunately, addTerm don't take expression, takes only variables !!
Please could help me how can I multiply two expressions ?
Thanks in advance.
-
Hi,
You are right that
GRBQuadExpr.addTerm
only accepts variables, not complex expressions.There are two ways around this:
- You expand the quadratic expression yourself, i.e., \((K+x+y)^2 = K^2 + x^2 + y^2 + 2Kx + 2Ky + 2xy\), and then build the resulting expression using
addTerm
. - You introduce an auxiliary variable, say \$a\$, and add a constraint that \(a == q\). Then you can use this variable to build our objective expression.
0 - You expand the quadratic expression yourself, i.e., \((K+x+y)^2 = K^2 + x^2 + y^2 + 2Kx + 2Ky + 2xy\), and then build the resulting expression using
-
Hi,
Thanks for your response.
Before asking, I was using the first method, but in my thesis the objectif function will be more complicated and I'm looking for better method.
I'll implement your second solution, if i well understand :
objectif function will be min a^2 , a is a variable and add a new constraint a == K + x + y
Best regards
0
Please sign in to leave a comment.
Comments
2 comments