This seems like an issue with C++ objects. For example, you could be inadvertently referencing GRBVar objects from one model inside the copied model. One thing to check is to write the model file as an MPS file, along with the start values as a MST file. Then read the model file and the MST file, and adjust the bounds as you expected.
> Ideally, I would not copy the entire Model. However, I haven't been able to successfully solve the model the second time with an initial solution. When I only modify the bounds on the initial Model, the solution found from the first run is not longer stored.
Correct, your code needs to save the Start values, either via an additional array of doubles or via a MST file.
> I need the solution from the first solving to be used as the initial feasible solution for the second run to help reduce the solve time. However, when I define the start values using the solution from the first solve I get the following error:
>
> Gurobi Exception caught: Error code = 20001
> Variable not in model
>
> The error occurs on variable 1707095, but there are 1852429 variables total. I am setting the start values using the following code in c++:
>
> avars= model->getVars();
> for (int i = 0; i < model->get(GRB_IntAttr_NumVars)-1; i++){
> avars[i].set(GRB_DoubleAttr_Start,avars[i].get(GRB_DoubleAttr_X));
> }
It's hard to determine precisely from this code fragment, but one of two things may be happening:
1) If this code fragment corresponds to the case when you have 2 copies of the model, then 'avars' is referring to one model - you need an array of GRBVar objects to refer to the second model.
2) If this code fragment corresponds to the case when you are reusing 1 copy of the model, then you should try to use the X values to set the Start values prior to making any modifications to the model (such as changing the bounds).
> Do you have any ideas why this error could be happening?
One more thing may be that you've got an intervening call to the update() method (or forgotten it or something similar).
As you've figured, you can't cross-reference objects from one model to the other. The only way to identify specific variables like this is to do the following steps:
1. Assign unique string identifiers to the VarName attribute of each GRBVar
2. Retrieve the full set of GRBVar objects and filter out the ones that you want
There is no built-in 'find' or 'filter' function in the C++ library, but it's not difficult to code this yourself.
Alternately, you might find it easier to save the incumbent solution in a MIP start file (.mst). This could save you a lot of coding.
> The code was in reference to the case when only one model is used. I am using the X values to set the Start values prior to any modifications to the model. I just don't understand the reason for the "Gurobi Exception caught: Error code = 20001; Variable not in model" error when no changes have been made to the model.
This error code indicates you are referencing a GRBVar object that isn't in that model. It could be a valid variable from another model in memory.