|
| 1 | +mutable struct PSOCache{TP, TAlg, TPart, TGbest, TSampler} |
| 2 | + prob::TP |
| 3 | + alg::TAlg |
| 4 | + particles::TPart |
| 5 | + gbest::TGbest |
| 6 | + sampler::TSampler |
| 7 | +end |
| 8 | + |
| 9 | +struct HybridPSOCache{TPc, TSp, TAlg} |
| 10 | + pso_cache::TPc |
| 11 | + start_points::TSp |
| 12 | + alg::TAlg |
| 13 | +end |
| 14 | + |
| 15 | +function __init!(particles, prob::OptimizationProblem, |
| 16 | + opt::Union{ParallelPSOKernel, ParallelSyncPSOKernel}, sampler::T, |
| 17 | + args...; kwargs...) where {T <: QuasiMonteCarlo.SamplingAlgorithm} |
| 18 | + backend = opt.backend |
| 19 | + |
| 20 | + qmc_samples = QuasiMonteCarlo.sample(opt.num_particles, prob.lb, prob.ub, sampler) |
| 21 | + |
| 22 | + qmc_samples = adapt(backend, qmc_samples) |
| 23 | + |
| 24 | + kernel! = gpu_init_particles!(backend) |
| 25 | + |
| 26 | + kernel!( |
| 27 | + particles, qmc_samples, prob, opt, typeof(prob.u0), T; ndrange = opt.num_particles) |
| 28 | + |
| 29 | + best_particle = minimum(particles) |
| 30 | + init_gbest = SPSOGBest(best_particle.best_position, best_particle.best_cost) |
| 31 | + |
| 32 | + return particles, init_gbest |
| 33 | +end |
| 34 | + |
| 35 | +function __init!(particles, prob::OptimizationProblem, |
| 36 | + opt::Union{ParallelPSOKernel, ParallelSyncPSOKernel}, sampler::T, |
| 37 | + args...; kwargs...) where {T <: GPUSamplingAlgorithm} |
| 38 | + backend = opt.backend |
| 39 | + |
| 40 | + kernel! = gpu_init_particles!(backend) |
| 41 | + |
| 42 | + kernel!(particles, prob, opt, typeof(prob.u0), T; ndrange = opt.num_particles) |
| 43 | + |
| 44 | + best_particle = minimum(particles) |
| 45 | + |
| 46 | + init_gbest = SPSOGBest(best_particle.best_position, best_particle.best_cost) |
| 47 | + |
| 48 | + particles, init_gbest |
| 49 | +end |
| 50 | + |
| 51 | +function SciMLBase.init( |
| 52 | + prob::OptimizationProblem, opt::ParallelPSOKernel, args...; sampler = GPUUniformSampler(), kwargs...) |
| 53 | + @assert prob.u0 isa SArray |
| 54 | + |
| 55 | + ## Bounds check |
| 56 | + lb, ub = check_init_bounds(prob) |
| 57 | + lb, ub = check_init_bounds(prob) |
| 58 | + prob = remake(prob; lb = lb, ub = ub) |
| 59 | + |
| 60 | + particles = KernelAbstractions.allocate( |
| 61 | + opt.backend, SPSOParticle{typeof(prob.u0), eltype(typeof(prob.u0))}, opt.num_particles) |
| 62 | + |
| 63 | + _sampler = if lb === nothing || ub === nothing || (all(isinf, lb) && all(isinf, ub)) |
| 64 | + GPUUnboundedSampler() |
| 65 | + else |
| 66 | + sampler |
| 67 | + end |
| 68 | + |
| 69 | + particles, _init_gbest = __init!(particles, prob, opt, _sampler, args...; kwargs...) |
| 70 | + |
| 71 | + init_gbest = KernelAbstractions.allocate(opt.backend, typeof(_init_gbest), (1,)) |
| 72 | + copyto!(init_gbest, [_init_gbest]) |
| 73 | + |
| 74 | + return PSOCache{ |
| 75 | + typeof(prob), typeof(opt), typeof(particles), typeof(init_gbest), typeof(_sampler)}( |
| 76 | + prob, opt, particles, init_gbest, _sampler) |
| 77 | +end |
| 78 | + |
| 79 | +function SciMLBase.init( |
| 80 | + prob::OptimizationProblem, opt::ParallelSyncPSOKernel, args...; sampler = GPUUniformSampler(), kwargs...) |
| 81 | + @assert prob.u0 isa SArray |
| 82 | + |
| 83 | + ## Bounds check |
| 84 | + lb, ub = check_init_bounds(prob) |
| 85 | + lb, ub = check_init_bounds(prob) |
| 86 | + prob = remake(prob; lb = lb, ub = ub) |
| 87 | + |
| 88 | + particles = KernelAbstractions.allocate( |
| 89 | + opt.backend, SPSOParticle{typeof(prob.u0), eltype(typeof(prob.u0))}, opt.num_particles) |
| 90 | + |
| 91 | + _sampler = if lb === nothing || ub === nothing || (all(isinf, lb) && all(isinf, ub)) |
| 92 | + GPUUnboundedSampler() |
| 93 | + else |
| 94 | + sampler |
| 95 | + end |
| 96 | + |
| 97 | + particles, init_gbest = __init!(particles, prob, opt, _sampler, args...; kwargs...) |
| 98 | + |
| 99 | + return PSOCache{ |
| 100 | + typeof(prob), typeof(opt), typeof(particles), typeof(init_gbest), typeof(_sampler)}( |
| 101 | + prob, opt, particles, init_gbest, _sampler) |
| 102 | +end |
| 103 | + |
| 104 | +function SciMLBase.reinit!(cache::Union{PSOCache, HybridPSOCache}) |
| 105 | + reinit_cache!(cache, cache.alg) |
| 106 | +end |
| 107 | + |
| 108 | +function reinit_cache!(cache::PSOCache, opt::ParallelPSOKernel) |
| 109 | + prob = cache.prob |
| 110 | + particles = cache.particles |
| 111 | + |
| 112 | + particles, _init_gbest = __init!(particles, prob, opt, cache.sampler) |
| 113 | + |
| 114 | + copyto!(cache.gbest, [_init_gbest]) |
| 115 | + |
| 116 | + return nothing |
| 117 | +end |
| 118 | + |
| 119 | +function reinit_cache!(cache::PSOCache, opt::ParallelSyncPSOKernel) |
| 120 | + prob = cache.prob |
| 121 | + particles = cache.particles |
| 122 | + |
| 123 | + particles, init_gbest = __init!(particles, prob, opt, cache.sampler) |
| 124 | + |
| 125 | + cache.gbest = init_gbest |
| 126 | + |
| 127 | + return nothing |
| 128 | +end |
| 129 | + |
| 130 | +function SciMLBase.init( |
| 131 | + prob::OptimizationProblem, opt::HybridPSO{Backend, LocalOpt}, args...; |
| 132 | + kwargs...) where {Backend, LocalOpt <: Union{LBFGS, BFGS}} |
| 133 | + psoalg = opt.pso |
| 134 | + backend = opt.backend |
| 135 | + |
| 136 | + pso_cache = init(prob, psoalg, args...; kwargs...) |
| 137 | + |
| 138 | + start_points = KernelAbstractions.allocate( |
| 139 | + backend, typeof(prob.u0), opt.pso.num_particles) |
| 140 | + |
| 141 | + return HybridPSOCache{ |
| 142 | + typeof(pso_cache), typeof(start_points), typeof(opt)}(pso_cache, start_points, opt) |
| 143 | +end |
| 144 | + |
| 145 | +function reinit_cache!(cache::HybridPSOCache, |
| 146 | + opt::HybridPSO{Backend, LocalOpt}) where {Backend, LocalOpt <: Union{LBFGS, BFGS}} |
| 147 | + reinit!(cache.pso_cache) |
| 148 | + fill!(cache.start_points, zero(eltype(cache.start_points))) |
| 149 | + return nothing |
| 150 | +end |
| 151 | + |
| 152 | +function Base.getproperty(cache::HybridPSOCache, name::Symbol) |
| 153 | + if name ∈ (:start_points, :pso_cache, :alg) |
| 154 | + return getfield(cache, name) |
| 155 | + else |
| 156 | + return getproperty(cache.pso_cache, name) |
| 157 | + end |
| 158 | +end |
| 159 | + |
| 160 | +function Base.setproperty!(cache::HybridPSOCache, name::Symbol, val) |
| 161 | + if name ∈ (:start_points, :pso_cache, :alg) |
| 162 | + return setfield!(cache, name, val) |
| 163 | + else |
| 164 | + return setproperty!(cache.pso_cache, name, val) |
| 165 | + end |
| 166 | +end |
0 commit comments