-
Notifications
You must be signed in to change notification settings - Fork 198
/
position.go
40 lines (33 loc) · 944 Bytes
/
position.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
package pso
import "math/rand"
type Vector []float64
type Position struct {
Location Vector
Fitness float64
}
func NewPosition(dim int) *Position {
loc := make([]float64, dim)
return &Position{
Location: loc,
// Fitness: EvaluateFunction(settings.Function.Evaluate, loc),
}
}
func RandomPosition(function ObjectiveFunction, rng *rand.Rand) *Position {
pos := NewPosition(function.dim)
xLo := function.xLo
xHi := function.xHi
for i := 0; i < len(pos.Location); i++ {
pos.Location[i] = xLo + (xHi-xLo)*rng.Float64()
}
// pos.Fitness = EvaluateFunction(settings.Function.Evaluate, pos.Location)
return pos
}
func (position *Position) Copy() *Position {
newPosition := NewPosition(len(position.Location))
copy(newPosition.Location, position.Location)
newPosition.Fitness = position.Fitness
return newPosition
}
func (position *Position) IsBetterThan(other *Position) bool {
return position.Fitness < other.Fitness
}