-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketchAuto.js
More file actions
131 lines (101 loc) · 2.82 KB
/
Copy pathsketchAuto.js
File metadata and controls
131 lines (101 loc) · 2.82 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
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
// Daniel Shiffman
// Nature of Code: Intelligence and Learning
// https://github.com/shiffman/NOC-S17-2-Intelligence-Learning
// Evolve Traveling Salesperson
// Cities
var cities = [];
var totalCities = 10;
// Best path overall
var recordDistance = Infinity;
var bestEver;
// Population of possible orders
var population = [];
var popTotal = 200;
function setup() {
var gambar = document.getElementById('vi-auto-ga');
createCanvas(gambar.offsetWidth, gambar.offsetHeight).parent('vi-auto-ga');
frameRate(15)
// Make random cities
for (var i = 0; i < totalCities; i++) {
var v = createVector(random(10, width - 10), random(10, height / 2 - 10));
cities[i] = v;
}
// Create population
for (var i = 0; i < popTotal; i++) {
population[i] = new DNA(totalCities);
}
}
function draw() {
background('#2F2E41');
// Each round let's find the best and worst
var minDist = Infinity;
var maxDist = 0;
// Search for the best this round and overall
var bestNow;
for (var i = 0; i < population.length; i++) {
var d = population[i].calcDistance();
// Is this the best ever?
if (d < recordDistance) {
recordDistance = d;
bestEver = population[i];
}
// Is this the best this round?
if (d < minDist) {
minDist = d;
bestNow = population[i];
}
// Is this the worst?
if (d > maxDist) {
maxDist = d;
}
}
// Show the best this round
bestNow.show();
translate(0, height / 2);
line(0, 0, width, 0);
// Show the best ever!
bestEver.show();
// console.log(bestEver.dist);
// Map all the fitness values between 0 and 1
var sum = 0;
for (var i = 0; i < population.length; i++) {
sum += population[i].mapFitness(minDist, maxDist);
}
// Normalize them to a probability between 0 and 1
for (var i = 0; i < population.length; i++) {
population[i].normalizeFitness(sum);
}
// Selection
// A new population
var newPop = [];
// Sam population size
for (var i = 0; i < population.length; i++) {
// Pick two
var a = pickOne(population);
var b = pickOne(population);
// Crossover!
var order = a.crossover(b);
newPop[i] = new DNA(totalCities, order);
}
// New population!
population = newPop;
}
// This is a new algorithm to select based on fitness probability!
// It only works if all the fitness values are normalized and add up to 1
function pickOne() {
// Start at 0
var index = 0;
// Pick a random number between 0 and 1
var r = random(1);
// Keep subtracting probabilities until you get less than zero
// Higher probabilities will be more likely to be fixed since they will
// subtract a larger number towards zero
while (r > 0) {
r -= population[index].fitness;
// And move on to the next
index += 1;
}
// Go back one
index -= 1;
return population[index];
}