graph: export from google3

This commit is contained in:
Corentin Le Molgat
2024-10-03 11:00:02 +02:00
parent a72b8b25ff
commit bcbe132cb6
8 changed files with 41 additions and 32 deletions

View File

@@ -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",
],
)

View File

@@ -16,6 +16,7 @@
#include <limits>
#include "gtest/gtest.h"
#include "ortools/graph/ebert_graph.h"
namespace operations_research {

View File

@@ -13,8 +13,8 @@
#include "ortools/graph/christofides.h"
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <limits>
#include <string>
@@ -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<int> chris_solver(0, [](int i, int j) { return 0; });
ChristofidesPathSolver<int> chris_solver(0, [](int, int) { return 0; });
EXPECT_EQ(0, chris_solver.TravelingSalesmanCost());
EXPECT_TRUE(chris_solver.TravelingSalesmanPath().empty());
}
TEST(ChristofidesTest, SingleNodeModel) {
ChristofidesPathSolver<int> chris_solver(1, [](int i, int j) { return 0; });
ChristofidesPathSolver<int> 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<int64_t> chris_solver(
10, [](int i, int j) { return std::numeric_limits<int64_t>::max() / 2; });
10, [](int, int) { return std::numeric_limits<int64_t>::max() / 2; });
EXPECT_EQ(std::numeric_limits<int64_t>::max(),
chris_solver.TravelingSalesmanCost());
}
TEST(ChristofidesTest, SaturatedDouble) {
ChristofidesPathSolver<double> chris_solver(10, [](int i, int j) {
return std::numeric_limits<double>::max() / 2.0;
});
ChristofidesPathSolver<double> chris_solver(
10, [](int, int) { return std::numeric_limits<double>::max() / 2.0; });
EXPECT_EQ(std::numeric_limits<double>::infinity(),
chris_solver.TravelingSalesmanCost());
}

View File

@@ -24,6 +24,7 @@
#include <vector>
#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<TimeLimit> 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);

View File

@@ -20,12 +20,15 @@
#include <random>
#include <vector>
#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 <typename Graph>
void FullRandomAssignment(typename MinCostFlowSolver<Graph>::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 <typename Graph>
void PartialRandomAssignment(typename MinCostFlowSolver<Graph>::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;

View File

@@ -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);
}

View File

@@ -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.")

View File

@@ -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]