forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cscvrptw.cs
348 lines (318 loc) · 13.8 KB
/
cscvrptw.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright 2010-2021 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using Google.OrTools.ConstraintSolver;
/// <summary>
/// Sample showing how to model and solve a capacitated vehicle routing
/// problem with time windows using the swig-wrapped version of the vehicle
/// routing library in src/constraint_solver.
/// </summary>
public class CapacitatedVehicleRoutingProblemWithTimeWindows
{
/// <summary>
/// A position on the map with (x, y) coordinates.
/// </summary>
class Position
{
public Position()
{
this.x_ = 0;
this.y_ = 0;
}
public Position(int x, int y)
{
this.x_ = x;
this.y_ = y;
}
public int x_;
public int y_;
}
/// <summary>
/// A time window with start/end data.
/// </summary>
class TimeWindow
{
public TimeWindow()
{
this.start_ = -1;
this.end_ = -1;
}
public TimeWindow(int start, int end)
{
this.start_ = start;
this.end_ = end;
}
public int start_;
public int end_;
}
/// <summary>
/// Manhattan distance implemented as a callback. It uses an array of
/// positions and computes the Manhattan distance between the two
/// positions of two different indices.
/// </summary>
class Manhattan
{
public Manhattan(RoutingIndexManager manager, Position[] locations, int coefficient)
{
this.manager_ = manager;
this.locations_ = locations;
this.coefficient_ = coefficient;
}
public long Call(long first_index, long second_index)
{
if (first_index >= locations_.Length || second_index >= locations_.Length)
{
return 0;
}
int first_node = manager_.IndexToNode(first_index);
int second_node = manager_.IndexToNode(second_index);
return (Math.Abs(locations_[first_node].x_ - locations_[second_node].x_) +
Math.Abs(locations_[first_node].y_ - locations_[second_node].y_)) *
coefficient_;
}
private readonly RoutingIndexManager manager_;
private readonly Position[] locations_;
private readonly int coefficient_;
};
/// <summary>
/// A callback that computes the volume of a demand stored in an
/// integer array.
/// </summary>
class Demand
{
public Demand(RoutingIndexManager manager, int[] order_demands)
{
this.manager_ = manager;
this.order_demands_ = order_demands;
}
public long Call(long index)
{
if (index < order_demands_.Length)
{
int node = manager_.IndexToNode(index);
return order_demands_[node];
}
return 0;
}
private readonly RoutingIndexManager manager_;
private readonly int[] order_demands_;
};
/// Locations representing either an order location or a vehicle route
/// start/end.
private Position[] locations_;
/// Quantity to be picked up for each order.
private int[] order_demands_;
/// Time window in which each order must be performed.
private TimeWindow[] order_time_windows_;
/// Penalty cost "paid" for dropping an order.
private int[] order_penalties_;
/// Capacity of the vehicles.
private int vehicle_capacity_ = 0;
/// Latest time at which each vehicle must end its tour.
private int[] vehicle_end_time_;
/// Cost per unit of distance of each vehicle.
private int[] vehicle_cost_coefficients_;
/// Vehicle start and end indices. They have to be implemented as int[] due
/// to the available SWIG-ed interface.
private int[] vehicle_starts_;
private int[] vehicle_ends_;
/// Random number generator to produce data.
private Random random_generator = new Random(0xBEEF);
/// <summary>
/// Constructs a capacitated vehicle routing problem with time windows.
/// </summary>
private CapacitatedVehicleRoutingProblemWithTimeWindows()
{
}
/// <summary>
/// Creates order data. Location of the order is random, as well
/// as its demand (quantity), time window and penalty. ///
/// </summary>
/// <param name="number_of_orders"> number of orders to build. </param>
/// <param name="x_max"> maximum x coordinate in which orders are located.
/// </param>
/// <param name="y_max"> maximum y coordinate in which orders are located.
/// </param>
/// <param name="demand_max"> maximum quantity of a demand. </param>
/// <param name="time_window_max"> maximum starting time of the order time
/// window. </param>
/// <param name="time_window_width"> duration of the order time window.
/// </param>
/// <param name="penalty_min"> minimum pernalty cost if order is dropped.
/// </param>
/// <param name="penalty_max"> maximum pernalty cost if order is dropped.
/// </param>
private void BuildOrders(int number_of_orders, int number_of_vehicles, int x_max, int y_max, int demand_max,
int time_window_max, int time_window_width, int penalty_min, int penalty_max)
{
Console.WriteLine("Building orders.");
locations_ = new Position[number_of_orders + 2 * number_of_vehicles];
order_demands_ = new int[number_of_orders];
order_time_windows_ = new TimeWindow[number_of_orders];
order_penalties_ = new int[number_of_orders];
for (int order = 0; order < number_of_orders; ++order)
{
locations_[order] = new Position(random_generator.Next(x_max + 1), random_generator.Next(y_max + 1));
order_demands_[order] = random_generator.Next(demand_max + 1);
int time_window_start = random_generator.Next(time_window_max + 1);
order_time_windows_[order] = new TimeWindow(time_window_start, time_window_start + time_window_width);
order_penalties_[order] = random_generator.Next(penalty_max - penalty_min + 1) + penalty_min;
}
}
/// <summary>
/// Creates fleet data. Vehicle starting and ending locations are
/// random, as well as vehicle costs per distance unit.
/// </summary>
///
/// <param name="number_of_orders"> number of orders</param>
/// <param name="number_of_vehicles"> number of vehicles</param>
/// <param name="x_max"> maximum x coordinate in which orders are located.
/// </param>
/// <param name="y_max"> maximum y coordinate in which orders are located.
/// </param>
/// <param name="end_time"> latest end time of a tour of a vehicle. </param>
/// <param name="capacity"> capacity of a vehicle. </param>
/// <param name="cost_coefficient_max"> maximum cost per distance unit of a
/// vehicle (minimum is 1)</param>
private void BuildFleet(int number_of_orders, int number_of_vehicles, int x_max, int y_max, int end_time,
int capacity, int cost_coefficient_max)
{
Console.WriteLine("Building fleet.");
vehicle_capacity_ = capacity;
vehicle_starts_ = new int[number_of_vehicles];
vehicle_ends_ = new int[number_of_vehicles];
vehicle_end_time_ = new int[number_of_vehicles];
vehicle_cost_coefficients_ = new int[number_of_vehicles];
for (int vehicle = 0; vehicle < number_of_vehicles; ++vehicle)
{
int index = 2 * vehicle + number_of_orders;
vehicle_starts_[vehicle] = index;
locations_[index] = new Position(random_generator.Next(x_max + 1), random_generator.Next(y_max + 1));
vehicle_ends_[vehicle] = index + 1;
locations_[index + 1] = new Position(random_generator.Next(x_max + 1), random_generator.Next(y_max + 1));
vehicle_end_time_[vehicle] = end_time;
vehicle_cost_coefficients_[vehicle] = random_generator.Next(cost_coefficient_max) + 1;
}
}
/// <summary>
/// Solves the current routing problem.
/// </summary>
private void Solve(int number_of_orders, int number_of_vehicles)
{
Console.WriteLine("Creating model with " + number_of_orders + " orders and " + number_of_vehicles +
" vehicles.");
// Finalizing model
int number_of_locations = locations_.Length;
RoutingIndexManager manager =
new RoutingIndexManager(number_of_locations, number_of_vehicles, vehicle_starts_, vehicle_ends_);
RoutingModel model = new RoutingModel(manager);
// Setting up dimensions
const int big_number = 100000;
Manhattan manhattan_callback = new Manhattan(manager, locations_, 1);
model.AddDimension(model.RegisterTransitCallback(manhattan_callback.Call), big_number, big_number, false,
"time");
RoutingDimension time_dimension = model.GetDimensionOrDie("time");
Demand demand_callback = new Demand(manager, order_demands_);
model.AddDimension(model.RegisterUnaryTransitCallback(demand_callback.Call), 0, vehicle_capacity_, true,
"capacity");
RoutingDimension capacity_dimension = model.GetDimensionOrDie("capacity");
// Setting up vehicles
Manhattan[] cost_callbacks = new Manhattan[number_of_vehicles];
for (int vehicle = 0; vehicle < number_of_vehicles; ++vehicle)
{
int cost_coefficient = vehicle_cost_coefficients_[vehicle];
Manhattan manhattan_cost_callback = new Manhattan(manager, locations_, cost_coefficient);
cost_callbacks[vehicle] = manhattan_cost_callback;
int manhattan_cost_index = model.RegisterTransitCallback(manhattan_cost_callback.Call);
model.SetArcCostEvaluatorOfVehicle(manhattan_cost_index, vehicle);
time_dimension.CumulVar(model.End(vehicle)).SetMax(vehicle_end_time_[vehicle]);
}
// Setting up orders
for (int order = 0; order < number_of_orders; ++order)
{
time_dimension.CumulVar(order).SetRange(order_time_windows_[order].start_, order_time_windows_[order].end_);
long[] orders = { manager.NodeToIndex(order) };
model.AddDisjunction(orders, order_penalties_[order]);
}
// Solving
RoutingSearchParameters search_parameters =
operations_research_constraint_solver.DefaultRoutingSearchParameters();
search_parameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.AllUnperformed;
Console.WriteLine("Search...");
Assignment solution = model.SolveWithParameters(search_parameters);
if (solution != null)
{
String output = "Total cost: " + solution.ObjectiveValue() + "\n";
// Dropped orders
String dropped = "";
for (int order = 0; order < number_of_orders; ++order)
{
if (solution.Value(model.NextVar(order)) == order)
{
dropped += " " + order;
}
}
if (dropped.Length > 0)
{
output += "Dropped orders:" + dropped + "\n";
}
// Routes
for (int vehicle = 0; vehicle < number_of_vehicles; ++vehicle)
{
String route = "Vehicle " + vehicle + ": ";
long order = model.Start(vehicle);
if (model.IsEnd(solution.Value(model.NextVar(order))))
{
route += "Empty";
}
else
{
for (; !model.IsEnd(order); order = solution.Value(model.NextVar(order)))
{
IntVar local_load = capacity_dimension.CumulVar(order);
IntVar local_time = time_dimension.CumulVar(order);
route += order + " Load(" + solution.Value(local_load) + ") " + "Time(" +
solution.Min(local_time) + ", " + solution.Max(local_time) + ") -> ";
}
IntVar load = capacity_dimension.CumulVar(order);
IntVar time = time_dimension.CumulVar(order);
route += order + " Load(" + solution.Value(load) + ") " + "Time(" + solution.Min(time) + ", " +
solution.Max(time) + ")";
}
output += route + "\n";
}
Console.WriteLine(output);
}
}
public static void Main(String[] args)
{
CapacitatedVehicleRoutingProblemWithTimeWindows problem = new CapacitatedVehicleRoutingProblemWithTimeWindows();
int x_max = 20;
int y_max = 20;
int demand_max = 3;
int time_window_max = 24 * 60;
int time_window_width = 4 * 60;
int penalty_min = 50;
int penalty_max = 100;
int end_time = 24 * 60;
int cost_coefficient_max = 3;
int orders = 100;
int vehicles = 20;
int capacity = 50;
problem.BuildOrders(orders, vehicles, x_max, y_max, demand_max, time_window_max, time_window_width, penalty_min,
penalty_max);
problem.BuildFleet(orders, vehicles, x_max, y_max, end_time, capacity, cost_coefficient_max);
problem.Solve(orders, vehicles);
}
}