-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
151 lines (128 loc) · 3.47 KB
/
main.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
const maxApi = require("max-api");
//----ALGORITHM INPUTS
const inputs = {
target: [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
populationSize: 51,
mutationChance: 0.2,
breedingPopulationSize: 2,
iterationsPerCycle: 1,
seqLength: 16
};
//-----GENETIC ALGORITHM FUNCTIONS
const toScore = (steps, target) =>
target.reduce((acc, v, i) => (v !== steps[i] ? ++acc : acc), 0);
const toss = _ => Math.round(Math.random());
const range = to => [...Array(to).keys()];
const cross = ([g1, g2]) => g1.map((v, i) => (toss() ? v : g2[i]));
const negate = v => (v === 1 ? 0 : 1);
const mutateEvery = (g, mutationChance) =>
g.map(v => (Math.random() <= mutationChance ? negate(v) : v));
const mutateOne = (g, mutationChance) => {
if (Math.random() <= mutationChance) {
const index = randInt(g.length);
g[index] = negate(g[index]);
}
return g;
};
const mutate = mutateEvery;
const randInt = max => Math.floor(Math.random() * max);
const removeIndex = (arr, index) => [
...arr.slice(0, index),
...arr.slice(index + 1)
];
const randomPairFrom = arrs => {
const i1 = randInt(arrs.length);
const i2 = randInt(arrs.length - 1);
return [arrs[i1], removeIndex(arrs, i1)[i2]];
};
const selectClosest = ({ population, target, breedingPopulationSize }) => {
population.sort((a, b) => toScore(a, target) - toScore(b, target));
return population.slice(0, breedingPopulationSize);
};
const iterate = ({
population,
populationSize,
mutationChance,
breedingPopulationSize,
target
}) => {
const parents = selectClosest({
population,
target,
breedingPopulationSize
});
return range(populationSize).map(_ =>
mutate(cross(randomPairFrom(parents)), mutationChance)
);
};
const initPopulation = ({ target, populationSize }) =>
range(populationSize).map(_ => range(target.length).map(toss));
let population = initPopulation(inputs);
//---MAX INPUT HANDLERS
[
{ name: "mutationChance" },
{ name: "populationSize", type: "int" },
{ name: "breedingPopulationSize", type: "int" },
{ name: "iterationsPerCycle", type: "int" }
].forEach(({ name, scale, min, type }) =>
maxApi.addHandler(name, val => {
inputs[name] = type == "int" ? Math.floor(val) : val;
})
);
maxApi.addHandler(
"targetChange",
(index, val) => (inputs.target[index - 1] = val)
);
maxApi.addHandler("targetInit", (command, ...steps) => {
if (command == "extra1") {
inputs.target = steps;
}
});
maxApi.addHandler("seqLength", seqLength => {
const increased = seqLength > inputs.seqLength;
population.forEach(p => {
p.length = seqLength;
if (increased) {
for (let i = inputs.seqLength; i < seqLength; i++) {
p[i] = toss();
}
}
});
inputs.seqLength = seqLength;
});
maxApi.addHandler("randomizePopulation", _ => {
population = initPopulation(inputs);
const { target, breedingPopulationSize } = inputs;
const closest = selectClosest({
population,
target,
breedingPopulationSize
});
maxApi.outlet(closest[0]);
});
maxApi.addHandler("generate", () => {
const {
mutationChance,
populationSize,
breedingPopulationSize,
target,
iterationsPerCycle
} = inputs;
population = range(iterationsPerCycle).reduce(
population =>
iterate({
population,
populationSize,
mutationChance,
breedingPopulationSize,
target
}),
population
);
const closest = selectClosest({
population,
target,
breedingPopulationSize
});
maxApi.outlet(closest[0]);
});