C++: Error code 10003 of getVar(int) to call variable
AnsweredHi all. My name is mason.
I found a strange behavior of getVar function; (1) Sometimes the function works, but (2) sometimes it doesn't.
getVar function works after model's optimize() function. Here I use the basic code "mip1_c++.cpp".
(1) I added getVar function after optimize()
// Optimize model
model.optimize();
/*********** CUSTOM CODES ************/
int x_idx = x.index();
cout<<"x's index : "<<x_idx<<endl;
GRBVarx_grbvar = model.getVar(x_idx);
cout<<"returned gurobi var's x index : "<<x_grbvar.index() <<endl;
/*************************************/
and I get the following result:
Optimal solution found (tolerance 1.00e-04)
Best objective 3.000000000000e+00, best bound 3.000000000000e+00, gap 0.0000%
x's index : 0
returned gurobi var's x index : 0
x 1
y 0
z 1
num_variable : 3
Obj: 3
(2) Now, I moved the getVar function before optimize function. Then, it throws an exception:
/*********** CUSTOM CODES ************/
int x_idx = x.index();
cout<<"x's index : "<<x_idx<<endl;
GRBVarx_grbvar = model.getVar(x_idx);
cout<<"returned gurobi var's x index : "<<x_grbvar.index() <<endl;
/*************************************/
// Optimize model
model.optimize();
Scanning dependencies of target gurobi-template
[ 50%] Building CXX object CMakeFiles/gurobi-template.dir/mip1_c++.cpp.o
[100%] Linking CXX executable gurobi-template
[100%] Built target gurobi-template
Academic license - for non-commercial use only - expires 2021-04-21
x's index : 0
Error code = 10003
Invalid arguments
Here, what I am missing and what is wrong?? Shall you help me understand?
-
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,
This behavior is caused by Gurobi's lazy updates. You have to call the update function before trying to access any of the optimization objects. The optimize function automatically performs an update. You can find more information in the documentation of the UpdateMode.
Best regards,
Jaromił1 -
Thanks a lot for your prompt answer!!
Sincerely,
Mason
0
Post is closed for comments.
Comments
3 comments