diff --git a/examples/cpp/BUILD.bazel b/examples/cpp/BUILD.bazel index ca10bd0bca..96783238d4 100644 --- a/examples/cpp/BUILD.bazel +++ b/examples/cpp/BUILD.bazel @@ -1,3 +1,16 @@ +# Copyright 2010-2022 Google LLC +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + cc_binary( name = "binpacking_2d_sat", srcs = [ diff --git a/examples/cpp/costas_array_sat.cc b/examples/cpp/costas_array_sat.cc index 334faf3d2a..cefcf202e1 100644 --- a/examples/cpp/costas_array_sat.cc +++ b/examples/cpp/costas_array_sat.cc @@ -27,6 +27,7 @@ #include #include #include +#include #include "absl/flags/flag.h" #include "absl/strings/str_cat.h" diff --git a/examples/cpp/course_scheduling.cc b/examples/cpp/course_scheduling.cc index 9cce42e037..fcb682b36f 100644 --- a/examples/cpp/course_scheduling.cc +++ b/examples/cpp/course_scheduling.cc @@ -15,6 +15,8 @@ #include #include +#include +#include #include #include "absl/container/flat_hash_set.h" diff --git a/examples/cpp/course_scheduling.h b/examples/cpp/course_scheduling.h index 1de03e93f1..98f8dfabb9 100644 --- a/examples/cpp/course_scheduling.h +++ b/examples/cpp/course_scheduling.h @@ -15,6 +15,7 @@ #define OR_TOOLS_EXAMPLES_COURSE_SCHEDULING_H_ #include +#include #include #include "absl/container/flat_hash_set.h" diff --git a/examples/cpp/dobble_ls.cc b/examples/cpp/dobble_ls.cc index 6643d6a1d7..1da50046b6 100644 --- a/examples/cpp/dobble_ls.cc +++ b/examples/cpp/dobble_ls.cc @@ -32,6 +32,8 @@ #include #include #include +#include +#include #include #include "absl/random/random.h" diff --git a/examples/cpp/golomb_sat.cc b/examples/cpp/golomb_sat.cc index 99210fcca2..d966b7f10c 100644 --- a/examples/cpp/golomb_sat.cc +++ b/examples/cpp/golomb_sat.cc @@ -26,6 +26,7 @@ #include #include +#include #include "absl/flags/flag.h" #include "absl/strings/str_format.h" diff --git a/examples/cpp/integer_programming.cc b/examples/cpp/integer_programming.cc index 1c65696f50..a70082467d 100644 --- a/examples/cpp/integer_programming.cc +++ b/examples/cpp/integer_programming.cc @@ -13,6 +13,9 @@ // Integer programming example that shows how to use the API. +#include +#include + #include "absl/strings/match.h" #include "absl/strings/string_view.h" #include "ortools/base/init_google.h" diff --git a/examples/cpp/linear_assignment_api.cc b/examples/cpp/linear_assignment_api.cc index 1675711eea..3125253672 100644 --- a/examples/cpp/linear_assignment_api.cc +++ b/examples/cpp/linear_assignment_api.cc @@ -12,6 +12,7 @@ // limitations under the License. #include +#include #include "ortools/base/init_google.h" #include "ortools/base/logging.h" diff --git a/examples/cpp/linear_programming.cc b/examples/cpp/linear_programming.cc index e519486772..ce51d33524 100644 --- a/examples/cpp/linear_programming.cc +++ b/examples/cpp/linear_programming.cc @@ -14,6 +14,8 @@ // Linear programming example that shows how to use the API. #include +#include +#include #include "absl/flags/flag.h" #include "absl/strings/match.h" diff --git a/examples/cpp/magic_sequence_sat.cc b/examples/cpp/magic_sequence_sat.cc index 8ef5268b6b..3c086f1b5a 100644 --- a/examples/cpp/magic_sequence_sat.cc +++ b/examples/cpp/magic_sequence_sat.cc @@ -18,6 +18,9 @@ #include #include +#include +#include +#include #include "absl/flags/flag.h" #include "absl/strings/str_format.h" diff --git a/examples/cpp/pdptw.cc b/examples/cpp/pdptw.cc index 31e81f4548..8e9c4b0ff0 100644 --- a/examples/cpp/pdptw.cc +++ b/examples/cpp/pdptw.cc @@ -36,8 +36,11 @@ // Reads data in the format defined by Li & Lim // (https://www.sintef.no/projectweb/top/pdptw/li-lim-benchmark/documentation/). +#include #include #include +#include +#include #include #include @@ -48,6 +51,7 @@ #include "google/protobuf/text_format.h" #include "ortools/base/commandlineflags.h" #include "ortools/base/file.h" +#include "ortools/base/helpers.h" #include "ortools/base/init_google.h" #include "ortools/base/mathutil.h" #include "ortools/base/timer.h" diff --git a/examples/cpp/print_dimacs_assignment.h b/examples/cpp/print_dimacs_assignment.h index a209fc6985..b25fded577 100644 --- a/examples/cpp/print_dimacs_assignment.h +++ b/examples/cpp/print_dimacs_assignment.h @@ -24,6 +24,7 @@ #include "absl/status/status.h" #include "absl/strings/str_format.h" #include "ortools/base/file.h" +#include "ortools/base/helpers.h" #include "ortools/base/logging.h" #include "ortools/graph/ebert_graph.h" #include "ortools/graph/linear_assignment.h" diff --git a/examples/cpp/sat_runner.cc b/examples/cpp/sat_runner.cc index a118062d07..1003dde440 100644 --- a/examples/cpp/sat_runner.cc +++ b/examples/cpp/sat_runner.cc @@ -19,7 +19,6 @@ #include #include "absl/flags/flag.h" -#include "absl/memory/memory.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" @@ -31,10 +30,10 @@ #include "examples/cpp/sat_cnf_reader.h" #include "google/protobuf/text_format.h" #include "ortools/algorithms/sparse_permutation.h" -#include "ortools/base/file.h" +#include "ortools/base/flags.h" +#include "ortools/base/helpers.h" #include "ortools/base/init_google.h" #include "ortools/base/logging.h" -#include "ortools/base/logging_flags.h" #include "ortools/base/timer.h" #include "ortools/linear_solver/linear_solver.pb.h" #include "ortools/lp_data/lp_data.h" @@ -335,7 +334,7 @@ int Run() { } if (result == SatSolver::LIMIT_REACHED) { if (absl::GetFlag(FLAGS_qmaxsat)) { - solver = absl::make_unique(); + solver = std::make_unique(); solver->SetParameters(parameters); CHECK(LoadBooleanProblem(problem, solver.get())); result = SolveWithCardinalityEncoding(STDOUT_LOG, problem, solver.get(), diff --git a/examples/cpp/shift_minimization_sat.cc b/examples/cpp/shift_minimization_sat.cc index fc7b806601..4fcfe93643 100644 --- a/examples/cpp/shift_minimization_sat.cc +++ b/examples/cpp/shift_minimization_sat.cc @@ -26,6 +26,7 @@ // - The objective it to minimize the number of active workers, while // performing all the jobs. +#include #include #include #include diff --git a/examples/cpp/sports_scheduling_sat.cc b/examples/cpp/sports_scheduling_sat.cc index b7ae350621..9a27225666 100644 --- a/examples/cpp/sports_scheduling_sat.cc +++ b/examples/cpp/sports_scheduling_sat.cc @@ -43,6 +43,9 @@ // We also introduces a variable at_home[d][i] which is true if team i // plays any opponent at home on day d. +#include +#include + #include "absl/strings/str_cat.h" #include "absl/strings/str_format.h" #include "absl/strings/str_join.h" diff --git a/examples/cpp/strawberry_fields_with_column_generation.cc b/examples/cpp/strawberry_fields_with_column_generation.cc index 2c0933fe0e..40669efa91 100644 --- a/examples/cpp/strawberry_fields_with_column_generation.cc +++ b/examples/cpp/strawberry_fields_with_column_generation.cc @@ -55,6 +55,7 @@ #include // strlen #include #include +#include #include #include #include diff --git a/examples/cpp/vector_bin_packing_solver.cc b/examples/cpp/vector_bin_packing_solver.cc index d975916500..66601d26e0 100644 --- a/examples/cpp/vector_bin_packing_solver.cc +++ b/examples/cpp/vector_bin_packing_solver.cc @@ -12,6 +12,7 @@ // limitations under the License. #include +#include #include "absl/flags/flag.h" #include "absl/status/status.h" @@ -21,6 +22,7 @@ #include "absl/strings/string_view.h" #include "ortools/base/commandlineflags.h" #include "ortools/base/file.h" +#include "ortools/base/helpers.h" #include "ortools/base/init_google.h" #include "ortools/base/logging.h" #include "ortools/base/timer.h" diff --git a/examples/cpp/weighted_tardiness_sat.cc b/examples/cpp/weighted_tardiness_sat.cc index 78946c1b79..8bde2fc951 100644 --- a/examples/cpp/weighted_tardiness_sat.cc +++ b/examples/cpp/weighted_tardiness_sat.cc @@ -13,6 +13,7 @@ #include +#include #include #include #include diff --git a/examples/dotnet/BalanceGroupSat.cs b/examples/dotnet/BalanceGroupSat.cs index 1ee4fb8b36..74d679a92b 100644 --- a/examples/dotnet/BalanceGroupSat.cs +++ b/examples/dotnet/BalanceGroupSat.cs @@ -75,7 +75,7 @@ public class BalanceGroupSat model.AddLinearConstraint(LinearExpr.Sum(itemsInGroup), numItemsPerGroup, numItemsPerGroup); } - //# One item must belong to exactly one group. + // # One item must belong to exactly one group. foreach (var item in allItems) { var groupsForItem = allGroups.Select(x => itemInGroup[item, x]).ToArray(); diff --git a/examples/tests/LinearSolverTest.java b/examples/tests/LinearSolverTest.java index 2d75796e7c..455aae6b03 100644 --- a/examples/tests/LinearSolverTest.java +++ b/examples/tests/LinearSolverTest.java @@ -334,8 +334,8 @@ public final class LinearSolverTest { final MPSolver solver = new MPSolver("Solver", problemType); assertNotNull(solver); - final MPVariable x1 = solver.makeNumVar(1.0, 10.0, "x1"); - final MPVariable x2 = solver.makeNumVar(1.0, 10.0, "x2"); + final MPVariable x1 = solver.makeIntVar(1.0, 10.0, "x1"); + final MPVariable x2 = solver.makeIntVar(1.0, 10.0, "x2"); final MPConstraint ct = solver.makeConstraint(0, 4.0); ct.setCoefficient(x1, 1); diff --git a/examples/tests/ModelBuilderTest.java b/examples/tests/ModelBuilderTest.java index 58e89f02f7..10390ff1c8 100644 --- a/examples/tests/ModelBuilderTest.java +++ b/examples/tests/ModelBuilderTest.java @@ -16,6 +16,7 @@ package com.google.ortools.modelbuilder; import static com.google.common.truth.Truth.assertThat; import com.google.ortools.Loader; +import java.time.Duration; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -29,7 +30,7 @@ public final class ModelBuilderTest { public void runMinimalLinearExample_ok() { ModelBuilder model = new ModelBuilder(); model.setName("minimal_linear_example"); - double infinity = java.lang.Double.POSITIVE_INFINITY; + double infinity = Double.POSITIVE_INFINITY; Variable x1 = model.newNumVar(0.0, infinity, "x1"); Variable x2 = model.newNumVar(0.0, infinity, "x2"); Variable x3 = model.newNumVar(0.0, infinity, "x3"); @@ -62,6 +63,7 @@ public final class ModelBuilderTest { ModelSolver solver = new ModelSolver("glop"); assertThat(solver.solverIsSupported()).isTrue(); + solver.setTimeLimit(Duration.ofSeconds(1)); assertThat(solver.solve(model)).isEqualTo(SolveStatus.OPTIMAL); assertThat(solver.getObjectiveValue()) @@ -85,4 +87,46 @@ public final class ModelBuilderTest { assertThat(model.exportToLpString(false)).contains("minimal_linear_example"); assertThat(model.exportToMpsString(false)).contains("minimal_linear_example"); } + + @Test + public void importFromMpsString() { + ModelBuilder model = new ModelBuilder(); + String mpsData = "* Generated by MPModelProtoExporter\n" + + "* Name : SupportedMaximizationProblem\n" + + "* Format : Free\n" + + "* Constraints : 0\n" + + "* Variables : 1\n" + + "* Binary : 0\n" + + "* Integer : 0\n" + + "* Continuous : 1\n" + + "NAME SupportedMaximizationProblem\n" + + "OBJSENSE\n" + + " MAX\n" + + "ROWS\n" + + " N COST\n" + + "COLUMNS\n" + + " X_ONE COST 1\n" + + "BOUNDS\n" + + " UP BOUND X_ONE 4\n" + + "ENDATA"; + assertThat(model.importFromMpsString(mpsData)).isTrue(); + assertThat(model.getName()).isEqualTo("SupportedMaximizationProblem"); + } + + @Test + public void importFromLpString() { + ModelBuilder model = new ModelBuilder(); + String lpData = "min: x + y;\n" + + "bin: b1, b2, b3;\n" + + "1 <= x <= 42;\n" + + "constraint_num1: 5 b1 + 3b2 + x <= 7;\n" + + "4 y + b2 - 3 b3 <= 2;\n" + + "constraint_num2: -4 b1 + b2 - 3 z <= -2;\n"; + assertThat(model.importFromLpString(lpData)).isTrue(); + assertThat(model.numVariables()).isEqualTo(6); + assertThat(model.numConstraints()).isEqualTo(3); + assertThat(model.varFromIndex(0).getLowerBound()).isEqualTo(1.0); + assertThat(model.varFromIndex(0).getUpperBound()).isEqualTo(42.0); + assertThat(model.varFromIndex(0).getName()).isEqualTo("x"); + } } diff --git a/examples/tests/bug_fz1.cc b/examples/tests/bug_fz1.cc index f2acf9a2df..0654c3065a 100644 --- a/examples/tests/bug_fz1.cc +++ b/examples/tests/bug_fz1.cc @@ -11,6 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "absl/flags/parse.h" #include "ortools/base/hash.h" #include "ortools/base/map_util.h" #include "ortools/base/stl_util.h" diff --git a/examples/tests/forbidden_intervals_test.cc b/examples/tests/forbidden_intervals_test.cc index eb02dc982c..9cfbe02cdb 100644 --- a/examples/tests/forbidden_intervals_test.cc +++ b/examples/tests/forbidden_intervals_test.cc @@ -11,6 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "absl/flags/parse.h" #include "ortools/constraint_solver/constraint_solver.h" namespace operations_research { diff --git a/examples/tests/issue1303.cc b/examples/tests/issue1303.cc index a40066f603..197e36fffa 100644 --- a/examples/tests/issue1303.cc +++ b/examples/tests/issue1303.cc @@ -11,6 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "absl/flags/parse.h" #include "ortools/base/logging.h" #include "ortools/base/version.h" diff --git a/examples/tests/issue57.cc b/examples/tests/issue57.cc index c42edb50f7..ffd0c0ec55 100644 --- a/examples/tests/issue57.cc +++ b/examples/tests/issue57.cc @@ -11,6 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "absl/flags/parse.h" #include "ortools/base/hash.h" #include "ortools/base/map_util.h" #include "ortools/base/stl_util.h" diff --git a/examples/tests/lp_test.py b/examples/tests/lp_test.py index 0cdd062ae6..e7e72bd806 100755 --- a/examples/tests/lp_test.py +++ b/examples/tests/lp_test.py @@ -169,14 +169,20 @@ class PyWrapLpTest(unittest.TestCase): (i, constraint.dual_value(), activities[constraint.index()]))) def testApi(self): + print('testApi', flush=True) all_names_and_problem_types = (list( linear_solver_pb2.MPModelRequest.SolverType.items())) for name, problem_type in all_names_and_problem_types: with self.subTest(f'{name}: {problem_type}'): + print(f'######## {name}:{problem_type} #######', flush=True) if not pywraplp.Solver.SupportsProblemType(problem_type): continue if name.startswith('GUROBI'): continue + if name.startswith('KNAPSACK'): + continue + if not name.startswith('SCIP'): + continue if name.endswith('LINEAR_PROGRAMMING'): print(('\n------ Linear programming example with %s ------' % name)) @@ -199,7 +205,7 @@ class PyWrapLpTest(unittest.TestCase): print('ERROR: %s unsupported' % name) def testSetHint(self): - print('testSetHint') + print('testSetHint', flush=True) solver = pywraplp.Solver('RunBooleanExampleCppStyle', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING) infinity = solver.infinity() @@ -223,7 +229,7 @@ class PyWrapLpTest(unittest.TestCase): self.assertEqual(1, len(solver.constraints())) def testBopInfeasible(self): - print('testBopInfeasible') + print('testBopInfeasible', flush=True) solver = pywraplp.Solver('test', pywraplp.Solver.BOP_INTEGER_PROGRAMMING) solver.EnableOutput() @@ -234,12 +240,12 @@ class PyWrapLpTest(unittest.TestCase): print(result_status) # outputs: 0 def testLoadSolutionFromProto(self): - print('testLoadSolutionFromProto') + print('testLoadSolutionFromProto', flush=True) solver = pywraplp.Solver('', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING) solver.LoadSolutionFromProto(linear_solver_pb2.MPSolutionResponse()) def testSolveFromProto(self): - print('testSolveFromProto') + print('testSolveFromProto', flush=True) request_str = ''' model { maximize: false @@ -309,6 +315,7 @@ class PyWrapLpTest(unittest.TestCase): def testExportToMps(self): """Test MPS export.""" + print('testExportToMps', flush=True) solver = pywraplp.Solver('ExportMps', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING) infinity = solver.infinity() diff --git a/examples/tests/min_max_test.cc b/examples/tests/min_max_test.cc index 10b4cff6b3..38207c1289 100644 --- a/examples/tests/min_max_test.cc +++ b/examples/tests/min_max_test.cc @@ -11,6 +11,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "absl/flags/parse.h" +#include "ortools/base/logging.h" #include "ortools/base/hash.h" #include "ortools/base/map_util.h" #include "ortools/base/stl_util.h" diff --git a/examples/tests/pywrapcp_test.py b/examples/tests/pywrapcp_test.py index 2424047402..b7b0e72abf 100755 --- a/examples/tests/pywrapcp_test.py +++ b/examples/tests/pywrapcp_test.py @@ -13,7 +13,6 @@ # limitations under the License. """Unit tests for python/constraint_solver.swig. Not exhaustive.""" - import sys import unittest from ortools.constraint_solver import pywrapcp diff --git a/examples/tests/pywraplp_test.py b/examples/tests/pywraplp_test.py index d90632d5fe..baddac5d95 100755 --- a/examples/tests/pywraplp_test.py +++ b/examples/tests/pywraplp_test.py @@ -13,7 +13,6 @@ # limitations under the License. """Simple unit tests for python/linear_solver.swig. Not exhaustive.""" - import unittest from ortools.linear_solver import linear_solver_pb2 from ortools.linear_solver import pywraplp