1/
<ilcplex/ilocplex.h>
void build_model(IloEnv &env, IloModel &model, IloCplex &cplex, int nships) {
// Your existing code here
}
int main() {
int nships = 10;
IloEnv env;
IloModel model(env);
IloCplex cplex(env);
// Build the model
build_model(env, model, cplex, nships);
// Access the existing objective function
IloObjective currentObjective = model.getObjective();
// Define a new objective function (for example, maximize instead of minimize)
IloObjective newObjective = IloMaximize(env, currentObjective.getExpr());
// Update the model with the new objective function
model.remove(currentObjective); // Remove the old objective
model.add(newObjective); // Add the new objective
// Now model has the new objective function
// Solve the modified model (if needed)
cplex.extract(model);
cplex.solve();
// Other operations with the modified model...
// Clean up
env.end();
return 0;
}
2/
<ilcplex/ilocplex.h>
void build_model(IloEnv &env, IloModel &model, IloCplex &cplex, int nships) {
// Your existing code here
IloRange wait_3_constraint = ...; // Assume you have added this constraint
wait_3_constraint.setName("wait_3");
model.add(wait_3_constraint); // Adding the constraint
}
int main() {
int nships = 10;
IloEnv env;
IloModel model(env);
IloCplex cplex(env);
// Build the model
build_model(env, model, cplex, nships);
// Delete a constraint named "wait_3" from the model
IloRangeArray constraints = model.getRangeArray();
for (IloInt i = 0; i < constraints.getSize(); ++i) {
if (constraints[i].getName() == "wait_3") {
model.remove(constraints[i]);
break; // Assuming "wait_3" is unique, we can break after removing it
}
}
// Now "wait_3" constraint is removed from the model
// Solve the modified model (if needed)
cplex.extract(model);
cplex.solve();
// Other operations with the modified model...
// Clean up
env.end();
return 0;
}