-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossover.js
51 lines (44 loc) · 1.31 KB
/
crossover.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
/*
* Crossover Operator
*
* The crossover operator is a genetic operator that will
* take 2 programs and mix them together in order to create a new one
*
* In this scenario, crossoverOnce will take two programs and return a single one,
* and crossover will take an array of programs and return an array of new programs
*
*/
/*
* For each parent,
* we choose a random split point
* then we take the first part of parent1 and concat it with the last part of parent2
*
* parent1 => {Program}
* parent2 => {Program}
*
* return => {Program}
*/
export const crossoverOnce = (parent1, parent2) => {
const p1 = Math.floor(Math.random() * parent1.length)
const p2 = Math.floor(Math.random() * parent2.length)
return [ ...parent1.slice(0, p1), ...parent2.slice(p2) ]
}
/*
* Given an amount representing how many children we want to create,
* return an Array of programs with the given population
*
* amount => {Integer}
* population => {Array<Program>}
*
* return => {Array<Program>}
*/
export const crossover = amount => population => {
let i = 0
const ret = []
while (i++ < amount) {
const parent1 = population[Math.floor(Math.random() * population.length)]
const parent2 = population[Math.floor(Math.random() * population.length)]
ret.push(crossoverOnce(parent1, parent2))
}
return ret
}