-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_3.js
59 lines (48 loc) · 1.49 KB
/
example_3.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
// @ts-check
'use strict'
const GA = require('./vovk-ga')
const trainer = new GA()
console.log(`Example 3: Intermediate function optimization`)
const parameters = { variable: ['x', 'y'], type: 'number', range: { min: -2, max: 2 }, snap: 0.0001 }
const desiredOutput = 3
const test = (x, y) => {
const a = 1 + (x + y + 1) ** 2 * (19 - 14 * x + 3 * x ** 2 - 14 * y + 6 * x * y + 3 * y ** 2)
const b = 30 + (2 * x - 3 * y) ** 2 * (18 - 32 * x + 12 * x ** 2 + 48 * y - 36 * x * y + 27 * y ** 2)
const output = a * b
return output
}
const fitnessFunction = (sample) => {
const { x, y } = sample
const output = test(x, y)
return output - desiredOutput
}
const conf = {
//debug: false,
maxPopulation: 1000,
survivorsPERCENT: 0.05,
crossoverChance: 0.05,
mutationChance: 0.45,
mutationPower: 0.1,
bestSurvive: true,
parameters: parameters,
//initialValues: { x: 0, y: 0 },
fitnessFunction: fitnessFunction,
fitnessTargetValue: 0,
fitnessTimeout: 10000,
}
const logProgress = progress => {
console.log(progress.message)
}
const logResult = result => { // Final results
console.log(result.message)
const { x, y } = result.parameters
console.log(`Test ${test.toString()} ---> ${test(x, y)}`)
}
const catchError = e => {
if (e === 'timeout') console.log('Training timeout!')
else throw e
}
console.log(`Starting training...`)
trainer.configure(conf).evolve(100, logProgress)
.then(logResult)
.catch(catchError)