tweak model_builder java cloning API

This commit is contained in:
Laurent Perron
2023-10-25 15:38:06 +02:00
parent 08e53974c7
commit f5c865d6ab
2 changed files with 9 additions and 5 deletions

View File

@@ -55,22 +55,24 @@ public final class CloneModelMb {
model.maximize(LinearExpr.newBuilder().add(x).addTerm(y, 10.0)); model.maximize(LinearExpr.newBuilder().add(x).addTerm(y, 10.0));
// [END objective] // [END objective]
// Deep copy // [Start clone]
// Clone the model.
System.out.println("Cloning the model"); System.out.println("Cloning the model");
ModelBuilder modelCopy = model.clone(); ModelBuilder modelCopy = model.getClone();
Variable xCopy = modelCopy.varFromIndex(x.getIndex()); Variable xCopy = modelCopy.varFromIndex(x.getIndex());
Variable yCopy = modelCopy.varFromIndex(y.getIndex()); Variable yCopy = modelCopy.varFromIndex(y.getIndex());
Variable zCopy = modelCopy.newBoolVar("z"); Variable zCopy = modelCopy.newBoolVar("z");
LinearConstraint c2Copy = modelCopy.constraintFromIndex(c2.getIndex()); LinearConstraint c2Copy = modelCopy.constraintFromIndex(c2.getIndex());
// Add new constraint. // Add a new constraint.
LinearConstraint c3Copy = modelCopy.addGreaterOrEqual(xCopy, 1); LinearConstraint unusedC3Copy = modelCopy.addGreaterOrEqual(xCopy, 1);
// Modify a constraint. // Modify a constraint.
c2Copy.addTerm(zCopy, 2.0); c2Copy.addTerm(zCopy, 2.0);
System.out.println("Number of constraints in the original model = " + model.numConstraints()); System.out.println("Number of constraints in the original model = " + model.numConstraints());
System.out.println("Number of constraints in the cloned model = " + modelCopy.numConstraints()); System.out.println("Number of constraints in the cloned model = " + modelCopy.numConstraints());
// [END clone]
// [START solve] // [START solve]
// Solve with the SCIP MIP solver. // Solve with the SCIP MIP solver.

View File

@@ -46,7 +46,8 @@ def main():
model.maximize(x + 10 * y) model.maximize(x + 10 * y)
# [END objective] # [END objective]
# Deep copy. # [Start clone]
# Clone the model.
print("Cloning the model.") print("Cloning the model.")
model_copy = model.clone() model_copy = model.clone()
x_copy = model_copy.var_from_index(x.index) x_copy = model_copy.var_from_index(x.index)
@@ -61,6 +62,7 @@ def main():
# Modify a constraint. # Modify a constraint.
c2_copy.add_term(z_copy, 2.0) c2_copy.add_term(z_copy, 2.0)
# [END clone]
# [START solve] # [START solve]
# Create the solver with the SCIP backend, and solve the model. # Create the solver with the SCIP backend, and solve the model.