forked from mlubin/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoutingSolverTests.cs
202 lines (194 loc) · 10.6 KB
/
RoutingSolverTests.cs
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2010-2021 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.
using System;
using Xunit;
using Google.OrTools.ConstraintSolver;
namespace Google.OrTools.Tests
{
public class RoutingSolverTest
{
[Theory]
[InlineData(false)]
[InlineData(true)]
public void SimpleLambdaCallback(bool callGC)
{
// Create Routing Index Manager
RoutingIndexManager manager = new RoutingIndexManager(5 /*locations*/, 1 /*vehicle*/, 0 /*depot*/);
// Create Routing Model.
RoutingModel routing = new RoutingModel(manager);
// Create a distance callback.
int transitCallbackIndex = routing.RegisterTransitCallback((long fromIndex, long toIndex) =>
{
// Convert from routing variable Index to
// distance matrix NodeIndex.
var fromNode = manager.IndexToNode(fromIndex);
var toNode = manager.IndexToNode(toIndex);
return Math.Abs(toNode - fromNode);
});
// Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
if (callGC)
{
GC.Collect();
}
// Setting first solution heuristic.
RoutingSearchParameters searchParameters =
operations_research_constraint_solver.DefaultRoutingSearchParameters();
searchParameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;
Assignment solution = routing.SolveWithParameters(searchParameters);
// 0 --(+1)-> 1 --(+1)-> 2 --(+1)-> 3 --(+1)-> 4 --(+4)-> 0 := +8
Assert.Equal(8, solution.ObjectiveValue());
}
[Fact]
public void TestTransitMatrix()
{
// Create Routing Index Manager
RoutingIndexManager manager = new RoutingIndexManager(5 /*locations*/, 1 /*vehicle*/, 0 /*depot*/);
// Create Routing Model.
RoutingModel routing = new RoutingModel(manager);
// Create a distance callback.
long[][] matrix = new long[][] {
new long[] { 1, 1, 1, 1, 1 }, new long[] { 1, 1, 1, 1, 1 }, new long[] { 1, 1, 1, 1, 1 },
new long[] { 1, 1, 1, 1, 1 }, new long[] { 1, 1, 1, 1, 1 },
};
int transitCallbackIndex = routing.RegisterTransitMatrix(matrix);
// Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// Setting first solution heuristic.
RoutingSearchParameters searchParameters =
operations_research_constraint_solver.DefaultRoutingSearchParameters();
searchParameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;
Assignment solution = routing.SolveWithParameters(searchParameters);
// 0 --(+1)-> 1 --(+1)-> 2 --(+1)-> 3 --(+1)-> 4 --(+1)-> 0 := +5
Assert.Equal(5, solution.ObjectiveValue());
}
[Fact]
public void TestTransitCallback()
{
// Create Routing Index Manager
RoutingIndexManager manager = new RoutingIndexManager(5 /*locations*/, 1 /*vehicle*/, 0 /*depot*/);
// Create Routing Model.
RoutingModel routing = new RoutingModel(manager);
// Create a distance callback.
int transitCallbackIndex = routing.RegisterTransitCallback((long fromIndex, long toIndex) =>
{
// Convert from routing variable Index to
// distance matrix NodeIndex.
var fromNode = manager.IndexToNode(fromIndex);
var toNode = manager.IndexToNode(toIndex);
return Math.Abs(toNode - fromNode);
});
// Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// Setting first solution heuristic.
RoutingSearchParameters searchParameters =
operations_research_constraint_solver.DefaultRoutingSearchParameters();
searchParameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;
Assignment solution = routing.SolveWithParameters(searchParameters);
Assert.Equal(8, solution.ObjectiveValue());
}
[Fact]
public void TestMatrixDimension()
{
// Create Routing Index Manager
RoutingIndexManager manager = new RoutingIndexManager(5 /*locations*/, 1 /*vehicle*/, 0 /*depot*/);
// Create Routing Model.
RoutingModel routing = new RoutingModel(manager);
// Create a distance callback.
long[][] matrix = new long[][] {
new long[] { 1, 1, 1, 1, 1 }, new long[] { 1, 1, 1, 1, 1 }, new long[] { 1, 1, 1, 1, 1 },
new long[] { 1, 1, 1, 1, 1 }, new long[] { 1, 1, 1, 1, 1 },
};
IntBoolPair result = routing.AddMatrixDimension(matrix,
/*capacity=*/10,
/*fix_start_cumul_to_zero=*/true, "Dimension");
// Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(result.first);
// Setting first solution heuristic.
RoutingSearchParameters searchParameters =
operations_research_constraint_solver.DefaultRoutingSearchParameters();
searchParameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;
Assignment solution = routing.SolveWithParameters(searchParameters);
// 0 --(+1)-> 1 --(+1)-> 2 --(+1)-> 3 --(+1)-> 4 --(+1)-> 0 := +5
Assert.Equal(5, solution.ObjectiveValue());
}
[Fact]
public void TestUnaryTransitVector()
{
// Create Routing Index Manager
RoutingIndexManager manager = new RoutingIndexManager(5 /*locations*/, 1 /*vehicle*/, 0 /*depot*/);
// Create Routing Model.
RoutingModel routing = new RoutingModel(manager);
// Create a distance callback.
long[] vector = { 1, 1, 1, 1, 1 };
int transitCallbackIndex = routing.RegisterUnaryTransitVector(vector);
// Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// Setting first solution heuristic.
RoutingSearchParameters searchParameters =
operations_research_constraint_solver.DefaultRoutingSearchParameters();
searchParameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;
Assignment solution = routing.SolveWithParameters(searchParameters);
// 0 --(+1)-> 1 --(+1)-> 2 --(+1)-> 3 --(+1)-> 4 --(+1)-> 0 := +5
Assert.Equal(5, solution.ObjectiveValue());
}
[Fact]
public void TestUnaryTransitCallback()
{
// Create Routing Index Manager
RoutingIndexManager manager = new RoutingIndexManager(5 /*locations*/, 1 /*vehicle*/, 0 /*depot*/);
// Create Routing Model.
RoutingModel routing = new RoutingModel(manager);
// Create a distance callback.
int transitCallbackIndex = routing.RegisterUnaryTransitCallback((long fromIndex) =>
{
// Convert from routing variable Index to
// distance matrix NodeIndex.
var fromNode =
manager.IndexToNode(fromIndex);
return fromNode + 1;
});
// Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// Setting first solution heuristic.
RoutingSearchParameters searchParameters =
operations_research_constraint_solver.DefaultRoutingSearchParameters();
searchParameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;
Assignment solution = routing.SolveWithParameters(searchParameters);
// 0 --(+1)-> 1 --(+2)-> 2 --(+3)-> 3 --(+4)-> 4 --(+5)-> 0 := +15
Assert.Equal(15, solution.ObjectiveValue());
}
[Fact]
public void TestVectorDimension()
{
// Create Routing Index Manager
RoutingIndexManager manager = new RoutingIndexManager(5 /*locations*/, 1 /*vehicle*/, 0 /*depot*/);
// Create Routing Model.
RoutingModel routing = new RoutingModel(manager);
// Create a distance callback.
long[] vector = new long[] { 1, 1, 1, 1, 1 };
IntBoolPair result = routing.AddVectorDimension(vector,
/*capacity=*/10,
/*fix_start_cumul_to_zero=*/true, "Dimension");
// Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(result.first);
// Setting first solution heuristic.
RoutingSearchParameters searchParameters =
operations_research_constraint_solver.DefaultRoutingSearchParameters();
searchParameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;
Assignment solution = routing.SolveWithParameters(searchParameters);
// 0 --(+1)-> 1 --(+1)-> 2 --(+1)-> 3 --(+1)-> 4 --(+1)-> 0 := +5
Assert.Equal(5, solution.ObjectiveValue());
}
}
} // namespace Google.OrTools.Tests