From d4e68ff40033e1ebeb6c372a044d513d4c3999cc Mon Sep 17 00:00:00 2001 From: Corentin Le Molgat Date: Wed, 20 Oct 2021 11:39:23 +0200 Subject: [PATCH] Sync Google with github --- ortools/sat/samples/BUILD | 2 ++ ortools/sat/samples/CpIsFunSat.java | 20 ++++++++---- ortools/sat/samples/CpSatExample.cs | 10 +++--- ortools/sat/samples/CpSatExample.csproj | 24 ++++++++++++++ ortools/sat/samples/CpSatExample.java | 17 ++++++---- ortools/sat/samples/cp_is_fun_sat.cc | 17 +++++++--- ortools/sat/samples/cp_is_fun_sat.py | 37 ++++++++++++---------- ortools/sat/samples/cp_sat_example.cc | 15 +++++---- ortools/sat/samples/cp_sat_example.py | 10 +++--- ortools/sat/samples/minimal_jobshop_sat.cc | 9 +++--- 10 files changed, 105 insertions(+), 56 deletions(-) create mode 100644 ortools/sat/samples/CpSatExample.csproj diff --git a/ortools/sat/samples/BUILD b/ortools/sat/samples/BUILD index a2081d1b15..15fff3f776 100644 --- a/ortools/sat/samples/BUILD +++ b/ortools/sat/samples/BUILD @@ -14,6 +14,8 @@ code_sample_cc(name = "copy_model_sample_sat") code_sample_cc(name = "cp_is_fun_sat") +code_sample_cc(name = "cp_sat_example") + code_sample_cc(name = "earliness_tardiness_cost_sample_sat") code_sample_cc(name = "interval_sample_sat") diff --git a/ortools/sat/samples/CpIsFunSat.java b/ortools/sat/samples/CpIsFunSat.java index cdc90f8ed6..1422a91b78 100644 --- a/ortools/sat/samples/CpIsFunSat.java +++ b/ortools/sat/samples/CpIsFunSat.java @@ -13,17 +13,18 @@ // [START program] package com.google.ortools.sat.samples; - +// [START import] import com.google.ortools.Loader; import com.google.ortools.sat.CpModel; import com.google.ortools.sat.CpSolver; import com.google.ortools.sat.CpSolverSolutionCallback; import com.google.ortools.sat.IntVar; import com.google.ortools.sat.LinearExpr; +// [END import] /** Cryptarithmetic puzzle. */ -public class CpIsFunSat { - // [START solution_printing] +public final class CpIsFunSat { + // [START solution_printer] static class VarArraySolutionPrinter extends CpSolverSolutionCallback { public VarArraySolutionPrinter(IntVar[] variables) { variableArray = variables; @@ -45,12 +46,14 @@ public class CpIsFunSat { private int solutionCount; private final IntVar[] variableArray; } - // [END solution_printing] + // [END solution_printer] public static void main(String[] args) throws Exception { Loader.loadNativeLibraries(); // Create the model. + // [START model] CpModel model = new CpModel(); + // [END model] // [START variables] int base = 10; @@ -69,8 +72,8 @@ public class CpIsFunSat { IntVar[] letters = new IntVar[] {c, p, i, s, f, u, n, t, r, e}; // [END variables] - // [START constraints] // Define constraints. + // [START constraints] model.addAllDifferent(letters); // CP + IS + FUN = TRUE @@ -80,8 +83,8 @@ public class CpIsFunSat { 0); // [END constraints] - // [START solve] // Create a solver and solve the model. + // [START solve] CpSolver solver = new CpSolver(); VarArraySolutionPrinter cb = new VarArraySolutionPrinter(letters); // Tell the solver to enumerate all solutions. @@ -90,11 +93,16 @@ public class CpIsFunSat { solver.solve(model, cb); // [END solve] + // Statistics. + // [START statistics] System.out.println("Statistics"); System.out.println(" - conflicts : " + solver.numConflicts()); System.out.println(" - branches : " + solver.numBranches()); System.out.println(" - wall time : " + solver.wallTime() + " s"); System.out.println(" - solutions : " + cb.getSolutionCount()); + // [END statistics] } + + private CpIsFunSat() {} } // [END program] diff --git a/ortools/sat/samples/CpSatExample.cs b/ortools/sat/samples/CpSatExample.cs index 2e01c1d761..a6d93100b1 100644 --- a/ortools/sat/samples/CpSatExample.cs +++ b/ortools/sat/samples/CpSatExample.cs @@ -29,7 +29,7 @@ public class CpSatExample // Creates the variables. // [START variables] - int varUpperBound = new int[]{50, 45, 37}.Max(); + int varUpperBound = new int[] { 50, 45, 37 }.Max(); IntVar x = model.NewIntVar(0, varUpperBound, "x"); IntVar y = model.NewIntVar(0, varUpperBound, "y"); @@ -38,13 +38,13 @@ public class CpSatExample // Creates the constraints. // [START constraints] - model.Add(2*x + 7*y + 3*z <= 50); - model.Add(3*x - 5*y + 7*z <= 45); - model.Add(5*x + 2*y - 6*z <= 37); + model.Add(2 * x + 7 * y + 3 * z <= 50); + model.Add(3 * x - 5 * y + 7 * z <= 45); + model.Add(5 * x + 2 * y - 6 * z <= 37); // [END constraints] // [START objective] - model.Maximize(2*x + 2*y + 3*z); + model.Maximize(2 * x + 2 * y + 3 * z); // [END objective] // Creates a solver and solves the model. diff --git a/ortools/sat/samples/CpSatExample.csproj b/ortools/sat/samples/CpSatExample.csproj new file mode 100644 index 0000000000..a6904c5704 --- /dev/null +++ b/ortools/sat/samples/CpSatExample.csproj @@ -0,0 +1,24 @@ + + + Exe + 7.3 + netcoreapp3.1 + false + + LatestMajor + ../../../temp_dotnet/packages;$(RestoreSources);https://api.nuget.org/v3/index.json + Google.OrTools.CpSatExample + true + + + + full + true + true + + + + + + + diff --git a/ortools/sat/samples/CpSatExample.java b/ortools/sat/samples/CpSatExample.java index d78a4894be..0b9ec90e96 100644 --- a/ortools/sat/samples/CpSatExample.java +++ b/ortools/sat/samples/CpSatExample.java @@ -14,18 +14,19 @@ // [START program] package com.google.ortools.sat.samples; // [START import] +import static java.util.Arrays.stream; + import com.google.ortools.Loader; import com.google.ortools.sat.CpModel; import com.google.ortools.sat.CpSolver; import com.google.ortools.sat.CpSolverStatus; import com.google.ortools.sat.IntVar; import com.google.ortools.sat.LinearExpr; -import java.util.Arrays; // [END import] /** Minimal CP-SAT example to showcase calling the solver. */ -public class CpSatExample { - public static void main(String[] args) throws Exception { +public final class CpSatExample { + public static void main(String[] args) { Loader.loadNativeLibraries(); // Create the model. // [START model] @@ -34,7 +35,7 @@ public class CpSatExample { // Create the variables. // [START variables] - int varUpperBound = Arrays.stream(new int[] {50, 45, 37}).max().getAsInt(); + int varUpperBound = stream(new int[] {50, 45, 37}).max().getAsInt(); IntVar x = model.newIntVar(0, varUpperBound, "x"); IntVar y = model.newIntVar(0, varUpperBound, "y"); @@ -44,12 +45,12 @@ public class CpSatExample { // Create the constraints. // [START constraints] model.addLessOrEqual(LinearExpr.scalProd(new IntVar[] {x, y, z}, new int[] {2, 7, 3}), 50); - model.addLessOrEqual(LinearExpr.scalProd(new IntVar[] {x, y, z},new int[] {3, -5, 7}), 45); - model.addLessOrEqual(LinearExpr.scalProd(new IntVar[] {x, y, z},new int[] {5, 2, -6}), 37); + model.addLessOrEqual(LinearExpr.scalProd(new IntVar[] {x, y, z}, new int[] {3, -5, 7}), 45); + model.addLessOrEqual(LinearExpr.scalProd(new IntVar[] {x, y, z}, new int[] {5, 2, -6}), 37); // [END constraints] // [START objective] - model.maximize(LinearExpr.scalProd(new IntVar[] {x, y, z},new int[] {2, 2, 3})); + model.maximize(LinearExpr.scalProd(new IntVar[] {x, y, z}, new int[] {2, 2, 3})); // [END objective] // Create a solver and solve the model. @@ -77,5 +78,7 @@ public class CpSatExample { System.out.printf(" wall time: %f s%n", solver.wallTime()); // [END statistics] } + + private CpSatExample() {} } // [END program] diff --git a/ortools/sat/samples/cp_is_fun_sat.cc b/ortools/sat/samples/cp_is_fun_sat.cc index 1148001990..8dfb32f015 100644 --- a/ortools/sat/samples/cp_is_fun_sat.cc +++ b/ortools/sat/samples/cp_is_fun_sat.cc @@ -18,20 +18,23 @@ // where each letter represents a unique digit. // // This problem has 72 different solutions in base 10. - +// [START import] #include #include #include "ortools/sat/cp_model.h" #include "ortools/sat/model.h" #include "ortools/sat/sat_parameters.pb.h" +// [END import] namespace operations_research { namespace sat { void CPIsFunSat() { // Instantiate the solver. + // [START model] CpModelBuilder cp_model; + // [END model] // [START variables] const int64_t kBase = 10; @@ -64,7 +67,7 @@ void CPIsFunSat() { {kBase * kBase * kBase, kBase * kBase, kBase, 1})); // [END constraints] - // [START solution_printing] + // [START solution_printer] Model model; int num_solutions = 0; model.Add(NewFeasibleSolutionObserver([&](const CpSolverResponse& response) { @@ -81,7 +84,7 @@ void CPIsFunSat() { << "E=" << SolutionIntegerValue(response, e); num_solutions++; })); - // [END solution_printing] + // [END solution_printer] // [START solve] // Tell the solver to enumerate all solutions. @@ -92,15 +95,19 @@ void CPIsFunSat() { const CpSolverResponse response = SolveCpModel(cp_model.Build(), &model); LOG(INFO) << "Number of solutions found: " << num_solutions; // [END solve] + + // Statistics. + // [START statistics] + LOG(INFO) << "Statistics"; + LOG(INFO) << CpSolverResponseStats(response); + // [END statistics] } } // namespace sat } // namespace operations_research -// ----- MAIN ----- int main(int argc, char** argv) { operations_research::sat::CPIsFunSat(); - return EXIT_SUCCESS; } // [END program] diff --git a/ortools/sat/samples/cp_is_fun_sat.py b/ortools/sat/samples/cp_is_fun_sat.py index 883aa643b5..5454ea5a1b 100755 --- a/ortools/sat/samples/cp_is_fun_sat.py +++ b/ortools/sat/samples/cp_is_fun_sat.py @@ -19,13 +19,12 @@ where each letter represents a unique digit. This problem has 72 different solutions in base 10. """ - -# [START program] - +# [START import] from ortools.sat.python import cp_model +# [END import] -# [START solution_printing] +# [START solution_printer] class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback): """Print intermediate solutions.""" @@ -42,13 +41,15 @@ class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback): def solution_count(self): return self.__solution_count - # [END solution_printing] + # [END solution_printer] -def CPIsFunSat(): +def main(): """Solve the CP+IS+FUN==TRUE cryptarithm.""" # Constraint programming engine + # [START model] model = cp_model.CpModel() + # [END model] # [START variables] base = 10 @@ -71,8 +72,8 @@ def CPIsFunSat(): assert base >= len(letters) # [END variables] - # [START constraints] # Define constraints. + # [START constraints] model.AddAllDifferent(letters) # CP + IS + FUN = TRUE @@ -80,8 +81,8 @@ def CPIsFunSat(): n == t * base * base * base + r * base * base + u * base + e) # [END constraints] + # Creates a solver and solves the model. # [START solve] - ### Solve model. solver = cp_model.CpSolver() solution_printer = VarArraySolutionPrinter(letters) # Enumerate all solutions. @@ -90,15 +91,17 @@ def CPIsFunSat(): status = solver.Solve(model, solution_printer) # [END solve] - print() - print('Statistics') - print(' - status : %s' % solver.StatusName(status)) - print(' - conflicts : %i' % solver.NumConflicts()) - print(' - branches : %i' % solver.NumBranches()) - print(' - wall time : %f s' % solver.WallTime()) - print(' - solutions found : %i' % solution_printer.solution_count()) + # Statistics. + # [START statistics] + print('\nStatistics') + print(f' status : {solver.StatusName(status)}') + print(f' conflicts: {solver.NumConflicts()}') + print(f' branches : {solver.NumBranches()}') + print(f' wall time: {solver.WallTime()} s') + print(f' sol found: {solution_printer.solution_count()}') + # [END statistics] if __name__ == '__main__': - CPIsFunSat() - # [END probram] + main() +# [END program] diff --git a/ortools/sat/samples/cp_sat_example.cc b/ortools/sat/samples/cp_sat_example.cc index 1372de44a9..60c645e2cd 100644 --- a/ortools/sat/samples/cp_sat_example.cc +++ b/ortools/sat/samples/cp_sat_example.cc @@ -14,6 +14,7 @@ // [START program] // [START import] #include + #include "ortools/sat/cp_model.h" // [END import] @@ -34,13 +35,13 @@ void CpSatExample() { // [END variables] // [START constraints] - cp_model.AddLessOrEqual(LinearExpr::ScalProd({x, y, z},{2, 7, 3}), 50); - cp_model.AddLessOrEqual(LinearExpr::ScalProd({x, y, z},{3, -5, 7}), 45); - cp_model.AddLessOrEqual(LinearExpr::ScalProd({x, y, z},{5, 2, -6}), 37); + cp_model.AddLessOrEqual(LinearExpr::ScalProd({x, y, z}, {2, 7, 3}), 50); + cp_model.AddLessOrEqual(LinearExpr::ScalProd({x, y, z}, {3, -5, 7}), 45); + cp_model.AddLessOrEqual(LinearExpr::ScalProd({x, y, z}, {5, 2, -6}), 37); // [END constraints] // [START objective] - cp_model.Maximize(LinearExpr::ScalProd({x, y, z},{2, 2, 3})); + cp_model.Maximize(LinearExpr::ScalProd({x, y, z}, {2, 2, 3})); // [END objective] // Solving part. @@ -49,9 +50,11 @@ void CpSatExample() { // [END solve] // [START print_solution] - if (response.status() == CpSolverStatus::OPTIMAL || response.status() == CpSolverStatus::FEASIBLE) { + if (response.status() == CpSolverStatus::OPTIMAL || + response.status() == CpSolverStatus::FEASIBLE) { // Get the value of x in the solution. - LOG(INFO) << "Maximum of objective function: " << response.objective_value(); + LOG(INFO) << "Maximum of objective function: " + << response.objective_value(); LOG(INFO) << "x = " << SolutionIntegerValue(response, x); LOG(INFO) << "y = " << SolutionIntegerValue(response, y); LOG(INFO) << "z = " << SolutionIntegerValue(response, z); diff --git a/ortools/sat/samples/cp_sat_example.py b/ortools/sat/samples/cp_sat_example.py index 4ee567fb45..49046b4f88 100755 --- a/ortools/sat/samples/cp_sat_example.py +++ b/ortools/sat/samples/cp_sat_example.py @@ -11,7 +11,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - # [START program] """Simple solve.""" # [START import] @@ -36,13 +35,13 @@ def main(): # Creates the constraints. # [START constraints] - model.Add(2*x + 7*y + 3*z <= 50) - model.Add(3*x - 5*y + 7*z <= 45) - model.Add(5*x + 2*y - 6*z <= 37) + model.Add(2 * x + 7 * y + 3 * z <= 50) + model.Add(3 * x - 5 * y + 7 * z <= 45) + model.Add(5 * x + 2 * y - 6 * z <= 37) # [END constraints] # [START objective] - model.Maximize(2*x + 2*y + 3*z) + model.Maximize(2 * x + 2 * y + 3 * z) # [END objective] # Creates a solver and solves the model. @@ -64,6 +63,7 @@ def main(): # Statistics. # [START statistics] print('\nStatistics') + print(f' status : {solver.StatusName(status)}') print(f' conflicts: {solver.NumConflicts()}') print(f' branches : {solver.NumBranches()}') print(f' wall time: {solver.WallTime()} s') diff --git a/ortools/sat/samples/minimal_jobshop_sat.cc b/ortools/sat/samples/minimal_jobshop_sat.cc index a8a3fce25e..9c154c7ec0 100644 --- a/ortools/sat/samples/minimal_jobshop_sat.cc +++ b/ortools/sat/samples/minimal_jobshop_sat.cc @@ -152,11 +152,10 @@ void MinimalJobshopSat() { TaskID key = std::make_tuple(job_id, task_id); int64_t start = SolutionIntegerValue(response, all_tasks[key].start); assigned_jobs[machine].push_back( - AssignedTaskType{ - /*.job_id=*/job_id, - /*.task_id=*/task_id, - /*.start=*/start, - /*.duration=*/duration}); + AssignedTaskType{/*.job_id=*/job_id, + /*.task_id=*/task_id, + /*.start=*/start, + /*.duration=*/duration}); } }