From da1be192d6f8656d4a243249b7bb7aca9ee7e6d1 Mon Sep 17 00:00:00 2001 From: Corentin Le Molgat Date: Thu, 3 Oct 2024 11:00:02 +0200 Subject: [PATCH] graph: export from google3 --- ortools/graph/BUILD.bazel | 9 +++++++-- ortools/graph/assignment_test.cc | 1 + ortools/graph/christofides_test.cc | 14 ++++++-------- ortools/graph/cliques_test.cc | 9 +++++---- ortools/graph/min_cost_flow_test.cc | 8 ++++++-- ortools/graph/one_tree_lower_bound_test.cc | 14 +++++++------- ortools/graph/samples/assignment_min_flow.py | 15 +++++++-------- .../graph/samples/simple_min_cost_flow_program.py | 3 ++- 8 files changed, 41 insertions(+), 32 deletions(-) diff --git a/ortools/graph/BUILD.bazel b/ortools/graph/BUILD.bazel index 0d1be0e5a5..45811e3633 100644 --- a/ortools/graph/BUILD.bazel +++ b/ortools/graph/BUILD.bazel @@ -178,10 +178,11 @@ cc_test( deps = [ ":cliques", "//ortools/base:gmock_main", - "//ortools/base:mathutil", "//ortools/util:time_limit", "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/flags:flag", "@com_google_absl//absl/functional:bind_front", + "@com_google_absl//absl/log", "@com_google_absl//absl/log:check", "@com_google_absl//absl/random:distributions", "@com_google_absl//absl/strings", @@ -317,8 +318,8 @@ cc_test( ":one_tree_lower_bound", "//ortools/base:gmock_main", "//ortools/base:path", - "//ortools/base:types", "//ortools/routing/parsers:tsplib_parser", + "@com_google_absl//absl/log", "@com_google_absl//absl/types:span", ], ) @@ -512,9 +513,12 @@ cc_test( size = "medium", srcs = ["min_cost_flow_test.cc"], deps = [ + ":ebert_graph", ":graphs", ":min_cost_flow", "//ortools/base:gmock_main", + "@com_google_absl//absl/log", + "@com_google_absl//absl/log:check", "@com_google_absl//absl/random:distributions", "@com_google_absl//absl/strings:str_format", "@com_google_absl//absl/types:span", @@ -567,6 +571,7 @@ cc_test( srcs = ["assignment_test.cc"], deps = [ ":assignment", + ":ebert_graph", "//ortools/base:gmock_main", ], ) diff --git a/ortools/graph/assignment_test.cc b/ortools/graph/assignment_test.cc index c36a795699..4c169f9d45 100644 --- a/ortools/graph/assignment_test.cc +++ b/ortools/graph/assignment_test.cc @@ -16,6 +16,7 @@ #include #include "gtest/gtest.h" +#include "ortools/graph/ebert_graph.h" namespace operations_research { diff --git a/ortools/graph/christofides_test.cc b/ortools/graph/christofides_test.cc index c092b06722..5058f9a489 100644 --- a/ortools/graph/christofides_test.cc +++ b/ortools/graph/christofides_test.cc @@ -13,8 +13,8 @@ #include "ortools/graph/christofides.h" +#include #include -#include #include #include #include @@ -26,7 +26,6 @@ #include "benchmark/benchmark.h" #include "gtest/gtest.h" #include "ortools/base/logging.h" -#include "ortools/base/macros.h" namespace operations_research { @@ -210,28 +209,27 @@ TEST(HamiltonianPathTest, Ulysses) { } TEST(ChristofidesTest, EmptyModel) { - ChristofidesPathSolver chris_solver(0, [](int i, int j) { return 0; }); + ChristofidesPathSolver chris_solver(0, [](int, int) { return 0; }); EXPECT_EQ(0, chris_solver.TravelingSalesmanCost()); EXPECT_TRUE(chris_solver.TravelingSalesmanPath().empty()); } TEST(ChristofidesTest, SingleNodeModel) { - ChristofidesPathSolver chris_solver(1, [](int i, int j) { return 0; }); + ChristofidesPathSolver chris_solver(1, [](int, int) { return 0; }); EXPECT_EQ(0, chris_solver.TravelingSalesmanCost()); EXPECT_EQ("0 0 ", PathToString(chris_solver.TravelingSalesmanPath())); } TEST(ChristofidesTest, Int64Overflow) { ChristofidesPathSolver chris_solver( - 10, [](int i, int j) { return std::numeric_limits::max() / 2; }); + 10, [](int, int) { return std::numeric_limits::max() / 2; }); EXPECT_EQ(std::numeric_limits::max(), chris_solver.TravelingSalesmanCost()); } TEST(ChristofidesTest, SaturatedDouble) { - ChristofidesPathSolver chris_solver(10, [](int i, int j) { - return std::numeric_limits::max() / 2.0; - }); + ChristofidesPathSolver chris_solver( + 10, [](int, int) { return std::numeric_limits::max() / 2.0; }); EXPECT_EQ(std::numeric_limits::infinity(), chris_solver.TravelingSalesmanCost()); } diff --git a/ortools/graph/cliques_test.cc b/ortools/graph/cliques_test.cc index b28bbda188..ddb68665ac 100644 --- a/ortools/graph/cliques_test.cc +++ b/ortools/graph/cliques_test.cc @@ -24,6 +24,7 @@ #include #include "absl/container/flat_hash_set.h" +#include "absl/flags/flag.h" #include "absl/functional/bind_front.h" #include "absl/log/check.h" #include "absl/random/distributions.h" @@ -31,7 +32,7 @@ #include "absl/types/span.h" #include "benchmark/benchmark.h" #include "gtest/gtest.h" -#include "ortools/base/mathutil.h" +#include "ortools/base/logging.h" #include "ortools/util/time_limit.h" namespace operations_research { @@ -98,7 +99,7 @@ class CliqueSizeVerifier { int64_t num_cliques_; }; -inline bool FullGraph(int index1, int index2) { return true; } +inline bool FullGraph(int /*index1*/, int /*index2*/) { return true; } inline bool EmptyGraph(int index1, int index2) { return (index1 == index2); } @@ -562,7 +563,7 @@ TEST(BronKerboschAlgorithmTest, WallTimeLimit) { absl::SetFlag(&FLAGS_time_limit_use_usertime, true); TimeLimit time_limit(kTimeLimitSeconds); - const auto graph = [kNumPartitions](int index1, int index2) { + const auto graph = [](int index1, int index2) { return FullKPartiteGraph(kNumPartitions, index1, index2); }; CliqueSizeVerifier verifier(kExpectedCliqueSize, kExpectedCliqueSize); @@ -583,7 +584,7 @@ TEST(BronKerboschAlgorithmTest, DeterministicTimeLimit) { std::unique_ptr time_limit = TimeLimit::FromDeterministicTime(kDeterministicLimit); - const auto graph = [kNumPartitions](int index1, int index2) { + const auto graph = [](int index1, int index2) { return FullKPartiteGraph(kNumPartitions, index1, index2); }; CliqueSizeVerifier verifier(kExpectedCliqueSize, kExpectedCliqueSize); diff --git a/ortools/graph/min_cost_flow_test.cc b/ortools/graph/min_cost_flow_test.cc index 8ad26727b2..3391167d48 100644 --- a/ortools/graph/min_cost_flow_test.cc +++ b/ortools/graph/min_cost_flow_test.cc @@ -20,12 +20,15 @@ #include #include +#include "absl/log/check.h" #include "absl/random/distributions.h" #include "absl/strings/str_format.h" #include "absl/types/span.h" #include "benchmark/benchmark.h" #include "gtest/gtest.h" #include "ortools/algorithms/binary_search.h" +#include "ortools/base/logging.h" +#include "ortools/graph/ebert_graph.h" #include "ortools/graph/graph.h" #include "ortools/graph/graphs.h" #include "ortools/linear_solver/linear_solver.h" @@ -631,7 +634,8 @@ struct MinCostFlowSolver { template void FullRandomAssignment(typename MinCostFlowSolver::Solver f, NodeIndex num_sources, NodeIndex num_targets, - CostValue expected_cost1, CostValue expected_cost2) { + CostValue expected_cost1, + CostValue /*expected_cost2*/) { const CostValue kCostRange = 1000; Graph graph; GenerateCompleteGraph(num_sources, num_targets, &graph); @@ -656,7 +660,7 @@ template void PartialRandomAssignment(typename MinCostFlowSolver::Solver f, NodeIndex num_sources, NodeIndex num_targets, CostValue expected_cost1, - CostValue expected_cost2) { + CostValue /*expected_cost2*/) { const NodeIndex kDegree = 10; const CostValue kCostRange = 1000; Graph graph; diff --git a/ortools/graph/one_tree_lower_bound_test.cc b/ortools/graph/one_tree_lower_bound_test.cc index 977b0c5236..0554d56e69 100644 --- a/ortools/graph/one_tree_lower_bound_test.cc +++ b/ortools/graph/one_tree_lower_bound_test.cc @@ -21,15 +21,15 @@ #include "absl/types/span.h" #include "gtest/gtest.h" +#include "ortools/base/logging.h" #include "ortools/base/path.h" -#include "ortools/base/types.h" #include "ortools/routing/parsers/tsplib_parser.h" namespace operations_research { namespace { TEST(OneTreeLBTest, VolgenantJonkerEmpty) { - const double cost = ComputeOneTreeLowerBound(0, [](int from, int to) { + const double cost = ComputeOneTreeLowerBound(0, [](int /*from*/, int /*to*/) { ADD_FAILURE(); // Making sure the function is not being called. return 0; }); @@ -42,7 +42,7 @@ TEST(OneTreeLBTest, HeldWolfeCrowderEmpty) { TravelingSalesmanLowerBoundParameters::HeldWolfeCrowder; const double cost = ComputeOneTreeLowerBoundWithParameters( 0, - [](int from, int to) { + [](int /*from*/, int /*to*/) { ADD_FAILURE(); // Making sure the function is not being called. return 0; }, @@ -52,7 +52,7 @@ TEST(OneTreeLBTest, HeldWolfeCrowderEmpty) { TEST(OneTreeLBTest, VolgenantJonkerOneNode) { const double cost = - ComputeOneTreeLowerBound(1, [](int from, int to) { return 0; }); + ComputeOneTreeLowerBound(1, [](int /*from*/, int /*to*/) { return 0; }); EXPECT_EQ(0, cost); } @@ -61,13 +61,13 @@ TEST(OneTreeLBTest, HeldWolfeCrowderOneNode) { parameters.algorithm = TravelingSalesmanLowerBoundParameters::HeldWolfeCrowder; const double cost = ComputeOneTreeLowerBoundWithParameters( - 1, [](int from, int to) { return 0; }, parameters); + 1, [](int /*from*/, int /*to*/) { return 0; }, parameters); EXPECT_EQ(0, cost); } TEST(OneTreeLBTest, VolgenantJonkerTwoNodes) { const double cost = - ComputeOneTreeLowerBound(2, [](int from, int to) { return 1; }); + ComputeOneTreeLowerBound(2, [](int /*from*/, int /*to*/) { return 1; }); EXPECT_EQ(2, cost); } @@ -76,7 +76,7 @@ TEST(OneTreeLBTest, HeldWolfeCrowderTwoNodes) { parameters.algorithm = TravelingSalesmanLowerBoundParameters::HeldWolfeCrowder; const double cost = ComputeOneTreeLowerBoundWithParameters( - 2, [](int from, int to) { return 1; }, parameters); + 2, [](int /*from*/, int /*to*/) { return 1; }, parameters); EXPECT_EQ(2, cost); } diff --git a/ortools/graph/samples/assignment_min_flow.py b/ortools/graph/samples/assignment_min_flow.py index 22f9cd8e18..381a75da5f 100755 --- a/ortools/graph/samples/assignment_min_flow.py +++ b/ortools/graph/samples/assignment_min_flow.py @@ -51,13 +51,13 @@ def main(): # [START constraints] # Add each arc. - for i in range(len(start_nodes)): + for idx, _ in enumerate(start_nodes): smcf.add_arc_with_capacity_and_unit_cost( - start_nodes[i], end_nodes[i], capacities[i], costs[i] + start_nodes[idx], end_nodes[idx], capacities[idx], costs[idx] ) # Add node supplies. - for i in range(len(supplies)): - smcf.set_node_supply(i, supplies[i]) + for idx, supply in enumerate(supplies): + smcf.set_node_supply(idx, supply) # [END constraints] # [START solve] @@ -67,8 +67,7 @@ def main(): # [START print_solution] if status == smcf.OPTIMAL: - print("Total cost = ", smcf.optimal_cost()) - print() + print(f"Total cost = {smcf.optimal_cost()}") for arc in range(smcf.num_arcs()): # Can ignore arcs leading out of source or into sink. if smcf.tail(arc) != source and smcf.head(arc) != sink: @@ -77,8 +76,8 @@ def main(): # give an assignment of worker to task. if smcf.flow(arc) > 0: print( - "Worker %d assigned to task %d. Cost = %d" - % (smcf.tail(arc), smcf.head(arc), smcf.unit_cost(arc)) + f"Worker {smcf.tail(arc)} assigned to task {smcf.head(arc)}. " + f"Cost = {smcf.unit_cost(arc)}" ) else: print("There was an issue with the min cost flow input.") diff --git a/ortools/graph/samples/simple_min_cost_flow_program.py b/ortools/graph/samples/simple_min_cost_flow_program.py index b3b06856bf..569058e037 100755 --- a/ortools/graph/samples/simple_min_cost_flow_program.py +++ b/ortools/graph/samples/simple_min_cost_flow_program.py @@ -68,7 +68,8 @@ def main(): costs = solution_flows * unit_costs for arc, flow, cost in zip(all_arcs, solution_flows, costs): print( - f"{smcf.tail(arc):1} -> {smcf.head(arc)} {flow:3} / {smcf.capacity(arc):3} {cost}" + f"{smcf.tail(arc):1} -> " + f"{smcf.head(arc)} {flow:3} / {smcf.capacity(arc):3} {cost}" ) # [END print_solution]