Create new model in the same C++ file
OngoingI am modelling in GUROBI C++. I generate a model with several decision variables and constraints model. Next, I copy the orignal model in new_model and add new constraint refering the variables names and sets from the orignal model since understood that the entire model is copied
However, the constraint don't works in the new model. So how a copied model works for Gurobi C++ ? Are all decision variables and constraints of the orginal model are in new model?I stll use the same variable names in the new model, since don't worth copy if cannot use them.What is necessary to do in the model to make effective the new connstraint ? How can I call decision variables and constraints of new model in the while loop?
I appreciate any help!
Thank you in advance
Regards,
Alessandra
-
Hi Alessandra,
The model is copied but the variable objects that you created for the first model are still only attached to that one. You have to get the variables associated with the second model by name, as these are preserved, to use them for example using the model.getVarByName function.
Cheers,
David0 -
Thank you for your answer!
You wrote: "The model was copied but the variable objects you created for the first model are still attached only to this one.", so
I can't understand how: after using reset() for both models, I only called new_model optimization: new_model.optimize(); and part of the variables got values! The second point is that you also wrote: "You need to get the variables associated with the second model by name because they are preserved to use them." Do you mean all variables or only the ones I use in the new constraints? Could you please tell me the syntax to get VarbyName() in C++ because new_model.getbyName("x") doesn't work.Thanks in advance!
Regards
Alessandra0 -
Hi Alessandra,
The syntax is model.getVarByName
new_model.getVarByName("x")
Of course you only need to get the variables you want to use in the new constraint. Or the ones you want to query solution values later.
I can't understand how: after using reset() for both models, I only called new_model optimization: new_model.optimize(); and part of the variables got values!
This is a bit strange, I can't replicate this. Can you provide a small example showing this?
How are you copying the model? Note the important remark in the documentation GRBModel:
Note that due to the lazy update approach in Gurobi, you have to call
update
before copying it.Cheers,
David0 -
Hello David,
Thank you for your answers.
The code that referred to :
new_model.reset();
original_model.reset();
new_model.write("problem_2.lp");
new_model.optimize();
// Retrieving variables from the new_model.
try {
if (status == GRB_OPTIMAL) {
// Get the objective value
double objVal = new_model.get(GRB_DoubleAttr_ObjVal);
double run_time = new_model.get(GRB_DoubleAttr_Runtime);
double iterations = new_model.get(GRB_DoubleAttr_IterCount);
int n_sol = new_model.get(GRB_IntAttr_SolCount);
std::cout << "K: " << k << std::endl;
std::cout << "Optimal Objective Value: " << objVal << std::endl;
std::cout << "Run_time " << run_time << std::endl;
std::cout << "iterations: " << iterations << std::endl;
// Iterate over variables to get their values
GRBVar *vars = new_model.getVars();
int numVars = new_model.get(GRB_IntAttr_NumVars);
for (int t = 0; t < T; t++) {
// outFile << "Variable Values:" << std::endl;
std::cout << "t_" << t << " Variable Values : " << std::endl;
cout << "\n";
for (int i = 0; i < numVars; ++i) {
std::string varName = vars[i].get(GRB_StringAttr_VarName);
double varValue = vars[i].get(GRB_DoubleAttr_X
// int vBasis = vars[i].get(GRB_IntAttr_VBasis);
if (varValue != 0.0) {
std::cout << varName << " = " << varValue
<< "\t\t\t"; // << vBasis << "\t\t\t";
}
}
}
delete[] vars; // Clean up allocated memory
} else if (status == GRB_INFEASIBLE) {
std::cout << "Model is infeasible." << std::endl;
}
} catch (const GRBException &e) {
// Handle Gurobi-specific exceptions
std::cerr << "Error code = " << e.getErrorCode() << std::endl;
std::cerr << e.getMessage() << std::endl;
} catch (...) {
// Handle other exceptions
std::cerr << "Exception during optimization" << std::endl;
}One more question: Can the original_model after the new_model optimization running result independently run? Will the new constraints, implemented as
new_model.addQConstr(limit >= bound)
, not affect the optimization of the original_model()
? Because that is what I need. Please note that the limit is not a variable but a GRB QuadExpr, containing little expressions related to some variables. One further question: Should the "model.update()" be directly after or directly before a new insertion?Regards,
Alessandra.
0
Please sign in to leave a comment.
Comments
4 comments