-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsp_mibs.js
274 lines (247 loc) · 7.05 KB
/
tsp_mibs.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
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
require('./util.js');
var json2csv = require('json2csv');
const fs = require('fs');
var points = require('./points.json');
var xMax = 5120;
var yMax = 4096;
var xCent = 1323;
var yCent = 1624;
var POPULATION_SIZE = 30;
var ELITE_RATE = 0.3;
var CROSSOVER_PROBABILITY = 0.9;
var MUTATION_PROBABILITY = 0.01;
var UNCHANGED_GENS = 0;
var mutationTimes = 0;
var bestValue = undefined;
var best = [];
var currentGeneration = 0;
var currentBest;
var population = [];
var values = new Array(POPULATION_SIZE);
var fitnessValues = new Array(POPULATION_SIZE);
var roulette = new Array(POPULATION_SIZE);
var convpoints = [];
points.forEach(convertToCoords);
var distance = countDistances();
for (var i=0; i<POPULATION_SIZE; i++) {
population.push(randomInd(convpoints.length));
}
setBestValue();
while (UNCHANGED_GENS < 100000) {
GANextGeneration();
}
var res = printResults();
saveResults(res);
function printResults() {
var result = [];
result.push(convpoints[0]);
for (var i = 1; i < convpoints.length; i++) {
result.push(convpoints[best[i]]);
}
console.log("This order of points yields the best path after " + currentGeneration + " generations");
console.log(result);
return result;
}
function saveResults(res) {
fs.writeFileSync('./result.json', JSON.stringify(res));
var fields = ['xCoord', 'yCoord'];
json2csv({ data: res, fields: fields }, function(err, csv) {
if (err) console.log(err);
fs.writeFileSync('./result.csv', csv);
});
var mapfile = "3\n";
for (var i = 0; i < res.length; i++) {
mapfile += "+treasure: " + res[i].xCoord + " " + res[i].yCoord + " 0 [MIB]\n"
}
fs.writeFileSync('./Mibs.MAP', mapfile);
}
function convertToCoords(point, index, arr) {
var result = {};
var longDeg = Number(point.longDeg) + (Number(point.longMin) / 60);
var latDeg = Number(point.latDeg) + (Number(point.latMin) / 60);
// Normalize to SE Quad
if (point.longQuad === 'W') {
longDeg = 180 + (180 - longDeg);
}
if (point.latQuad === 'N') {
latDeg = 180 + (180 - latDeg);
}
var xCoord = longDeg * xMax / 360 + xCent;
xCoord %= xMax;
var yCoord = latDeg * yMax / 360 + yCent;
yCoord %= yMax;
result.xCoord = Math.round(xCoord);
result.yCoord = Math.round(yCoord);
convpoints.push(result);
}
function distanceFormula(point1, point2) {
var xDist = Math.abs(point2.xCoord - point1.xCoord);
xDist = Math.min(xDist, xMax - xDist);
var yDist = Math.abs(point2.yCoord - point1.yCoord);
yDist = Math.min(yDist, yMax - yDist);
return Math.sqrt(Math.pow(xDist, 2) + Math.pow(yDist, 2));
}
function countDistances() {
var length = convpoints.length;
var dist = new Array(length);
for (var i = 0; i < length; i++) {
dist[i] = new Array(length);
for (var j = 0; j < length; j++) {
dist[i][j] = ~~distanceFormula(convpoints[i], convpoints[j]);
}
}
return dist;
}
function setBestValue() {
for(var i=0; i<population.length; i++) {
values[i] = evaluate(population[i]);
}
currentBest = getCurrentBest();
if(bestValue === undefined || bestValue > currentBest.bestValue) {
best = population[currentBest.bestPosition].clone();
bestValue = currentBest.bestValue;
UNCHANGED_GENS = 0;
} else {
UNCHANGED_GENS += 1;
}
}
function getCurrentBest() {
var bestP = 0,
currentBestValue = values[0];
for(var i=1; i<population.length; i++) {
if(values[i] < currentBestValue) {
currentBestValue = values[i];
bestP = i;
}
}
return {
bestPosition : bestP
, bestValue : currentBestValue
}
}
function randomInd(n) {
var a = [];
for(var i=0; i<n; i++) {
a.push(i);
}
return a.shuffle();
}
function GANextGeneration() {
currentGeneration++;
selection();
crossover();
mutation();
setBestValue();
}
function selection() {
var parents = new Array();
var initnum = 4;
parents.push(population[currentBest.bestPosition]);
parents.push(doMutate(best.clone()));
parents.push(pushMutate(best.clone()));
parents.push(best.clone());
setRoulette();
for(var i=initnum; i<POPULATION_SIZE; i++) {
parents.push(population[wheelOut(Math.random())]);
}
population = parents;
}
function crossover() {
var queue = new Array();
for(var i=0; i<POPULATION_SIZE; i++) {
if( Math.random() < CROSSOVER_PROBABILITY ) {
queue.push(i);
}
}
queue.shuffle();
for(var i=0, j=queue.length-1; i<j; i+=2) {
doCrossover(queue[i], queue[i+1]);
}
}
function mutation() {
for(var i=0; i<POPULATION_SIZE; i++) {
if(Math.random() < MUTATION_PROBABILITY) {
if(Math.random() > 0.5) {
population[i] = pushMutate(population[i]);
} else {
population[i] = doMutate(population[i]);
}
i--;
}
}
}
function doMutate(seq) {
mutationTimes++;
// m and n refers to the actual index in the array
// m range from 0 to length-2, n range from 2...length-m
do {
m = randomNumber(seq.length - 2);
n = randomNumber(seq.length);
} while (m>=n)
for(var i=0, j=(n-m+1)>>1; i<j; i++) {
seq.swap(m+i, n-i);
}
return seq;
}
function pushMutate(seq) {
mutationTimes++;
var m,n;
do {
m = randomNumber(seq.length>>1);
n = randomNumber(seq.length);
} while (m>=n)
var s1 = seq.slice(0,m);
var s2 = seq.slice(m,n)
var s3 = seq.slice(n,seq.length);
return s2.concat(s1).concat(s3).clone();
}
function setRoulette() {
//calculate all the fitness
for(var i=0; i<values.length; i++) { fitnessValues[i] = 1.0/values[i]; }
//set the roulette
var sum = 0;
for(var i=0; i<fitnessValues.length; i++) { sum += fitnessValues[i]; }
for(var i=0; i<roulette.length; i++) { roulette[i] = fitnessValues[i]/sum; }
for(var i=1; i<roulette.length; i++) { roulette[i] += roulette[i-1]; }
}
function wheelOut(rand) {
var i;
for(i=0; i<roulette.length; i++) {
if( rand <= roulette[i] ) {
return i;
}
}
}
function doCrossover(x, y) {
child1 = getChild('next', x, y);
child2 = getChild('previous', x, y);
population[x] = child1;
population[y] = child2;
}
function getChild(fun, x, y) {
solution = new Array();
var px = population[x].clone();
var py = population[y].clone();
var dx,dy;
var c = px[randomNumber(px.length)];
solution.push(c);
while(px.length > 1) {
dx = px[fun](px.indexOf(c));
dy = py[fun](py.indexOf(c));
px.deleteByValue(c);
py.deleteByValue(c);
c = distance[c][dx] < distance[c][dy] ? dx : dy;
solution.push(c);
}
return solution;
}
function evaluate(ind) {
var sum = distance[ind[0]][ind[ind.length - 1]];
for(var i=1; i<ind.length; i++) {
sum += distance[ind[i]][ind[i-1]];
}
return sum;
}
function randomNumber(boundary) {
return parseInt(Math.random() * boundary);
}