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

@@ -238,7 +238,7 @@ DEFINE_ARGS_TO_R_CALLBACK(
#undef DEFINE_ARGS_TO_R_CALLBACK
#undef DEFINE_VOID_TO_STRING_CALLBACK
// RENAMING
// Renaming
namespace operations_research {
// Decision

View File

@@ -15,7 +15,7 @@ This repository contains several component:
@li @ref Google.OrTools.Graph "Google.OrTools.Graph"
@li @ref Google.OrTools.LinearSolver "Google.OrTools.LinearSolver"
@li @ref Google.OrTools.LinearSolver "Google.OrTools.ModelBuilder"
@li @ref Google.OrTools.ModelBuilder "Google.OrTools.ModelBuilder"
@li @ref Google.OrTools.Bop "Google.OrTools.Bop"
@li @ref Google.OrTools.Glop "Google.OrTools.Glop"
@li @ref Google.OrTools.Sat "Google.OrTools.SAT"

View File

@@ -329,9 +329,9 @@ public class ModelBuilder
private ModelBuilderHelper helper_;
private Dictionary<double, int> constantMap_;
// Used to process linear exppressions.
// Used to process linear expressions.
private SortedDictionary<int, double> tmp_var_value_map_;
private Queue<Term> tmp_terms_;
}
} // namespace Google.OrTools.ModelBuilder
} // namespace Google.OrTools.ModelBuilder

View File

@@ -125,7 +125,8 @@ public class ModelSolver
}
/// <summary>
/// The best objective value found during search. This raises a ModelSolverException is no solution has been found, or if Solve() has not been called.
/// The best objective value found during search. This raises a ModelSolverException is no solution has been found,
/// or if Solve() has not been called.
/// </summary>
public double ObjectiveValue
{
@@ -140,7 +141,8 @@ public class ModelSolver
}
/// <summary>
/// The best objective bound found during search. This raises a ModelSolverException is no solution has been found, or if Solve() has not been called.
/// The best objective bound found during search. This raises a ModelSolverException is no solution has been found,
/// or if Solve() has not been called.
/// </summary>
public double BestObjectiveBound
{
@@ -155,19 +157,20 @@ public class ModelSolver
}
/// <summary>
/// The value of a variable in the current solution. This raises a ModelSolverException is no solution has been found, or if Solve() has not been called.
/// The value of a variable in the current solution. This raises a ModelSolverException is no solution has been
/// found, or if Solve() has not been called.
/// </summary>
public double Value(Variable var)
{
if (!helper_.HasSolution())
{
throw new ModelSolverException("ModelSolver.Value())",
"Solve() was not called or no solution was found");
throw new ModelSolverException("ModelSolver.Value())", "Solve() was not called or no solution was found");
}
return helper_.VariableValue(var.Index);
}
/// <summary>
/// The reduced cost of a variable in the current solution. This raises a ModelSolverException is no solution has been found, or if Solve() has not been called.
/// The reduced cost of a variable in the current solution. This raises a ModelSolverException is no solution has
/// been found, or if Solve() has not been called.
/// </summary>
public double ReducedCost(Variable var)
{
@@ -180,7 +183,8 @@ public class ModelSolver
}
/// <summary>
/// The dual value of a linear constraint in the current solution. This raises a ModelSolverException is no solution has been found, or if Solve() has not been called.
/// The dual value of a linear constraint in the current solution. This raises a ModelSolverException is no solution
/// has been found, or if Solve() has not been called.
/// </summary>
public double DualValue(LinearConstraint ct)
{
@@ -193,7 +197,8 @@ public class ModelSolver
}
/// <summary>
/// The activity of a constraint in the current solution. This raises a ModelSolverException is no solution has been found, or if Solve() has not been called.
/// The activity of a constraint in the current solution. This raises a ModelSolverException is no solution has been
/// found, or if Solve() has not been called.
/// </summary>
public double Activity(LinearConstraint ct)
{

View File

@@ -15,12 +15,15 @@
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <limits>
#include <memory>
#include <numeric>
#include <string>
#include <vector>
#include "absl/base/attributes.h"
#include "absl/log/check.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
@@ -28,8 +31,11 @@
#include "absl/strings/str_join.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "absl/types/optional.h"
#include "ortools/base/cleanup.h"
#include "ortools/base/logging.h"
#include "ortools/base/status_macros.h"
#include "ortools/base/timer.h"
#include "ortools/gurobi/environment.h"

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]

View File

@@ -251,4 +251,3 @@
// By default vector<vector<Type>> is mapped to a jagged array i.e. .Net type[][]
// If you want a regular matrix i.e. .Net type[,] use REGULAR_MATRIX_AS_CSHARP_ARRAY instead.
%include "std_vector.i"