forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 9
/
one_tree_lower_bound_test.cc
95 lines (83 loc) · 2.99 KB
/
one_tree_lower_bound_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2010-2024 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 "ortools/graph/one_tree_lower_bound.h"
#include <cstdint>
#include <cstdlib>
#include <string>
#include <utility>
#include <vector>
#include "absl/types/span.h"
#include "gtest/gtest.h"
#include "ortools/base/logging.h"
#include "ortools/base/path.h"
#include "ortools/routing/parsers/tsplib_parser.h"
namespace operations_research {
namespace {
TEST(OneTreeLBTest, VolgenantJonkerEmpty) {
const double cost = ComputeOneTreeLowerBound(0, [](int /*from*/, int /*to*/) {
ADD_FAILURE(); // Making sure the function is not being called.
return 0;
});
EXPECT_EQ(0, cost);
}
TEST(OneTreeLBTest, HeldWolfeCrowderEmpty) {
TravelingSalesmanLowerBoundParameters parameters;
parameters.algorithm =
TravelingSalesmanLowerBoundParameters::HeldWolfeCrowder;
const double cost = ComputeOneTreeLowerBoundWithParameters(
0,
[](int /*from*/, int /*to*/) {
ADD_FAILURE(); // Making sure the function is not being called.
return 0;
},
parameters);
EXPECT_EQ(0, cost);
}
TEST(OneTreeLBTest, VolgenantJonkerOneNode) {
const double cost =
ComputeOneTreeLowerBound(1, [](int /*from*/, int /*to*/) { return 0; });
EXPECT_EQ(0, cost);
}
TEST(OneTreeLBTest, HeldWolfeCrowderOneNode) {
TravelingSalesmanLowerBoundParameters parameters;
parameters.algorithm =
TravelingSalesmanLowerBoundParameters::HeldWolfeCrowder;
const double cost = ComputeOneTreeLowerBoundWithParameters(
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; });
EXPECT_EQ(2, cost);
}
TEST(OneTreeLBTest, HeldWolfeCrowderTwoNodes) {
TravelingSalesmanLowerBoundParameters parameters;
parameters.algorithm =
TravelingSalesmanLowerBoundParameters::HeldWolfeCrowder;
const double cost = ComputeOneTreeLowerBoundWithParameters(
2, [](int /*from*/, int /*to*/) { return 1; }, parameters);
EXPECT_EQ(2, cost);
}
TEST(OneTreeLBTest, Small) {
const std::vector<int> x = {0, 0, 0, 1, 1, 1, 2, 2, 2};
const std::vector<int> y = {0, 1, 2, 0, 1, 2, 0, 1, 2};
double cost = ComputeOneTreeLowerBound(9, [&x, &y](int from, int to) {
const int dx = std::abs(x[from] - x[to]);
const int dy = std::abs(y[from] - y[to]);
return dx + dy;
});
EXPECT_EQ(9, cost);
}
} // namespace
} // namespace operations_research