-
Notifications
You must be signed in to change notification settings - Fork 85
/
GraphHopperOptimization.js
247 lines (221 loc) · 9.22 KB
/
GraphHopperOptimization.js
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
let request = require('axios');
let GHUtil = require("./GHUtil");
let ghUtil = new GHUtil();
GraphHopperOptimization = function (args) {
this.key = args.key;
this.host = args.host ? args.host : "https://graphhopper.com/api/1";
this.endpoint = args.endpoint ? args.endpoint : '/vrp';
this.timeout = args.timeout ? args.timeout : 10000;
this.waitInMillis = args.waitInMillis ? args.waitInMillis : 1000;
this.postTimeout = args.postTimeout ? args.postTimeout : 10000;
};
GraphHopperOptimization.prototype.doVRPRequest = function (points, vehicles) {
let that = this;
let firstPoint = points[0];
let servicesArray = [];
for (let pointIndex in points) {
if (pointIndex < 1)
continue;
let point = points[pointIndex];
let obj = {
"id": "_" + pointIndex,
"type": "pickup",
"name": "maintenance " + pointIndex,
"address": {
"location_id": "_location_" + pointIndex,
"lon": point[0],
"lat": point[1]
}
};
servicesArray.push(obj);
}
let list = [];
for (let i = 0; i < vehicles; i++) {
list.push({
"vehicle_id": "_vehicle_" + i,
"start_address": {
"location_id": "_start_location",
"lon": firstPoint[0],
"lat": firstPoint[1]
},
"type_id": "_vtype_1"
});
}
let jsonInput = {
"algorithm": {
"problem_type": "min-max"
},
"vehicles": list,
"vehicle_types": [{
"type_id": "_vtype_1",
"profile": "car"
}],
"services": servicesArray
};
//console.log(jsonInput);
return that.doRequest(jsonInput);
};
GraphHopperOptimization.prototype.doRawRequest = function (jsonInput) {
let that = this;
return new Promise(function (resolve, reject) {
let postURL = that.host + that.endpoint + "/optimize?key=" + that.key;
request.post(postURL, jsonInput, {
timeout: that.postTimeout,
headers: {'Content-Type': 'application/json'}
})
.then(res => {
if (res.status !== 200) {
reject(ghUtil.extractError(res, postURL));
} else if (res) {
let solutionUrl = that.host + that.endpoint + "/solution/" + res.data.job_id + "?key=" + that.key;
let timerRet;
let pollTrigger = function () {
// console.log("poll solution " + solutionUrl);
request.get(solutionUrl, {timeout: that.timeout})
.then(res => {
if (res.status !== 200 || res.data === undefined) {
clearInterval(timerRet);
reject(ghUtil.extractError(res, solutionUrl));
} else if (res) {
//console.log(res.body);
if (res.data.status === "finished") {
//console.log("finished");
clearInterval(timerRet);
resolve(res.data);
} else if (res.data.message) {
clearInterval(timerRet);
resolve(res.data);
}
}
});
};
if (that.waitInMillis > 0)
timerRet = setInterval(pollTrigger, that.waitInMillis);
else
pollTrigger();
}
})
.catch(err => {
reject(ghUtil.extractError(err.response, postURL));
});
});
};
GraphHopperOptimization.prototype.doRequest = function (jsonInput) {
let vehicleTypeProfileMap = {};
let vehicleTypeMap = {};
let vehicleProfileMap = {};
let serviceMap = {};
let shipmentMap = {};
let locationMap = {};
let hasGeometries = false;
let hasCustomCostMatrix = false;
if (jsonInput.cost_matrices && jsonInput.cost_matrices.length > 0)
hasCustomCostMatrix = true;
if (jsonInput.configuration && !hasCustomCostMatrix)
if (jsonInput.configuration.routing.calc_points === true)
hasGeometries = true;
if (!hasGeometries) {
if (!jsonInput.configuration && !hasCustomCostMatrix) {
jsonInput.configuration = {"routing": {"calc_points": true}};
}
}
if (jsonInput.vehicle_types) {
for (let typeIndex = 0; typeIndex < jsonInput.vehicle_types.length; typeIndex++) {
let type = jsonInput.vehicle_types[typeIndex];
vehicleTypeProfileMap[type.type_id] = type.profile;
vehicleTypeMap[type.type_id] = type;
}
}
if (jsonInput.services) {
for (let serviceIndex = 0; serviceIndex < jsonInput.services.length; serviceIndex++) {
let service = jsonInput.services[serviceIndex];
locationMap[service.address.location_id] = service.address;
serviceMap[service.id] = service;
}
}
if (jsonInput.shipments) {
for (let shipmentIndex = 0; shipmentIndex < jsonInput.shipments.length; shipmentIndex++) {
let shipment = jsonInput.shipments[shipmentIndex];
locationMap[shipment.pickup.address.location_id] = shipment.pickup.address;
locationMap[shipment.delivery.address.location_id] = shipment.delivery.address;
shipmentMap[shipment.id] = shipment;
}
}
let breakMap = {};
let vehicleMap = {};
if (jsonInput.vehicles) {
for (let vehicleIndex = 0; vehicleIndex < jsonInput.vehicles.length; vehicleIndex++) {
let vehicle = jsonInput.vehicles[vehicleIndex];
vehicleMap[vehicle.vehicle_id] = vehicle;
let profile = null;
if (vehicle.type_id !== null) {
profile = vehicleTypeProfileMap[vehicle.type_id];
if (profile !== null) {
vehicleProfileMap[vehicle.vehicle_id] = profile;
} else {
vehicleProfileMap[vehicle.vehicle_id] = "car";
}
} else
vehicleProfileMap[vehicle.vehicle_id] = "car";
if (vehicle.start_address) {
locationMap[vehicle.start_address.location_id] = vehicle.start_address;
}
if (vehicle.end_address) {
locationMap[vehicle.end_address.location_id] = vehicle.end_address;
}
if (vehicle.break) {
let break_id = vehicle.vehicle_id + "_break";
breakMap[break_id] = vehicle.break;
}
}
}
let promise = this.doRawRequest(jsonInput);
promise.then(function (json) {
if (json.solution) {
let sol = json.solution;
json.raw_solution = JSON.parse(JSON.stringify(sol));
sol["calc_points"] = hasGeometries;
for (let routeIndex = 0; routeIndex < sol.routes.length; routeIndex++) {
let route = sol.routes[routeIndex];
let vehicleId = route.vehicle_id;
let profile = vehicleProfileMap[vehicleId];
route["profile"] = profile;
for (let actIndex = 0; actIndex < route.activities.length; actIndex++) {
let act = route.activities[actIndex];
act["address"] = locationMap[act.location_id];
if (act.id) {
let driverBreak = breakMap[act.id];
// console.log(act.id + " " + driverBreak);
if (driverBreak) {
act["break"] = breakMap[act.id];
} else if (serviceMap[act.id]) {
act["service"] = serviceMap[act.id];
} else if (shipmentMap[act.id]) {
act["shipment"] = shipmentMap[act.id];
}
} else {
let vehicle = vehicleMap[vehicleId];
act["vehicle"] = vehicle;
act["vehicle_type"] = vehicleTypeMap[vehicle.type_id];
}
}
}
let unassignedServices = new Array();
for (let i = 0; i < sol.unassigned.services.length; i++) {
let serviceId = sol.unassigned.services[i];
unassignedServices.push(serviceMap[serviceId]);
unassignedServices.push(serviceMap[serviceId]);
}
sol["unassigned_services"] = unassignedServices;
let unassignedShipments = new Array();
for (let i = 0; i < sol.unassigned.shipments.length; i++) {
let shipmentId = sol.unassigned.shipments[i];
unassignedShipments.push(shipmentMap[shipmentId]);
}
sol["unassigned_shipments"] = unassignedShipments;
}
return json;
});
return promise;
};
module.exports = GraphHopperOptimization;