-
Notifications
You must be signed in to change notification settings - Fork 202
/
particle.go
56 lines (46 loc) · 1.52 KB
/
particle.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
package pso
import (
"math/rand"
"time"
)
type Particle struct {
Position *Position
Pbest *Position
Velocity Vector
}
func NewParticle(swarm *Swarm) *Particle {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
particle := new(Particle)
particle.Position = RandomPosition(swarm.Settings.function, rng)
particle.Pbest = particle.Position.Copy()
particle.Pbest.Fitness = 1e20
particle.Velocity = make([]float64, swarm.Settings.function.dim)
xLo := swarm.Settings.function.xLo
xHi := swarm.Settings.function.xHi
for i := 0; i < swarm.Settings.function.dim; i++ {
a := xLo + (xHi-xLo)*rng.Float64()
b := xLo + (xHi-xLo)*rng.Float64()
particle.Velocity[i] = (a - b) / 2.0
}
return particle
}
func (particle *Particle) UpdateLocation(swarm *Swarm) {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < swarm.Settings.function.dim; i++ {
// calculate stochastic coefficients
rho1 := swarm.Settings.C1 * rng.Float64()
rho2 := swarm.Settings.C2 * rng.Float64()
// update velocity
particle.Velocity[i] =
swarm.Settings.Inertia*particle.Velocity[i] +
rho1*(particle.Pbest.Location[i]-particle.Position.Location[i]) +
rho2*(swarm.Gbest.Location[i]-particle.Position.Location[i])
particle.Position.Location[i] += particle.Velocity[i]
}
}
func (particle *Particle) UpdateFitness(swarm *Swarm) {
particle.Position.Fitness = swarm.Settings.function.Evaluate(particle.Position.Location)
if particle.Position.IsBetterThan(particle.Pbest) {
particle.Pbest = particle.Position.Copy()
}
}