forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoutingSolverTests.cs
40 lines (38 loc) · 1.52 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
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());
}
}
} // namespace Google.OrTools.Tests