DotNet Reference

DotNet Reference

VrpWithTimeLimit.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;
17 using System.Collections.Generic;
19 using Google.Protobuf.WellKnownTypes; // Duration
20 // [END import]
21 
25 public class Vrp {
26  // [START solution_printer]
30  static void PrintSolution(
31  in RoutingIndexManager manager,
32  in RoutingModel routing,
33  in Assignment solution) {
34  // Inspect solution.
35  long maxRouteDistance = 0;
36  for (int i = 0; i < manager.GetNumberOfVehicles(); ++i) {
37  Console.WriteLine("Route for Vehicle {0}:", i);
38  long routeDistance = 0;
39  var index = routing.Start(i);
40  while (routing.IsEnd(index) == false) {
41  Console.Write("{0} -> ", manager.IndexToNode((int)index));
42  var previousIndex = index;
43  index = solution.Value(routing.NextVar(index));
44  routeDistance += routing.GetArcCostForVehicle(previousIndex, index, 0);
45  }
46  Console.WriteLine("{0}", manager.IndexToNode((int)index));
47  Console.WriteLine("Distance of the route: {0}m", routeDistance);
48  maxRouteDistance = Math.Max(routeDistance, maxRouteDistance);
49  }
50  Console.WriteLine("Maximum distance of the routes: {0}m", maxRouteDistance);
51  }
52  // [END solution_printer]
53 
54  public static void Main(String[] args) {
55  // Instantiate the data problem.
56  // [START data]
57  int locationNumber = 20;
58  int vehicleNumber = 5;
59  int depot = 0;
60  // [END data]
61 
62  // Create Routing Index Manager
63  // [START index_manager]
65  locationNumber,
66  vehicleNumber,
67  depot);
68 
69  // [END index_manager]
70 
71  // Create Routing Model.
72  // [START routing_model]
73  RoutingModel routing = new RoutingModel(manager);
74  // [END routing_model]
75 
76  // Create and register a transit callback.
77  // [START transit_callback]
78  int transitCallbackIndex = routing.RegisterTransitCallback(
79  (long fromIndex, long toIndex) => {
80  // Convert from routing variable Index to distance matrix NodeIndex.
81  var fromNode = manager.IndexToNode(fromIndex);
82  var toNode = manager.IndexToNode(toIndex);
83  return 1; }
84  );
85  // [END transit_callback]
86 
87  // Define cost of each arc.
88  // [START arc_cost]
89  routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
90  // [END arc_cost]
91 
92  // Add Distance constraint.
93  // [START distance_constraint]
94  routing.AddDimension(
95  transitCallbackIndex,
96  /*slack=*/0,
97  /*horizon=*/3000,
98  /*start_cumul_to_zero=*/true,
99  "Distance");
100  RoutingDimension distanceDimension = routing.GetMutableDimension("Distance");
101  distanceDimension.SetGlobalSpanCostCoefficient(100);
102  // [END distance_constraint]
103 
104  // Setting first solution heuristic.
105  // [START parameters]
106  RoutingSearchParameters searchParameters =
108  searchParameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;
109  searchParameters.LocalSearchMetaheuristic = LocalSearchMetaheuristic.Types.Value.GuidedLocalSearch;
110  searchParameters.LogSearch = true;
111  searchParameters.TimeLimit = new Duration { Seconds = 10 };
112  // [END parameters]
113 
114  // Solve the problem.
115  // [START solve]
116  Assignment solution = routing.SolveWithParameters(searchParameters);
117  // [END solve]
118 
119  // Print solution on console.
120  // [START print_solution]
121  PrintSolution(manager, routing, solution);
122  // [END print_solution]
123  }
124 }
125 // [END program]
void SetArcCostEvaluatorOfAllVehicles(int evaluator_index)
Value
global::Google.OrTools.ConstraintSolver.FirstSolutionStrategy.Types.Value FirstSolutionStrategy
First solution strategies, used as starting point of local search.
global::Google.OrTools.ConstraintSolver.LocalSearchMetaheuristic.Types.Value LocalSearchMetaheuristic
Local search metaheuristics used to guide the search.
Minimal TSP using distance matrix.
Definition: Vrp.cs:24
global::Google.Protobuf.WellKnownTypes.Duration TimeLimit
Limit to the time spent in the search.
Container for nested types declared in the LocalSearchMetaheuristic message type.
Definition: Assignment.cs:11
int IndexToNode(long index)
Value
Definition: RoutingModel.cs:18
static void Main(String[] args)
Local search metaheuristics used to guide the search.
First solution strategies, used as starting point of local search.
void SetGlobalSpanCostCoefficient(long coefficient)
bool AddDimension(int evaluator_index, long slack_max, long capacity, bool fix_start_cumul_to_zero, string name)
static Google.OrTools.ConstraintSolver.RoutingSearchParameters DefaultRoutingSearchParameters()
Definition: Assignment.cs:18
Container for nested types declared in the FirstSolutionStrategy message type.
int RegisterTransitCallback(LongLongToLong callback)
Assignment SolveWithParameters(Google.OrTools.ConstraintSolver.RoutingSearchParameters search_parameters)
RoutingDimension GetMutableDimension(string dimension_name)
bool LogSearch
— Miscellaneous — Some of these are advanced settings which should not be modified unless you know wh...
Parameters defining the search used to solve vehicle routing problems.