gurobi enviroment,model and C++ header file issue
Hi all, I meet a issue about GRBModel in a C++ header file. Let me use TSP to illustrate my problem.
I am writing a TSP model in main.cpp file.
And a cutting plane struct in CutGeneration.h header file.
To find a cutting plane, I need to solve an optimization problem with Gurobi( I know for TSP we can write algorithm instead of Gurobi model, but my problem really needs Gurobi ).
To avoid reading gurobi license too many times, I try to use the same gurobi enviroment from main.cpp file to create models.
But in that case I need to access that GRBEnv variable from main.cpp file, so I try to declare a gurobi model without setting enviroment first. Then define a initilization function in struc. Then pass GRBEnv to the struc through initilization function.
But error happens in attached figure. I am not an expert of C++..., so can you help me on this?
Because in my understanding, I can declare a GurobiModel( which is essentially a class object ), then construct details latter(using passed GRBEnv). What is wrong with my approach?
What your code is implicitly doing is to create a "model" object with the default constructor. Then, you are creating another GRBModel object passing the "env" to it and assign this using the assignment operator "=" to the existing "model". But the assignment operator for GRBModel objects is not implemented (Gurobi does not support this operation for good reason).
What you need to do instead is to use pointers. Declare your "model" variable to be a pointer to a GRBModel object, i.e.,
Then, in your "inti" function, use the "new" operator:
model = new GRBModel(env);
Finally, you need to make sure that you destruct the "model" pointer again using the "delete" operator when you don't use it anymore.
But there might be a simpler solution. If you need to solve a Gurobi model inside your cut separation procedure, you can just use local variables to do this. Like this:
void separatecuts(GRBEnv env)
GRBModel model = GRBModel(env);
... use model ...
The last time I wrote C++ is already quite a while ago for me, but I still hope that what I said is correct.
Thanks a lot! Now on some small instances my code works.
Just one quick question.. When the following code is excuted,
model = new GRBModel(env);
Every time the system allocate some new location to pointer model, but we have no idea what that location previously stored( as we are dynamically allocate and delete model ).
Is it safe to just use the new location or should we clear everything that originally stored at new location?
I did not find any gurobi function that clears variables and constraints in some GRBModel... what should I do?0
Please sign in to leave a comment.