-
Notifications
You must be signed in to change notification settings - Fork 123
/
random.go
33 lines (29 loc) · 888 Bytes
/
random.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
package mutation
import (
"github.com/RH12503/Triangula/normgeom"
"math/rand"
)
// randomMethod is a simple implementation of a Method.
type randomMethod struct {
rate float64 // The probability of a point being mutated.
amount float64 // The amount a point's coordinates are changed.
}
func (r randomMethod) Mutate(points normgeom.NormPointGroup, mutated func(mutation Mutation)) {
for i := range points {
if rand.Float64() < r.rate {
old := points[i]
points[i].X += (rand.Float64() - 0.5) * r.amount
points[i].Y += (rand.Float64() - 0.5) * r.amount
points[i].Constrain()
mutated(Mutation{
Old: old,
New: points[i],
Index: i,
})
}
}
}
// NewRandomMethod returns a randomMethod with specified a mutation rate and amount.
func NewRandomMethod(rate float64, amount float64) randomMethod {
return randomMethod{rate: rate, amount: amount}
}