polish model_builder code and samples; tweak C# wrapping code

This commit is contained in:
Laurent Perron
2023-11-03 15:09:13 +01:00
parent 44948b1860
commit d229e48c88
15 changed files with 70 additions and 37 deletions

View File

@@ -30,7 +30,7 @@ public class AssignmentMb
int numTasks = costs.GetLength(1);
// [END data_model]
// [START model]
// [START model]
ModelBuilder model = new ModelBuilder();
// [END model]
@@ -58,7 +58,7 @@ public class AssignmentMb
{
assignedWork.Add(x[i, j]);
}
model.Add(assignedWork <= 1);
model.Add(assignedWork <= 1);
}
// Each task is assigned to exactly one worker.
@@ -69,7 +69,7 @@ public class AssignmentMb
{
assignedWorker.Add(x[i, j]);
}
model.Add(assignedWorker == 1);
model.Add(assignedWorker == 1);
}
// [END constraints]
@@ -89,9 +89,10 @@ public class AssignmentMb
// [START solver]
// Create the solver with the SCIP backend and check it is supported.
ModelSolver solver = new ModelSolver("SCIP");
if (!solver.SolverIsSupported()) return;
if (!solver.SolverIsSupported())
return;
// [END solver]
// Solve
// [START solve]
SolveStatus resultStatus = solver.Solve(model);

View File

@@ -13,21 +13,23 @@
// [START program]
package com.google.ortools.linearsolver.samples;
// [START import]
import com.google.ortools.Loader;
import com.google.ortools.modelbuilder.LinearConstraint;
import com.google.ortools.modelbuilder.LinearExpr;
import com.google.ortools.modelbuilder.LinearExprBuilder;
import com.google.ortools.modelbuilder.ModelBuilder;
import com.google.ortools.modelbuilder.ModelSolver;
import com.google.ortools.modelbuilder.SolveStatus;
import com.google.ortools.modelbuilder.Variable;
// [END import]
/** MIP example that solves an assignment problem. */
public class AssignmentMb {
public static void main(String[] args) {
Loader.loadNativeLibraries();
// Data
// [START data_model]
double[][] costs = {
@@ -92,7 +94,9 @@ public class AssignmentMb {
// [START solver]
// Create the solver with the SCIP backend and check it is supported.
ModelSolver solver = new ModelSolver("scip");
if (!solver.solverIsSupported()) return;
if (!solver.solverIsSupported()) {
return;
}
// [END solver]
// [START solve]

View File

@@ -36,6 +36,10 @@ code_sample_cc(name = "stigler_diet")
# Model Builder
code_sample_java(name = "AssignmentMb")
code_sample_java(name = "BinPackingMb")
code_sample_java(name = "CloneModelMb")
code_sample_java(name = "SimpleLpProgramMb")

View File

@@ -86,9 +86,10 @@ public class BinPackingMb
// [START solver]
// Create the solver with the SCIP backend and check it is supported.
ModelSolver solver = new ModelSolver("SCIP");
if (!solver.SolverIsSupported()) return;
if (!solver.SolverIsSupported())
return;
// [END solver]
// [START solve]
SolveStatus resultStatus = solver.Solve(model);
// [END solve]

View File

@@ -14,15 +14,16 @@
// MIP example that solves a bin packing problem.
// [START program]
package com.google.ortools.linearsolver.samples;
// [START import]
import com.google.ortools.Loader;
import com.google.ortools.modelbuilder.LinearConstraint;
import com.google.ortools.modelbuilder.LinearExpr;
import com.google.ortools.modelbuilder.LinearExprBuilder;
import com.google.ortools.modelbuilder.ModelBuilder;
import com.google.ortools.modelbuilder.ModelSolver;
import com.google.ortools.modelbuilder.SolveStatus;
import com.google.ortools.modelbuilder.Variable;
// [END import]
/** Bin packing problem. */
@@ -35,6 +36,7 @@ public class BinPackingMb {
public final int numBins = weights.length;
public final int binCapacity = 100;
}
// [END data_model]
public static void main(String[] args) throws Exception {
@@ -71,7 +73,7 @@ public class BinPackingMb {
model.addEquality(oneCopy, 1);
}
// The bin capacity contraint for bin j is
// The bin capacity constraint for bin j is
// sum_i w_i x_ij <= C*y_j
// To define this constraint, first subtract the left side from the right to get
// 0 <= C*y_j - sum_i w_i x_ij
@@ -95,7 +97,9 @@ public class BinPackingMb {
// [START solver]
// Create the solver with the SCIP backend and check it is supported.
ModelSolver solver = new ModelSolver("scip");
if (!solver.solverIsSupported()) return;
if (!solver.solverIsSupported()) {
return;
}
// [END solver]
// [START solve]

View File

@@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Minimal example to call the SCIP solver.
// Minimal example to clone a model.
// [START program]
// [START import]
using System;
@@ -63,12 +63,13 @@ public class SimpleMipProgramMb
Console.WriteLine("Number of constraints in the original model = " + model.ConstraintsCount());
Console.WriteLine("Number of constraints in the cloned model = " + modelCopy.ConstraintsCount());
// [END clone]
// [END clone]
// [START solver]
// Create the solver with the SCIP backend and checks it is supported.
ModelSolver solver = new ModelSolver("SCIP");
if (!solver.SolverIsSupported()) return;
// Create the solver with the CP-SAT backend and checks it is supported.
ModelSolver solver = new ModelSolver("sat");
if (!solver.SolverIsSupported())
return;
// [END solver]
// [START solve]

View File

@@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Minimal example to call the MIP solver.
// Minimal example to clone a model.
// [START program]
package com.google.ortools.linearsolver.samples;
@@ -75,9 +75,11 @@ public final class CloneModelMb {
// [END clone]
// [START solver]
// Create the solver with the SCIP backend and check it is supported.
ModelSolver solver = new ModelSolver("scip");
if (!solver.solverIsSupported()) return;
// Create the solver with the CP-SAT backend and check it is supported.
ModelSolver solver = new ModelSolver("sat");
if (!solver.solverIsSupported()) {
return;
}
// [END solver]
// [START solve]

View File

@@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Minimal example to call the SCIP solver.
// Minimal example to solve a MIP problem.
// [START program]
// [START import]
using System;

View File

@@ -56,9 +56,15 @@ public final class SimpleMipProgramMb {
model.maximize(LinearExpr.newBuilder().add(x).addTerm(y, 10.0));
// [END objective]
// [START solve]
// Solve with the SCIP MIP solver.
// [START solver]
// Create the solver with the SCIP backend and check it is supported.
ModelSolver solver = new ModelSolver("scip");
if (!solver.solverIsSupported()) {
return;
}
// [END solver]
// [START solve]
final SolveStatus status = solver.solve(model);
// [END solve]