Retrieving Solution to a Model Copy

1,277 views
Skip to first unread message

Kate

unread,
Jun 14, 2011, 2:45:14 PM6/14/11
to Gurobi Optimization
I am trying to perform two sequential solves of the same model. The
first solve, the values for a subset of the variables are fixed to
determine the cost of the current solution using variable bounds.
Then, the variable fixings are removed by resetting the variable
bounds to the appropriate values. To solve the model the second time,
a copy of the model is created. The values of the variables from the
previous solve are set as start values for all variables. At this
point I am able to successfully set start values for the variables
using the originally defined variable objects. After solving the
model, I am unable to retrieve the solution. When trying to find the
value of a variable, the following error is reported:

Gurobi Exception caught: Error code = 10004
Var::get

What is the proper way to retrieve the solution using the defined
variable objects?

I've tried using only the initial model for both solves, but get
errors when trying to set the initial start values. The solution to
the first solve is lost when changing the variable bounds, so it is
necessary to set the initial start values.

Thanks for your help!

Greg Glockner

unread,
Jun 14, 2011, 2:54:48 PM6/14/11
to gur...@googlegroups.com
Error code 10004 is 'unknown attribute'. You may need to check the spelling of the attribute name. It could also be that you are referencing the wrong variable objects. Finally, it's inefficient to copy the entire Model object if you simply are adjusting bounds on a set of variables; better to save the values in question and modify the main Model object.

Kate Abercrombie Lindsey

unread,
Jun 14, 2011, 4:29:37 PM6/14/11
to gur...@googlegroups.com
I don't think that the problem has to do with spelling the attribute name or referencing the wrong variable because I am using the same commands as I did after the first run, which successfully accessed the variable solution.

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. 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));
}

Do you have any ideas why this error could be happening?

This is the reason I started using the copy of the model to solve the second time.

Thanks!

Greg Glockner

unread,
Jun 14, 2011, 4:50:57 PM6/14/11
to gur...@googlegroups.com
> I don't think that the problem has to do with spelling the attribute name or referencing the wrong variable because I am using the same commands as I did after the first run, which successfully accessed the variable solution.

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).

Kate Abercrombie Lindsey

unread,
Jun 14, 2011, 7:07:12 PM6/14/11
to gur...@googlegroups.com
>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.

Perhaps this addresses the question that I really need answered. I'm primarily interested in the values of a subset of variables. Take for example a variable that has been created in the original model to keep track of the transportation costs (GRBVar loadedTrailerCost). If I wanted to access the value of this variable after solving the copy of the model, how do I reference this variable individually in the new model that is the copy since accessing GRBVar loadedTrailerCost does not work?



>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).

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. I also tried saving all of the solution values at one time and then setting the start values separately. When setting the start values this way, I got the same error when trying to set the start values. So, I was able to successfully get every solution value, but on the second pass of the variables when assigning the start value the error occurred.



>One more thing may be that you've got an intervening call to the update() method (or forgotten it or something similar).
Update is not called until after all Start values are defined and the bounds are changed.

Greg Glockner

unread,
Jun 14, 2011, 8:45:24 PM6/14/11
to gur...@googlegroups.com
> Perhaps this addresses the question that I really need answered. I'm primarily interested in the values of a subset of variables. Take for example a variable that has been created in the original model to keep track of the transportation costs (GRBVar loadedTrailerCost). If I wanted to access the value of this variable after solving the copy of the model, how do I reference this variable individually in the new model that is the copy since accessing GRBVar loadedTrailerCost does not work?

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.

Reply all
Reply to author
Forward
0 new messages