From 622483b682a1f41baf4fd2def8bb999288d4eaaa Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Mon, 8 Apr 2024 11:34:45 +0200 Subject: [PATCH] mostly reformat --- .../algorithms/python/knapsack_solver_test.py | 1 + ortools/algorithms/set_cover_orlib_test.cc | 29 +++++++++---------- ortools/graph/samples/assignment_min_flow.py | 1 + ortools/graph/samples/balance_min_flow.py | 1 + ortools/init/python/init_test.py | 1 + ortools/linear_solver/python/model_builder.py | 12 +++----- .../python/model_builder_helper_test.py | 1 + .../python/model_builder_test.py | 7 +++++ ortools/linear_solver/python/pywraplp_test.py | 1 + .../linear_solver/samples/basic_example.py | 12 ++++---- ortools/sat/python/cp_model.py | 10 +++---- ortools/sat/python/cp_model_helper_test.py | 5 ++-- ortools/sat/python/cp_model_test.py | 1 + ortools/sat/python/swig_helper_test.py | 2 ++ .../scheduling/jobshop_scheduling_parser.cc | 2 +- .../scheduling/jobshop_scheduling_parser.h | 2 +- ortools/scheduling/python/rcpsp_test.py | 1 + .../util/python/sorted_interval_list_test.py | 1 + ortools/util/qap_reader.cc | 3 +- ortools/util/qap_reader.h | 4 ++- 20 files changed, 56 insertions(+), 41 deletions(-) diff --git a/ortools/algorithms/python/knapsack_solver_test.py b/ortools/algorithms/python/knapsack_solver_test.py index 39f153ec56..8809980ca3 100755 --- a/ortools/algorithms/python/knapsack_solver_test.py +++ b/ortools/algorithms/python/knapsack_solver_test.py @@ -22,6 +22,7 @@ from ortools.algorithms.python import knapsack_solver class PyWrapAlgorithmsKnapsackSolverTest(absltest.TestCase): + def RealSolve(self, profits, weights, capacities, solver_type, use_reduction): solver = knapsack_solver.KnapsackSolver(solver_type, "solver") solver.set_use_reduction(use_reduction) diff --git a/ortools/algorithms/set_cover_orlib_test.cc b/ortools/algorithms/set_cover_orlib_test.cc index 20f1b542ae..08bb6be01a 100644 --- a/ortools/algorithms/set_cover_orlib_test.cc +++ b/ortools/algorithms/set_cover_orlib_test.cc @@ -15,7 +15,6 @@ #include #include -#include "absl/flags/flag.h" #include "absl/log/check.h" #include "absl/strings/str_cat.h" #include "absl/time/time.h" @@ -122,26 +121,26 @@ const char data_dir[] = #define ORLIB_TEST(name, best_objective, expected_objective, size, function) \ TEST(OrlibTest, APPEND_AND_EVAL(TestOnLine, __LINE__)) { \ - auto filespec = file::JoinPathRespectAbsolute( \ - absl::GetFlag(FLAGS_test_srcdir), data_dir, name); \ + auto filespec = \ + file::JoinPathRespectAbsolute(::testing::SrcDir(), data_dir, name); \ LOG(INFO) << "Reading " << name; \ operations_research::SetCoverModel model = function(filespec); \ double cost = RunSolver(name, &model); \ (void)cost; \ } -#define ORLIB_UNICOST_TEST(name, best_objective, expected_objective, size, \ - function) \ - TEST(OrlibUnicostTest, APPEND_AND_EVAL(TestOnLine, __LINE__)) { \ - auto filespec = file::JoinPathRespectAbsolute( \ - absl::GetFlag(FLAGS_test_srcdir), data_dir, name); \ - LOG(INFO) << "Reading " << name; \ - operations_research::SetCoverModel model = function(filespec); \ - for (int i = 0; i < model.num_subsets(); ++i) { \ - model.SetSubsetCost(i, 1.0); \ - } \ - double cost = RunSolver(absl::StrCat(name, "_unicost"), &model); \ - (void)cost; \ +#define ORLIB_UNICOST_TEST(name, best_objective, expected_objective, size, \ + function) \ + TEST(OrlibUnicostTest, APPEND_AND_EVAL(TestOnLine, __LINE__)) { \ + auto filespec = \ + file::JoinPathRespectAbsolute(::testing::SrcDir(), data_dir, name); \ + LOG(INFO) << "Reading " << name; \ + operations_research::SetCoverModel model = function(filespec); \ + for (int i = 0; i < model.num_subsets(); ++i) { \ + model.SetSubsetCost(i, 1.0); \ + } \ + double cost = RunSolver(absl::StrCat(name, "_unicost"), &model); \ + (void)cost; \ } #define SCP_TEST(name, best_objective, expected_objective, size) \ diff --git a/ortools/graph/samples/assignment_min_flow.py b/ortools/graph/samples/assignment_min_flow.py index 42d775c3e8..22f9cd8e18 100755 --- a/ortools/graph/samples/assignment_min_flow.py +++ b/ortools/graph/samples/assignment_min_flow.py @@ -72,6 +72,7 @@ def main(): for arc in range(smcf.num_arcs()): # Can ignore arcs leading out of source or into sink. if smcf.tail(arc) != source and smcf.head(arc) != sink: + # Arcs in the solution have a flow value of 1. Their start and end nodes # give an assignment of worker to task. if smcf.flow(arc) > 0: diff --git a/ortools/graph/samples/balance_min_flow.py b/ortools/graph/samples/balance_min_flow.py index 8a0c97a2e2..3c13128949 100755 --- a/ortools/graph/samples/balance_min_flow.py +++ b/ortools/graph/samples/balance_min_flow.py @@ -103,6 +103,7 @@ def main(): and smcf.tail(arc) != 12 and smcf.head(arc) != sink ): + # Arcs in the solution will have a flow value of 1. # There start and end nodes give an assignment of worker to task. if smcf.flow(arc) > 0: diff --git a/ortools/init/python/init_test.py b/ortools/init/python/init_test.py index c1de7baa79..5a02981e00 100755 --- a/ortools/init/python/init_test.py +++ b/ortools/init/python/init_test.py @@ -19,6 +19,7 @@ from ortools.init.python import init class InitTest(absltest.TestCase): + def test_logging(self): print("test_logging") init.CppBridge.init_logging("pywrapinit_test.py") diff --git a/ortools/linear_solver/python/model_builder.py b/ortools/linear_solver/python/model_builder.py index 05355071fe..9a759ceb2f 100644 --- a/ortools/linear_solver/python/model_builder.py +++ b/ortools/linear_solver/python/model_builder.py @@ -932,12 +932,10 @@ class Model: return clone @typing.overload - def _get_linear_constraints(self, constraints: Optional[pd.Index]) -> pd.Index: - ... + def _get_linear_constraints(self, constraints: Optional[pd.Index]) -> pd.Index: ... @typing.overload - def _get_linear_constraints(self, constraints: pd.Series) -> pd.Series: - ... + def _get_linear_constraints(self, constraints: pd.Series) -> pd.Series: ... def _get_linear_constraints( self, constraints: Optional[_IndexOrSeries] = None @@ -947,12 +945,10 @@ class Model: return constraints @typing.overload - def _get_variables(self, variables: Optional[pd.Index]) -> pd.Index: - ... + def _get_variables(self, variables: Optional[pd.Index]) -> pd.Index: ... @typing.overload - def _get_variables(self, variables: pd.Series) -> pd.Series: - ... + def _get_variables(self, variables: pd.Series) -> pd.Series: ... def _get_variables( self, variables: Optional[_IndexOrSeries] = None diff --git a/ortools/linear_solver/python/model_builder_helper_test.py b/ortools/linear_solver/python/model_builder_helper_test.py index ab6ff00ea4..a2b143a392 100644 --- a/ortools/linear_solver/python/model_builder_helper_test.py +++ b/ortools/linear_solver/python/model_builder_helper_test.py @@ -27,6 +27,7 @@ from ortools.linear_solver.python import model_builder_helper class PywrapModelBuilderHelperTest(absltest.TestCase): + def test_export_model_proto_to_mps_string(self): model = model_builder_helper.ModelBuilderHelper() model.set_name("testmodel") diff --git a/ortools/linear_solver/python/model_builder_test.py b/ortools/linear_solver/python/model_builder_test.py index 1567b7cb87..04a31548fe 100644 --- a/ortools/linear_solver/python/model_builder_test.py +++ b/ortools/linear_solver/python/model_builder_test.py @@ -420,6 +420,7 @@ ENDATA class InternalHelperTest(absltest.TestCase): + def test_anonymous_variables(self): helper = mb.Model().helper index = helper.add_var() @@ -434,6 +435,7 @@ class InternalHelperTest(absltest.TestCase): class LinearBaseTest(parameterized.TestCase): + def setUp(self): super().setUp() simple_model = mb.Model() @@ -614,6 +616,7 @@ class LinearBaseTest(parameterized.TestCase): class LinearBaseErrorsTest(absltest.TestCase): + def test_unknown_linear_type(self): with self.assertRaisesRegex(TypeError, r"Unrecognized linear expression"): @@ -636,6 +639,7 @@ class LinearBaseErrorsTest(absltest.TestCase): class BoundedLinearBaseTest(parameterized.TestCase): + def setUp(self): super().setUp() simple_model = mb.Model() @@ -729,6 +733,7 @@ class BoundedLinearBaseTest(parameterized.TestCase): class BoundedLinearBaseErrorsTest(absltest.TestCase): + def test_bounded_linear_expression_as_bool(self): with self.assertRaisesRegex(NotImplementedError, "Boolean value"): model = mb.Model() @@ -737,6 +742,7 @@ class BoundedLinearBaseErrorsTest(absltest.TestCase): class ModelBuilderErrorsTest(absltest.TestCase): + def test_new_var_series_errors(self): with self.assertRaisesRegex(TypeError, r"Non-index object"): model = mb.Model() @@ -1565,6 +1571,7 @@ class ModelBuilderObjectiveTest(parameterized.TestCase): class ModelBuilderProtoTest(absltest.TestCase): + def test_export_to_proto(self): expected = linear_solver_pb2.MPModelProto() text_format.Parse( diff --git a/ortools/linear_solver/python/pywraplp_test.py b/ortools/linear_solver/python/pywraplp_test.py index a683d4be0c..e0633e390f 100644 --- a/ortools/linear_solver/python/pywraplp_test.py +++ b/ortools/linear_solver/python/pywraplp_test.py @@ -42,6 +42,7 @@ constraint { class PyWrapLp(unittest.TestCase): + def test_proto(self): input_proto = linear_solver_pb2.MPModelProto() text_format.Merge(TEXT_MODEL, input_proto) diff --git a/ortools/linear_solver/samples/basic_example.py b/ortools/linear_solver/samples/basic_example.py index 1a2fbaced0..409a071fe1 100644 --- a/ortools/linear_solver/samples/basic_example.py +++ b/ortools/linear_solver/samples/basic_example.py @@ -65,12 +65,12 @@ def main(): # [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("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()) diff --git a/ortools/sat/python/cp_model.py b/ortools/sat/python/cp_model.py index dfe50ed4c5..d8ed4ec3dd 100644 --- a/ortools/sat/python/cp_model.py +++ b/ortools/sat/python/cp_model.py @@ -676,7 +676,7 @@ class LinearExpr: class _Sum(LinearExpr): """Represents the sum of two LinearExprs.""" - def __init__(self, left, right): + def __init__(self, left, right) -> None: for x in [left, right]: if not isinstance(x, (NumberTypes, LinearExpr)): raise TypeError("not an linear expression: " + str(x)) @@ -699,7 +699,7 @@ class _Sum(LinearExpr): class _ProductCst(LinearExpr): """Represents the product of a LinearExpr by a constant.""" - def __init__(self, expr, coeff): + def __init__(self, expr, coeff) -> None: coeff = cmh.assert_is_a_number(coeff) if isinstance(expr, _ProductCst): self.__expr = expr.expression() @@ -727,7 +727,7 @@ class _ProductCst(LinearExpr): class _SumArray(LinearExpr): """Represents the sum of a list of LinearExpr and a constant.""" - def __init__(self, expressions, constant=0): + def __init__(self, expressions, constant=0) -> None: self.__expressions = [] self.__constant = constant for x in expressions: @@ -764,7 +764,7 @@ class _SumArray(LinearExpr): class _WeightedSum(LinearExpr): """Represents sum(ai * xi) + b.""" - def __init__(self, expressions, coefficients, constant=0): + def __init__(self, expressions, coefficients, constant=0) -> None: self.__expressions = [] self.__coefficients = [] self.__constant = constant @@ -948,7 +948,7 @@ class IntVar(LinearExpr): class _NotBooleanVariable(LinearExpr): """Negation of a boolean variable.""" - def __init__(self, boolvar: IntVar): + def __init__(self, boolvar: IntVar) -> None: self.__boolvar: IntVar = boolvar @property diff --git a/ortools/sat/python/cp_model_helper_test.py b/ortools/sat/python/cp_model_helper_test.py index 35f9347eb9..40fe7b2c25 100644 --- a/ortools/sat/python/cp_model_helper_test.py +++ b/ortools/sat/python/cp_model_helper_test.py @@ -20,6 +20,7 @@ from ortools.sat.python import cp_model_helper class CpModelHelperTest(absltest.TestCase): + def test_is_boolean(self): print("test_is_boolean") self.assertTrue(cp_model_helper.is_boolean(True)) @@ -34,9 +35,7 @@ class CpModelHelperTest(absltest.TestCase): self.assertRaises(TypeError, cp_model_helper.assert_is_int64, "Hello") self.assertRaises(TypeError, cp_model_helper.assert_is_int64, 1.2) self.assertRaises(OverflowError, cp_model_helper.assert_is_int64, 2**63) - self.assertRaises( - OverflowError, cp_model_helper.assert_is_int64, -(2**63) - 1 - ) + self.assertRaises(OverflowError, cp_model_helper.assert_is_int64, -(2**63) - 1) cp_model_helper.assert_is_int64(123) cp_model_helper.assert_is_int64(2**63 - 1) cp_model_helper.assert_is_int64(-(2**63)) diff --git a/ortools/sat/python/cp_model_test.py b/ortools/sat/python/cp_model_test.py index e36c32c3b2..76c07e27a7 100644 --- a/ortools/sat/python/cp_model_test.py +++ b/ortools/sat/python/cp_model_test.py @@ -111,6 +111,7 @@ class LogToString: class CpModelTest(absltest.TestCase): + def testCreateIntegerVariable(self): print("testCreateIntegerVariable") model = cp_model.CpModel() diff --git a/ortools/sat/python/swig_helper_test.py b/ortools/sat/python/swig_helper_test.py index f41364180d..adb9011f95 100644 --- a/ortools/sat/python/swig_helper_test.py +++ b/ortools/sat/python/swig_helper_test.py @@ -22,6 +22,7 @@ from ortools.sat.python import swig_helper class Callback(swig_helper.SolutionCallback): + def __init__(self): swig_helper.SolutionCallback.__init__(self) self.__solution_count = 0 @@ -35,6 +36,7 @@ class Callback(swig_helper.SolutionCallback): class SwigHelperTest(absltest.TestCase): + def testVariableDomain(self): model_string = """ variables { domain: [ -10, 10 ] } diff --git a/ortools/scheduling/jobshop_scheduling_parser.cc b/ortools/scheduling/jobshop_scheduling_parser.cc index 9360af5545..3076a2656d 100644 --- a/ortools/scheduling/jobshop_scheduling_parser.cc +++ b/ortools/scheduling/jobshop_scheduling_parser.cc @@ -57,7 +57,7 @@ void JsspParser::SetMachines(int machine_count) { } } -bool JsspParser::ParseFile(const std::string& filename) { +bool JsspParser::ParseFile(absl::string_view filename) { problem_.Clear(); // Try to detect the type of the data file. // - fjs suffix -> Flexible Jobshop diff --git a/ortools/scheduling/jobshop_scheduling_parser.h b/ortools/scheduling/jobshop_scheduling_parser.h index 946aa0f01c..d11aac5d06 100644 --- a/ortools/scheduling/jobshop_scheduling_parser.h +++ b/ortools/scheduling/jobshop_scheduling_parser.h @@ -59,7 +59,7 @@ class JsspParser { // Parses a file to load a jobshop problem. // Tries to auto detect the file format. - bool ParseFile(const std::string& filename); + bool ParseFile(absl::string_view filename); // Returns the loaded problem. const JsspInputProblem& problem() const { return problem_; } diff --git a/ortools/scheduling/python/rcpsp_test.py b/ortools/scheduling/python/rcpsp_test.py index 4b3bfa1396..b50aaa1ee4 100644 --- a/ortools/scheduling/python/rcpsp_test.py +++ b/ortools/scheduling/python/rcpsp_test.py @@ -23,6 +23,7 @@ FLAGS = flags.FLAGS class RcpspTest(absltest.TestCase): + def testParseAndAccess(self): parser = rcpsp.RcpspParser() data = "ortools/scheduling/testdata/j301_1.sm" diff --git a/ortools/util/python/sorted_interval_list_test.py b/ortools/util/python/sorted_interval_list_test.py index 99f2887f61..b33eb7d878 100755 --- a/ortools/util/python/sorted_interval_list_test.py +++ b/ortools/util/python/sorted_interval_list_test.py @@ -19,6 +19,7 @@ from ortools.util.python import sorted_interval_list class SortedIntervalListTest(absltest.TestCase): + def testCtorAndGetter(self): bool_domain = sorted_interval_list.Domain(0, 1) self.assertEqual(2, bool_domain.size()) diff --git a/ortools/util/qap_reader.cc b/ortools/util/qap_reader.cc index 38f350fb14..8ad105dc82 100644 --- a/ortools/util/qap_reader.cc +++ b/ortools/util/qap_reader.cc @@ -20,13 +20,14 @@ #include "absl/log/check.h" #include "absl/strings/numbers.h" #include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" #include "ortools/util/filelineiter.h" namespace operations_research { // TODO(user): Unit test cases when the function dies, or return // (and test) a status instead. -QapProblem ReadQapProblemOrDie(const std::string& filepath) { +QapProblem ReadQapProblemOrDie(absl::string_view filepath) { QapProblem qap_problem; int n = 0; diff --git a/ortools/util/qap_reader.h b/ortools/util/qap_reader.h index c1cd2c58b2..02145fca6f 100644 --- a/ortools/util/qap_reader.h +++ b/ortools/util/qap_reader.h @@ -18,6 +18,8 @@ #include #include +#include "absl/strings/string_view.h" + namespace operations_research { // Quadratic assignment problem (QAP) is a classical combinatorial optimization @@ -63,7 +65,7 @@ struct QapProblem { // supply flowing from factory 0 to factory 1, and 3 units of supply flowing // from factory 1 to 0. The distance from location 0 to location 1 is equal // to 2, and the distance from location 1 to 0 is equal to 1. -QapProblem ReadQapProblemOrDie(const std::string& filepath); +QapProblem ReadQapProblemOrDie(absl::string_view filepath); } // namespace operations_research