DotNet Reference

DotNet Reference

SimpleRoutingProgram.cs
Go to the documentation of this file.
1 // Copyright 2010-2018 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 // [START program]
15 // [START import]
16 using System;
18 // [END import]
19 
23 public class SimpleRoutingProgram {
24  public static void Main(String[] args) {
25  // Instantiate the data problem.
26  // [START data]
27  const int numLocation = 5;
28  const int numVehicles = 1;
29  const int depot = 0;
30  // [END data]
31 
32  // Create Routing Index Manager
33  // [START index_manager]
35  numLocation,
36  numVehicles,
37  depot);
38  // [END index_manager]
39 
40  // Create Routing Model.
41  // [START routing_model]
42  RoutingModel routing = new RoutingModel(manager);
43  // [END routing_model]
44 
45  // Create and register a transit callback.
46  // [START transit_callback]
47  int transitCallbackIndex = routing.RegisterTransitCallback(
48  (long fromIndex, long toIndex) => {
49  // Convert from routing variable Index to distance matrix NodeIndex.
50  var fromNode = manager.IndexToNode(fromIndex);
51  var toNode = manager.IndexToNode(toIndex);
52  return Math.Abs(toNode - fromNode);
53  });
54  // [END transit_callback]
55 
56  // Define cost of each arc.
57  // [START arc_cost]
58  routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
59  // [END arc_cost]
60 
61  // Setting first solution heuristic.
62  // [START parameters]
63  RoutingSearchParameters searchParameters =
65  searchParameters.FirstSolutionStrategy =
66  FirstSolutionStrategy.Types.Value.PathCheapestArc;
67  // [END parameters]
68 
69  // Solve the problem.
70  // [START solve]
71  Assignment solution = routing.SolveWithParameters(searchParameters);
72  // [END solve]
73 
74  // Print solution on console.
75  // [START print_solution]
76  Console.WriteLine("Objective: {0}", solution.ObjectiveValue());
77  // Inspect solution.
78  long index = routing.Start(0);
79  Console.WriteLine("Route for Vehicle 0:");
80  long route_distance = 0;
81  while (routing.IsEnd(index) == false) {
82  Console.Write("{0} -> ", manager.IndexToNode((int)index));
83  long previousIndex = index;
84  index = solution.Value(routing.NextVar(index));
85  route_distance += routing.GetArcCostForVehicle(previousIndex, index, 0);
86  }
87  Console.WriteLine("{0}", manager.IndexToNode(index));
88  Console.WriteLine("Distance of the route: {0}m", route_distance);
89  // [END print_solution]
90  }
91 }
92 // [END program]
void SetArcCostEvaluatorOfAllVehicles(int evaluator_index)
long Value(IntVar var)
Definition: Assignment.cs:167
global::Google.OrTools.ConstraintSolver.FirstSolutionStrategy.Types.Value FirstSolutionStrategy
First solution strategies, used as starting point of local search.
Definition: Assignment.cs:11
int IndexToNode(long index)
Value
long Start(int vehicle)
Definition: RoutingModel.cs:18
bool IsEnd(long index)
This is a sample using the routing library .Net wrapper.
First solution strategies, used as starting point of local search.
IntVar NextVar(long index)
static void Main(String[] args)
long ObjectiveValue()
Definition: Assignment.cs:114
static Google.OrTools.ConstraintSolver.RoutingSearchParameters DefaultRoutingSearchParameters()
Definition: Assignment.cs:18
Container for nested types declared in the FirstSolutionStrategy message type.
int RegisterTransitCallback(LongLongToLong callback)
long GetArcCostForVehicle(long from_index, long to_index, long vehicle)
Assignment SolveWithParameters(Google.OrTools.ConstraintSolver.RoutingSearchParameters search_parameters)
Parameters defining the search used to solve vehicle routing problems.