1
+ @inbounds function uniform_itr (
2
+ dim:: Int , lb:: AbstractArray{T} , ub:: AbstractArray{T} ) where {T}
3
+ (rand (T) * (ub[i] - lb[i]) + lb[i] for i in 1 : dim)
4
+ end
5
+
1
6
function uniform (dim:: Int , lb:: AbstractArray{T} , ub:: AbstractArray{T} ) where {T}
2
7
arr = rand (T, dim)
3
8
@inbounds for i in 1 : dim
@@ -6,7 +11,7 @@ function uniform(dim::Int, lb::AbstractArray{T}, ub::AbstractArray{T}) where {T}
6
11
return arr
7
12
end
8
13
9
- function init_particles ( prob, opt, :: Type{T} ) where {T <: SArray }
14
+ function init_particles! (particles, prob, opt, :: Type{T} ) where {T <: SArray }
10
15
dim = length (prob. u0)
11
16
lb = prob. lb
12
17
ub = prob. ub
@@ -15,47 +20,102 @@ function init_particles(prob, opt, ::Type{T}) where {T <: SArray}
15
20
num_particles = opt. num_particles
16
21
17
22
if lb === nothing || (all (isinf, lb) && all (isinf, ub))
18
- gbest_position = Array {eltype(prob.u0), 1} (undef, dim)
19
- for i in 1 : dim
20
- if abs (prob. u0[i]) > 0
21
- gbest_position[i] = prob. u0[i] + rand (eltype (prob. u0)) * abs (prob. u0[i])
22
- else
23
- gbest_position[i] = rand (eltype (prob. u0))
24
- end
23
+ gbest_position = StaticArrays. sacollect (T,
24
+ ifelse (
25
+ abs (prob. u0[i]) > 0 , prob. u0[i] + rand (eltype (prob. u0)) * abs (prob. u0[i]),
26
+ rand (eltype (prob. u0))) for i in 1 : dim)
27
+ else
28
+ gbest_position = StaticArrays. sacollect (T, uniform_itr (dim, lb, ub))
29
+ end
30
+
31
+ gbest_position = convert (T, gbest_position)
32
+ gbest_cost = cost_func (gbest_position, p)
33
+ if ! isnothing (prob. f. cons)
34
+ penalty = calc_penalty (gbest_position, prob, 1 , opt. θ, opt. γ, opt. h)
35
+ gbest_cost = cost_func (gbest_position, p) + penalty
36
+ else
37
+ gbest_cost = cost_func (gbest_position, p)
38
+ end
39
+ gbest_cost = cost_func (gbest_position, p)
40
+ # particles = SPSOParticle[]
41
+
42
+ if ! (lb === nothing || (all (isinf, lb) && all (isinf, ub)))
43
+ positions = QuasiMonteCarlo. sample (num_particles, lb, ub, LatinHypercubeSample ())
44
+ end
45
+
46
+ for i in 1 : num_particles
47
+ if lb === nothing || (all (isinf, lb) && all (isinf, ub))
48
+ position = StaticArrays. sacollect (T,
49
+ ifelse (abs (prob. u0[i]) > 0 ,
50
+ prob. u0[i] + rand (eltype (prob. u0)) * abs (prob. u0[i]),
51
+ rand (eltype (prob. u0))) for i in 1 : dim)
52
+ else
53
+ @inbounds position = StaticArrays. sacollect (T, positions[j, i] for j in 1 : dim)
25
54
end
55
+
56
+ velocity = zero (T)
57
+
58
+ if ! isnothing (prob. f. cons)
59
+ penalty = calc_penalty (position, prob, 1 , opt. θ, opt. γ, opt. h)
60
+ cost = cost_func (position, p) + penalty
61
+ else
62
+ cost = cost_func (position, p)
63
+ end
64
+
65
+ best_position = position
66
+ best_cost = cost
67
+ @inbounds particles[i] = SPSOParticle (
68
+ position, velocity, cost, best_position, best_cost)
69
+
70
+ if best_cost < gbest_cost
71
+ gbest_position = best_position
72
+ gbest_cost = best_cost
73
+ end
74
+ end
75
+ gbest = SPSOGBest (gbest_position, gbest_cost)
76
+ return gbest, particles
77
+ end
78
+
79
+ function init_particles (prob, opt, :: Type{T} ) where {T <: SArray }
80
+ dim = length (prob. u0)
81
+ lb = prob. lb
82
+ ub = prob. ub
83
+ cost_func = prob. f
84
+ p = prob. p
85
+ num_particles = opt. num_particles
86
+
87
+ if lb === nothing || (all (isinf, lb) && all (isinf, ub))
88
+ gbest_position = StaticArrays. sacollect (T,
89
+ ifelse (
90
+ abs (prob. u0[i]) > 0 , prob. u0[i] + rand (eltype (prob. u0)) * abs (prob. u0[i]),
91
+ rand (eltype (prob. u0))) for i in 1 : dim)
26
92
else
27
- gbest_position = uniform ( dim, lb, ub)
93
+ gbest_position = StaticArrays . sacollect (T, uniform_itr ( dim, lb, ub) )
28
94
end
29
95
30
- gbest_position = SVector {length (gbest_position), eltype(gbest_position)} (gbest_position )
96
+ gbest_cost = cost_func (gbest_position, p )
31
97
if ! isnothing (prob. f. cons)
32
98
penalty = calc_penalty (gbest_position, prob, 1 , opt. θ, opt. γ, opt. h)
33
99
gbest_cost = cost_func (gbest_position, p) + penalty
34
100
else
35
101
gbest_cost = cost_func (gbest_position, p)
36
102
end
37
- # gbest_cost = cost_func(gbest_position, p)
38
- particles = SPSOParticle[]
103
+ particles = SPSOParticle{T, eltype (T)}[]
39
104
40
105
if ! (lb === nothing || (all (isinf, lb) && all (isinf, ub)))
41
106
positions = QuasiMonteCarlo. sample (num_particles, lb, ub, LatinHypercubeSample ())
42
107
end
43
108
44
109
for i in 1 : num_particles
45
110
if lb === nothing || (all (isinf, lb) && all (isinf, ub))
46
- position = Array {eltype(prob.u0), 1} (undef, dim)
47
- for i in 1 : dim
48
- if abs (prob. u0[i]) > 0
49
- position[i] = prob. u0[i] + rand (eltype (prob. u0)) * abs (prob. u0[i])
50
- else
51
- position[i] = rand (eltype (prob. u0))
52
- end
53
- end
111
+ @inbounds position = StaticArrays. sacollect (T,
112
+ ifelse (abs (prob. u0[i]) > 0 ,
113
+ prob. u0[i] + rand (eltype (prob. u0)) * abs (prob. u0[i]),
114
+ rand (eltype (prob. u0))) for i in 1 : dim)
54
115
else
55
- position = @view positions[: , i]
116
+ @inbounds position = StaticArrays . sacollect (T, positions[j , i] for j in 1 : dim)
56
117
end
57
- position = SVector {length(position), eltype(position)} (position)
58
- velocity = @SArray zeros (eltype (position), dim)
118
+ velocity = zero (T)
59
119
60
120
if ! isnothing (prob. f. cons)
61
121
penalty = calc_penalty (position, prob, 1 , opt. θ, opt. γ, opt. h)
@@ -74,7 +134,7 @@ function init_particles(prob, opt, ::Type{T}) where {T <: SArray}
74
134
end
75
135
end
76
136
gbest = SPSOGBest (gbest_position, gbest_cost)
77
- return gbest, convert (Vector{ typeof ( particles[ 1 ])}, particles)
137
+ return gbest, particles
78
138
end
79
139
80
140
function init_particles (prob, opt, :: Type{T} ) where {T <: AbstractArray }
0 commit comments