diff --git a/ortools/linear_solver/samples/BasicExample.cs b/ortools/linear_solver/samples/BasicExample.cs index 8aa512a483..6f7c4b6527 100644 --- a/ortools/linear_solver/samples/BasicExample.cs +++ b/ortools/linear_solver/samples/BasicExample.cs @@ -15,6 +15,7 @@ // [START program] // [START import] using System; +using Google.OrTools.Init; using Google.OrTools.LinearSolver; // [END import] @@ -22,11 +23,14 @@ public class BasicExample { static void Main() { + Console.WriteLine("Google.OrTools version: " + OrToolsVersion.VersionString()); + // [START solver] // Create the linear solver with the GLOP backend. Solver solver = Solver.CreateSolver("GLOP"); if (solver is null) { + Console.WriteLine("Could not create solver GLOP"); return; } // [END solver] @@ -40,10 +44,10 @@ public class BasicExample // [END variables] // [START constraints] - // Create a linear constraint, 0 <= x + y <= 2. - Constraint ct = solver.MakeConstraint(0.0, 2.0, "ct"); - ct.SetCoefficient(x, 1); - ct.SetCoefficient(y, 1); + // Create a linear constraint, x + y <= 2. + Constraint constraint = solver.MakeConstraint(double.NegativeInfinity, 2.0, "constraint"); + constraint.SetCoefficient(x, 1); + constraint.SetCoefficient(y, 1); Console.WriteLine("Number of constraints = " + solver.NumConstraints()); // [END constraints] @@ -57,15 +61,37 @@ public class BasicExample // [END objective] // [START solve] - solver.Solve(); + Console.WriteLine("Solving with " + solver.SolverVersion()); + Solver.ResultStatus resultStatus = solver.Solve(); // [END solve] // [START print_solution] + Console.WriteLine("Status: " + resultStatus); + if (resultStatus != Solver.ResultStatus.OPTIMAL) + { + Console.WriteLine("The problem does not have an optimal solution!"); + if (resultStatus == Solver.ResultStatus.FEASIBLE) + { + Console.WriteLine("A potentially suboptimal solution was found"); + } + else + { + Console.WriteLine("The solver could not solve the problem."); + return; + } + } + Console.WriteLine("Solution:"); Console.WriteLine("Objective value = " + solver.Objective().Value()); Console.WriteLine("x = " + x.SolutionValue()); Console.WriteLine("y = " + y.SolutionValue()); // [END print_solution] + + // [START advanced] + Console.WriteLine("Advanced usage:"); + Console.WriteLine("Problem solved in " + solver.WallTime() + " milliseconds"); + Console.WriteLine("Problem solved in " + solver.Iterations() + " iterations"); + // [END advanced] } } // [END program] diff --git a/ortools/linear_solver/samples/BasicExample.java b/ortools/linear_solver/samples/BasicExample.java index 563c79085a..7b80279a4f 100644 --- a/ortools/linear_solver/samples/BasicExample.java +++ b/ortools/linear_solver/samples/BasicExample.java @@ -14,8 +14,10 @@ // Minimal example to call the GLOP solver. // [START program] package com.google.ortools.linearsolver.samples; + // [START import] import com.google.ortools.Loader; +import com.google.ortools.init.OrToolsVersion; import com.google.ortools.linearsolver.MPConstraint; import com.google.ortools.linearsolver.MPObjective; import com.google.ortools.linearsolver.MPSolver; @@ -25,10 +27,19 @@ import com.google.ortools.linearsolver.MPVariable; /** Minimal Linear Programming example to showcase calling the solver. */ public final class BasicExample { public static void main(String[] args) { + // [START loader] Loader.loadNativeLibraries(); + // [END loader] + + System.out.println("Google OR-Tools version: " + OrToolsVersion.getVersionString()); + // [START solver] // Create the linear solver with the GLOP backend. MPSolver solver = MPSolver.createSolver("GLOP"); + if (solver == null) { + System.out.println("Could not create solver GLOP"); + return; + } // [END solver] // [START variables] @@ -40,8 +51,9 @@ public final class BasicExample { // [END variables] // [START constraints] - // Create a linear constraint, 0 <= x + y <= 2. - MPConstraint ct = solver.makeConstraint(0.0, 2.0, "ct"); + double infinity = Double.POSITIVE_INFINITY; + // Create a linear constraint, x + y <= 2. + MPConstraint ct = solver.makeConstraint(-infinity, 2.0, "ct"); ct.setCoefficient(x, 1); ct.setCoefficient(y, 1); @@ -57,15 +69,33 @@ public final class BasicExample { // [END objective] // [START solve] - solver.solve(); + System.out.println("Solving with " + solver.solverVersion()); + final MPSolver.ResultStatus resultStatus = solver.solve(); // [END solve] // [START print_solution] + System.out.println("Status: " + resultStatus); + if (resultStatus != MPSolver.ResultStatus.OPTIMAL) { + System.out.println("The problem does not have an optimal solution!"); + if (resultStatus == MPSolver.ResultStatus.FEASIBLE) { + System.out.println("A potentially suboptimal solution was found"); + } else { + System.out.println("The solver could not solve the problem."); + return; + } + } + System.out.println("Solution:"); System.out.println("Objective value = " + objective.value()); System.out.println("x = " + x.solutionValue()); System.out.println("y = " + y.solutionValue()); // [END print_solution] + + // [START advanced] + System.out.println("Advanced usage:"); + System.out.println("Problem solved in " + solver.wallTime() + " milliseconds"); + System.out.println("Problem solved in " + solver.iterations() + " iterations"); + // [END advanced] } private BasicExample() {} diff --git a/ortools/linear_solver/samples/basic_example.cc b/ortools/linear_solver/samples/basic_example.cc index 0f46c5440d..22e1dc77e9 100644 --- a/ortools/linear_solver/samples/basic_example.cc +++ b/ortools/linear_solver/samples/basic_example.cc @@ -11,20 +11,31 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Minimal example to call the GLOP solver. // [START program] +// Minimal example to call the GLOP solver. // [START import] +#include #include -#include +#include "absl/flags/flag.h" +#include "absl/log/flags.h" +#include "ortools/base/init_google.h" +#include "ortools/base/logging.h" +#include "ortools/init/init.h" #include "ortools/linear_solver/linear_solver.h" // [END import] namespace operations_research { void BasicExample() { + LOG(INFO) << "Google OR-Tools version : " << OrToolsVersion::VersionString(); + // [START solver] // Create the linear solver with the GLOP backend. std::unique_ptr solver(MPSolver::CreateSolver("GLOP")); + if (!solver) { + LOG(WARNING) << "Could not create solver GLOP"; + return; + } // [END solver] // [START variables] @@ -36,8 +47,9 @@ void BasicExample() { // [END variables] // [START constraints] - // Create a linear constraint, 0 <= x + y <= 2. - MPConstraint* const ct = solver->MakeRowConstraint(0.0, 2.0, "ct"); + // Create a linear constraint, x + y <= 2. + const double infinity = solver->infinity(); + MPConstraint* const ct = solver->MakeRowConstraint(-infinity, 2.0, "ct"); ct->SetCoefficient(x, 1); ct->SetCoefficient(y, 1); @@ -53,19 +65,40 @@ void BasicExample() { // [END objective] // [START solve] - solver->Solve(); + LOG(INFO) << "Solving with " << solver->SolverVersion(); + const MPSolver::ResultStatus result_status = solver->Solve(); // [END solve] // [START print_solution] - LOG(INFO) << "Solution:" << std::endl; + // Check that the problem has an optimal solution. + LOG(INFO) << "Status: " << result_status; + if (result_status != MPSolver::OPTIMAL) { + LOG(INFO) << "The problem does not have an optimal solution!"; + if (result_status == MPSolver::FEASIBLE) { + LOG(INFO) << "A potentially suboptimal solution was found"; + } else { + LOG(WARNING) << "The solver could not solve the problem."; + return; + } + } + + LOG(INFO) << "Solution:"; LOG(INFO) << "Objective value = " << objective->Value(); LOG(INFO) << "x = " << x->solution_value(); LOG(INFO) << "y = " << y->solution_value(); // [END print_solution] + + // [START advanced] + LOG(INFO) << "Advanced usage:"; + LOG(INFO) << "Problem solved in " << solver->wall_time() << " milliseconds"; + LOG(INFO) << "Problem solved in " << solver->iterations() << " iterations"; + // [END advanced] } } // namespace operations_research -int main() { +int main(int argc, char* argv[]) { + InitGoogle(argv[0], &argc, &argv, true); + absl::SetFlag(&FLAGS_stderrthreshold, 0); operations_research::BasicExample(); return EXIT_SUCCESS; } diff --git a/ortools/linear_solver/samples/basic_example.py b/ortools/linear_solver/samples/basic_example.py index 2f3530b01b..1a2fbaced0 100644 --- a/ortools/linear_solver/samples/basic_example.py +++ b/ortools/linear_solver/samples/basic_example.py @@ -15,31 +15,36 @@ """Minimal example to call the GLOP solver.""" # [START program] # [START import] +from ortools.init.python import init from ortools.linear_solver import pywraplp # [END import] def main(): + print("Google OR-Tools version:", init.OrToolsVersion.version_string()) + # [START solver] # Create the linear solver with the GLOP backend. solver = pywraplp.Solver.CreateSolver("GLOP") if not solver: + print("Could not create solver GLOP") return # [END solver] # [START variables] # Create the variables x and y. - x = solver.NumVar(0, 1, "x") - y = solver.NumVar(0, 2, "y") + x_var = solver.NumVar(0, 1, "x") + y_var = solver.NumVar(0, 2, "y") print("Number of variables =", solver.NumVariables()) # [END variables] # [START constraints] - # Create a linear constraint, 0 <= x + y <= 2. - ct = solver.Constraint(0, 2, "ct") - ct.SetCoefficient(x, 1) - ct.SetCoefficient(y, 1) + infinity = solver.infinity() + # Create a linear constraint, x + y <= 2. + constraint = solver.Constraint(-infinity, 2, "ct") + constraint.SetCoefficient(x_var, 1) + constraint.SetCoefficient(y_var, 1) print("Number of constraints =", solver.NumConstraints()) # [END constraints] @@ -47,24 +52,44 @@ def main(): # [START objective] # Create the objective function, 3 * x + y. objective = solver.Objective() - objective.SetCoefficient(x, 3) - objective.SetCoefficient(y, 1) + objective.SetCoefficient(x_var, 3) + objective.SetCoefficient(y_var, 1) objective.SetMaximization() # [END objective] # [START solve] print(f"Solving with {solver.SolverVersion()}") - solver.Solve() + result_status = solver.Solve() # [END solve] # [START print_solution] + print(f"Status: {result_status}") + if result_status != pywraplp.Solver.OPTIMAL: + print("The problem does not have an optimal solution!") + if result_status == pywraplp.Solver.FEASIBLE: + print("A potentially suboptimal solution was found") + else: + print("The solver could not solve the problem.") + return + print("Solution:") print("Objective value =", objective.Value()) - print("x =", x.solution_value()) - print("y =", y.solution_value()) + print("x =", x_var.solution_value()) + print("y =", y_var.solution_value()) # [END print_solution] + # [START advanced] + print("Advanced usage:") + print(f"Problem solved in {solver.wall_time():d} milliseconds") + print(f"Problem solved in {solver.iterations():d} iterations") + # [END advanced] + if __name__ == "__main__": + init.CppBridge.init_logging("basic_example.py") + cpp_flags = init.CppFlags() + cpp_flags.stderrthreshold = True + cpp_flags.log_prefix = False + init.CppBridge.set_flags(cpp_flags) main() # [END program] diff --git a/ortools/linear_solver/samples/code_samples.bzl b/ortools/linear_solver/samples/code_samples.bzl index 50b5bf8f90..51eae70e1b 100644 --- a/ortools/linear_solver/samples/code_samples.bzl +++ b/ortools/linear_solver/samples/code_samples.bzl @@ -21,6 +21,7 @@ def code_sample_cc(name): srcs = [name + ".cc"], deps = [ "//ortools/base", + "//ortools/init", "//ortools/linear_solver", "//ortools/linear_solver:linear_solver_cc_proto", ], @@ -33,6 +34,7 @@ def code_sample_cc(name): deps = [ ":" + name + "_cc", "//ortools/base", + "//ortools/init", "//ortools/linear_solver", "//ortools/linear_solver:linear_solver_cc_proto", ], @@ -48,6 +50,7 @@ def code_sample_py(name): requirement("protobuf"), requirement("numpy"), requirement("pandas"), + "//ortools/init/python:init", "//ortools/linear_solver/python:model_builder", ], python_version = "PY3", @@ -60,6 +63,7 @@ def code_sample_py(name): srcs = [name + ".py"], main = name + ".py", data = [ + "//ortools/init/python:init", "//ortools/linear_solver/python:model_builder", ], deps = [ @@ -80,6 +84,7 @@ def code_sample_java(name): main_class = "com.google.ortools.linearsolver.samples." + name, test_class = "com.google.ortools.linearsolver.samples." + name, deps = [ + "//ortools/init/java:init", "//ortools/linear_solver/java:modelbuilder", "//ortools/java/com/google/ortools/modelbuilder", "//ortools/java/com/google/ortools:Loader",