-
Notifications
You must be signed in to change notification settings - Fork 3
/
polish.go
45 lines (38 loc) · 935 Bytes
/
polish.go
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
package xgp
import (
"math"
"math/rand"
"github.com/MaxHalford/xgp/op"
xrand "golang.org/x/exp/rand"
"gonum.org/v1/gonum/optimize"
)
func polishProgram(prog Program, rng *rand.Rand) (Program, error) {
// Extract the Program's Consts
var consts = op.GetConsts(prog.Op)
// If there are no Consts then nothing can be done
if len(consts) == 0 {
return prog, nil
}
var (
problem = optimize.Problem{
Func: func(x []float64) float64 {
fitness, _ := Program{
Op: op.SetConsts(prog.Op, x),
GP: prog.GP,
}.Evaluate()
return fitness
},
}
method = &optimize.CmaEsChol{
Population: int(15 + math.Floor(3*math.Log(float64(len(consts))))), // MAGIC
Src: xrand.NewSource(rng.Uint64()),
}
)
// Run the optimisation
result, err := optimize.Minimize(problem, consts, nil, method)
if err != nil {
return prog, err
}
prog.Op = op.SetConsts(prog.Op, result.X)
return prog, nil
}