DotNet Reference

DotNet Reference

VrpTimeWindows.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 // [END import]
20 
24 public class VrpTimeWindows {
25  // [START data_model]
26  class DataModel {
27  public long[,] TimeMatrix = {
28  {0, 6, 9, 8, 7, 3, 6, 2, 3, 2, 6, 6, 4, 4, 5, 9, 7},
29  {6, 0, 8, 3, 2, 6, 8, 4, 8, 8, 13, 7, 5, 8, 12, 10, 14},
30  {9, 8, 0, 11, 10, 6, 3, 9, 5, 8, 4, 15, 14, 13, 9, 18, 9},
31  {8, 3, 11, 0, 1, 7, 10, 6, 10, 10, 14, 6, 7, 9, 14, 6, 16},
32  {7, 2, 10, 1, 0, 6, 9, 4, 8, 9, 13, 4, 6, 8, 12, 8, 14},
33  {3, 6, 6, 7, 6, 0, 2, 3, 2, 2, 7, 9, 7, 7, 6, 12, 8},
34  {6, 8, 3, 10, 9, 2, 0, 6, 2, 5, 4, 12, 10, 10, 6, 15, 5},
35  {2, 4, 9, 6, 4, 3, 6, 0, 4, 4, 8, 5, 4, 3, 7, 8, 10},
36  {3, 8, 5, 10, 8, 2, 2, 4, 0, 3, 4, 9, 8, 7, 3, 13, 6},
37  {2, 8, 8, 10, 9, 2, 5, 4, 3, 0, 4, 6, 5, 4, 3, 9, 5},
38  {6, 13, 4, 14, 13, 7, 4, 8, 4, 4, 0, 10, 9, 8, 4, 13, 4},
39  {6, 7, 15, 6, 4, 9, 12, 5, 9, 6, 10, 0, 1, 3, 7, 3, 10},
40  {4, 5, 14, 7, 6, 7, 10, 4, 8, 5, 9, 1, 0, 2, 6, 4, 8},
41  {4, 8, 13, 9, 8, 7, 10, 3, 7, 4, 8, 3, 2, 0, 4, 5, 6},
42  {5, 12, 9, 14, 12, 6, 6, 7, 3, 3, 4, 7, 6, 4, 0, 9, 2},
43  {9, 10, 18, 6, 8, 12, 15, 8, 13, 9, 13, 3, 4, 5, 9, 0, 9},
44  {7, 14, 9, 16, 14, 8, 5, 10, 6, 5, 4, 10, 8, 6, 2, 9, 0},
45  };
46  public long[,] TimeWindows = {
47  {0, 5}, // depot
48  {7, 12}, // 1
49  {10, 15}, // 2
50  {5, 14}, // 3
51  {5, 13}, // 4
52  {0, 5}, // 5
53  {5, 10}, // 6
54  {0, 10}, // 7
55  {5, 10}, // 8
56  {0, 5}, // 9
57  {10, 16}, // 10
58  {10, 15}, // 11
59  {0, 5}, // 12
60  {5, 10}, // 13
61  {7, 12}, // 14
62  {10, 15}, // 15
63  {5, 15}, // 16
64  };
65  public int VehicleNumber = 4;
66  public int Depot = 0;
67  };
68  // [END data_model]
69 
70  // [START solution_printer]
74  static void PrintSolution(
75  in DataModel data,
76  in RoutingModel routing,
77  in RoutingIndexManager manager,
78  in Assignment solution) {
79  RoutingDimension timeDimension = routing.GetMutableDimension("Time");
80  // Inspect solution.
81  long totalTime = 0;
82  for (int i = 0; i < data.VehicleNumber; ++i) {
83  Console.WriteLine("Route for Vehicle {0}:", i);
84  var index = routing.Start(i);
85  while (routing.IsEnd(index) == false) {
86  var timeVar = timeDimension.CumulVar(index);
87  Console.Write("{0} Time({1},{2}) -> ",
88  manager.IndexToNode(index),
89  solution.Min(timeVar),
90  solution.Max(timeVar));
91  index = solution.Value(routing.NextVar(index));
92  }
93  var endTimeVar = timeDimension.CumulVar(index);
94  Console.WriteLine("{0} Time({1},{2})",
95  manager.IndexToNode(index),
96  solution.Min(endTimeVar),
97  solution.Max(endTimeVar));
98  Console.WriteLine("Time of the route: {0}min", solution.Min(endTimeVar));
99  totalTime += solution.Min(endTimeVar);
100  }
101  Console.WriteLine("Total time of all routes: {0}min", totalTime);
102  }
103  // [END solution_printer]
104 
105  public static void Main(String[] args) {
106  // Instantiate the data problem.
107  // [START data]
108  DataModel data = new DataModel();
109  // [END data]
110 
111  // Create Routing Index Manager
112  // [START index_manager]
114  data.TimeMatrix.GetLength(0),
115  data.VehicleNumber,
116  data.Depot);
117  // [END index_manager]
118 
119  // Create Routing Model.
120  // [START routing_model]
121  RoutingModel routing = new RoutingModel(manager);
122  // [END routing_model]
123 
124  // Create and register a transit callback.
125  // [START transit_callback]
126  int transitCallbackIndex = routing.RegisterTransitCallback(
127  (long fromIndex, long toIndex) => {
128  // Convert from routing variable Index to distance matrix NodeIndex.
129  var fromNode = manager.IndexToNode(fromIndex);
130  var toNode = manager.IndexToNode(toIndex);
131  return data.TimeMatrix[fromNode, toNode]; }
132  );
133  // [END transit_callback]
134 
135  // Define cost of each arc.
136  // [START arc_cost]
137  routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
138  // [END arc_cost]
139 
140  // Add Distance constraint.
141  // [START time_constraint]
142  routing.AddDimension(
143  transitCallbackIndex, // transit callback
144  30, // allow waiting time
145  30, // vehicle maximum capacities
146  false, // start cumul to zero
147  "Time");
148  RoutingDimension timeDimension = routing.GetMutableDimension("Time");
149  // Add time window constraints for each location except depot.
150  for (int i = 1; i < data.TimeWindows.GetLength(0); ++i) {
151  long index = manager.NodeToIndex(i);
152  timeDimension.CumulVar(index).SetRange(
153  data.TimeWindows[i, 0],
154  data.TimeWindows[i, 1]);
155  }
156  // Add time window constraints for each vehicle start node.
157  for (int i = 0; i < data.VehicleNumber; ++i) {
158  long index = routing.Start(i);
159  timeDimension.CumulVar(index).SetRange(
160  data.TimeWindows[0, 0],
161  data.TimeWindows[0, 1]);
162  }
163  // [END time_constraint]
164 
165  // Instantiate route start and end times to produce feasible times.
166  // [START depot_start_end_times]
167  for (int i = 0; i < data.VehicleNumber; ++i) {
169  timeDimension.CumulVar(routing.Start(i)));
171  timeDimension.CumulVar(routing.End(i)));
172  }
173  // [END depot_start_end_times]
174 
175  // Setting first solution heuristic.
176  // [START parameters]
177  RoutingSearchParameters searchParameters =
179  searchParameters.FirstSolutionStrategy =
180  FirstSolutionStrategy.Types.Value.PathCheapestArc;
181  // [END parameters]
182 
183  // Solve the problem.
184  // [START solve]
185  Assignment solution = routing.SolveWithParameters(searchParameters);
186  // [END solve]
187 
188  // Print solution on console.
189  // [START print_solution]
190  PrintSolution(data, routing, manager, solution);
191  // [END print_solution]
192  }
193 }
194 // [END program]
virtual void SetRange(long l, long u)
Definition: IntExpr.cs:75
void SetArcCostEvaluatorOfAllVehicles(int evaluator_index)
long End(int vehicle)
virtual long Value()
Definition: IntVar.cs:62
global::Google.OrTools.ConstraintSolver.FirstSolutionStrategy.Types.Value FirstSolutionStrategy
First solution strategies, used as starting point of local search.
void AddVariableMinimizedByFinalizer(IntVar var)
long NodeToIndex(int node)
Definition: Assignment.cs:11
int IndexToNode(long index)
Value
long Start(int vehicle)
Definition: KInt64Vector.cs:11
IntVar CumulVar(long index)
Definition: RoutingModel.cs:18
static void Main(String[] args)
First solution strategies, used as starting point of local search.
Vehicles Routing Problem (VRP) with Time Windows.
virtual long Min()
Definition: IntExpr.cs:51
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
Definition: KInt64Vector.cs:11
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)
Parameters defining the search used to solve vehicle routing problems.