DotNet Reference

.Net Reference

RoutingSolverTests.cs
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 using System;
15 using System.Linq;
16 using Xunit;
18 
19 namespace Google.OrTools.Tests
20 {
21 public class RoutingSolverTest
22 {
23  [Theory]
24  [InlineData(false)]
25  [InlineData(true)]
26  public void SimpleLambdaCallback(bool callGC)
27  {
28  // Create Routing Index Manager
29  RoutingIndexManager manager = new RoutingIndexManager(5 /*locations*/, 1 /*vehicle*/, 0 /*depot*/);
30  Assert.NotNull(manager);
31  // Create Routing Model.
32  RoutingModel routing = new RoutingModel(manager);
33  Assert.NotNull(routing);
34  // Create a distance callback.
35  int transitCallbackIndex = routing.RegisterTransitCallback((long fromIndex, long toIndex) =>
36  {
37  // Convert from routing variable Index to
38  // distance matrix NodeIndex.
39  var fromNode = manager.IndexToNode(fromIndex);
40  var toNode = manager.IndexToNode(toIndex);
41  return Math.Abs(toNode - fromNode);
42  });
43  // Define cost of each arc.
44  routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
45  if (callGC)
46  {
47  GC.Collect();
48  }
49  // Setting first solution heuristic.
50  RoutingSearchParameters searchParameters =
51  operations_research_constraint_solver.DefaultRoutingSearchParameters();
52  searchParameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;
53  Assignment solution = routing.SolveWithParameters(searchParameters);
54  // 0 --(+1)-> 1 --(+1)-> 2 --(+1)-> 3 --(+1)-> 4 --(+4)-> 0 := +8
55  Assert.Equal(8, solution.ObjectiveValue());
56  }
57 
58  [Fact]
59  public void TestTransitMatrix()
60  {
61  // Create Routing Index Manager
62  RoutingIndexManager manager = new RoutingIndexManager(5 /*locations*/, 1 /*vehicle*/, 0 /*depot*/);
63  Assert.NotNull(manager);
64  // Create Routing Model.
65  RoutingModel routing = new RoutingModel(manager);
66  Assert.NotNull(routing);
67  // Create a distance callback.
68  long[][] matrix = new long[][] {
69  new long[] { 1, 1, 1, 1, 1 }, new long[] { 1, 1, 1, 1, 1 }, new long[] { 1, 1, 1, 1, 1 },
70  new long[] { 1, 1, 1, 1, 1 }, new long[] { 1, 1, 1, 1, 1 },
71  };
72  int transitCallbackIndex = routing.RegisterTransitMatrix(matrix);
73  // Define cost of each arc.
74  routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
75  // Setting first solution heuristic.
76  RoutingSearchParameters searchParameters =
77  operations_research_constraint_solver.DefaultRoutingSearchParameters();
78  searchParameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;
79  Assignment solution = routing.SolveWithParameters(searchParameters);
80  // 0 --(+1)-> 1 --(+1)-> 2 --(+1)-> 3 --(+1)-> 4 --(+1)-> 0 := +5
81  Assert.Equal(5, solution.ObjectiveValue());
82  }
83 
84  [Fact]
85  public void TestTransitCallback()
86  {
87  // Create Routing Index Manager
88  RoutingIndexManager manager = new RoutingIndexManager(5 /*locations*/, 1 /*vehicle*/, 0 /*depot*/);
89  Assert.NotNull(manager);
90  // Create Routing Model.
91  RoutingModel routing = new RoutingModel(manager);
92  Assert.NotNull(routing);
93  // Create a distance callback.
94  int transitCallbackIndex = routing.RegisterTransitCallback((long fromIndex, long toIndex) =>
95  {
96  // Convert from routing variable Index to
97  // distance matrix NodeIndex.
98  var fromNode = manager.IndexToNode(fromIndex);
99  var toNode = manager.IndexToNode(toIndex);
100  return Math.Abs(toNode - fromNode);
101  });
102  // Define cost of each arc.
103  routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
104  // Setting first solution heuristic.
105  RoutingSearchParameters searchParameters =
106  operations_research_constraint_solver.DefaultRoutingSearchParameters();
107  searchParameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;
108  Assignment solution = routing.SolveWithParameters(searchParameters);
109  Assert.Equal(8, solution.ObjectiveValue());
110  }
111 
112  [Fact]
113  public void TestMatrixDimension()
114  {
115  // Create Routing Index Manager
116  RoutingIndexManager manager = new RoutingIndexManager(5 /*locations*/, 1 /*vehicle*/, 0 /*depot*/);
117  Assert.NotNull(manager);
118  // Create Routing Model.
119  RoutingModel routing = new RoutingModel(manager);
120  Assert.NotNull(routing);
121  // Create a distance callback.
122  long[][] matrix = new long[][] {
123  new long[] { 1, 1, 1, 1, 1 }, new long[] { 1, 1, 1, 1, 1 }, new long[] { 1, 1, 1, 1, 1 },
124  new long[] { 1, 1, 1, 1, 1 }, new long[] { 1, 1, 1, 1, 1 },
125  };
126  IntBoolPair result = routing.AddMatrixDimension(matrix,
127  /*capacity=*/10,
128  /*fix_start_cumul_to_zero=*/true, "Dimension");
129  // Define cost of each arc.
130  routing.SetArcCostEvaluatorOfAllVehicles(result.first);
131  // Setting first solution heuristic.
132  RoutingSearchParameters searchParameters =
133  operations_research_constraint_solver.DefaultRoutingSearchParameters();
134  searchParameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;
135  Assignment solution = routing.SolveWithParameters(searchParameters);
136  // 0 --(+1)-> 1 --(+1)-> 2 --(+1)-> 3 --(+1)-> 4 --(+1)-> 0 := +5
137  Assert.Equal(5, solution.ObjectiveValue());
138  }
139 
140  [Fact]
142  {
143  // Create Routing Index Manager
144  RoutingIndexManager manager = new RoutingIndexManager(5 /*locations*/, 1 /*vehicle*/, 0 /*depot*/);
145  Assert.NotNull(manager);
146  // Create Routing Model.
147  RoutingModel routing = new RoutingModel(manager);
148  Assert.NotNull(routing);
149  // Create a distance callback.
150  long[] vector = { 1, 1, 1, 1, 1 };
151  int transitCallbackIndex = routing.RegisterUnaryTransitVector(vector);
152  // Define cost of each arc.
153  routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
154  // Setting first solution heuristic.
155  RoutingSearchParameters searchParameters =
156  operations_research_constraint_solver.DefaultRoutingSearchParameters();
157  searchParameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;
158  Assignment solution = routing.SolveWithParameters(searchParameters);
159  // 0 --(+1)-> 1 --(+1)-> 2 --(+1)-> 3 --(+1)-> 4 --(+1)-> 0 := +5
160  Assert.Equal(5, solution.ObjectiveValue());
161  }
162 
163  [Fact]
165  {
166  // Create Routing Index Manager
167  RoutingIndexManager manager = new RoutingIndexManager(5 /*locations*/, 1 /*vehicle*/, 0 /*depot*/);
168  Assert.NotNull(manager);
169  // Create Routing Model.
170  RoutingModel routing = new RoutingModel(manager);
171  Assert.NotNull(routing);
172  // Create a distance callback.
173  int transitCallbackIndex = routing.RegisterUnaryTransitCallback((long fromIndex) =>
174  {
175  // Convert from routing variable Index to
176  // distance matrix NodeIndex.
177  var fromNode =
178  manager.IndexToNode(fromIndex);
179  return fromNode + 1;
180  });
181  // Define cost of each arc.
182  routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
183  // Setting first solution heuristic.
184  RoutingSearchParameters searchParameters =
185  operations_research_constraint_solver.DefaultRoutingSearchParameters();
186  searchParameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;
187  Assignment solution = routing.SolveWithParameters(searchParameters);
188  // 0 --(+1)-> 1 --(+2)-> 2 --(+3)-> 3 --(+4)-> 4 --(+5)-> 0 := +15
189  Assert.Equal(15, solution.ObjectiveValue());
190  }
191 
192  [Fact]
193  public void TestVectorDimension()
194  {
195  // Create Routing Index Manager
196  RoutingIndexManager manager = new RoutingIndexManager(5 /*locations*/, 1 /*vehicle*/, 0 /*depot*/);
197  Assert.NotNull(manager);
198  // Create Routing Model.
199  RoutingModel routing = new RoutingModel(manager);
200  Assert.NotNull(routing);
201  // Create a distance callback.
202  long[] vector = new long[] { 1, 1, 1, 1, 1 };
203  IntBoolPair result = routing.AddVectorDimension(vector,
204  /*capacity=*/10,
205  /*fix_start_cumul_to_zero=*/true, "Dimension");
206  // Define cost of each arc.
207  routing.SetArcCostEvaluatorOfAllVehicles(result.first);
208  // Setting first solution heuristic.
209  RoutingSearchParameters searchParameters =
210  operations_research_constraint_solver.DefaultRoutingSearchParameters();
211  searchParameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;
212  Assignment solution = routing.SolveWithParameters(searchParameters);
213  // 0 --(+1)-> 1 --(+1)-> 2 --(+1)-> 3 --(+1)-> 4 --(+1)-> 0 := +5
214  Assert.Equal(5, solution.ObjectiveValue());
215  }
216 }
217 
218 public class BoundCostTest
219 {
220  [Fact]
221  public void TestCtor()
222  {
223  // Create Routing Index Manager
224  BoundCost boundCost = new BoundCost();
225  Assert.NotNull(boundCost);
226  Assert.Equal(0, boundCost.bound);
227  Assert.Equal(0, boundCost.cost);
228 
229  boundCost = new BoundCost(97 /*bound*/, 101 /*cost*/);
230  Assert.NotNull(boundCost);
231  Assert.Equal(97, boundCost.bound);
232  Assert.Equal(101, boundCost.cost);
233  }
234 }
235 
237 {
238  [Fact]
239  public void TestCtor()
240  {
241  // Create Routing Index Manager
242  RoutingIndexManager manager = new RoutingIndexManager(31 /*locations*/, 7 /*vehicle*/, 3 /*depot*/);
243  Assert.NotNull(manager);
244  // Create Routing Model.
245  RoutingModel routing = new RoutingModel(manager);
246  Assert.NotNull(routing);
247  // Create a distance callback.
248  int transitIndex = routing.RegisterTransitCallback((long fromIndex, long toIndex) =>
249  {
250  // Convert from routing variable Index to
251  // distance matrix NodeIndex.
252  var fromNode = manager.IndexToNode(fromIndex);
253  var toNode = manager.IndexToNode(toIndex);
254  return Math.Abs(toNode - fromNode);
255  });
256  Assert.True(routing.AddDimension(transitIndex, 100, 100, true, "Dimension"));
257  RoutingDimension dimension = routing.GetDimensionOrDie("Dimension");
258  }
259 
260  [Fact]
262  {
263  // Create Routing Index Manager
264  RoutingIndexManager manager = new RoutingIndexManager(31 /*locations*/, 7 /*vehicle*/, 3 /*depot*/);
265  Assert.NotNull(manager);
266  // Create Routing Model.
267  RoutingModel routing = new RoutingModel(manager);
268  Assert.NotNull(routing);
269  // Create a distance callback.
270  int transitIndex = routing.RegisterTransitCallback((long fromIndex, long toIndex) =>
271  {
272  // Convert from routing variable Index to
273  // distance matrix NodeIndex.
274  var fromNode = manager.IndexToNode(fromIndex);
275  var toNode = manager.IndexToNode(toIndex);
276  return Math.Abs(toNode - fromNode);
277  });
278  Assert.True(routing.AddDimension(transitIndex, 100, 100, true, "Dimension"));
279  RoutingDimension dimension = routing.GetDimensionOrDie("Dimension");
280 
281  BoundCost boundCost = new BoundCost(/*bound=*/97, /*cost=*/43);
282  Assert.NotNull(boundCost);
283  Assert.False(dimension.HasSoftSpanUpperBounds());
284  foreach (int v in Enumerable.Range(0, manager.GetNumberOfVehicles()).ToArray())
285  {
286  dimension.SetSoftSpanUpperBoundForVehicle(boundCost, v);
287  BoundCost bc = dimension.GetSoftSpanUpperBoundForVehicle(v);
288  Assert.NotNull(bc);
289  Assert.Equal(97, bc.bound);
290  Assert.Equal(43, bc.cost);
291  }
292  Assert.True(dimension.HasSoftSpanUpperBounds());
293  }
294 
295  [Fact]
297  {
298  // Create Routing Index Manager
299  RoutingIndexManager manager = new RoutingIndexManager(31 /*locations*/, 7 /*vehicle*/, 3 /*depot*/);
300  Assert.NotNull(manager);
301  // Create Routing Model.
302  RoutingModel routing = new RoutingModel(manager);
303  Assert.NotNull(routing);
304  // Create a distance callback.
305  int transitIndex = routing.RegisterTransitCallback((long fromIndex, long toIndex) =>
306  {
307  // Convert from routing variable Index to
308  // distance matrix NodeIndex.
309  var fromNode = manager.IndexToNode(fromIndex);
310  var toNode = manager.IndexToNode(toIndex);
311  return Math.Abs(toNode - fromNode);
312  });
313  Assert.True(routing.AddDimension(transitIndex, 100, 100, true, "Dimension"));
314  RoutingDimension dimension = routing.GetDimensionOrDie("Dimension");
315 
316  BoundCost boundCost = new BoundCost(/*bound=*/97, /*cost=*/43);
317  Assert.NotNull(boundCost);
318  Assert.False(dimension.HasQuadraticCostSoftSpanUpperBounds());
319  foreach (int v in Enumerable.Range(0, manager.GetNumberOfVehicles()).ToArray())
320  {
321  dimension.SetQuadraticCostSoftSpanUpperBoundForVehicle(boundCost, v);
322  BoundCost bc = dimension.GetQuadraticCostSoftSpanUpperBoundForVehicle(v);
323  Assert.NotNull(bc);
324  Assert.Equal(97, bc.bound);
325  Assert.Equal(43, bc.cost);
326  }
327  Assert.True(dimension.HasQuadraticCostSoftSpanUpperBounds());
328  }
329 }
330 
331 } // namespace Google.OrTools.Tests