Trouble with using a transforming function on a variable
AnsweredHey,
I am currently trying to optimize a model with parameteres that are influenced by economy of scale. Depending on the current value of the variable it is multiplied with the result of a transforming function. As a little example I replicated the problem with a short array:
double[] transform = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
GRBVar x = model.addVar(0.0, 10.0, 0.0, GRB.CONTINUOUS, "x");
model.update();
// Set objective: maximize x * transform(x)
int round = (int) Math.ceil(x.get(GRB.DoubleAttr.X));
GRBLinExpr expr = new GRBLinExpr();
expr.addTerm(transform[round], x);
model.setObjective(expr, GRB.MAXIMIZE);
model.optimize();
As far as I know this doesn't work since the get(GRB.DoubleAttr.X) function can only be used after the optimization. Any help on how to use the current variable double value for further calculations/transformations would be greatly appreciated.
Best regards,
Lukas
-
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?. -
Hi Lukas,
You could introduce the transform value as an integer valued optimization variable and model the ceil function. However note that you will have to introduce a tolerance because modeling strict inequalities is not possible in Gurobi. The stackexchange post Modeling floor function exactly might be helpful. You can then introduce nonconvex binary products of optimization variables \(\text{transform} \cdot x\).
Best regards,
Jaromił0 -
Thank you so much for your answer Jaromil, I think I' ve got the gist of it.
In case somebody finds this with the same problem you can define the transformation function through constraints. In the case of my example above the function would be y=10-x so you define y as a variable and add the constraint y+x=10. If you're working with a scaling factor of say 0.5 (meaning y=x^0.5) you have to add another variable that defines x^0.5 by adding a constraint for (x^0,5) * x(0,5) - x = 0.
GRBQuadExpr constr1 = new GRBQuadExpr();
constr1.addTerm(1, root2, root2);
constr1.addTerm(-1, x);
model.addQConstr(constr1, GRB.EQUAL, 0.0, "constr1");Thanks again and best regards,
Lukas
0
Post is closed for comments.
Comments
3 comments