forked from praeducer/genetic-experience-management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
65 lines (62 loc) · 2.17 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { Population } from "./src/Population";
import { Individual } from "./src/Individual";
import { arity, choose } from "./src/PhenotypeHelpers";
let target = "It works!";
let population = new Population({
proto: new Individual({
phenotype: {
mutate: [
{
name: "substitution",
selection: {
// We could keep this mutation rate from acting on itself.
//selection: "value",
// Let the individuals choose their own mutation rate on initialization.
//rate: g => g
// We're making it a little easier to hit lower numbers.
//rate: g => Math.tan(Math.PI / 4 * g)
rate: g => (g * 0.06) + 0.02
}
}
],
// The arity function returns a function of a given length.
// That length is used to populate the genome.
string: arity(target.length, function() {
// Encode each gene into a valid character code.
var codes = Array.from(arguments).map(arg => {
return 32 + Math.floor(arg * (126 - 32));
});
// Convert the character codes into a string.
var string = String.fromCharCode(...codes);
return string;
})
}
}),
individuals: 25
});
Population.Fitness["fitness"] = individual => {
// Give the individual an error point every time they don't match the target string.
return Array.from(target).reduce(
(n, v, i) => (v === individual.traits.string[i] ? n : n + 1),
0
);
};
for (let i = 0; i < 500; i++) {
// Sort the individuals by size.
let sorted = population.selection({
// Shuffle the selection to vary the best individual per generation when scores.
shuffle: true,
sort: { value: "fitness", comparison: "ascending" }
});
let mostFit = sorted.slice(0, 4);
// View the population's transformation over time.
console.log(
sorted
.map(i => Population.Fitness.fitness(population.individuals[i]))
.join(""), population.individuals[mostFit[0]].traits.string
);
// Crossover all the individuals with the most fit.
population.operation("crossoverAndReplace", [true, mostFit]);
// Mutate all the individuals.
population.mutate(true);
}