-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathindex.js
More file actions
168 lines (124 loc) · 4.36 KB
/
index.js
File metadata and controls
168 lines (124 loc) · 4.36 KB
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
'use strict';
// Query the Mapbox Distance Matrix API, run VRP solver on its output.
//
// The API returns a matrix with
// - the duration between location i and j
// - thus `0` on its diagonal i == j
// - and `null` if a route can not be found between i and j
//
// https://github.com/mapbox/mapbox-sdk-js/blob/master/API.md#getdistances
// https://www.mapbox.com/api-documentation/#directions-matrix
// https://www.mapbox.com/api-documentation/#directions
//
// Example output solution:
// http://bl.ocks.org/d/d0e91bc26f437aba812c554f7a5b1c2b
var util = require('util');
var Solver = require('../');
var Mapbox = require('mapbox');
// Here are all the tunables you might be interested in
var locations = [
[13.414649963378906, 52.522905940278065],
[13.363409042358397, 52.549218541178455],
[13.394737243652344, 52.55062769982075],
[13.426065444946289, 52.54640008814808],
[13.375682830810547, 52.536534077147714],
[13.39010238647461, 52.546191306649376],
[13.351736068725584, 52.50754964045259],
[13.418254852294922, 52.52927670688215],
];
var depotIndex = 0;
var numVehicles = 3;
var vehicleCapacity = 3;
var computeTimeLimit = 1000;
var profile = 'driving';
// that was a lie, there are more tunables below - start with the ones above first.
var MbxToken = process.env.MAPBOX_ACCESS_TOKEN;
if (!MbxToken) {
console.error('Please set your Mapbox API Token: export MAPBOX_ACCESS_TOKEN=YourToken');
process.exit(1);
}
var MbxClient = new Mapbox(MbxToken)
function hasNoRouteFound(matrix) {
matrix.some(function (inner) {
return inner.some(function (v) {
return v === null;
});
});
}
MbxClient.getDistances(locations, {profile: profile}, function(err, results) {
if (err) {
console.error('Error: ' + err.message);
process.exit(1);
}
if (hasNoRouteFound(results.durations)) {
console.error('Error: distance matrix is not complete');
process.exit(1);
}
// 9am -- 5pm
var dayStarts = 0;
var dayEnds = 8 * 60 * 60;
var costs = results.durations;
// Dummy durations, no service times included
var durations = results.durations;
// Dummy time windows for the full day
var timeWindows = new Array(results.durations.length);
for (var at = 0; at < results.durations.length; ++at)
timeWindows[at] = [[dayStarts, dayEnds]];
// Dummy demands of one except at the depot
var demands = new Array(results.durations.length);
for (var from = 0; from < results.durations.length; ++from) {
demands[from] = new Array(results.durations.length);
for (var to = 0; to < results.durations.length; ++to) {
demands[from][to] = (from === depotIndex) ? 0 : 1;
}
}
// No route locks per vehicle, let solver decide freely
var routeLocks = new Array(numVehicles);
routeLocks.fill([]);
var solverOpts = {
numNodes: results.durations.length,
costs: costs,
durations: durations,
timeWindows: timeWindows,
demands: demands
};
var VRP = new Solver.VRP(solverOpts);
var timeHorizon = dayEnds - dayStarts;
var searchOpts = {
computeTimeLimit: computeTimeLimit,
numVehicles: numVehicles,
depotNode: depotIndex,
timeHorizon: timeHorizon,
vehicleCapacity: vehicleCapacity,
routeLocks: routeLocks,
pickups: [],
deliveries: []
};
VRP.Solve(searchOpts, function (err, result) {
if (err) {
console.error('Error: ' + err.message);
process.exit(1);
}
console.log(util.inspect(result, {showHidden: false, depth: null}));
// Now that we have the location orders per vehicle make route requests to extract their geometry
for (var i = 0; i < result.routes.length; ++i) {
var route = result.routes[i];
// Unused vehicle
if (route.length === 0)
continue;
var waypoints = route.map(function(idx) {
return {'longitude': locations[idx][0], 'latitude': locations[idx][1]};
});
// Add depot explicitly as start and end
waypoints.unshift({'longitude': locations[depotIndex][0], 'latitude': locations[depotIndex][1]});
waypoints.push({'longitude': locations[depotIndex][0], 'latitude': locations[depotIndex][1]});
MbxClient.getDirections(waypoints, {profile: profile, alternatives: false}, function(err, results) {
if (err) {
console.error('Error: ' + err.message);
process.exit(1);
}
console.log(results.routes[0].geometry);
});
}
});
});