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+
16function uniform (dim:: Int , lb:: AbstractArray{T} , ub:: AbstractArray{T} ) where {T}
27 arr = rand (T, dim)
38 @inbounds for i in 1 : dim
@@ -6,7 +11,7 @@ function uniform(dim::Int, lb::AbstractArray{T}, ub::AbstractArray{T}) where {T}
611 return arr
712end
813
9- function init_particles ( prob, opt, :: Type{T} ) where {T <: SArray }
14+ function init_particles! (particles, prob, opt, :: Type{T} ) where {T <: SArray }
1015 dim = length (prob. u0)
1116 lb = prob. lb
1217 ub = prob. ub
@@ -15,47 +20,102 @@ function init_particles(prob, opt, ::Type{T}) where {T <: SArray}
1520 num_particles = opt. num_particles
1621
1722 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)
2554 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)
2692 else
27- gbest_position = uniform ( dim, lb, ub)
93+ gbest_position = StaticArrays . sacollect (T, uniform_itr ( dim, lb, ub) )
2894 end
2995
30- gbest_position = SVector {length (gbest_position), eltype(gbest_position)} (gbest_position )
96+ gbest_cost = cost_func (gbest_position, p )
3197 if ! isnothing (prob. f. cons)
3298 penalty = calc_penalty (gbest_position, prob, 1 , opt. θ, opt. γ, opt. h)
3399 gbest_cost = cost_func (gbest_position, p) + penalty
34100 else
35101 gbest_cost = cost_func (gbest_position, p)
36102 end
37- # gbest_cost = cost_func(gbest_position, p)
38- particles = SPSOParticle[]
103+ particles = SPSOParticle{T, eltype (T)}[]
39104
40105 if ! (lb === nothing || (all (isinf, lb) && all (isinf, ub)))
41106 positions = QuasiMonteCarlo. sample (num_particles, lb, ub, LatinHypercubeSample ())
42107 end
43108
44109 for i in 1 : num_particles
45110 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)
54115 else
55- position = @view positions[: , i]
116+ @inbounds position = StaticArrays . sacollect (T, positions[j , i] for j in 1 : dim)
56117 end
57- position = SVector {length(position), eltype(position)} (position)
58- velocity = @SArray zeros (eltype (position), dim)
118+ velocity = zero (T)
59119
60120 if ! isnothing (prob. f. cons)
61121 penalty = calc_penalty (position, prob, 1 , opt. θ, opt. γ, opt. h)
@@ -74,7 +134,7 @@ function init_particles(prob, opt, ::Type{T}) where {T <: SArray}
74134 end
75135 end
76136 gbest = SPSOGBest (gbest_position, gbest_cost)
77- return gbest, convert (Vector{ typeof ( particles[ 1 ])}, particles)
137+ return gbest, particles
78138end
79139
80140function init_particles (prob, opt, :: Type{T} ) where {T <: AbstractArray }
0 commit comments