-
Notifications
You must be signed in to change notification settings - Fork 0
/
manualGA.js
168 lines (158 loc) · 4.15 KB
/
manualGA.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
//Scientific Research
//Tangguh Destio Pramono
//15117880
//number of cities
var num = 0;
//Max number of cities. try 15
var maxNum = 10;
//Order
var order = [];
var BFOrder = [];
//Track cities location
var cities = [];
var BFCities = [];
//Population
var population = [];
//Fiteness
var fitness = [];
//MaxPop. try 1000
var popSize = 200;
var currentBest = [];
var buttonStart;
// var statistik
var statsCurr = [];
var statsBest = [];
var BFStatsCurr = [];
var BFStatsBest = [];
var bestEverText = [];
var currentBestText = [];
//recordedDistance
var recordDistance = Infinity;
var BFBestRecordDist = Infinity;
var bestEver = [];
var BFBestEver = [];
function setup() {
var canvasDiv = document.getElementById('canvas');
var widthCanvas = canvasDiv.offsetWidth;
//Create canvas
var c = createCanvas(widthCanvas, 500);
c.mouseClicked(addCityPos);
c.parent('canvas');
frameRate(30)
//Initiate no loop
noLoop();
}
function draw() {
background('#2F2E41');
//StartBF
startBF();
// initializeBF();
//Implementation GA
calculateFitness();
normalizeFitness();
nextGeneration();
// stroke('blue');
// strokeWeight(2);
noFill();
beginShape();
for (let i = 0; i < bestEver.length; i++) {
var loc = bestEver[i];
stroke(255);
strokeWeight(1);
text(bestEver[i] + 1, cities[loc].x, cities[loc].y + 130);
stroke('blue');
strokeWeight(2);
vertex(cities[loc].x, cities[loc].y + 150);
ellipse(cities[loc].x, cities[loc].y + 150, 15);
}
endShape();
// translate(0,height/2);
stroke('red');
strokeWeight(2);
noFill();
beginShape();
for (let i = 0; i < currentBest.length; i++) {
var loc = currentBest[i];
vertex(cities[loc].x, cities[loc].y);
ellipse(cities[loc].x, cities[loc].y, 15);
}
endShape();
// console.log('order ' + order);
// for displaying route the way user understand
for (let i = 0; i < bestEver.length; i++) {
bestEverText[i] = bestEver[i] + 1;
currentBestText[i] = currentBest[i] + 1;
}
// console.log('best ever ' + bestEver);
document.getElementById('bestRoute').textContent = bestEverText;
// console.log(recordDistance);
document.getElementById('bestDist').textContent = recordDistance.toFixed(4);
// console.log('current best ' + currentBest);
document.getElementById('currRoute').textContent = currentBestText;
statsBest.push(recordDistance);
}
function addCityPos() {
//If city reach maxNum don't draw any more city
if (num >= maxNum) {
return;
} else {
//Create location of city
var v = createVector(mouseX, mouseY);
cities[num] = v;
BFCities[num] = v;
//Create order of city
order[num] = num;
BFOrder[num] = num;
//Continue to next city
num++;
}
//Draw City
noStroke();
if (num === 1) {
text('t', mouseX, mouseY - 20);
fill(255);
}
text(num, mouseX, mouseY - 20);
fill(255);
ellipse(mouseX, mouseY, 15);
//Draw line between
if (num >= 2) {
stroke(255);
strokeWeight(1);
line(cities[num - 2].x, cities[num - 2].y, mouseX, mouseY);
}
// console.log(cities[num-1].x);
// console.log(order);
}
function startGA() {
// text("U click GA", 200,200);
//create population
for (let i = 0; i < popSize; i++) {
population[i] = order.slice();
acak(population[i]);
}
//BF
initializeBF(cities, order);
//Start drawing and looping
loop();
}
function acak(urut) {
result = shuffle(urut.slice(1, maxNum - 1));
urut.splice(1, maxNum - 2);
for (let i = 0; i < result.length; i++) {
urut.splice(i + 1, 0, result[i]);
}
// console.log('order' + order);
}
function calcDistance(points, order) {
var sum = 0;
for (let i = 0; i < order.length - 1; i++) {
var cityAIndex = order[i];
var cityA = points[cityAIndex];
var cityBIndex = order[i + 1];
var cityB = points[cityBIndex];
var d = dist(cityA.x, cityA.y, cityB.x, cityB.y);
sum += d;
}
return sum;
}