Repository navigation
Expand file tree
/
Copy pathevolve.js
More file actions
128 lines (128 loc) · 4.19 KB
/
Copy pathevolve.js
File metadata and controls
128 lines (128 loc) · 4.19 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
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var Individual = /** @class */ (function () {
function Individual() {
this.token = ['+', '-', ''];
this.targetSumValue = 100;
this.numberOfNumbers = 10;
this.sum = 0;
this.fitness = 0;
// TODO: generate the numbers instead
this.numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
this.randomizeSigns();
}
Individual.prototype.print = function () {
console.log("sum: " + this.sum + " fitness: " + this.fitness + " gene: " + this.combine());
};
Individual.prototype.randomSign = function () {
return this.token[randomInt(0, 2)];
};
Individual.prototype.randomizeSigns = function () {
this.chromosome = [];
for (var i_1 = 0; i_1 < 9; i_1++) {
this.chromosome[i_1] = this.randomSign();
}
};
Individual.prototype.mutate = function () {
// console.log(this.chromosome, "before")
var location = randomInt(0, this.numberOfNumbers - 2);
this.chromosome[location] = this.randomSign();
// console.log(this.chromosome, "after")
};
Individual.prototype.combine = function () {
var _this = this;
return this.numbers.map(function (e, i) {
return e + _this.chromosome.concat([""])[i];
}).reduce(function (total, e) {
return total + e;
});
};
Individual.prototype.evaluate = function () {
this.sum = eval(this.combine());
// this.fitness = result
var f;
var difference = this.sum - this.targetSumValue;
if (difference < 0) {
f = Math.log(-difference);
}
else {
f = Math.log(difference);
}
this.fitness = f;
};
return Individual;
}());
var i = new Individual();
// population
var Population = /** @class */ (function () {
function Population(size) {
this.size = size;
this.individual = [];
this.mutationRate = 0.1;
this.createNewPopulation();
}
Population.prototype.evaluateIndividuals = function () {
};
Population.prototype.createNewPopulation = function () {
for (var i_2 = 0; i_2 < this.size; i_2++) {
this.individual.push(new Individual());
}
};
Population.prototype.printPopulation = function () {
for (var _i = 0, _a = this.individual; _i < _a.length; _i++) {
var individual = _a[_i];
individual.print();
}
};
Population.prototype.evolve = function () {
// evaluate all individuals of the current generation
for (var _i = 0, _a = this.individual; _i < _a.length; _i++) {
var individual = _a[_i];
individual.evaluate();
}
this.individual.sort(function (a, b) {
if (a.fitness > b.fitness) {
return 1;
}
if (a.fitness < b.fitness) {
return -1;
}
return 0;
});
// this.printPopulation()
// TODO:sort individuals according to fitness
// this.printPopulation()
// select the top 10 solutions
// make 10 copies so that we maintain the population size
var nextGeneration = [];
for (var i_3 = 0; i_3 < 10; i_3++) {
for (var clone = 0; clone < 10; clone++) {
var newIndividual = Object.create(this.individual[i_3]);
// // Mutate
// if (Math.random() <= this.mutationRate) {
// newIndividual.mutate();
// }
// Mutate all clones
if (i_3 > 0) {
newIndividual.mutate();
}
nextGeneration.push(newIndividual);
}
}
this.individual = nextGeneration;
};
return Population;
}());
// generation[]
var population = new Population(100);
// for (let epoch=0; epoch < 10; epoch++) {
// population.evolve()
// }
population.printPopulation();
population.evolve();
population.printPopulation();
// document.body.innerHTML = "testing";
setTimeout(function () {
// document.body.innerHTML = "testing";
}, 0);