-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (129 loc) · 3.88 KB
/
index.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
/* eslint-disable no-fallthrough */
const { randomNumber } = require('./util');
class Genetic {
constructor(options) {
this.options = {
...Genetic.defaultOptions,
...options,
};
this.state = Genetic.state.initial;
this.population = [];
this.populationHistory = [];
this.children = [];
}
static get defaultOptions() {
return {
populationSize: 10,
crossoverRate: 0.5,
mutationRate: 0.5,
populationFunction: () => [1],
crossoverFunction: (a, b) => [a, b],
mutationFunction: a => a,
fitnessFunction: () => 1,
selectionFunction: (a, b) => (a.fitness > b.fitness ? a : b),
};
}
static get state() {
return {
initial: 'INITIALIZED',
populate: 'POPULATED',
crossover: 'CROSSED OVER',
mutate: 'MUTATED',
fit: 'FITTED',
select: 'SELECTED',
evolve: 'EVOLVED',
};
}
populate() {
const { populationFunction, populationSize, fitnessFunction } = this.options;
while (this.population.length < populationSize) {
this.population.push(populationFunction());
}
this.populationHistory.push(this.population.map(individual => ({
fitness: fitnessFunction(individual),
individual,
})));
this.state = Genetic.state.populate;
return this;
}
crossover() {
const { crossoverFunction, crossoverRate, populationSize } = this.options;
const rate = crossoverRate * populationSize;
const children = [];
while (children.length < rate) {
const mother = this.population[randomNumber(populationSize - 1)];
const father = this.population[randomNumber(populationSize - 1)];
children.push(...crossoverFunction(mother, father));
}
this.children.push(...children.slice(0, rate));
this.state = Genetic.state.crossover;
return this;
}
mutate() {
const { mutationFunction, mutationRate, populationSize } = this.options;
const rate = mutationRate * populationSize;
const children = [];
while (children.length < rate) {
const parent = this.population[randomNumber(populationSize - 1)];
children.push(mutationFunction(parent));
}
this.children.push(...children);
this.state = Genetic.state.mutate;
return this;
}
fit() {
const { fitnessFunction } = this.options;
this.populationWithFitness = this.population.map(individual => ({
fitness: fitnessFunction(individual),
individual,
}));
this.childrenWithFitness = this.children.map(individual => ({
fitness: fitnessFunction(individual),
individual,
}));
this.state = Genetic.state.fitness;
return this;
}
select() {
const { populationSize, selectionFunction } = this.options;
const population = [...this.populationWithFitness, ...this.childrenWithFitness];
const selected = [];
while (selected.length < populationSize) {
const home = population[randomNumber(population.length - 1)];
const away = population[randomNumber(population.length - 1)];
selected.push(selectionFunction(home, away));
}
this.populationHistory.push(selected);
this.population = selected.map(i => i.individual);
this.state = Genetic.state.select;
return this;
}
evolve() {
switch (this.state) {
case Genetic.state.initial:
this.populate();
case Genetic.state.evolve:
case Genetic.state.populate:
this.crossover();
case Genetic.state.crossover:
this.mutate();
case Genetic.state.mutate:
this.fit();
case Genetic.state.fit:
this.select();
default:
break;
}
this.state = Genetic.state.evolve;
return this;
}
get best() {
if (!this.populationHistory.length) {
return 0;
}
return this.populationHistory
.slice(-1)[0]
.reduce((best, i) => (best.fitness > i.fitness ? best : i));
}
}
module.exports = Genetic;