Skip to content

Commit

Permalink
backport graph/ from main
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizux committed Nov 12, 2024
1 parent bc4c73a commit 3bf66db
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
6 changes: 4 additions & 2 deletions ortools/graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ if(BUILD_TESTING)
${_FILE_NAME}
LINK_LIBRARIES
benchmark::benchmark
GTest::gmock
GTest::gtest
GTest::gtest_main
GTest::gmock
)
endforeach()
ortools_cxx_test(
Expand All @@ -64,8 +65,9 @@ if(BUILD_TESTING)
"max_flow_test.cc"
LINK_LIBRARIES
benchmark::benchmark
GTest::gmock
GTest::gtest
GTest::gtest_main
GTest::gmock
COMPILE_DEFINITIONS
-DROOT_DIR="$<$<BOOL:${isMultiConfig}>:../>../../"
)
Expand Down
12 changes: 7 additions & 5 deletions ortools/graph/christofides_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <limits>
#include <string>
#include <vector>
Expand Down Expand Up @@ -77,7 +78,7 @@ void TestChristofides(const std::string& name, const int size,
chris_solver.SetMatchingAlgorithm(
MatchingAlgorithm::MINIMUM_WEIGHT_MATCHING_WITH_MIP);
#else
LOG(FATAL) << "Not supported when both CBC and SCIP solvers are disable.";
GTEST_SKIP() << "Test requires CBC or SCIP support.";
#endif // defined(USE_CBC) || defined(USE_SCIP)
} else {
chris_solver.SetMatchingAlgorithm(
Expand Down Expand Up @@ -112,10 +113,8 @@ TEST(HamiltonianPathTest, Gr17) {
165, 383, 240, 140, 448, 202, 57, 0, 246, 745, 472, 237, 528, 364,
332, 349, 202, 685, 542, 157, 289, 426, 483, 0, 121, 518, 142, 84,
297, 35, 29, 36, 236, 390, 238, 301, 55, 96, 153, 336, 0};
#if defined(USE_CBC) || defined(USE_SCIP)
TestChristofides("Gr17", kGr17Size, gr17_data, false, /*use_mip=*/true, 2190,
"0 12 6 7 5 10 4 1 9 2 14 13 16 3 8 11 15 0 ");
#endif // defined(USE_CBC) || defined(USE_SCIP)
TestChristofides("Gr17", kGr17Size, gr17_data, false, /*use_mip=*/false, 2190,
"0 12 6 7 5 10 4 1 9 2 14 13 16 3 8 11 15 0 ");
TestChristofides("Gr17", kGr17Size, gr17_data, true, /*use_mip=*/false, 2421,
Expand Down Expand Up @@ -150,11 +149,9 @@ TEST(HamiltonianPathTest, Gr24) {
235, 108, 119, 165, 178, 154, 71, 136, 262, 110, 74, 96, 264, 187, 182,
261, 239, 165, 151, 221, 0, 121, 142, 99, 84, 35, 29, 42, 36, 220,
70, 126, 55, 249, 104, 178, 60, 96, 175, 153, 146, 47, 135, 169, 0};
#if defined(USE_CBC) || defined(USE_SCIP)
TestChristofides(
"Gr24", kGr24Size, gr24_data, false, /*use_mip=*/true, 1407,
"0 15 5 6 2 10 7 20 4 9 16 21 17 18 1 14 19 12 8 22 13 23 11 3 0 ");
#endif // defined(USE_CBC) || defined(USE_SCIP)
TestChristofides(
"Gr24", kGr24Size, gr24_data, false, /*use_mip=*/false, 1407,
"0 15 5 6 2 10 7 20 4 9 16 21 17 18 1 14 19 12 8 22 13 23 11 3 0 ");
Expand Down Expand Up @@ -276,7 +273,12 @@ void BM_ChristofidesPathSolver(benchmark::State& state) {
}
}
auto cost = [&costs](int i, int j) { return costs[i][j]; };
// TODO: MSVC v19.41 can't convert lambda to std::function.
#if defined(_MSC_VER)
using Cost = std::function<int(int, int)>;
#else
using Cost = decltype(cost);
#endif
using MatchingAlgorithm =
typename ChristofidesPathSolver<int, int, int, Cost>::MatchingAlgorithm;
for (auto _ : state) {
Expand Down
8 changes: 6 additions & 2 deletions ortools/graph/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,8 @@ void StaticGraph<NodeIndexType, ArcIndexType>::Build(
template <typename NodeIndexType, typename ArcIndexType>
class StaticGraph<NodeIndexType, ArcIndexType>::OutgoingArcIterator {
public:
OutgoingArcIterator(const OutgoingArcIterator&) = default;
OutgoingArcIterator& operator=(const OutgoingArcIterator&) = default;
OutgoingArcIterator(const StaticGraph& graph, NodeIndexType node)
: index_(graph.start_[node]), limit_(graph.DirectArcLimit(node)) {}
OutgoingArcIterator(const StaticGraph& graph, NodeIndexType node,
Expand All @@ -1461,7 +1463,7 @@ class StaticGraph<NodeIndexType, ArcIndexType>::OutgoingArcIterator {

private:
ArcIndexType index_;
const ArcIndexType limit_;
ArcIndexType limit_;
};

// ReverseArcListGraph implementation ------------------------------------------
Expand Down Expand Up @@ -2090,6 +2092,8 @@ void ReverseArcMixedGraph<NodeIndexType, ArcIndexType>::Build(
template <typename NodeIndexType, typename ArcIndexType>
class ReverseArcMixedGraph<NodeIndexType, ArcIndexType>::OutgoingArcIterator {
public:
OutgoingArcIterator(const OutgoingArcIterator&) = default;
OutgoingArcIterator& operator=(const OutgoingArcIterator&) = default;
OutgoingArcIterator(const ReverseArcMixedGraph& graph, NodeIndexType node)
: index_(graph.start_[node]), limit_(graph.DirectArcLimit(node)) {}
OutgoingArcIterator(const ReverseArcMixedGraph& graph, NodeIndexType node,
Expand All @@ -2111,7 +2115,7 @@ class ReverseArcMixedGraph<NodeIndexType, ArcIndexType>::OutgoingArcIterator {

private:
ArcIndexType index_;
const ArcIndexType limit_;
ArcIndexType limit_;
};

template <typename NodeIndexType, typename ArcIndexType>
Expand Down
2 changes: 1 addition & 1 deletion ortools/graph/max_flow_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include "ortools/util/file_util.h"

#if not defined(ROOT_DIR)
#define ROOT_DIR "com_google_ortools/"
#define ROOT_DIR "_main/"
#endif

namespace operations_research {
Expand Down
4 changes: 2 additions & 2 deletions ortools/graph/shortest_paths_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ TYPED_TEST_SUITE(GraphShortestPathsTest, GraphTypesForShortestPathsTesting);

// Test on an empty graph.
TYPED_TEST(GraphShortestPathsDeathTest, ShortestPathsEmptyGraph) {
const int kExpectedPaths[] = {};
const PathDistance kExpectedDistances[] = {};
const auto kExpectedPaths = nullptr;
const auto kExpectedDistances = nullptr;
TypeParam graph;
std::vector<PathDistance> lengths;
TestShortestPathsFromGraph(graph, lengths, kExpectedPaths,
Expand Down

0 comments on commit 3bf66db

Please sign in to comment.