Setting lower and upper bounds on multiple variables in a single API call
回答済みFrom https://docs.gurobi.com/projects/optimizer/en/current/concepts/attributes/examples.html I am able to gather the following as the way to set bounds on ALL variables in a model.
double lb[NUMVARS];error = GRBsetdblattrarray(model, "LB", 0, cols, lb);
double ub[NUMVARS];error = GRBsetdblattrarray(model, "UB", 0, cols, ub);
What if NUMVARS = 100, but I would like to change the lower and upper bounds on some arbitrary selective indexed variables, say, the lower bound on 3rd index variable to 1 and the upper bound on the 5th indexed variable to 0?
In CPLEX, for instance, the following would work:
int cnt = 2;
int indices[2]{3, 5};
double bounds[2]{1, 0};
char boundtype[2]{'L', ‘U’};
const int status = CPXchgbds(env, lp, cnt, indices, boundtype, bounds); Could you please suggest the canonical idiomatic way to obtain the equivalent functionality in Gurobi?
I could loop through individually the elements and change the bounds on a single variable at a time, but I was wondering if there is a more efficient way to do it
Thank you.
-
Thank you for the question. To set upper and lower bounds on indexed lists of variables in our C API, you can use GRBsetdblattrlist. Slightly modifying an example from that linked documentation, you could set upper bounds on variables 0, 1 and 3 as follows:
int change[] = {0, 1, 3}; double newub[] = {1.0, 3.0, 2.0}; error = GRBsetdblattrlist(model, "UB", 3, change, newub);Note that you will have to make a separate call with appropriate input to set lower bounds. Some additional information about switching from CPLEX to Gurobi can be found on our Switching from CPLEX page. Another useful page is the C API Reference section of our reference manual.
1
サインインしてコメントを残してください。
コメント
1件のコメント