@@ -13,7 +13,8 @@ struct HybridPSOCache{TPc, TSp, TAlg}
1313 alg:: TAlg
1414end
1515
16- function __init (prob:: OptimizationProblem , opt:: ParallelPSOKernel , sampler:: T ,
16+ function __init (prob:: OptimizationProblem ,
17+ opt:: Union{ParallelPSOKernel, ParallelSyncPSOKernel} , sampler:: T ,
1718 args... ; kwargs... ) where {T <: QuasiMonteCarlo.SamplingAlgorithm }
1819 backend = opt. backend
1920
@@ -30,16 +31,13 @@ function __init(prob::OptimizationProblem, opt::ParallelPSOKernel, sampler::T,
3031 particles, qmc_samples, prob, opt, typeof (prob. u0), T; ndrange = opt. num_particles)
3132
3233 best_particle = minimum (particles)
33- _init_gbest = SPSOGBest (best_particle. best_position, best_particle. best_cost)
34+ init_gbest = SPSOGBest (best_particle. best_position, best_particle. best_cost)
3435
35- init_gbest = KernelAbstractions. allocate (backend, typeof (_init_gbest), (1 ,))
36- copyto! (init_gbest, [_init_gbest])
37- return PSOCache{
38- typeof (prob), typeof (opt), typeof (particles), typeof (init_gbest)}(
39- prob, opt, particles, init_gbest)
36+ return particles, init_gbest
4037end
4138
42- function __init (prob:: OptimizationProblem , opt:: ParallelPSOKernel , sampler:: T ,
39+ function __init (prob:: OptimizationProblem ,
40+ opt:: Union{ParallelPSOKernel, ParallelSyncPSOKernel} , sampler:: T ,
4341 args... ; kwargs... ) where {T <: GPUSamplingAlgorithm }
4442 backend = opt. backend
4543
@@ -51,13 +49,9 @@ function __init(prob::OptimizationProblem, opt::ParallelPSOKernel, sampler::T,
5149
5250 best_particle = minimum (particles)
5351
54- _init_gbest = SPSOGBest (best_particle. best_position, best_particle. best_cost)
52+ init_gbest = SPSOGBest (best_particle. best_position, best_particle. best_cost)
5553
56- init_gbest = KernelAbstractions. allocate (backend, typeof (_init_gbest), (1 ,))
57- copyto! (init_gbest, [_init_gbest])
58- return PSOCache{
59- typeof (prob), typeof (opt), typeof (particles), typeof (init_gbest)}(
60- prob, opt, particles, init_gbest)
54+ particles, init_gbest
6155end
6256
6357function SciMLBase. init (
@@ -69,37 +63,66 @@ function SciMLBase.init(
6963 lb, ub = check_init_bounds (prob)
7064 prob = remake (prob; lb = lb, ub = ub)
7165
72- if lb === nothing || ub === nothing || (all (isinf, lb) && all (isinf, ub))
66+ particles, _init_gbest = if lb === nothing || ub === nothing ||
67+ (all (isinf, lb) && all (isinf, ub))
7368 __init (prob, opt, GPUUnboundedSampler (), args... ; kwargs... )
7469 else
7570 __init (prob, opt, sampler, args... ; kwargs... )
7671 end
72+
73+ init_gbest = KernelAbstractions. allocate (opt. backend, typeof (_init_gbest), (1 ,))
74+ copyto! (init_gbest, [_init_gbest])
75+
76+ return PSOCache{
77+ typeof (prob), typeof (opt), typeof (particles), typeof (init_gbest)}(
78+ prob, opt, particles, init_gbest)
7779end
7880
7981function SciMLBase. init (
80- prob:: OptimizationProblem , opt:: ParallelSyncPSOKernel , args... ; kwargs... )
81- backend = opt. backend
82+ prob:: OptimizationProblem , opt:: ParallelSyncPSOKernel , args... ; sampler = GPUUniformSampler (), kwargs... )
8283 @assert prob. u0 isa SArray
8384
8485 # # Bounds check
8586 lb, ub = check_init_bounds (prob)
8687 lb, ub = check_init_bounds (prob)
8788 prob = remake (prob; lb = lb, ub = ub)
8889
89- particles = KernelAbstractions. allocate (
90- backend, SPSOParticle{typeof (prob. u0), eltype (typeof (prob. u0))}, opt. num_particles)
91- kernel! = gpu_init_particles! (backend)
92-
93- kernel! (particles, prob, opt, typeof (prob. u0); ndrange = opt. num_particles)
94-
95- best_particle = minimum (particles)
96- init_gbest = SPSOGBest (best_particle. best_position, best_particle. best_cost)
90+ particles, init_gbest = if lb === nothing || ub === nothing ||
91+ (all (isinf, lb) && all (isinf, ub))
92+ __init (prob, opt, GPUUnboundedSampler (), args... ; kwargs... )
93+ else
94+ __init (prob, opt, sampler, args... ; kwargs... )
95+ end
9796
9897 return PSOCache{
9998 typeof (prob), typeof (opt), typeof (particles), typeof (init_gbest)}(
10099 prob, opt, particles, init_gbest)
101100end
102101
102+ # function SciMLBase.init(
103+ # prob::OptimizationProblem, opt::ParallelSyncPSOKernel, args...; kwargs...)
104+ # backend = opt.backend
105+ # @assert prob.u0 isa SArray
106+
107+ # ## Bounds check
108+ # lb, ub = check_init_bounds(prob)
109+ # lb, ub = check_init_bounds(prob)
110+ # prob = remake(prob; lb = lb, ub = ub)
111+
112+ # particles = KernelAbstractions.allocate(
113+ # backend, SPSOParticle{typeof(prob.u0), eltype(typeof(prob.u0))}, opt.num_particles)
114+ # kernel! = gpu_init_particles!(backend)
115+
116+ # kernel!(particles, prob, opt, typeof(prob.u0); ndrange = opt.num_particles)
117+
118+ # best_particle = minimum(particles)
119+ # init_gbest = SPSOGBest(best_particle.best_position, best_particle.best_cost)
120+
121+ # return PSOCache{
122+ # typeof(prob), typeof(opt), typeof(particles), typeof(init_gbest)}(
123+ # prob, opt, particles, init_gbest)
124+ # end
125+
103126function SciMLBase. reinit! (cache:: Union{PSOCache, HybridPSOCache} ; kwargs... )
104127 reinit_cache! (cache, cache. alg)
105128end
@@ -170,6 +193,7 @@ function SciMLBase.solve!(
170193 cache. particles,
171194 opt,
172195 args... ;
196+ maxiters,
173197 kwargs... )
174198 t1 = time ()
175199
182206function SciMLBase. solve (prob:: OptimizationProblem ,
183207 opt:: Union{ParallelPSOKernel, ParallelSyncPSOKernel, HybridPSO} ,
184208 args... ; maxiters = 100 , kwargs... )
185- solve! (init (prob, opt, args... ; maxiters, kwargs... ), opt)
209+ solve! (init (prob, opt, args... ; kwargs... ), opt, args ... ; maxiters, kwargs ... )
186210end
187211
188212function SciMLBase. __solve (prob:: OptimizationProblem ,
0 commit comments