-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (72 loc) · 2.52 KB
/
index.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
//use strict;
var math = require('mathjs');
var express = require('express');
var util= require('./getclosest.js');
var mdcvrp = require('./mdcvrp.js');
var express = require('express')
var app = express()
var concat = require('concat-stream');
app.use(function(req, res, next) {
req.pipe(concat(function(data) {
req.body = data;
next();
}));
});
app.get('/', function(req, res) {
res.send("let's rock!")
})
app.post('/mdcvrp',mdcvrp);
app.post('/cvrp', function(req, res) {
var raw = req.body.toString();
var data = JSON.parse(raw);
var cordinator = {};
var tracks = [];
var main = data.problem_data
var targets = main.customer_locations;
// while still have target
// while enough capacity
//step 1: set depot is current
//step 2: find closest fit capacity target
//step 3: +capacity
//step 4: check capacity if > than vehicle then return
//step 5: set current = target; pop target; repeat
var occupied={};
while (targets.length > Object.keys(occupied).length) {
var current_route = []; // duong di hien tai
console.log(current_route);
var current = main.depot;
var current_capacity = 0;
while (current_capacity < data.problem_data.vehicle_capacity && targets.length > Object.keys(occupied).length) {
// console.log('find close',occupied,current_capacity)
var j = util.getClosest(current, targets ,occupied);
if (j!==undefined) {
newcapa = main.customer_demands[j] + current_capacity;
// if qua tai
//console.log('max',main.vehicle_capacity)
if (newcapa > main.vehicle_capacity) {
// finish the route,
// return to depot
break;
} else {
//enough capacity, continue to go
current_route.push(parseInt(j));
//console.log('new cap',newcapa);
current_capacity=newcapa
current=targets[j]
occupied[j]=true;
if (targets.length == Object.keys(occupied).length) {
break;
}
}
}
}
tracks.push(current_route);
console.log(current_route,current_capacity, 'end',Object.keys(occupied).length);
}
res.send({
"routes": tracks
});
})
app.listen(3001, function() {
console.log('Example app listening on port 3001!')
})