-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.go
79 lines (70 loc) · 1.9 KB
/
init.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
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
package xgp
import (
"math/rand"
"github.com/MaxHalford/xgp/op"
)
// An Initializer generates a random Operator with random operands.
type Initializer interface {
Apply(
minHeight uint,
maxHeight uint,
newOp func(leaf bool, rng *rand.Rand) op.Operator,
rng *rand.Rand,
) op.Operator
}
// FullInit can generate an Operator of height maxHeight.
type FullInit struct{}
// Apply FullInit returns an Operator of height maxHeight, hence minHeight is
// not taken into account.
func (full FullInit) Apply(
minHeight uint,
maxHeight uint,
newOp func(leaf bool, rng *rand.Rand) op.Operator,
rng *rand.Rand,
) op.Operator {
var op = newOp(maxHeight == 0, rng)
for i := uint(0); i < op.Arity(); i++ {
op = op.SetOperand(i, full.Apply(0, maxHeight-1, newOp, rng))
}
return op
}
// GrowInit can generate an Operator who's depth lies in [MinHeight, MaxHeight].
type GrowInit struct {
PLeaf float64
}
// Apply GrowInit.
func (grow GrowInit) Apply(
minHeight uint,
maxHeight uint,
newOp func(leaf bool, rng *rand.Rand) op.Operator,
rng *rand.Rand,
) op.Operator {
var (
leaf = maxHeight == 0 || (minHeight <= 0 && rng.Float64() < grow.PLeaf)
op = newOp(leaf, rng)
)
for i := uint(0); i < op.Arity(); i++ {
op = op.SetOperand(i, grow.Apply(minHeight-1, maxHeight-1, newOp, rng))
}
return op
}
// RampedHaldAndHalfInit randomly uses GrowInit and FullInit. If it uses
// FullInit then it uses a random height.
type RampedHaldAndHalfInit struct {
PFull float64
FullInit FullInit
GrowInit GrowInit
}
// Apply RampedHaldAndHalfInit.
func (rhah RampedHaldAndHalfInit) Apply(
minHeight uint,
maxHeight uint,
newOp func(leaf bool, rng *rand.Rand) op.Operator,
rng *rand.Rand,
) op.Operator {
if rng.Float64() < rhah.PFull {
var height = randInt(int(minHeight), int(maxHeight), rng)
return rhah.FullInit.Apply(0, uint(height), newOp, rng)
}
return rhah.GrowInit.Apply(minHeight, maxHeight, newOp, rng)
}