-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (59 loc) · 2.1 KB
/
index.js
File metadata and controls
66 lines (59 loc) · 2.1 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
const fs = require( "fs" );
const vrptojson = require( "vrptojson" );
function parse( instance ) {
if ( !instance.coords || !instance.demand || !instance.depots || !instance.EDGE_WEIGHT_TYPE || !instance.CAPACITY ) throw "Illegal instance argument";
if ( instance.EDGE_WEIGHT_TYPE != 'EUC_2D' ) throw "Parser does not support this Edge Weight Type";
const distances = {}; // distanceMatrix
Object.keys( instance.coords ).forEach( c => distances[ c ] = {} );
Object.keys( instance.coords ).forEach( ai => {
let a = instance.coords[ ai ];
Object.keys( instance.coords ).forEach( bi => {
let b = instance.coords[ bi ];
distances[ ai ][ bi ] = dist( a, b );
} );
} );
const returnedInstance = {};
if ( instance.BEST_KNOWN ) returnedInstance.best = instance.BEST_KNOWN;
returnedInstance.n = instance.DIMENSION;
returnedInstance.distances = distances;
returnedInstance.demand = instance.demand;
returnedInstance.c = instance.CAPACITY;
returnedInstance.depot = instance.depots[ 0 ];
returnedInstance.coords = instance.coords;
return returnedInstance;
}
function dist( a, b ) {
return Math.sqrt( Math.pow( a.x - b.x, 2 ) + Math.pow( a.y - b.y, 2 ) );
}
const hooks = {
COMMENT: function ( value ) {
const words = value.split( " " );
const idx = words.findIndex( x => x == "value:" );
let best = words[ idx + 1 ];
best = best.substring( 0, best.length - 1 );
return {
BEST_KNOWN: Number( best )
}
}
}
function get( name ) {
const dir = __dirname + "/instances/";
const files = fs.readdirSync( dir );
if ( files.indexOf( name + ".vrp" ) >= 0 ) {
const file = fs.readFileSync( dir + name + '.vrp' );
const json = vrptojson( file, hooks );
return parse( json );
} else {
throw "Instance not in library"
}
}
function listInstances() {
const dir = __dirname + "/instances/";
const files = fs.readdirSync( dir );
return files.map( x => x.split( "." )[ 0 ] );
}
module.exports = {
parse,
get,
listInstances
}