-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortednumbers.js
35 lines (27 loc) · 1015 Bytes
/
sortednumbers.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
const {apply, compose, converge, curry, filter, init, length, lift, map, range, subtract, sum, tail, zip} = require('ramda')
const genetic = require('./genetic')
const {log} = console
const
geneset = range(0, 100),
target = range(0, 40),
fitness = genes => converge(
(numbersinseq, totalgap) => ({
numbersinseq,
totalgap,
isbetter: other =>
numbersinseq === other.numbersinseq
? totalgap < other.totalgap
: numbersinseq > other.numbersinseq,
isequal: other => numbersinseq === other.numbersinseq && totalgap === other.totalgap
}),
[
compose(length, filter(([a, b]) => a <= b), lift(zip)(init)(tail)),
compose(sum, map(apply(subtract)), filter(([a, b]) => a > b), lift(zip)(init)(tail))
]
)(genes),
_display = curry((start, genes, fitness) => {
log(Date.now() - start, genes.join(' '), fitness.numbersinseq, fitness.totalgap)
}),
display = _display(Date.now()),
engine = genetic(fitness, length(target), fitness(target), geneset, display)
engine.getbest()