From aa0e719d0215d2b9e680624e41b9637a24c31d34 Mon Sep 17 00:00:00 2001 From: Corentin Le Molgat Date: Wed, 21 Jun 2023 17:31:06 +0200 Subject: [PATCH] Export examples/cpp/ from google3 * Rework network_routing_sat --- examples/cpp/BUILD.bazel | 559 ++++++++++-------- examples/cpp/binpacking_2d_sat.cc | 23 +- examples/cpp/constraint_programming_cp.cc | 4 +- examples/cpp/costas_array_sat.cc | 1 - examples/cpp/course_scheduling.cc | 5 +- examples/cpp/course_scheduling.h | 2 +- examples/cpp/course_scheduling_run.cc | 11 +- examples/cpp/dobble_ls.cc | 10 +- examples/cpp/fap_model_printer.h | 5 +- examples/cpp/fap_parser.h | 101 ++-- examples/cpp/fap_utilities.h | 18 +- examples/cpp/frequency_assignment_problem.cc | 42 +- examples/cpp/integer_programming.cc | 2 +- examples/cpp/jobshop_sat.cc | 12 +- examples/cpp/linear_assignment_api.cc | 8 +- examples/cpp/linear_programming.cc | 2 +- examples/cpp/max_flow.cc | 7 +- examples/cpp/min_cost_flow.cc | 7 +- examples/cpp/mps_driver.cc | 2 +- examples/cpp/network_routing_sat.cc | 100 ++-- examples/cpp/nqueens.cc | 18 +- examples/cpp/parse_dimacs_assignment.h | 18 +- examples/cpp/pdptw.cc | 1 + examples/cpp/print_dimacs_assignment.h | 7 +- examples/cpp/qap_sat.cc | 1 - examples/cpp/random_tsp.cc | 12 +- examples/cpp/shift_minimization_sat.cc | 5 +- examples/cpp/slitherlink_sat.cc | 56 +- ...trawberry_fields_with_column_generation.cc | 2 +- .../cpp/uncapacitated_facility_location.cc | 17 +- examples/cpp/variable_intervals_sat.cc | 17 + examples/cpp/weighted_tardiness_sat.cc | 3 +- 32 files changed, 586 insertions(+), 492 deletions(-) diff --git a/examples/cpp/BUILD.bazel b/examples/cpp/BUILD.bazel index 2f7b869f757..4418453d963 100644 --- a/examples/cpp/BUILD.bazel +++ b/examples/cpp/BUILD.bazel @@ -11,6 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +# Constraint solver examples. cc_binary( name = "binpacking_2d_sat", srcs = [ @@ -42,258 +43,346 @@ cc_binary( "//ortools/base", "//ortools/sat:cp_model", "//ortools/sat:model", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/flags:parse", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", ], ) -cc_library( - name = "cvrptw_lib", - hdrs = ["cvrptw_lib.h"], +cc_binary( + name = "cryptarithm_sat", + srcs = ["cryptarithm_sat.cc"], + deps = [ + "//ortools/sat:cp_model", + "//ortools/sat:model", + ], +) + +cc_binary( + name = "dobble_ls", + srcs = ["dobble_ls.cc"], deps = [ "//ortools/base", - "//ortools/constraint_solver:routing", - "//ortools/constraint_solver:routing_flags", - "//ortools/util:random_engine", + "//ortools/base:map_util", + "//ortools/constraint_solver:cp", + "//ortools/util:bitset", + "@com_google_absl//absl/random", + "@com_google_absl//absl/strings:str_format", ], ) cc_binary( - name = "cvrptw", - srcs = ["cvrptw.cc"], + name = "golomb_sat", + srcs = ["golomb_sat.cc"], deps = [ - ":cvrptw_lib", "//ortools/base", - "//ortools/constraint_solver:routing", - "//ortools/constraint_solver:routing_flags", + "//ortools/sat:cp_model", + "//ortools/sat:model", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/flags:parse", + "@com_google_absl//absl/strings:str_format", + "@com_google_protobuf//:protobuf", ], ) cc_binary( - name = "cvrp_disjoint_tw", - srcs = ["cvrp_disjoint_tw.cc"], + name = "knapsack_2d_sat", + srcs = [ + "knapsack_2d_sat.cc", + ], deps = [ - ":cvrptw_lib", "//ortools/base", - "//ortools/constraint_solver:routing", - "//ortools/constraint_solver:routing_flags", + "//ortools/packing:binpacking_2d_parser", + "//ortools/packing:multiple_dimensions_bin_packing_cc_proto", + "//ortools/sat:cp_model", + "//ortools/sat:cp_model_solver", + "@com_google_absl//absl/flags:flag", + "@com_google_protobuf//:protobuf", ], ) cc_binary( - name = "cvrptw_with_breaks", - srcs = ["cvrptw_with_breaks.cc"], + name = "jobshop_sat", + srcs = [ + "jobshop_sat.cc", + ], deps = [ - ":cvrptw_lib", "//ortools/base", - "//ortools/constraint_solver:routing", - "//ortools/constraint_solver:routing_enums_cc_proto", - "//ortools/constraint_solver:routing_flags", + "//ortools/base:file", + "//ortools/base:timer", + "//ortools/sat:cp_model", + "//ortools/sat:cp_model_solver", + "//ortools/sat:model", + "//ortools/scheduling:jobshop_scheduling_cc_proto", + "//ortools/scheduling:jobshop_scheduling_parser", + "@com_google_absl//absl/flags:flag", "@com_google_absl//absl/strings", ], ) cc_binary( - name = "cvrptw_with_resources", - srcs = ["cvrptw_with_resources.cc"], + name = "magic_sequence_sat", + srcs = ["magic_sequence_sat.cc"], deps = [ - ":cvrptw_lib", "//ortools/base", - "//ortools/constraint_solver:routing", - "//ortools/constraint_solver:routing_flags", + "//ortools/sat:cp_model", + "//ortools/sat:model", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/flags:parse", + "@com_google_absl//absl/strings:str_format", ], ) cc_binary( - name = "cvrptw_with_stop_times_and_resources", - srcs = ["cvrptw_with_stop_times_and_resources.cc"], + name = "multi_knapsack_sat", + srcs = [ + "multi_knapsack_sat.cc", + ], deps = [ - ":cvrptw_lib", "//ortools/base", - "//ortools/constraint_solver:routing", - "//ortools/constraint_solver:routing_flags", - "@com_google_absl//absl/strings", + "//ortools/sat:cp_model", + "@com_google_absl//absl/flags:flag", ], ) cc_binary( - name = "cvrptw_with_refueling", - srcs = ["cvrptw_with_refueling.cc"], + name = "shift_minimization_sat", + srcs = [ + "shift_minimization_sat.cc", + ], deps = [ - ":cvrptw_lib", "//ortools/base", - "//ortools/constraint_solver:routing", - "//ortools/constraint_solver:routing_flags", + "//ortools/base:file", + "//ortools/sat:cp_model", + "//ortools/sat:model", + "//ortools/util:filelineiter", + "@com_google_absl//absl/container:btree", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/strings", ], ) -cc_library( - name = "print_dimacs_assignment", - hdrs = ["print_dimacs_assignment.h"], +cc_binary( + name = "weighted_tardiness_sat", + srcs = [ + "weighted_tardiness_sat.cc", + ], deps = [ "//ortools/base", "//ortools/base:file", - "//ortools/graph:ebert_graph", - "//ortools/graph:linear_assignment", - "@com_google_absl//absl/status", + "//ortools/sat:cp_model", + "//ortools/sat:cp_model_solver", + "//ortools/sat:disjunctive", + "//ortools/sat:integer", + "//ortools/sat:integer_expr", + "//ortools/sat:intervals", + "//ortools/sat:model", + "//ortools/sat:optimization", + "//ortools/sat:precedences", + "//ortools/sat:sat_solver", + "//ortools/util:filelineiter", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/strings", + "@com_google_protobuf//:protobuf", ], ) -cc_library( - name = "parse_dimacs_assignment", - hdrs = ["parse_dimacs_assignment.h"], +cc_binary( + name = "magic_square_sat", + srcs = ["magic_square_sat.cc"], deps = [ "//ortools/base", - "//ortools/graph:ebert_graph", - "//ortools/graph:linear_assignment", - "//ortools/util:filelineiter", + "//ortools/sat:cp_model", + "//ortools/sat:model", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/flags:parse", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", ], ) cc_binary( - name = "dimacs_assignment", - srcs = ["dimacs_assignment.cc"], + name = "network_routing_sat", + srcs = ["network_routing_sat.cc"], deps = [ - ":parse_dimacs_assignment", - ":print_dimacs_assignment", - "//ortools/algorithms:hungarian", "//ortools/base", - "//ortools/base:timer", - "//ortools/graph:ebert_graph", - "//ortools/graph:linear_assignment", + "//ortools/base:hash", + "//ortools/base:map_util", + "//ortools/graph:shortest_paths", + "//ortools/sat:cp_model", + "//ortools/sat:model", + "//ortools/util:tuple_set", + "@com_google_absl//absl/container:btree", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", ], ) cc_binary( - name = "dobble_ls", - srcs = ["dobble_ls.cc"], + name = "nqueens", + srcs = ["nqueens.cc"], deps = [ "//ortools/base", "//ortools/base:map_util", "//ortools/constraint_solver:cp", - "//ortools/util:bitset", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/strings:str_format", ], ) cc_binary( - name = "flow_api", - srcs = ["flow_api.cc"], + name = "sports_scheduling_sat", + srcs = ["sports_scheduling_sat.cc"], deps = [ "//ortools/base", - "//ortools/graph:ebert_graph", - "//ortools/graph:max_flow", - "//ortools/graph:min_cost_flow", + "//ortools/sat:cp_model", + "//ortools/sat:model", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", ], ) -cc_library( - name = "fap_parser", - hdrs = ["fap_parser.h"], +cc_binary( + name = "vector_bin_packing_solver", + srcs = [ + "vector_bin_packing_solver.cc", + ], deps = [ "//ortools/base", "//ortools/base:file", - "//ortools/base:hash", - "//ortools/base:map_util", + "//ortools/packing:arc_flow_builder", + "//ortools/packing:arc_flow_solver", + "//ortools/packing:vector_bin_packing_cc_proto", + "//ortools/packing:vector_bin_packing_parser", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/status", "@com_google_absl//absl/strings", - "@com_google_absl//absl/container:btree", ], ) -cc_library( - name = "fap_model_printer", - hdrs = ["fap_model_printer.h"], +# Routing examples. +cc_binary( + name = "random_tsp", + srcs = ["random_tsp.cc"], deps = [ - ":fap_parser", "//ortools/base", - "//ortools/base:file", + "//ortools/constraint_solver:routing", + "//ortools/constraint_solver:routing_flags", + "//ortools/util:random_engine", "@com_google_absl//absl/strings", - "@com_google_absl//absl/container:btree", + "@com_google_protobuf//:protobuf", ], ) cc_library( - name = "fap_utilities", - hdrs = ["fap_utilities.h"], + name = "cvrptw_lib", + hdrs = ["cvrptw_lib.h"], deps = [ - ":fap_parser", "//ortools/base", - "//ortools/base:map_util", - "//ortools/constraint_solver:cp", - "@com_google_absl//absl/container:btree", + "//ortools/constraint_solver:routing", + "//ortools/constraint_solver:routing_flags", + "//ortools/util:random_engine", ], ) cc_binary( - name = "frequency_assignment_problem", - srcs = ["frequency_assignment_problem.cc"], + name = "cvrptw", + srcs = ["cvrptw.cc"], deps = [ - ":fap_model_printer", - ":fap_parser", - ":fap_utilities", + ":cvrptw_lib", "//ortools/base", - "//ortools/base:map_util", - "//ortools/constraint_solver:cp", + "//ortools/constraint_solver:routing", + "//ortools/constraint_solver:routing_flags", ], ) cc_binary( - name = "golomb_sat", - srcs = ["golomb_sat.cc"], + name = "cvrp_disjoint_tw", + srcs = ["cvrp_disjoint_tw.cc"], deps = [ + ":cvrptw_lib", "//ortools/base", - "//ortools/sat:cp_model", - "//ortools/sat:model", - "@com_google_absl//absl/strings", + "//ortools/constraint_solver:routing", + "//ortools/constraint_solver:routing_flags", ], ) cc_binary( - name = "integer_programming", - srcs = ["integer_programming.cc"], + name = "cvrptw_with_breaks", + srcs = ["cvrptw_with_breaks.cc"], deps = [ + ":cvrptw_lib", "//ortools/base", - "//ortools/linear_solver", + "//ortools/constraint_solver:routing", + "//ortools/constraint_solver:routing_enums_cc_proto", + "//ortools/constraint_solver:routing_flags", + "@com_google_absl//absl/strings", ], ) cc_binary( - name = "jobshop_sat", - srcs = [ - "jobshop_sat.cc", + name = "cvrptw_with_resources", + srcs = ["cvrptw_with_resources.cc"], + deps = [ + ":cvrptw_lib", + "//ortools/base", + "//ortools/constraint_solver:routing", + "//ortools/constraint_solver:routing_flags", ], +) + +cc_binary( + name = "cvrptw_with_stop_times_and_resources", + srcs = ["cvrptw_with_stop_times_and_resources.cc"], deps = [ + ":cvrptw_lib", "//ortools/base", - "//ortools/base:file", - "//ortools/base:timer", - "//ortools/sat:cp_model", - "//ortools/sat:cp_model_solver", - "//ortools/sat:model", - "//ortools/scheduling:jobshop_scheduling_cc_proto", - "//ortools/scheduling:jobshop_scheduling_parser", + "//ortools/constraint_solver:routing", + "//ortools/constraint_solver:routing_flags", "@com_google_absl//absl/strings", ], ) cc_binary( - name = "knapsack_2d_sat", - srcs = [ - "knapsack_2d_sat.cc", + name = "cvrptw_with_refueling", + srcs = ["cvrptw_with_refueling.cc"], + deps = [ + ":cvrptw_lib", + "//ortools/base", + "//ortools/constraint_solver:routing", + "//ortools/constraint_solver:routing_flags", ], +) + +cc_binary( + name = "pdptw", + srcs = ["pdptw.cc"], deps = [ "//ortools/base", - "//ortools/packing:binpacking_2d_parser", - "//ortools/packing:multiple_dimensions_bin_packing_cc_proto", - "//ortools/sat:cp_model", + "//ortools/base:file", + "//ortools/base:mathutil", + "//ortools/constraint_solver:routing", + "//ortools/constraint_solver:routing_flags", "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", "@com_google_protobuf//:protobuf", ], ) +# Linear and integer programming examples. cc_binary( - name = "linear_assignment_api", - srcs = ["linear_assignment_api.cc"], + name = "integer_programming", + srcs = ["integer_programming.cc"], deps = [ "//ortools/base", - "//ortools/graph:ebert_graph", - "//ortools/graph:linear_assignment", + "//ortools/linear_solver", + "@com_google_absl//absl/strings", ], ) @@ -304,6 +393,8 @@ cc_binary( "//ortools/base", "//ortools/linear_solver", "//ortools/linear_solver:linear_solver_cc_proto", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/strings", ], ) @@ -318,45 +409,59 @@ cc_binary( ) cc_binary( - name = "magic_sequence_sat", - srcs = ["magic_sequence_sat.cc"], + name = "strawberry_fields_with_column_generation", + srcs = ["strawberry_fields_with_column_generation.cc"], deps = [ "//ortools/base", - "//ortools/sat:cp_model", - "//ortools/sat:model", - "@com_google_absl//absl/strings", + "//ortools/linear_solver", + "@com_google_absl//absl/strings:str_format", ], ) -cc_binary( - name = "magic_square_sat", - srcs = ["magic_square_sat.cc"], +# Dimacs assignment problems +cc_library( + name = "print_dimacs_assignment", + hdrs = ["print_dimacs_assignment.h"], deps = [ "//ortools/base", - "//ortools/sat:cp_model", - "//ortools/sat:model", + "//ortools/base:file", + "//ortools/graph:ebert_graph", + "//ortools/graph:linear_assignment", + "@com_google_absl//absl/status", "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", ], ) -cc_binary( - name = "max_flow", - srcs = ["max_flow.cc"], +cc_library( + name = "parse_dimacs_assignment", + hdrs = ["parse_dimacs_assignment.h"], deps = [ "//ortools/base", - "//ortools/graph:max_flow", + "//ortools/graph:ebert_graph", + "//ortools/graph:linear_assignment", + "//ortools/util:filelineiter", + "@com_google_absl//absl/strings", ], ) cc_binary( - name = "min_cost_flow", - srcs = ["min_cost_flow.cc"], + name = "dimacs_assignment", + srcs = ["dimacs_assignment.cc"], deps = [ + ":parse_dimacs_assignment", + ":print_dimacs_assignment", + "//ortools/algorithms:hungarian", "//ortools/base", - "//ortools/graph:min_cost_flow", + "//ortools/base:timer", + "//ortools/graph:ebert_graph", + "//ortools/graph:linear_assignment", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/strings:str_format", ], ) +# MPS driver for LP and MIP. cc_binary( name = "mps_driver", srcs = ["mps_driver.cc"], @@ -367,144 +472,131 @@ cc_binary( "//ortools/glop:parameters_cc_proto", "//ortools/lp_data:mps_reader", "//ortools/util:proto_tools", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/status", "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", "@com_google_protobuf//:protobuf", ], ) +# Linear Assignment C++ Example cc_binary( - name = "multi_knapsack_sat", - srcs = ["multi_knapsack_sat.cc"], + name = "linear_assignment_api", + srcs = ["linear_assignment_api.cc"], deps = [ "//ortools/base", - "//ortools/sat:cp_model", - "//ortools/sat:model", - "@com_google_absl//absl/strings", + "//ortools/graph:ebert_graph", + "//ortools/graph:linear_assignment", ], ) +# Flow C++ Example cc_binary( - name = "network_routing_sat", - srcs = ["network_routing_sat.cc"], + name = "flow_api", + srcs = ["flow_api.cc"], deps = [ "//ortools/base", - "//ortools/base:hash", - "//ortools/base:map_util", - "//ortools/graph:shortestpaths", - "//ortools/sat:cp_model", - "//ortools/sat:model", - "//ortools/util:tuple_set", - "@com_google_absl//absl/strings", + "//ortools/graph:ebert_graph", + "//ortools/graph:max_flow", + "//ortools/graph:min_cost_flow", ], ) cc_binary( - name = "nqueens", - srcs = ["nqueens.cc"], + name = "max_flow", + srcs = ["max_flow.cc"], deps = [ "//ortools/base", - "//ortools/base:map_util", - "//ortools/constraint_solver:cp", + "//ortools/graph:max_flow", ], ) cc_binary( - name = "pdlp_solve", - srcs = ["pdlp_solve.cc"], + name = "min_cost_flow", + srcs = ["min_cost_flow.cc"], deps = [ "//ortools/base", - "//ortools/pdlp:primal_dual_hybrid_gradient", - "//ortools/pdlp:solvers_cc_proto", - "//ortools/pdlp:solve_log_cc_proto", - "//ortools/pdlp:quadratic_program_io", - "//ortools/linear_solver:linear_solver_cc_proto", - "//ortools/port:proto_utils", - "//ortools/util:sigint", - "@com_google_absl//absl/time", + "//ortools/graph:min_cost_flow", ], ) -cc_binary( - name = "pdptw", - srcs = ["pdptw.cc"], +# Frequency Assignment Problem +cc_library( + name = "fap_parser", + hdrs = ["fap_parser.h"], deps = [ "//ortools/base", "//ortools/base:file", - "//ortools/base:mathutil", - "//ortools/constraint_solver:routing", - "//ortools/constraint_solver:routing_flags", + "//ortools/base:hash", + "//ortools/base:map_util", + "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/strings", ], ) -cc_binary( - name = "qap_sat", - srcs = ["qap_sat.cc"], +cc_library( + name = "fap_model_printer", + hdrs = ["fap_model_printer.h"], deps = [ + ":fap_parser", "//ortools/base", - "//ortools/sat:cp_model", - "//ortools/sat:model", - "//ortools/sat:sat_parameters_cc_proto", - "//ortools/util:qap_reader", + "//ortools/base:file", "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", ], ) -cc_binary( - name = "random_tsp", - srcs = ["random_tsp.cc"], +cc_library( + name = "fap_utilities", + hdrs = ["fap_utilities.h"], deps = [ + ":fap_parser", "//ortools/base", - "//ortools/constraint_solver:routing", - "//ortools/constraint_solver:routing_flags", - "//ortools/util:random_engine", - "@com_google_absl//absl/strings", - "@com_google_protobuf//:protobuf", + "//ortools/base:map_util", + "//ortools/constraint_solver:cp", + "@com_google_absl//absl/container:btree", ], ) cc_binary( - name = "shift_minimization_sat", - srcs = [ - "shift_minimization_sat.cc", - ], + name = "frequency_assignment_problem", + srcs = ["frequency_assignment_problem.cc"], deps = [ + ":fap_model_printer", + ":fap_parser", + ":fap_utilities", "//ortools/base", - "//ortools/base:file", - "//ortools/sat:cp_model", - "//ortools/sat:model", - "//ortools/util:filelineiter", + "//ortools/base:map_util", + "//ortools/constraint_solver:cp", + "@com_google_absl//absl/container:btree", "@com_google_absl//absl/strings", ], ) +# Quadratic assignment problem. cc_binary( - name = "slitherlink_sat", - srcs = ["slitherlink_sat.cc"], + name = "qap_sat", + srcs = ["qap_sat.cc"], deps = [ "//ortools/base", "//ortools/sat:cp_model", "//ortools/sat:model", + "//ortools/sat:sat_parameters_cc_proto", + "//ortools/util:qap_reader", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/strings", + "@com_google_protobuf//:protobuf", ], ) cc_binary( - name = "sports_scheduling_sat", - srcs = ["sports_scheduling_sat.cc"], + name = "slitherlink_sat", + srcs = ["slitherlink_sat.cc"], deps = [ - "//ortools/base", "//ortools/sat:cp_model", "//ortools/sat:model", - "@com_google_absl//absl/strings", - ], -) - -cc_binary( - name = "strawberry_fields_with_column_generation", - srcs = ["strawberry_fields_with_column_generation.cc"], - deps = [ - "//ortools/base", - "//ortools/linear_solver", + "@com_google_absl//absl/strings:str_format", ], ) @@ -514,6 +606,10 @@ cc_binary( deps = [ "//ortools/base", "//ortools/linear_solver", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/flags:parse", + "@com_google_absl//absl/random", + "@com_google_protobuf//:protobuf", ], ) @@ -522,48 +618,23 @@ cc_binary( srcs = ["variable_intervals_sat.cc"], deps = [ "//ortools/sat:cp_model", + "//ortools/sat:sat_parameters_cc_proto", "//ortools/util:time_limit", ], ) cc_binary( - name = "vector_bin_packing_solver", - srcs = [ - "vector_bin_packing_solver.cc", - ], - deps = [ - "//ortools/base", - "//ortools/base:file", - "//ortools/packing:arc_flow_builder", - "//ortools/packing:arc_flow_solver", - "//ortools/packing:vector_bin_packing_cc_proto", - "//ortools/packing:vector_bin_packing_parser", - "@com_google_absl//absl/flags:flag", - "@com_google_absl//absl/status", - "@com_google_absl//absl/strings", - ], -) - -cc_binary( - name = "weighted_tardiness_sat", - srcs = [ - "weighted_tardiness_sat.cc", - ], + name = "pdlp_solve", + srcs = ["pdlp_solve.cc"], deps = [ "//ortools/base", - "//ortools/base:file", - "//ortools/sat:cp_model", - "//ortools/sat:cp_model_solver", - "//ortools/sat:disjunctive", - "//ortools/sat:integer", - "//ortools/sat:integer_expr", - "//ortools/sat:intervals", - "//ortools/sat:model", - "//ortools/sat:optimization", - "//ortools/sat:precedences", - "//ortools/sat:sat_solver", - "//ortools/util:filelineiter", - "@com_google_absl//absl/strings", - "@com_google_protobuf//:protobuf", + "//ortools/linear_solver:linear_solver_cc_proto", + "//ortools/pdlp:primal_dual_hybrid_gradient", + "//ortools/pdlp:quadratic_program_io", + "//ortools/pdlp:solve_log_cc_proto", + "//ortools/pdlp:solvers_cc_proto", + "//ortools/port:proto_utils", + "//ortools/util:sigint", + "@com_google_absl//absl/time", ], ) diff --git a/examples/cpp/binpacking_2d_sat.cc b/examples/cpp/binpacking_2d_sat.cc index 51df755c27c..424fa94dcc6 100644 --- a/examples/cpp/binpacking_2d_sat.cc +++ b/examples/cpp/binpacking_2d_sat.cc @@ -16,15 +16,12 @@ // too), and tries to fit all rectangles in the minimum numbers of bins (they // have the size of the main rectangle.) -#include #include -#include #include #include #include "absl/flags/flag.h" #include "google/protobuf/text_format.h" -#include "ortools/base/commandlineflags.h" #include "ortools/base/init_google.h" #include "ortools/base/logging.h" #include "ortools/packing/binpacking_2d_parser.h" @@ -38,15 +35,13 @@ ABSL_FLAG(int, max_bins, 0, "Maximum number of bins. The 0 default value implies the code will " "use some heuristics to compute this number."); ABSL_FLAG(bool, symmetry_breaking, true, "Use symmetry breaking constraints"); -ABSL_FLAG(bool, global_area_constraint, false, - "Redundant constraint to link the global area covered"); ABSL_FLAG(bool, alternate_model, true, "A different way to express the objective"); namespace operations_research { namespace sat { -// Load a 2D binpacking problem and solve it. +// Load a 2D bin packing problem and solve it. void LoadAndSolve(const std::string& file_name, int instance) { packing::BinPacking2dParser parser; if (!parser.Load2BPFile(file_name, instance)) { @@ -139,20 +134,6 @@ void LoadAndSolve(const std::string& file_name, int instance) { LOG(FATAL) << num_dimensions << " dimensions not supported."; } - // Redundant constraint. - // The sum of areas in each bin is the sum of all items area. - if (absl::GetFlag(FLAGS_global_area_constraint)) { - LinearExpr sum_of_areas; - for (int item = 0; item < num_items; ++item) { - const int64_t item_area = problem.items(item).shapes(0).dimensions(0) * - problem.items(item).shapes(0).dimensions(1); - for (int b = 0; b < max_bins; ++b) { - sum_of_areas += item_to_bin[item][b] * item_area; - } - } - cp_model.AddEquality(sum_of_areas, sum_of_items_area); - } - if (absl::GetFlag(FLAGS_alternate_model)) { const IntVar obj = cp_model.NewIntVar(Domain(trivial_lb, max_bins)); cp_model.Minimize(obj); @@ -203,8 +184,6 @@ void LoadAndSolve(const std::string& file_name, int instance) { // Setup parameters. SatParameters parameters; parameters.set_log_search_progress(true); - parameters.set_use_timetabling_in_no_overlap_2d(true); - parameters.set_use_energetic_reasoning_in_no_overlap_2d(true); // Parse the --params flag. if (!absl::GetFlag(FLAGS_params).empty()) { diff --git a/examples/cpp/constraint_programming_cp.cc b/examples/cpp/constraint_programming_cp.cc index ca1f35c275c..0a53692907f 100644 --- a/examples/cpp/constraint_programming_cp.cc +++ b/examples/cpp/constraint_programming_cp.cc @@ -13,6 +13,8 @@ // Constraint programming example that shows how to use the API. +#include + #include "ortools/base/init_google.h" #include "ortools/base/logging.h" #include "ortools/constraint_solver/constraint_solver.h" @@ -55,8 +57,8 @@ void RunConstraintProgrammingExample() { } // namespace operations_research int main(int argc, char** argv) { - google::InitGoogleLogging(argv[0]); absl::SetFlag(&FLAGS_stderrthreshold, 0); + InitGoogle(argv[0], &argc, &argv, true); operations_research::RunConstraintProgrammingExample(); return EXIT_SUCCESS; } diff --git a/examples/cpp/costas_array_sat.cc b/examples/cpp/costas_array_sat.cc index e27d15e957f..74f51877a70 100644 --- a/examples/cpp/costas_array_sat.cc +++ b/examples/cpp/costas_array_sat.cc @@ -11,7 +11,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -// // Costas Array Problem // // Finds an NxN matrix of 0s and 1s, with only one 1 per row, diff --git a/examples/cpp/course_scheduling.cc b/examples/cpp/course_scheduling.cc index 078e5a95f43..b63e8613603 100644 --- a/examples/cpp/course_scheduling.cc +++ b/examples/cpp/course_scheduling.cc @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -169,7 +170,7 @@ CourseSchedulingResult CourseSchedulingSolver::Solve( if (!validation_status.ok()) { result.set_solver_status( CourseSchedulingResultStatus::SOLVER_MODEL_INVALID); - result.set_message(std::string(validation_status.message())); + result.set_message(validation_status.message()); return result; } @@ -184,7 +185,7 @@ CourseSchedulingResult CourseSchedulingSolver::Solve( const auto verifier_status = VerifyCourseSchedulingResult(model, result); if (!verifier_status.ok()) { result.set_solver_status(CourseSchedulingResultStatus::ABNORMAL); - result.set_message(std::string(verifier_status.message())); + result.set_message(verifier_status.message()); } return result; diff --git a/examples/cpp/course_scheduling.h b/examples/cpp/course_scheduling.h index 98f8dfabb91..e0db57b6479 100644 --- a/examples/cpp/course_scheduling.h +++ b/examples/cpp/course_scheduling.h @@ -29,7 +29,7 @@ namespace operations_research { class CourseSchedulingSolver { public: CourseSchedulingSolver() : solve_for_rooms_(false) {} - virtual ~CourseSchedulingSolver() {} + virtual ~CourseSchedulingSolver() = default; using ConflictPairs = absl::flat_hash_set>; diff --git a/examples/cpp/course_scheduling_run.cc b/examples/cpp/course_scheduling_run.cc index 691212f5b7e..c6d26cf869a 100644 --- a/examples/cpp/course_scheduling_run.cc +++ b/examples/cpp/course_scheduling_run.cc @@ -21,13 +21,13 @@ #include -#include "absl/flags/parse.h" -#include "absl/flags/usage.h" #include "examples/cpp/course_scheduling.h" -#include "examples/cpp/course_scheduling.pb.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/options.h" #include "ortools/base/timer.h" +#include "ortools/scheduling/course_scheduling.pb.h" ABSL_FLAG(std::string, input, "", "Input file containing a CourseSchedulingModel in text format."); @@ -105,8 +105,7 @@ void Main() { } // namespace operations_research int main(int argc, char** argv) { - google::InitGoogleLogging(argv[0]); - absl::ParseCommandLine(argc, argv); + InitGoogle(argv[0], &argc, &argv, /*remove_flags=*/true); operations_research::Main(); return EXIT_SUCCESS; } diff --git a/examples/cpp/dobble_ls.cc b/examples/cpp/dobble_ls.cc index 1da50046b6a..82d017470dd 100644 --- a/examples/cpp/dobble_ls.cc +++ b/examples/cpp/dobble_ls.cc @@ -90,7 +90,7 @@ class SymbolsSharedByTwoCardsConstraint : public Constraint { } } - ~SymbolsSharedByTwoCardsConstraint() override {} + ~SymbolsSharedByTwoCardsConstraint() override = default; // Adds observers (named Demon) to variable events. These demons are // responsible for implementing the propagation algorithm of the @@ -255,7 +255,7 @@ class DobbleOperator : public IntVarLocalSearchOperator { } } - ~DobbleOperator() override {} + ~DobbleOperator() override = default; protected: // OnStart() simply stores the current symbols per card in @@ -314,7 +314,7 @@ class SwapSymbols : public DobbleOperator { current_symbol1_(-1), current_symbol2_(-1) {} - ~SwapSymbols() override {} + ~SwapSymbols() override = default; // Finds the next swap, returns false when it has finished. bool MakeOneNeighbor() override { @@ -385,7 +385,7 @@ class SwapSymbolsOnCardPairs : public DobbleOperator { CHECK_GE(max_num_swaps, 2); } - ~SwapSymbolsOnCardPairs() override {} + ~SwapSymbolsOnCardPairs() override = default; protected: bool MakeOneNeighbor() override { @@ -547,7 +547,7 @@ class DobbleFilter : public IntVarLocalSearchFilter { void ClearBitset() { temporary_bitset_ = 0; } // For each touched card, compare against all others to compute the - // delta in term of cost. We use an bitset to avoid counting twice + // delta in term of cost. We use a bitset to avoid counting twice // between two cards appearing in the local search move. int ComputeNewCost(const std::vector& touched_cards) { ClearBitset(); diff --git a/examples/cpp/fap_model_printer.h b/examples/cpp/fap_model_printer.h index 313c085f6fd..56204ef61ce 100644 --- a/examples/cpp/fap_model_printer.h +++ b/examples/cpp/fap_model_printer.h @@ -19,6 +19,7 @@ #ifndef OR_TOOLS_EXAMPLES_FAP_MODEL_PRINTER_H_ #define OR_TOOLS_EXAMPLES_FAP_MODEL_PRINTER_H_ +#include #include #include @@ -33,7 +34,7 @@ class FapModelPrinter { public: FapModelPrinter(const absl::btree_map& variables, const std::vector& constraints, - const std::string& objective, const std::vector& values); + absl::string_view objective, const std::vector& values); ~FapModelPrinter(); void PrintFapObjective(); @@ -51,7 +52,7 @@ class FapModelPrinter { FapModelPrinter::FapModelPrinter(const absl::btree_map& variables, const std::vector& constraints, - const std::string& objective, + absl::string_view objective, const std::vector& values) : variables_(variables), constraints_(constraints), diff --git a/examples/cpp/fap_parser.h b/examples/cpp/fap_parser.h index 450c47bd9fc..d44a1701cf8 100644 --- a/examples/cpp/fap_parser.h +++ b/examples/cpp/fap_parser.h @@ -11,23 +11,25 @@ // See the License for the specific language governing permissions and // limitations under the License. -// // Reading and parsing the data of Frequency Assignment Problem // Format: http://www.inra.fr/mia/T/schiex/Doc/CELAR.shtml#synt -// #ifndef OR_TOOLS_EXAMPLES_FAP_PARSER_H_ #define OR_TOOLS_EXAMPLES_FAP_PARSER_H_ +#include #include #include #include +#include "absl/container/btree_map.h" #include "absl/container/flat_hash_map.h" +#include "absl/strings/match.h" #include "absl/strings/numbers.h" #include "absl/strings/str_split.h" #include "ortools/base/file.h" #include "ortools/base/logging.h" +#include "ortools/base/macros.h" #include "ortools/base/map_util.h" namespace operations_research { @@ -105,7 +107,7 @@ struct FapConstraint { bool hard = false; }; -// The FapComponent struct represents an component of the RLFAP graph. +// The FapComponent struct represents a component of the RLFAP graph. // It models an independent sub-problem of the initial instance. struct FapComponent { // Fields: @@ -124,7 +126,9 @@ class VariableParser { explicit VariableParser(const std::string& data_directory); ~VariableParser(); - const absl::btree_map& variables() const { return variables_; } + const absl::btree_map& variables() const { + return variables_; + } void Parse(); @@ -146,7 +150,9 @@ class DomainParser { explicit DomainParser(const std::string& data_directory); ~DomainParser(); - const absl::btree_map >& domains() const { return domains_; } + const absl::btree_map >& domains() const { + return domains_; + } void Parse(); @@ -198,9 +204,9 @@ class ParametersParser { private: const std::string filename_; - static constexpr int constraint_coefficient_no_ = 4; - static constexpr int variable_coefficient_no_ = 4; - static constexpr int coefficient_no_ = 8; + static constexpr int kConstraintCoefficientNo = 4; + static constexpr int kVariableCoefficientNo = 4; + static constexpr int kCoefficientNo = 8; std::string objective_; std::vector constraint_weights_; std::vector variable_weights_; @@ -217,13 +223,12 @@ int strtoint32(const std::string& word) { // Function that finds the disjoint sub-graphs of the graph of the instance. void FindComponents(const std::vector& constraints, const absl::btree_map& variables, - const int maximum_variable_id, + int maximum_variable_id, absl::flat_hash_map* components); // Function that computes the impact of a constraint. int EvaluateConstraintImpact(const absl::btree_map& variables, - const int max_weight_cost, - const FapConstraint constraint); + int max_weight_cost, FapConstraint constraint); // Function that parses an instance of frequency assignment problem. void ParseInstance(const std::string& data_directory, bool find_components, @@ -244,7 +249,7 @@ void ParseFileByLines(const std::string& filename, VariableParser::VariableParser(const std::string& data_directory) : filename_(data_directory + "/var.txt") {} -VariableParser::~VariableParser() {} +VariableParser::~VariableParser() = default; void VariableParser::Parse() { std::vector lines; @@ -258,12 +263,12 @@ void VariableParser::Parse() { CHECK_GE(tokens.size(), 2); FapVariable variable; - variable.domain_index = strtoint32(tokens[1].c_str()); + variable.domain_index = strtoint32(tokens[1]); if (tokens.size() > 3) { - variable.initial_position = strtoint32(tokens[2].c_str()); - variable.mobility_index = strtoint32(tokens[3].c_str()); + variable.initial_position = strtoint32(tokens[2]); + variable.mobility_index = strtoint32(tokens[3]); } - gtl::InsertOrUpdate(&variables_, strtoint32(tokens[0].c_str()), variable); + gtl::InsertOrUpdate(&variables_, strtoint32(tokens[0]), variable); } } @@ -271,7 +276,7 @@ void VariableParser::Parse() { DomainParser::DomainParser(const std::string& data_directory) : filename_(data_directory + "/dom.txt") {} -DomainParser::~DomainParser() {} +DomainParser::~DomainParser() = default; void DomainParser::Parse() { std::vector lines; @@ -284,12 +289,12 @@ void DomainParser::Parse() { } CHECK_GE(tokens.size(), 2); - const int key = strtoint32(tokens[0].c_str()); + const int key = strtoint32(tokens[0]); std::vector domain; domain.clear(); for (int i = 2; i < tokens.size(); ++i) { - domain.push_back(strtoint32(tokens[i].c_str())); + domain.push_back(strtoint32(tokens[i])); } if (!domain.empty()) { @@ -302,7 +307,7 @@ void DomainParser::Parse() { ConstraintParser::ConstraintParser(const std::string& data_directory) : filename_(data_directory + "/ctr.txt") {} -ConstraintParser::~ConstraintParser() {} +ConstraintParser::~ConstraintParser() = default; void ConstraintParser::Parse() { std::vector lines; @@ -316,31 +321,31 @@ void ConstraintParser::Parse() { CHECK_GE(tokens.size(), 5); FapConstraint constraint; - constraint.variable1 = strtoint32(tokens[0].c_str()); - constraint.variable2 = strtoint32(tokens[1].c_str()); + constraint.variable1 = strtoint32(tokens[0]); + constraint.variable2 = strtoint32(tokens[1]); constraint.type = tokens[2]; constraint.operation = tokens[3]; - constraint.value = strtoint32(tokens[4].c_str()); + constraint.value = strtoint32(tokens[4]); if (tokens.size() > 5) { - constraint.weight_index = strtoint32(tokens[5].c_str()); + constraint.weight_index = strtoint32(tokens[5]); } constraints_.push_back(constraint); } } // ParametersParser Implementation -const int ParametersParser::constraint_coefficient_no_; -const int ParametersParser::variable_coefficient_no_; -const int ParametersParser::coefficient_no_; +const int ParametersParser::kConstraintCoefficientNo; +const int ParametersParser::kVariableCoefficientNo; +const int ParametersParser::kCoefficientNo; ParametersParser::ParametersParser(const std::string& data_directory) : filename_(data_directory + "/cst.txt"), objective_(""), - constraint_weights_(constraint_coefficient_no_, 0), - variable_weights_(variable_coefficient_no_, 0) {} + constraint_weights_(kConstraintCoefficientNo, 0), + variable_weights_(kVariableCoefficientNo, 0) {} -ParametersParser::~ParametersParser() {} +ParametersParser::~ParametersParser() = default; void ParametersParser::Parse() { bool objective = true; @@ -355,35 +360,33 @@ void ParametersParser::Parse() { ParseFileByLines(filename_, &lines); for (const std::string& line : lines) { if (objective) { - largest_token = - largest_token || (line.find("largest") != std::string::npos); - value_token = value_token || (line.find("value") != std::string::npos); - number_token = number_token || (line.find("number") != std::string::npos); - values_token = values_token || (line.find("values") != std::string::npos); - coefficient = - coefficient || (line.find("coefficient") != std::string::npos); + largest_token = largest_token || absl::StrContains(line, "largest"); + value_token = value_token || absl::StrContains(line, "value"); + number_token = number_token || absl::StrContains(line, "number"); + values_token = values_token || absl::StrContains(line, "values"); + coefficient = coefficient || absl::StrContains(line, "coefficient"); } if (coefficient) { - CHECK_EQ(coefficient_no_, - constraint_coefficient_no_ + variable_coefficient_no_); + CHECK_EQ(kCoefficientNo, + kConstraintCoefficientNo + kVariableCoefficientNo); objective = false; - if (line.find("=") != std::string::npos) { + if (absl::StrContains(line, "=")) { std::vector tokens = absl::StrSplit(line, ' ', absl::SkipEmpty()); CHECK_GE(tokens.size(), 3); - coefficients.push_back(strtoint32(tokens[2].c_str())); + coefficients.push_back(strtoint32(tokens[2])); } } } if (coefficient) { - CHECK_EQ(coefficient_no_, coefficients.size()); - for (int i = 0; i < coefficient_no_; i++) { - if (i < constraint_coefficient_no_) { + CHECK_EQ(kCoefficientNo, coefficients.size()); + for (int i = 0; i < kCoefficientNo; i++) { + if (i < kConstraintCoefficientNo) { constraint_weights_[i] = coefficients[i]; } else { - variable_weights_[i - constraint_coefficient_no_] = coefficients[i]; + variable_weights_[i - kConstraintCoefficientNo] = coefficients[i]; } } } @@ -428,7 +431,7 @@ void FindComponents(const std::vector& constraints, // If variable1 belongs to an existing component, variable2 should // also be included in the same component. const int component_index = in_component[variable_id1]; - CHECK(gtl::ContainsKey(*components, component_index)); + CHECK(components->contains(component_index)); gtl::InsertOrUpdate(&((*components)[component_index].variables), variable_id2, variable2); in_component[variable_id2] = component_index; @@ -438,7 +441,7 @@ void FindComponents(const std::vector& constraints, // If variable2 belongs to an existing component, variable1 should // also be included in the same component. const int component_index = in_component[variable_id2]; - CHECK(gtl::ContainsKey(*components, component_index)); + CHECK(components->contains(component_index)); gtl::InsertOrUpdate(&((*components)[component_index].variables), variable_id1, variable1); in_component[variable_id1] = component_index; @@ -451,8 +454,8 @@ void FindComponents(const std::vector& constraints, std::min(component_index1, component_index2); const int max_component_index = std::max(component_index1, component_index2); - CHECK(gtl::ContainsKey(*components, min_component_index)); - CHECK(gtl::ContainsKey(*components, max_component_index)); + CHECK(components->contains(min_component_index)); + CHECK(components->contains(max_component_index)); if (min_component_index != max_component_index) { // Update the component_index of maximum indexed component's variables. for (const auto& variable : diff --git a/examples/cpp/fap_utilities.h b/examples/cpp/fap_utilities.h index b2aaca0bec8..580ed3ee8d3 100644 --- a/examples/cpp/fap_utilities.h +++ b/examples/cpp/fap_utilities.h @@ -40,29 +40,29 @@ bool CheckConstraintSatisfaction( // Checks if the solution given from the Solver has not modified the values of // the variables that were initially assigned and denoted as hard in var.txt. -bool CheckVariablePosition(const absl::btree_map& data_variables, - const std::vector& variables, - const absl::btree_map& index_from_key); +bool CheckVariablePosition( + const absl::btree_map& data_variables, + const std::vector& variables, + const absl::btree_map& index_from_key); // Counts the number of different values in the variable vector. int NumberOfAssignedValues(const std::vector& variables); // Prints the duration of the solving process. -void PrintElapsedTime(const int64_t time1, const int64_t time2); +void PrintElapsedTime(int64_t time1, int64_t time2); // Prints the solution found by the Hard Solver for feasible instances. -void PrintResultsHard(SolutionCollector* const collector, +void PrintResultsHard(SolutionCollector* collector, const std::vector& variables, - IntVar* const objective_var, + IntVar* objective_var, const absl::btree_map& data_variables, const std::vector& data_constraints, const absl::btree_map& index_from_key, const std::vector& key_from_index); // Prints the solution found by the Soft Solver for unfeasible instances. -void PrintResultsSoft(SolutionCollector* const collector, - const std::vector& variables, - IntVar* const total_cost, +void PrintResultsSoft(SolutionCollector* collector, + const std::vector& variables, IntVar* total_cost, const absl::btree_map& hard_variables, const std::vector& hard_constraints, const absl::btree_map& soft_variables, diff --git a/examples/cpp/frequency_assignment_problem.cc b/examples/cpp/frequency_assignment_problem.cc index 0f416ea6404..443564dffe1 100644 --- a/examples/cpp/frequency_assignment_problem.cc +++ b/examples/cpp/frequency_assignment_problem.cc @@ -25,7 +25,7 @@ // The solution of the problem can be based on various criteria: // - Simple satisfaction // - Minimizing the number of distinct frequencies used -// - Minimizing the maximum frequency used, i.e minimizing the total width of +// - Minimizing the maximum frequency used, i.e. minimizing the total width of // the spectrum // - Minimizing a weighted sum of violated constraints if the problem is // inconsistent @@ -57,7 +57,6 @@ #include "examples/cpp/fap_model_printer.h" #include "examples/cpp/fap_parser.h" #include "examples/cpp/fap_utilities.h" -#include "ortools/base/commandlineflags.h" #include "ortools/base/init_google.h" #include "ortools/base/logging.h" #include "ortools/base/map_util.h" @@ -101,7 +100,7 @@ class OrderingDecision : public Decision { variable2_(variable2), value_(value), operator_(std::move(operation)) {} - ~OrderingDecision() override {} + ~OrderingDecision() override = default; // Apply will be called first when the decision is executed. void Apply(Solver* const s) override { @@ -143,16 +142,16 @@ class ConstraintDecision : public Decision { explicit ConstraintDecision(IntVar* const constraint_violation) : constraint_violation_(constraint_violation) {} - ~ConstraintDecision() override {} + ~ConstraintDecision() override = default; // Apply will be called first when the decision is executed. - void Apply(Solver* const s) override { + void Apply(Solver* const) override { // The constraint with which the builder is dealing, will be satisfied. constraint_violation_->SetValue(0); } // Refute will be called after a backtrack. - void Refute(Solver* const s) override { + void Refute(Solver* const) override { // The constraint with which the builder is dealing, will not be satisfied. constraint_violation_->SetValue(1); } @@ -193,7 +192,7 @@ class OrderingBuilder : public DecisionBuilder { CHECK_EQ(variable_state_.size(), variables_.size()); } - ~OrderingBuilder() override {} + ~OrderingBuilder() override = default; Decision* Next(Solver* const s) override { if (iter_ < size_) { @@ -373,9 +372,10 @@ int64_t ValueEvaluator( // The variables which participate in more constraints and have the // smaller domain should be in higher priority for assignment. -int64_t VariableEvaluator(const std::vector& key_from_index, - const absl::btree_map& data_variables, - int64_t variable_index) { +int64_t VariableEvaluator( + const std::vector& key_from_index, + const absl::btree_map& data_variables, + int64_t variable_index) { FapVariable variable = gtl::FindOrDie(data_variables, key_from_index[variable_index]); int64_t result = -(variable.degree * 100 / variable.domain_size); @@ -383,10 +383,11 @@ int64_t VariableEvaluator(const std::vector& key_from_index, } // Creates the variables of the solver from the parsed data. -void CreateModelVariables(const absl::btree_map& data_variables, - Solver* solver, std::vector* model_variables, - absl::btree_map* index_from_key, - std::vector* key_from_index) { +void CreateModelVariables( + const absl::btree_map& data_variables, Solver* solver, + std::vector* model_variables, + absl::btree_map* index_from_key, + std::vector* key_from_index) { CHECK(solver != nullptr); CHECK(model_variables != nullptr); CHECK(index_from_key != nullptr); @@ -630,9 +631,10 @@ void HardFapSolver(const absl::btree_map& data_variables, } // Splits variables of the instance to hard and soft. -void SplitVariablesHardSoft(const absl::btree_map& data_variables, - absl::btree_map* hard_variables, - absl::btree_map* soft_variables) { +void SplitVariablesHardSoft( + const absl::btree_map& data_variables, + absl::btree_map* hard_variables, + absl::btree_map* soft_variables) { for (const auto& it : data_variables) { if (it.second.initial_position != -1) { if (it.second.hard) { @@ -730,8 +732,8 @@ void PenalizeConstraintsViolation( // equal to 0 denotes that the instance is feasible. int SoftFapSolver(const absl::btree_map& data_variables, const std::vector& data_constraints, - absl::string_view data_objective, - const std::vector& values) { + absl::string_view /*data_objective*/, + const std::vector& /*values*/) { Solver solver("SoftFapSolver"); std::vector monitors; @@ -834,7 +836,7 @@ int SoftFapSolver(const absl::btree_map& data_variables, void SolveProblem(const absl::btree_map& variables, const std::vector& constraints, - const std::string& objective, const std::vector& values, + absl::string_view objective, const std::vector& values, bool soft) { // Print Instance! FapModelPrinter model_printer(variables, constraints, objective, values); diff --git a/examples/cpp/integer_programming.cc b/examples/cpp/integer_programming.cc index cb5f98909d3..86400484f43 100644 --- a/examples/cpp/integer_programming.cc +++ b/examples/cpp/integer_programming.cc @@ -91,8 +91,8 @@ void RunAllExamples() { } // namespace operations_research int main(int argc, char** argv) { - InitGoogle(argv[0], &argc, &argv, true); absl::SetFlag(&FLAGS_stderrthreshold, 0); + InitGoogle(argv[0], &argc, &argv, true); operations_research::RunAllExamples(); return EXIT_SUCCESS; } diff --git a/examples/cpp/jobshop_sat.cc b/examples/cpp/jobshop_sat.cc index ab5611c4afc..25147ab9767 100644 --- a/examples/cpp/jobshop_sat.cc +++ b/examples/cpp/jobshop_sat.cc @@ -34,7 +34,7 @@ ABSL_FLAG(std::string, params, "", "Sat parameters in text proto format."); ABSL_FLAG(bool, use_optional_variables, false, "Whether we use optional variables for bounds of an optional " "interval or not."); -ABSL_FLAG(bool, use_interval_makespan, true, +ABSL_FLAG(bool, use_interval_makespan, false, "Whether we encode the makespan using an interval or not."); ABSL_FLAG(bool, use_variable_duration_to_encode_transition, false, "Whether we move the transition cost to the alternative duration."); @@ -680,10 +680,10 @@ void Solve(const JsspInputProblem& problem) { // gives us a better lower bound on the makespan because this way we known // that it must be after all other intervals in each no-overlap constraint. // - // Otherwise, we will just add precence constraints between the last task of + // Otherwise, we will just add precedence constraints between the last task of // each job and the makespan variable. Alternatively, we could have added a // precedence relation between all tasks and the makespan for a similar - // propagation thanks to our "precedence" propagator in the dijsunctive but + // propagation thanks to our "precedence" propagator in the disjunctive but // that was slower than the interval trick when I tried. const IntVar makespan = cp_model.NewIntVar(Domain(0, horizon)); IntervalVar makespan_interval; @@ -753,6 +753,12 @@ void Solve(const JsspInputProblem& problem) { << absl::GetFlag(FLAGS_params); } + // Prefer objective_shaving_search over objective_lb_search. + if (parameters.num_workers() >= 16 && parameters.num_workers() < 24) { + parameters.add_ignore_subsolvers("objective_lb_search"); + parameters.add_extra_subsolvers("objective_shaving_search"); + } + const CpSolverResponse response = SolveWithParameters(cp_model.Build(), parameters); diff --git a/examples/cpp/linear_assignment_api.cc b/examples/cpp/linear_assignment_api.cc index 31252536724..dd432d9e0b1 100644 --- a/examples/cpp/linear_assignment_api.cc +++ b/examples/cpp/linear_assignment_api.cc @@ -53,17 +53,17 @@ void AnotherAssignment() { {{8, 7, 9, 9}, {5, 2, 7, 8}, {6, 1, 4, 9}, {2, 3, 2, 6}}); const int kSize = matrice.size(); ForwardStarGraph graph(2 * kSize, kSize * kSize); - LinearSumAssignment assignement(graph, kSize); + LinearSumAssignment assignment(graph, kSize); for (int i = 0; i < kSize; ++i) { CHECK_EQ(kSize, matrice[i].size()); for (int j = 0; j < kSize; ++j) { int arcIndex = graph.AddArc(i, j + kSize); - assignement.SetArcCost(arcIndex, matrice[i][j]); + assignment.SetArcCost(arcIndex, matrice[i][j]); } } - assignement.ComputeAssignment(); - LOG(INFO) << "Cost : " << assignement.GetCost(); + assignment.ComputeAssignment(); + LOG(INFO) << "Cost : " << assignment.GetCost(); } } // namespace operations_research diff --git a/examples/cpp/linear_programming.cc b/examples/cpp/linear_programming.cc index 427fd7bff19..d21d9cb8ffe 100644 --- a/examples/cpp/linear_programming.cc +++ b/examples/cpp/linear_programming.cc @@ -121,8 +121,8 @@ void RunAllExamples() { } // namespace operations_research int main(int argc, char** argv) { - InitGoogle(argv[0], &argc, &argv, true); absl::SetFlag(&FLAGS_stderrthreshold, 0); + InitGoogle(argv[0], &argc, &argv, true); operations_research::RunAllExamples(); return EXIT_SUCCESS; } diff --git a/examples/cpp/max_flow.cc b/examples/cpp/max_flow.cc index d6f225e88ac..52b44b4a7f2 100644 --- a/examples/cpp/max_flow.cc +++ b/examples/cpp/max_flow.cc @@ -1,4 +1,4 @@ -// Copyright 2018 Google LLC +// 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 @@ -13,6 +13,9 @@ #include "ortools/graph/max_flow.h" +#include +#include + #include "ortools/base/init_google.h" #include "ortools/base/logging.h" @@ -53,8 +56,8 @@ void SolveMaxFlow() { } // namespace operations_research int main(int argc, char** argv) { - google::InitGoogleLogging(argv[0]); absl::SetFlag(&FLAGS_stderrthreshold, 0); + InitGoogle(argv[0], &argc, &argv, true); operations_research::SolveMaxFlow(); return EXIT_SUCCESS; } diff --git a/examples/cpp/min_cost_flow.cc b/examples/cpp/min_cost_flow.cc index 2600188f36c..01e20b3ff17 100644 --- a/examples/cpp/min_cost_flow.cc +++ b/examples/cpp/min_cost_flow.cc @@ -1,4 +1,4 @@ -// Copyright 2018 Google LLC +// 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 @@ -13,6 +13,9 @@ #include "ortools/graph/min_cost_flow.h" +#include +#include + #include "ortools/base/init_google.h" #include "ortools/base/logging.h" @@ -69,8 +72,8 @@ void SolveMinCostFlow() { } // namespace operations_research int main(int argc, char** argv) { - google::InitGoogleLogging(argv[0]); absl::SetFlag(&FLAGS_stderrthreshold, 0); + InitGoogle(argv[0], &argc, &argv, true); operations_research::SolveMinCostFlow(); return EXIT_SUCCESS; } diff --git a/examples/cpp/mps_driver.cc b/examples/cpp/mps_driver.cc index a262ce8e923..9c02546bcb9 100644 --- a/examples/cpp/mps_driver.cc +++ b/examples/cpp/mps_driver.cc @@ -71,7 +71,7 @@ void ReadGlopParameters(GlopParameters* parameters) { if (!absl::GetFlag(FLAGS_params_file).empty()) { std::string params; CHECK_OK(file::GetContents(absl::GetFlag(FLAGS_params_file), ¶ms, - file::Defaults())); + file::Defaults())); CHECK(TextFormat::ParseFromString(params, parameters)) << params; } if (!absl::GetFlag(FLAGS_params).empty()) { diff --git a/examples/cpp/network_routing_sat.cc b/examples/cpp/network_routing_sat.cc index c462c70eb00..9bc26774a6d 100644 --- a/examples/cpp/network_routing_sat.cc +++ b/examples/cpp/network_routing_sat.cc @@ -17,7 +17,7 @@ // (source, destination, traffic), the goal is to assign one unique // path for each demand such that the cost is minimized. The cost is // defined by the maximum ratio utilization (traffic/capacity) for all -// arcs. There is also a penalty associated with an traffic of an arc +// arcs. There is also a penalty associated with a traffic of an arc // being above the comfort zone, 85% of the capacity by default. // Please note that constraint programming is well suited here because // we cannot have multiple active paths for a single demand. @@ -28,7 +28,6 @@ #include #include #include -#include #include #include #include @@ -39,12 +38,11 @@ #include "absl/container/flat_hash_set.h" #include "absl/flags/flag.h" #include "absl/random/uniform_int_distribution.h" -#include "absl/strings/str_cat.h" #include "absl/strings/str_format.h" #include "ortools/base/init_google.h" -#include "ortools/base/integral_types.h" #include "ortools/base/logging.h" -#include "ortools/graph/shortestpaths.h" +#include "ortools/graph/graph.h" +#include "ortools/graph/shortest_paths.h" #include "ortools/sat/cp_model.h" #include "ortools/sat/model.h" #include "ortools/util/time_limit.h" @@ -89,7 +87,6 @@ ABSL_FLAG(std::string, params, "", "Sat parameters."); namespace operations_research { namespace sat { // ---------- Data and Data Generation ---------- -static const int64_t kDisconnectedDistance = -1LL; // ----- Data ----- // Contains problem data. It assumes capacities are symmetrical: @@ -97,17 +94,14 @@ static const int64_t kDisconnectedDistance = -1LL; // Demands are not symmetrical. class NetworkRoutingData { public: - NetworkRoutingData() - : name_(""), num_nodes_(-1), max_capacity_(-1), fixed_charge_cost_(-1) {} + NetworkRoutingData() : name_(""), max_capacity_(-1), fixed_charge_cost_(-1) {} // Name of the problem. const std::string& name() const { return name_; } // Properties of the model. - int num_nodes() const { return num_nodes_; } - + int num_nodes() const { return graph_.num_nodes(); } int num_arcs() const { return all_arcs_.size(); } - int num_demands() const { return all_demands_.size(); } // Returns the capacity of an arc, and 0 if the arc is not defined. @@ -125,10 +119,13 @@ class NetworkRoutingData { } // External building API. - void set_num_nodes(int num_nodes) { num_nodes_ = num_nodes; } + void set_num_nodes(int num_nodes) { graph_.ReserveNodes(num_nodes); } void AddArc(int node1, int node2, int capacity) { - all_arcs_[std::make_pair(std::min(node1, node2), std::max(node1, node2))] = - capacity; + const int tail = std::min(node1, node2); + const int head = std::max(node1, node2); + graph_.AddArc(head, tail); + graph_.AddArc(tail, head); + all_arcs_[{tail, head}] = capacity; } void AddDemand(int source, int destination, int traffic) { all_demands_[std::make_pair(source, destination)] = traffic; @@ -136,14 +133,15 @@ class NetworkRoutingData { void set_name(absl::string_view name) { name_ = name; } void set_max_capacity(int max_capacity) { max_capacity_ = max_capacity; } void set_fixed_charge_cost(int cost) { fixed_charge_cost_ = cost; } + const util::ListGraph& graph() const { return graph_; } private: std::string name_; - int num_nodes_; int max_capacity_; int fixed_charge_cost_; - std::map, int> all_arcs_; - std::map, int> all_demands_; + absl::flat_hash_map, int> all_arcs_; + absl::flat_hash_map, int> all_demands_; + util::ListGraph graph_; }; // ----- Data Generation ----- @@ -364,7 +362,7 @@ class NetworkRoutingSolver { public: typedef absl::flat_hash_set OnePath; - NetworkRoutingSolver() : num_nodes_(-1) {} + NetworkRoutingSolver() = default; void ComputeAllPathsForOneDemandAndOnePathLength(int demand_index, int max_length, @@ -374,7 +372,7 @@ class NetworkRoutingSolver { std::vector arc_vars; std::vector node_vars; for (int i = 0; i < max_length; ++i) { - node_vars.push_back(cp_model.NewIntVar(Domain(0, num_nodes_ - 1))); + node_vars.push_back(cp_model.NewIntVar(Domain(0, num_nodes() - 1))); } for (int i = 0; i < max_length - 1; ++i) { arc_vars.push_back(cp_model.NewIntVar(Domain(-1, count_arcs() - 1))); @@ -422,7 +420,7 @@ class NetworkRoutingSolver { SolveCpModel(cp_model.Build(), &model); } - // This method will fill the all_paths_ data structure. all_paths + // This method will fill the all_paths_ data structure. all_paths_ // contains, for each demand, a vector of possible paths, stored as // a hash_set of arc indices. int ComputeAllPaths(int extra_hops, int max_paths) { @@ -430,6 +428,7 @@ class NetworkRoutingSolver { for (int demand_index = 0; demand_index < demands_array_.size(); ++demand_index) { const int min_path_length = all_min_path_lengths_[demand_index]; + CHECK_GE(min_path_length, 1); for (int max_length = min_path_length + 1; max_length <= min_path_length + extra_hops + 1; ++max_length) { ComputeAllPathsForOneDemandAndOnePathLength(demand_index, max_length, @@ -450,13 +449,13 @@ class NetworkRoutingSolver { void InitArcInfo(const NetworkRoutingData& data) { const int num_arcs = data.num_arcs(); capacity_.clear(); - capacity_.resize(num_nodes_); - for (int node_index = 0; node_index < num_nodes_; ++node_index) { - capacity_[node_index].resize(num_nodes_, 0); + capacity_.resize(num_nodes()); + for (int node_index = 0; node_index < num_nodes(); ++node_index) { + capacity_[node_index].resize(num_nodes(), 0); } int arc_id = 0; - for (int i = 0; i < num_nodes_ - 1; ++i) { - for (int j = i + 1; j < num_nodes_; ++j) { + for (int i = 0; i < num_nodes() - 1; ++i) { + for (int j = i + 1; j < num_nodes(); ++j) { const int capacity = data.Capacity(i, j); if (capacity > 0) { AddArcData(i, j, arc_id); @@ -478,8 +477,8 @@ class NetworkRoutingSolver { int InitDemandInfo(const NetworkRoutingData& data) { const int num_demands = data.num_demands(); int total_demand = 0; - for (int i = 0; i < num_nodes_; ++i) { - for (int j = 0; j < num_nodes_; ++j) { + for (int i = 0; i < num_nodes(); ++i) { + for (int j = 0; j < num_nodes(); ++j) { const int traffic = data.Demand(i, j); if (traffic > 0) { demands_array_.push_back(Demand(i, j, traffic)); @@ -495,15 +494,21 @@ class NetworkRoutingSolver { const int num_demands = data.num_demands(); int64_t total_cumulated_traffic = 0; all_min_path_lengths_.clear(); - std::vector paths; - for (int demand_index = 0; demand_index < num_demands; ++demand_index) { - paths.clear(); - const Demand& demand = demands_array_[demand_index]; - CHECK(DijkstraShortestPath( - num_nodes_, demand.source, demand.destination, - [this](int x, int y) { return HasArc(x, y); }, kDisconnectedDistance, - &paths)); - all_min_path_lengths_.push_back(paths.size() - 1); + + // Dummy vector for edge costs: always 1. + const std::vector distances(2 * count_arcs(), 1); + + for (const Demand& demand : demands_array_) { + PathContainer paths; + PathContainer::BuildInMemoryCompactPathContainer(&paths); + + ComputeOneToManyShortestPaths(graph_, distances, demand.source, + {demand.destination}, &paths); + + std::vector path; + paths.GetPath(demand.source, demand.destination, &path); + CHECK_GE(path.size(), 1); + all_min_path_lengths_.push_back(path.size() - 1); } for (int i = 0; i < num_demands; ++i) { @@ -534,9 +539,7 @@ class NetworkRoutingSolver { void Init(const NetworkRoutingData& data, int extra_hops, int max_paths) { LOG(INFO) << "Model " << data.name(); - num_nodes_ = data.num_nodes(); - const int num_arcs = data.num_arcs(); - const int num_demands = data.num_demands(); + graph_ = data.graph(); InitArcInfo(data); const int total_demand = InitDemandInfo(data); @@ -546,25 +549,15 @@ class NetworkRoutingSolver { // ----- Report Problem Sizes ----- LOG(INFO) << "Model created:"; - LOG(INFO) << " - " << num_nodes_ << " nodes"; - LOG(INFO) << " - " << num_arcs << " arcs"; - LOG(INFO) << " - " << num_demands << " demands"; + LOG(INFO) << " - " << data.num_nodes() << " nodes"; + LOG(INFO) << " - " << data.num_arcs() << " arcs"; + LOG(INFO) << " - " << data.num_demands() << " demands"; LOG(INFO) << " - a total traffic of " << total_demand; LOG(INFO) << " - a minimum cumulated traffic of " << total_cumulated_traffic; LOG(INFO) << " - " << num_paths << " possible paths for all demands"; } - // ----- Callback for Dijkstra Shortest Path ----- - - int64_t HasArc(int i, int j) { - if (capacity_[i][j] > 0) { - return 1; - } else { - return kDisconnectedDistance; // disconnected distance. - } - } - // ----- Main Solve routine ----- int64_t Solve() { @@ -663,12 +656,13 @@ class NetworkRoutingSolver { } private: + int num_nodes() const { return graph_.num_nodes(); } int count_arcs() const { return arcs_data_.size() / 2; } std::vector> arcs_data_; std::vector arc_capacity_; std::vector demands_array_; - int num_nodes_; + util::ListGraph graph_; std::vector all_min_path_lengths_; std::vector> capacity_; std::vector> all_paths_; diff --git a/examples/cpp/nqueens.cc b/examples/cpp/nqueens.cc index ef709560f69..7898f6e008b 100644 --- a/examples/cpp/nqueens.cc +++ b/examples/cpp/nqueens.cc @@ -60,7 +60,7 @@ class NQueenSymmetry : public SymmetryBreaker { indices_[vars[i]] = i; } } - ~NQueenSymmetry() override {} + ~NQueenSymmetry() override = default; protected: int Index(IntVar* const var) const { @@ -73,7 +73,7 @@ class NQueenSymmetry : public SymmetryBreaker { } int size() const { return size_; } int symmetric(int index) const { return size_ - 1 - index; } - Solver* const solver() const { return solver_; } + Solver* solver() const { return solver_; } private: Solver* const solver_; @@ -87,7 +87,7 @@ class SX : public NQueenSymmetry { public: SX(Solver* const s, const std::vector& vars) : NQueenSymmetry(s, vars) {} - ~SX() override {} + ~SX() override = default; void VisitSetVariableValue(IntVar* const var, int64_t value) override { const int index = Index(var); @@ -101,7 +101,7 @@ class SY : public NQueenSymmetry { public: SY(Solver* const s, const std::vector& vars) : NQueenSymmetry(s, vars) {} - ~SY() override {} + ~SY() override = default; void VisitSetVariableValue(IntVar* const var, int64_t value) override { AddIntegerVariableEqualValueClause(var, symmetric(value)); @@ -113,7 +113,7 @@ class SD1 : public NQueenSymmetry { public: SD1(Solver* const s, const std::vector& vars) : NQueenSymmetry(s, vars) {} - ~SD1() override {} + ~SD1() override = default; void VisitSetVariableValue(IntVar* const var, int64_t value) override { const int index = Index(var); @@ -127,7 +127,7 @@ class SD2 : public NQueenSymmetry { public: SD2(Solver* const s, const std::vector& vars) : NQueenSymmetry(s, vars) {} - ~SD2() override {} + ~SD2() override = default; void VisitSetVariableValue(IntVar* const var, int64_t value) override { const int index = Index(var); @@ -141,7 +141,7 @@ class R90 : public NQueenSymmetry { public: R90(Solver* const s, const std::vector& vars) : NQueenSymmetry(s, vars) {} - ~R90() override {} + ~R90() override = default; void VisitSetVariableValue(IntVar* const var, int64_t value) override { const int index = Index(var); @@ -155,7 +155,7 @@ class R180 : public NQueenSymmetry { public: R180(Solver* const s, const std::vector& vars) : NQueenSymmetry(s, vars) {} - ~R180() override {} + ~R180() override = default; void VisitSetVariableValue(IntVar* const var, int64_t value) override { const int index = Index(var); @@ -169,7 +169,7 @@ class R270 : public NQueenSymmetry { public: R270(Solver* const s, const std::vector& vars) : NQueenSymmetry(s, vars) {} - ~R270() override {} + ~R270() override = default; void VisitSetVariableValue(IntVar* const var, int64_t value) override { const int index = Index(var); diff --git a/examples/cpp/parse_dimacs_assignment.h b/examples/cpp/parse_dimacs_assignment.h index 6a3d53b5a53..5e4b051df87 100644 --- a/examples/cpp/parse_dimacs_assignment.h +++ b/examples/cpp/parse_dimacs_assignment.h @@ -20,11 +20,14 @@ #define OR_TOOLS_EXAMPLES_PARSE_DIMACS_ASSIGNMENT_H_ #include +#include +#include #include #include #include #include +#include "absl/strings/string_view.h" #include "ortools/base/commandlineflags.h" #include "ortools/base/logging.h" #include "ortools/graph/ebert_graph.h" @@ -44,7 +47,7 @@ class LinearSumAssignment; template class DimacsAssignmentParser { public: - explicit DimacsAssignmentParser(const std::string& filename) + explicit DimacsAssignmentParser(absl::string_view filename) : filename_(filename), graph_builder_(nullptr), assignment_(nullptr) {} // Reads an assignment problem description from the given file in @@ -128,7 +131,7 @@ void DimacsAssignmentParser::ParseNodeLine(const std::string& line) { NodeIndex node_id; if (sscanf(line.c_str(), "%*c%d", &node_id) != 1) { state_.bad = true; - state_.reason = "Syntax error in node desciption."; + state_.reason = "Syntax error in node description."; state_.bad_line.reset(new std::string(line)); return; } @@ -158,15 +161,18 @@ void DimacsAssignmentParser::ParseArcLine(const std::string& line) { } NodeIndex tail; NodeIndex head; - CostValue cost; - if (sscanf(line.c_str(), "%*c%d%d%lld", &tail, &head, &cost) != 3) { + // We don't use int64_t/CostValue here because of go/int64_t-cleanup. + int64_t cost; + if (sscanf(line.c_str(), "%*c%d%d%" SCNd64, &tail, &head, &cost) != 3) { state_.bad = true; state_.reason = "Syntax error in arc descriptor."; state_.bad_line.reset(new std::string(line)); } + const CostValue cost_value{cost}; ArcIndex arc = graph_builder_->AddArc(tail - 1, head - 1); - assignment_->SetArcCost( - arc, absl::GetFlag(FLAGS_assignment_maximize_cost) ? -cost : cost); + assignment_->SetArcCost(arc, absl::GetFlag(FLAGS_assignment_maximize_cost) + ? -cost_value + : cost_value); } // Parameters out of style-guide order because this function is used diff --git a/examples/cpp/pdptw.cc b/examples/cpp/pdptw.cc index 752c178a6e5..52c371d4d40 100644 --- a/examples/cpp/pdptw.cc +++ b/examples/cpp/pdptw.cc @@ -53,6 +53,7 @@ #include "ortools/base/file.h" #include "ortools/base/helpers.h" #include "ortools/base/init_google.h" +#include "ortools/base/logging.h" #include "ortools/base/mathutil.h" #include "ortools/base/timer.h" #include "ortools/constraint_solver/routing.h" diff --git a/examples/cpp/print_dimacs_assignment.h b/examples/cpp/print_dimacs_assignment.h index b25fded577e..cb5eac46f19 100644 --- a/examples/cpp/print_dimacs_assignment.h +++ b/examples/cpp/print_dimacs_assignment.h @@ -23,6 +23,7 @@ #include "absl/status/status.h" #include "absl/strings/str_format.h" +#include "absl/strings/string_view.h" #include "ortools/base/file.h" #include "ortools/base/helpers.h" #include "ortools/base/logging.h" @@ -34,7 +35,7 @@ namespace operations_research { template class LinearSumAssignment; -// Given a LinearSumAssigment object representing an assignment problem +// Given a LinearSumAssignment object representing an assignment problem // description, outputs the problem in DIMACS format in the output file. // For a description of the format, see // http://lpsolve.sourceforge.net/5.5/DIMACS_asn.htm @@ -42,13 +43,13 @@ template void PrintDimacsAssignmentProblem( const LinearSumAssignment& assignment, const TailArrayManager& tail_array_manager, - const std::string& output_filename); + absl::string_view output_filename); template void PrintDimacsAssignmentProblem( const LinearSumAssignment& assignment, const TailArrayManager& tail_array_manager, - const std::string& output_filename) { + absl::string_view output_filename) { File* output; CHECK_OK(file::Open(output_filename, "w", &output, file::Defaults())); const GraphType& graph(assignment.Graph()); diff --git a/examples/cpp/qap_sat.cc b/examples/cpp/qap_sat.cc index 6aead52dac5..92474cfa89c 100644 --- a/examples/cpp/qap_sat.cc +++ b/examples/cpp/qap_sat.cc @@ -131,7 +131,6 @@ static const char kUsageStr[] = int main(int argc, char** argv) { InitGoogle(kUsageStr, &argc, &argv, /*remove_flags=*/true); - if (absl::GetFlag(FLAGS_input).empty()) { LOG(INFO) << "--input is required"; return EXIT_SUCCESS; diff --git a/examples/cpp/random_tsp.cc b/examples/cpp/random_tsp.cc index 819ac37c9b4..6b3a610bdc9 100644 --- a/examples/cpp/random_tsp.cc +++ b/examples/cpp/random_tsp.cc @@ -11,7 +11,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -// // Traveling Salesman Sample. // // This is a sample using the routing library to solve a Traveling Salesman @@ -25,14 +24,15 @@ // Optionally one can randomly forbid a set of random connections between nodes // (forbidden arcs). +#include #include +#include -#include "absl/memory/memory.h" +#include "absl/flags/parse.h" #include "absl/random/random.h" #include "absl/strings/str_cat.h" #include "google/protobuf/text_format.h" -#include "ortools/base/commandlineflags.h" -#include "ortools/base/integral_types.h" +#include "ortools/base/logging.h" #include "ortools/constraint_solver/routing.h" #include "ortools/constraint_solver/routing_index_manager.h" #include "ortools/constraint_solver/routing_parameters.h" @@ -78,7 +78,7 @@ class RandomMatrix { public: explicit RandomMatrix(int size) : size_(size) {} void Initialize() { - matrix_ = absl::make_unique(size_ * size_); + matrix_ = std::make_unique(size_ * size_); const int64_t kDistanceMax = 100; random_engine_t randomizer(GetSeed()); for (RoutingIndexManager::NodeIndex from(0); from < size_; ++from) { @@ -123,7 +123,7 @@ void Tsp() { // Setting the cost function. // Put a permanent callback to the distance accessor here. The callback // has the following signature: ResultCallback2. - // The two arguments are the from and to node inidices. + // The two arguments are the from and to node indices. RandomMatrix matrix(absl::GetFlag(FLAGS_tsp_size)); if (absl::GetFlag(FLAGS_tsp_use_random_matrix)) { matrix.Initialize(); diff --git a/examples/cpp/shift_minimization_sat.cc b/examples/cpp/shift_minimization_sat.cc index c06754b25e6..be9aeaf2a97 100644 --- a/examples/cpp/shift_minimization_sat.cc +++ b/examples/cpp/shift_minimization_sat.cc @@ -16,7 +16,7 @@ // https://publications.csiro.au/rpr/download?pid=csiro:EP104071&dsid=DS2)/ // // Data files are in -// data/shift_scheduling/minization +// /cns/li-d/home/operations-research/shift_minization_scheduling // // The problem is the following: // - There is a list of jobs. Each job has a start date and an end date. They @@ -36,6 +36,7 @@ #include "absl/flags/flag.h" #include "absl/strings/numbers.h" #include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" #include "ortools/base/commandlineflags.h" #include "ortools/base/init_google.h" #include "ortools/base/logging.h" @@ -105,7 +106,7 @@ class ShiftMinimizationParser { private: enum LoadStatus { NOT_STARTED, STARTED, JOBS_SEEN, WORKERS_SEEN }; - int strtoint32(const std::string& word) { + int strtoint32(absl::string_view word) { int result; CHECK(absl::SimpleAtoi(word, &result)); return result; diff --git a/examples/cpp/slitherlink_sat.cc b/examples/cpp/slitherlink_sat.cc index acd4c169e75..4137cff7354 100644 --- a/examples/cpp/slitherlink_sat.cc +++ b/examples/cpp/slitherlink_sat.cc @@ -14,35 +14,17 @@ // Solve the Slitherlink problem: // see https://en.wikipedia.org/wiki/Slitherlink +#include #include #include +#include "absl/flags/parse.h" +#include "absl/log/check.h" #include "absl/strings/str_format.h" +#include "ortools/base/logging.h" #include "ortools/sat/cp_model.h" -#include "ortools/sat/model.h" - -const std::vector > tiny = {{3, 3, 1}}; - -const std::vector > small = { - {3, 2, -1, 3}, {-1, -1, -1, 2}, {3, -1, -1, -1}, {3, -1, 3, 1}}; - -const std::vector > medium = { - {-1, 0, -1, 1, -1, -1, 1, -1}, {-1, 3, -1, -1, 2, 3, -1, 2}, - {-1, -1, 0, -1, -1, -1, -1, 0}, {-1, 3, -1, -1, 0, -1, -1, -1}, - {-1, -1, -1, 3, -1, -1, 0, -1}, {1, -1, -1, -1, -1, 3, -1, -1}, - {3, -1, 1, 3, -1, -1, 3, -1}, {-1, 0, -1, -1, 3, -1, 3, -1}}; - -const std::vector > big = { - {3, -1, -1, -1, 2, -1, 1, -1, 1, 2}, - {1, -1, 0, -1, 3, -1, 2, 0, -1, -1}, - {-1, 3, -1, -1, -1, -1, -1, -1, 3, -1}, - {2, 0, -1, 3, -1, 2, 3, -1, -1, -1}, - {-1, -1, -1, 1, 1, 1, -1, -1, 3, 3}, - {2, 3, -1, -1, 2, 2, 3, -1, -1, -1}, - {-1, -1, -1, 1, 2, -1, 2, -1, 3, 3}, - {-1, 2, -1, -1, -1, -1, -1, -1, 2, -1}, - {-1, -1, 1, 1, -1, 2, -1, 1, -1, 3}, - {3, 3, -1, 1, -1, 2, -1, -1, -1, 2}}; +#include "ortools/sat/cp_model.pb.h" +#include "ortools/sat/cp_model_solver.h" namespace operations_research { namespace sat { @@ -246,7 +228,31 @@ void SlitherLink(const std::vector >& data) { } // namespace sat } // namespace operations_research -int main() { +int main(int argc, char** argv) { + absl::ParseCommandLine(argc, argv); + const std::vector > tiny = {{3, 3, 1}}; + + const std::vector > small = { + {3, 2, -1, 3}, {-1, -1, -1, 2}, {3, -1, -1, -1}, {3, -1, 3, 1}}; + + const std::vector > medium = { + {-1, 0, -1, 1, -1, -1, 1, -1}, {-1, 3, -1, -1, 2, 3, -1, 2}, + {-1, -1, 0, -1, -1, -1, -1, 0}, {-1, 3, -1, -1, 0, -1, -1, -1}, + {-1, -1, -1, 3, -1, -1, 0, -1}, {1, -1, -1, -1, -1, 3, -1, -1}, + {3, -1, 1, 3, -1, -1, 3, -1}, {-1, 0, -1, -1, 3, -1, 3, -1}}; + + const std::vector > big = { + {3, -1, -1, -1, 2, -1, 1, -1, 1, 2}, + {1, -1, 0, -1, 3, -1, 2, 0, -1, -1}, + {-1, 3, -1, -1, -1, -1, -1, -1, 3, -1}, + {2, 0, -1, 3, -1, 2, 3, -1, -1, -1}, + {-1, -1, -1, 1, 1, 1, -1, -1, 3, 3}, + {2, 3, -1, -1, 2, 2, 3, -1, -1, -1}, + {-1, -1, -1, 1, 2, -1, 2, -1, 3, 3}, + {-1, 2, -1, -1, -1, -1, -1, -1, 2, -1}, + {-1, -1, 1, 1, -1, 2, -1, 1, -1, 3}, + {3, 3, -1, 1, -1, 2, -1, -1, -1, 2}}; + std::cout << "Tiny problem" << std::endl; operations_research::sat::SlitherLink(tiny); std::cout << "Small problem" << std::endl; diff --git a/examples/cpp/strawberry_fields_with_column_generation.cc b/examples/cpp/strawberry_fields_with_column_generation.cc index a4b5a07e616..5726ef7affa 100644 --- a/examples/cpp/strawberry_fields_with_column_generation.cc +++ b/examples/cpp/strawberry_fields_with_column_generation.cc @@ -246,7 +246,7 @@ class Box { static constexpr int kAreaCost = 1; static constexpr int kFixedCost = 10; - Box() {} + Box() = default; Box(int x_min, int x_max, int y_min, int y_max) : x_min_(x_min), x_max_(x_max), y_min_(y_min), y_max_(y_max) { CHECK_GE(x_max, x_min); diff --git a/examples/cpp/uncapacitated_facility_location.cc b/examples/cpp/uncapacitated_facility_location.cc index 5cae1d01301..bd6b24b4e32 100644 --- a/examples/cpp/uncapacitated_facility_location.cc +++ b/examples/cpp/uncapacitated_facility_location.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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 @@ -11,7 +11,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -// // Uncapacitated Facility Location Problem. // A description of the problem can be found here: // https://en.wikipedia.org/wiki/Facility_location_problem. @@ -22,13 +21,14 @@ // are assumed to be in meters and times in seconds. #include +#include +#include #include +#include "absl/flags/parse.h" +#include "absl/flags/usage.h" +#include "absl/log/initialize.h" #include "absl/random/random.h" -#include "google/protobuf/text_format.h" -#include "ortools/base/commandlineflags.h" -#include "ortools/base/integral_types.h" -#include "ortools/base/init_google.h" #include "ortools/base/logging.h" #include "ortools/linear_solver/linear_solver.h" #include "ortools/util/random_engine.h" @@ -138,7 +138,8 @@ static void UncapacitatedFacilityLocation( // display on screen LP if small enough if (clients <= 10 && facilities <= 10) { std::string lp_string{}; - solver.ExportModelAsLpFormat(/* obfuscate */ false, &lp_string); + const bool obfuscate = false; + solver.ExportModelAsLpFormat(obfuscate, &lp_string); std::cout << "LP-Model:\n" << lp_string << std::endl; } // Set options and solve @@ -163,7 +164,7 @@ static void UncapacitatedFacilityLocation( } std::cout << "\tSolution:\n"; for (int f = 0; f < facilities; ++f) { - if (solution[f].size() < 1) continue; + if (solution[f].empty()) continue; assert(xf[f]->solution_value() > 0.5); snprintf(name_buffer, kStrLen, "\t Facility[%d](%g,%g):", f, facility[f].x, facility[f].y); diff --git a/examples/cpp/variable_intervals_sat.cc b/examples/cpp/variable_intervals_sat.cc index dc442f80ffa..a2ff5b7b79b 100644 --- a/examples/cpp/variable_intervals_sat.cc +++ b/examples/cpp/variable_intervals_sat.cc @@ -1,4 +1,21 @@ +// 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. + +#include + +#include "ortools/base/logging.h" #include "ortools/sat/cp_model.h" +#include "ortools/sat/cp_model_solver.h" #include "ortools/sat/sat_parameters.pb.h" #include "ortools/util/time_limit.h" diff --git a/examples/cpp/weighted_tardiness_sat.cc b/examples/cpp/weighted_tardiness_sat.cc index 2332031c6f8..886c99c507a 100644 --- a/examples/cpp/weighted_tardiness_sat.cc +++ b/examples/cpp/weighted_tardiness_sat.cc @@ -33,8 +33,7 @@ #include "ortools/sat/model.h" #include "ortools/util/filelineiter.h" -ABSL_FLAG(std::string, input, "examples/cpp/wt40.txt", - "wt data file name."); +ABSL_FLAG(std::string, input, "examples/cpp/wt40.txt", "wt data file name."); ABSL_FLAG(int, size, 40, "Size of the problem in the wt file."); ABSL_FLAG(int, n, 28, "1-based instance number in the wt file."); ABSL_FLAG(std::string, params, "", "Sat parameters in text proto format.");