From 1ba0177af58264f61019dc3d917c534be259c558 Mon Sep 17 00:00:00 2001 From: Corentin Le Molgat Date: Fri, 3 Jun 2022 17:09:29 +0200 Subject: [PATCH] python: Fix tests when SCIP and COINOR are disabled (#3261) --- examples/contrib/3_jugs_mip.py | 14 +++----------- examples/contrib/assignment6_mip.py | 10 ++++------ examples/contrib/blending.py | 10 ++++------ examples/contrib/coins_grid_mip.py | 9 +++------ examples/contrib/coloring_ip.py | 13 +++---------- examples/contrib/diet1_mip.py | 12 +++--------- examples/contrib/game_theory_taha.py | 12 +++--------- examples/contrib/knapsack_mip.py | 13 +++---------- examples/contrib/least_square.py | 12 +++--------- examples/contrib/magic_square.py | 2 ++ examples/contrib/magic_square_mip.py | 14 +++----------- examples/contrib/production.py | 12 +++--------- examples/contrib/stigler_contrib.py | 14 +++----------- examples/contrib/volsay.py | 5 +++-- examples/contrib/volsay2.py | 5 +++-- examples/contrib/volsay3.py | 5 +++-- examples/python/appointments.py | 15 +++++++++------ examples/python/steel_mill_slab_sat.py | 5 +++-- examples/tests/dual_loading.py | 2 +- examples/tests/pywraplp_test.py | 8 ++++---- .../samples/assignment_groups_mip.py | 2 ++ ortools/linear_solver/samples/assignment_mip.py | 3 ++- .../samples/assignment_task_sizes_mip.py | 3 ++- .../linear_solver/samples/assignment_teams_mip.py | 2 ++ ortools/linear_solver/samples/basic_example.py | 2 ++ ortools/linear_solver/samples/bin_packing_mip.py | 3 ++- .../samples/integer_programming_example.py | 3 ++- .../samples/linear_programming_example.py | 2 ++ ortools/linear_solver/samples/mip_var_array.py | 2 ++ .../linear_solver/samples/simple_lp_program.py | 2 ++ .../linear_solver/samples/simple_mip_program.py | 2 ++ ortools/linear_solver/samples/stigler_diet.py | 5 +++-- 32 files changed, 91 insertions(+), 132 deletions(-) diff --git a/examples/contrib/3_jugs_mip.py b/examples/contrib/3_jugs_mip.py index c1a8ee77c7..49b78a9529 100644 --- a/examples/contrib/3_jugs_mip.py +++ b/examples/contrib/3_jugs_mip.py @@ -32,19 +32,11 @@ from ortools.linear_solver import pywraplp def main(sol='CBC'): - # Create the solver. - print('Solver: ', sol) - - # using GLPK - if sol == 'GLPK': - solver = pywraplp.Solver('CoinsGridGLPK', - pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING) - else: - # Using CBC - solver = pywraplp.Solver('CoinsGridCBC', - pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver(sol) + if not solver: + return # # data diff --git a/examples/contrib/assignment6_mip.py b/examples/contrib/assignment6_mip.py index 907160222f..061e18dd53 100644 --- a/examples/contrib/assignment6_mip.py +++ b/examples/contrib/assignment6_mip.py @@ -48,14 +48,12 @@ def main(sol='CBC'): # Create the solver. print('Solver: ', sol) - # using GLPK if sol == 'GLPK': - solver = pywraplp.Solver('CoinsGridGLPK', - pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver('GLPK') else: - # Using CBC - solver = pywraplp.Solver('CoinsGridCBC', - pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver('CBC') + if not solver: + return # # data diff --git a/examples/contrib/blending.py b/examples/contrib/blending.py index 801156d65b..6334ad9c6d 100644 --- a/examples/contrib/blending.py +++ b/examples/contrib/blending.py @@ -31,14 +31,12 @@ def main(sol='CBC'): print('Solver: ', sol) - # using GLPK if sol == 'GLPK': - solver = pywraplp.Solver('CoinsGridGLPK', - pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver('GLPK') else: - # Using CBC - solver = pywraplp.Solver('CoinsGridCBC', - pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver('CBC') + if not solver: + return # # data diff --git a/examples/contrib/coins_grid_mip.py b/examples/contrib/coins_grid_mip.py index dffea82de2..afa35e81c4 100644 --- a/examples/contrib/coins_grid_mip.py +++ b/examples/contrib/coins_grid_mip.py @@ -47,12 +47,9 @@ def main(unused_argv): # Create the solver. # using CBC - solver = pywraplp.Solver('CoinsGridCBC', - pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) - - # Using CLP - # solver = pywraplp.Solver('CoinsGridCLP', - # pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver('CBC') + if not solver: + return # data n = 31 # the grid size diff --git a/examples/contrib/coloring_ip.py b/examples/contrib/coloring_ip.py index 0c21d3013a..1aeb324319 100644 --- a/examples/contrib/coloring_ip.py +++ b/examples/contrib/coloring_ip.py @@ -43,17 +43,10 @@ from ortools.linear_solver import pywraplp def main(sol='CBC'): # Create the solver. - print('Solver: ', sol) - - if sol == 'GLPK': - # using GLPK - solver = pywraplp.Solver('CoinsGridGLPK', - pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING) - else: - # Using CBC - solver = pywraplp.Solver('CoinsGridCLP', - pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver(sol) + if not solver: + return # # data diff --git a/examples/contrib/diet1_mip.py b/examples/contrib/diet1_mip.py index 86d9fabc4d..decb8dc445 100644 --- a/examples/contrib/diet1_mip.py +++ b/examples/contrib/diet1_mip.py @@ -43,15 +43,9 @@ def main(sol='CBC'): # Create the solver. print('Solver: ', sol) - - if sol == 'GLPK': - # using GLPK - solver = pywraplp.Solver('CoinsGridGLPK', - pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING) - else: - # Using CBC - solver = pywraplp.Solver('CoinsGridCLP', - pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver(sol) + if not solver: + return # # data diff --git a/examples/contrib/game_theory_taha.py b/examples/contrib/game_theory_taha.py index 1dbf3962bf..9f88819f91 100644 --- a/examples/contrib/game_theory_taha.py +++ b/examples/contrib/game_theory_taha.py @@ -30,15 +30,9 @@ from ortools.linear_solver import pywraplp def main(sol='CBC'): # Create the solver. - - # using GLPK - if sol == 'GLPK': - solver = pywraplp.Solver('CoinsGridGLPK', - pywraplp.Solver.GLPK_LINEAR_PROGRAMMING) - else: - # Using CLP - solver = pywraplp.Solver('CoinsGridCLP', - pywraplp.Solver.CLP_LINEAR_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver(sol) + if not solver: + return # data rows = 3 diff --git a/examples/contrib/knapsack_mip.py b/examples/contrib/knapsack_mip.py index 586bc70d45..d1c08e24c9 100644 --- a/examples/contrib/knapsack_mip.py +++ b/examples/contrib/knapsack_mip.py @@ -28,17 +28,10 @@ from ortools.linear_solver import pywraplp def main(sol='CBC'): # Create the solver. - print('Solver: ', sol) - - # using GLPK - if sol == 'GLPK': - solver = pywraplp.Solver('CoinsGridGLPK', - pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING) - else: - # Using CBC - solver = pywraplp.Solver('CoinsGridCBC', - pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver(sol) + if not solver: + return # # data diff --git a/examples/contrib/least_square.py b/examples/contrib/least_square.py index 00df4ac7d6..04b8dd4b6c 100644 --- a/examples/contrib/least_square.py +++ b/examples/contrib/least_square.py @@ -31,15 +31,9 @@ from ortools.linear_solver import pywraplp def main(sol='CBC'): # Create the solver. - - # using GLPK - if sol == 'GLPK': - solver = pywraplp.Solver('CoinsGridGLPK', - pywraplp.Solver.GLPK_LINEAR_PROGRAMMING) - else: - # Using CLP - solver = pywraplp.Solver('CoinsGridCLP', - pywraplp.Solver.CLP_LINEAR_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver(sol) + if not solver: + return # data # number of points diff --git a/examples/contrib/magic_square.py b/examples/contrib/magic_square.py index e658daa81f..d305726822 100644 --- a/examples/contrib/magic_square.py +++ b/examples/contrib/magic_square.py @@ -28,6 +28,8 @@ from ortools.constraint_solver import pywrapcp def main(n, limit): # Create the solver. solver = pywrapcp.Solver("n-queens") + if not solver: + return # # data diff --git a/examples/contrib/magic_square_mip.py b/examples/contrib/magic_square_mip.py index df73e2f0e4..c050389f8d 100644 --- a/examples/contrib/magic_square_mip.py +++ b/examples/contrib/magic_square_mip.py @@ -52,19 +52,11 @@ from ortools.linear_solver import pywraplp def main(n=3, sol='CBC', use_output_matrix=0): - # Create the solver. - print('Solver: ', sol) - - # using GLPK - if sol == 'GLPK': - solver = pywraplp.Solver('CoinsGridGLPK', - pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING) - else: - # Using CLP - solver = pywraplp.Solver('CoinsGridCLP', - pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver(sol) + if not solver: + return # # data diff --git a/examples/contrib/production.py b/examples/contrib/production.py index 68c537c88d..8d5275d459 100644 --- a/examples/contrib/production.py +++ b/examples/contrib/production.py @@ -28,15 +28,9 @@ from ortools.linear_solver import pywraplp def main(sol='CBC'): # Create the solver. - - # using GLPK - if sol == 'GLPK': - solver = pywraplp.Solver('CoinsGridGLPK', - pywraplp.Solver.GLPK_LINEAR_PROGRAMMING) - else: - # Using CLP - solver = pywraplp.Solver('CoinsGridCLP', - pywraplp.Solver.CLP_LINEAR_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver(sol) + if not solver: + return # # data diff --git a/examples/contrib/stigler_contrib.py b/examples/contrib/stigler_contrib.py index 094639e666..f56900dd49 100644 --- a/examples/contrib/stigler_contrib.py +++ b/examples/contrib/stigler_contrib.py @@ -122,19 +122,11 @@ from ortools.linear_solver import pywraplp def main(sol="CBC"): - # Create the solver. - print("Solver: ", sol) - - # using GLPK - if sol == "GLPK": - solver = pywraplp.Solver("CoinsGridGLPK", - pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING) - else: - # Using CLP - solver = pywraplp.Solver("CoinsGridCLP", - pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver(sol) + if not solver: + return # # data diff --git a/examples/contrib/volsay.py b/examples/contrib/volsay.py index b3d9fdf8bd..af1efe9f6a 100644 --- a/examples/contrib/volsay.py +++ b/examples/contrib/volsay.py @@ -33,8 +33,9 @@ def main(unused_argv): # pywraplp.Solver.GLPK_LINEAR_PROGRAMMING) # Using CLP - solver = pywraplp.Solver('CoinsGridCLP', - pywraplp.Solver.CLP_LINEAR_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver('CLP') + if not solver: + return # data diff --git a/examples/contrib/volsay2.py b/examples/contrib/volsay2.py index ebcab60262..ba3a3a5847 100644 --- a/examples/contrib/volsay2.py +++ b/examples/contrib/volsay2.py @@ -34,8 +34,9 @@ def main(unused_argv): # pywraplp.Solver.GLPK_LINEAR_PROGRAMMING) # Using CLP - solver = pywraplp.Solver('CoinsGridCLP', - pywraplp.Solver.CLP_LINEAR_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver('CLP') + if not solver: + return # data num_products = 2 diff --git a/examples/contrib/volsay3.py b/examples/contrib/volsay3.py index 7abeebb6a6..df389eace7 100644 --- a/examples/contrib/volsay3.py +++ b/examples/contrib/volsay3.py @@ -34,8 +34,9 @@ def main(unused_argv): # pywraplp.Solver.GLPK_LINEAR_PROGRAMMING) # Using CLP - solver = pywraplp.Solver('CoinsGridCLP', - pywraplp.Solver.CLP_LINEAR_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver('CLP') + if not solver: + return # data num_products = 2 diff --git a/examples/python/appointments.py b/examples/python/appointments.py index 876ef6bede..82a5149dc0 100755 --- a/examples/python/appointments.py +++ b/examples/python/appointments.py @@ -116,8 +116,9 @@ def AggregateItemCollectionsOptimally(item_collections, max_num_collections, - and its associated "num_selections" is the number of times it was selected. """ - solver = pywraplp.Solver('Select', - pywraplp.Solver.SCIP_MIXED_INTEGER_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver('SCIP') + if not solver: + return [] n = len(ideal_item_ratios) num_distinct_collections = len(item_collections) max_num_items_per_collection = 0 @@ -238,10 +239,12 @@ def solve_appointments(_): print() print('%d installations planned' % installed) for a in demand: - name = a[1] - per_type = installed_per_type[name] - print((' %d (%.2f%%) installations of type %s planned' % - (per_type, per_type * 100.0 / installed, name))) + name = a[1] + per_type = installed_per_type[name] + if installed != 0: + print(f' {per_type} ({per_type * 100.0 / installed}%) installations of type {name} planned') + else: + print(f' {per_type} installations of type {name} planned') # [END print_solution] diff --git a/examples/python/steel_mill_slab_sat.py b/examples/python/steel_mill_slab_sat.py index 845fea084f..71100cdc0d 100755 --- a/examples/python/steel_mill_slab_sat.py +++ b/examples/python/steel_mill_slab_sat.py @@ -705,8 +705,9 @@ def steel_mill_slab_with_mip_column_generation(problem): # create model and decision variables. start_time = time.time() - solver = pywraplp.Solver('Steel', - pywraplp.Solver.SCIP_MIXED_INTEGER_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver('SCIP') + if not solver: + return selected = [ solver.IntVar(0.0, 1.0, 'selected_%i' % i) for i in all_valid_slabs ] diff --git a/examples/tests/dual_loading.py b/examples/tests/dual_loading.py index f31e87fc52..8ab1c1c88e 100755 --- a/examples/tests/dual_loading.py +++ b/examples/tests/dual_loading.py @@ -5,7 +5,7 @@ from ortools.linear_solver import pywraplp def main(): cp = pywrapcp.Solver("test") - lp = pywraplp.Solver("test", pywraplp.Solver.CLP_LINEAR_PROGRAMMING) + lp = pywraplp.Solver.CreateSolver('GLOP') if __name__ == "__main__": diff --git a/examples/tests/pywraplp_test.py b/examples/tests/pywraplp_test.py index be9d03dd55..98465f334d 100755 --- a/examples/tests/pywraplp_test.py +++ b/examples/tests/pywraplp_test.py @@ -46,8 +46,9 @@ class PyWrapLp(unittest.TestCase): def test_proto(self): input_proto = linear_solver_pb2.MPModelProto() text_format.Merge(TEXT_MODEL, input_proto) - solver = pywraplp.Solver('solveFromProto', - pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver('CBC') + if not solver: + return # For now, create the model from the proto by parsing the proto errors = solver.LoadModelFromProto(input_proto) self.assertFalse(errors) @@ -61,8 +62,7 @@ class PyWrapLp(unittest.TestCase): self.assertEqual(solution.best_objective_bound, 3.0) def test_external_api(self): - solver = pywraplp.Solver('TestExternalAPI', - pywraplp.Solver.GLOP_LINEAR_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver('GLOP') infinity = solver.Infinity() infinity2 = solver.infinity() self.assertEqual(infinity, infinity2) diff --git a/ortools/linear_solver/samples/assignment_groups_mip.py b/ortools/linear_solver/samples/assignment_groups_mip.py index 22342f7d5c..db12bbdf72 100755 --- a/ortools/linear_solver/samples/assignment_groups_mip.py +++ b/ortools/linear_solver/samples/assignment_groups_mip.py @@ -70,6 +70,8 @@ def main(): # [START solver] # Create the mip solver with the SCIP backend. solver = pywraplp.Solver.CreateSolver('SCIP') + if not solver: + return # [END solver] # Variables diff --git a/ortools/linear_solver/samples/assignment_mip.py b/ortools/linear_solver/samples/assignment_mip.py index 39196bbefb..484f3a91a7 100755 --- a/ortools/linear_solver/samples/assignment_mip.py +++ b/ortools/linear_solver/samples/assignment_mip.py @@ -36,7 +36,8 @@ def main(): # [START solver] # Create the mip solver with the SCIP backend. solver = pywraplp.Solver.CreateSolver('SCIP') - + if not solver: + return # [END solver] # Variables diff --git a/ortools/linear_solver/samples/assignment_task_sizes_mip.py b/ortools/linear_solver/samples/assignment_task_sizes_mip.py index 0679fd119e..e24dc26b1c 100755 --- a/ortools/linear_solver/samples/assignment_task_sizes_mip.py +++ b/ortools/linear_solver/samples/assignment_task_sizes_mip.py @@ -45,7 +45,8 @@ def main(): # [START solver] # Create the mip solver with the SCIP backend. solver = pywraplp.Solver.CreateSolver('SCIP') - + if not solver: + return # [END solver] # Variables diff --git a/ortools/linear_solver/samples/assignment_teams_mip.py b/ortools/linear_solver/samples/assignment_teams_mip.py index 7c7f3c9908..615ca9c354 100755 --- a/ortools/linear_solver/samples/assignment_teams_mip.py +++ b/ortools/linear_solver/samples/assignment_teams_mip.py @@ -42,6 +42,8 @@ def main(): # [START solver] # Create the mip solver with the SCIP backend. solver = pywraplp.Solver.CreateSolver('SCIP') + if not solver: + return # [END solver] # Variables diff --git a/ortools/linear_solver/samples/basic_example.py b/ortools/linear_solver/samples/basic_example.py index 912d4d7bc5..a6f3855ba7 100755 --- a/ortools/linear_solver/samples/basic_example.py +++ b/ortools/linear_solver/samples/basic_example.py @@ -23,6 +23,8 @@ def main(): # [START solver] # Create the linear solver with the GLOP backend. solver = pywraplp.Solver.CreateSolver('GLOP') + if not solver: + return # [END solver] # [START variables] diff --git a/ortools/linear_solver/samples/bin_packing_mip.py b/ortools/linear_solver/samples/bin_packing_mip.py index 45c9b875a8..eeae66a833 100755 --- a/ortools/linear_solver/samples/bin_packing_mip.py +++ b/ortools/linear_solver/samples/bin_packing_mip.py @@ -42,7 +42,8 @@ def main(): # [START solver] # Create the mip solver with the SCIP backend. solver = pywraplp.Solver.CreateSolver('SCIP') - + if not solver: + return # [END solver] # [START program_part2] diff --git a/ortools/linear_solver/samples/integer_programming_example.py b/ortools/linear_solver/samples/integer_programming_example.py index 1ec2c9e35f..14c70d8a54 100755 --- a/ortools/linear_solver/samples/integer_programming_example.py +++ b/ortools/linear_solver/samples/integer_programming_example.py @@ -23,7 +23,8 @@ def IntegerProgrammingExample(): # [START solver] # Create the mip solver with the SCIP backend. solver = pywraplp.Solver.CreateSolver('SCIP') - + if not solver: + return # [END solver] # [START variables] diff --git a/ortools/linear_solver/samples/linear_programming_example.py b/ortools/linear_solver/samples/linear_programming_example.py index 09a002fb84..22b2b63bc3 100755 --- a/ortools/linear_solver/samples/linear_programming_example.py +++ b/ortools/linear_solver/samples/linear_programming_example.py @@ -23,6 +23,8 @@ def LinearProgrammingExample(): # Instantiate a Glop solver, naming it LinearExample. # [START solver] solver = pywraplp.Solver.CreateSolver('GLOP') + if not solver: + return # [END solver] # Create the two variables and let them take on any non-negative value. diff --git a/ortools/linear_solver/samples/mip_var_array.py b/ortools/linear_solver/samples/mip_var_array.py index f286ecd72a..296d988459 100755 --- a/ortools/linear_solver/samples/mip_var_array.py +++ b/ortools/linear_solver/samples/mip_var_array.py @@ -46,6 +46,8 @@ def main(): # [START solver] # Create the mip solver with the SCIP backend. solver = pywraplp.Solver.CreateSolver('SCIP') + if not solver: + return # [END solver] # [START program_part2] diff --git a/ortools/linear_solver/samples/simple_lp_program.py b/ortools/linear_solver/samples/simple_lp_program.py index e375bf7347..c275b4a987 100755 --- a/ortools/linear_solver/samples/simple_lp_program.py +++ b/ortools/linear_solver/samples/simple_lp_program.py @@ -22,6 +22,8 @@ def main(): # [START solver] # Create the linear solver with the GLOP backend. solver = pywraplp.Solver.CreateSolver('GLOP') + if not solver: + return # [END solver] # [START variables] diff --git a/ortools/linear_solver/samples/simple_mip_program.py b/ortools/linear_solver/samples/simple_mip_program.py index d4446cb084..28b23301e1 100755 --- a/ortools/linear_solver/samples/simple_mip_program.py +++ b/ortools/linear_solver/samples/simple_mip_program.py @@ -22,6 +22,8 @@ def main(): # [START solver] # Create the mip solver with the SCIP backend. solver = pywraplp.Solver.CreateSolver('SCIP') + if not solver: + return # [END solver] # [START variables] diff --git a/ortools/linear_solver/samples/stigler_diet.py b/ortools/linear_solver/samples/stigler_diet.py index 8edb48e8cc..63826e7302 100755 --- a/ortools/linear_solver/samples/stigler_diet.py +++ b/ortools/linear_solver/samples/stigler_diet.py @@ -224,8 +224,9 @@ def main(): # [START solver] # Instantiate a Glop solver and naming it. - solver = pywraplp.Solver('StiglerDietExample', - pywraplp.Solver.GLOP_LINEAR_PROGRAMMING) + solver = pywraplp.Solver.CreateSolver('GLOP') + if not solver: + return # [END solver] # [START variables]