-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
42 lines (37 loc) · 1.16 KB
/
example.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
import { mutate } from './mutate';
import { example as generate } from './generate';
import { crossover } from './crossover';
import { createPopulation } from './population';
import { createMathFitness } from './fitness';
import { createParser } from './parser';
import { evaluate } from './parser';
import { select2 } from './selection';
import { toString } from './utils';
import { createEvolver } from './evolve';
export const numericSolver = definition => {
const fitness = createMathFitness({
definition,
minDataset: -10,
maxDataset: 10,
evaluate
});
const population = createPopulation({
depth: 5,
size: 200,
fitness,
generate
});
const [bestProgram, bestFitness] = createEvolver({
mutate: mutate(generate),
crossover: crossover(20),
generate: generate,
population,
stopWhen: (counter, bestFitness) => counter === 2000 || bestFitness === 0,
onIteration: (counter, fitness) =>
console.log(`Iteration ${counter} : ${fitness}`),
select: select2(50, 20, fitness)
})();
return [bestProgram, bestFitness];
};
const [bestProgram] = numericSolver(x => 3 * x * x + 2 * x + 2);
console.log(bestProgram);