|
1 | 1 | function get_pos(particle) |
2 | 2 | return particle.position |
3 | 3 | end |
4 | | -mutable struct PSOCache{TP, TAlg, TPart, TGbest} |
5 | | - prob::TP |
6 | | - alg::TAlg |
7 | | - particles::TPart |
8 | | - gbest::TGbest |
9 | | -end |
10 | | -struct HybridPSOCache{TPc, TSp, TAlg} |
11 | | - pso_cache::TPc |
12 | | - start_points::TSp |
13 | | - alg::TAlg |
14 | | -end |
15 | | - |
16 | | -function __init(prob::OptimizationProblem, |
17 | | - opt::Union{ParallelPSOKernel, ParallelSyncPSOKernel}, sampler::T, |
18 | | - args...; kwargs...) where {T <: QuasiMonteCarlo.SamplingAlgorithm} |
19 | | - backend = opt.backend |
20 | | - |
21 | | - particles = KernelAbstractions.allocate( |
22 | | - backend, SPSOParticle{typeof(prob.u0), eltype(typeof(prob.u0))}, opt.num_particles) |
23 | | - |
24 | | - qmc_samples = QuasiMonteCarlo.sample(opt.num_particles, prob.lb, prob.ub, sampler) |
25 | | - |
26 | | - qmc_samples = adapt(backend, qmc_samples) |
27 | | - |
28 | | - kernel! = gpu_init_particles!(backend) |
29 | | - |
30 | | - kernel!( |
31 | | - particles, qmc_samples, prob, opt, typeof(prob.u0), T; ndrange = opt.num_particles) |
32 | | - |
33 | | - best_particle = minimum(particles) |
34 | | - init_gbest = SPSOGBest(best_particle.best_position, best_particle.best_cost) |
35 | | - |
36 | | - return particles, init_gbest |
37 | | -end |
38 | | - |
39 | | -function __init(prob::OptimizationProblem, |
40 | | - opt::Union{ParallelPSOKernel, ParallelSyncPSOKernel}, sampler::T, |
41 | | - args...; kwargs...) where {T <: GPUSamplingAlgorithm} |
42 | | - backend = opt.backend |
43 | | - |
44 | | - particles = KernelAbstractions.allocate( |
45 | | - backend, SPSOParticle{typeof(prob.u0), eltype(typeof(prob.u0))}, opt.num_particles) |
46 | | - kernel! = gpu_init_particles!(backend) |
47 | | - |
48 | | - kernel!(particles, prob, opt, typeof(prob.u0), T; ndrange = opt.num_particles) |
49 | | - |
50 | | - best_particle = minimum(particles) |
51 | | - |
52 | | - init_gbest = SPSOGBest(best_particle.best_position, best_particle.best_cost) |
53 | | - |
54 | | - particles, init_gbest |
55 | | -end |
56 | | - |
57 | | -function SciMLBase.init( |
58 | | - prob::OptimizationProblem, opt::ParallelPSOKernel, args...; sampler = GPUUniformSampler(), kwargs...) |
59 | | - @assert prob.u0 isa SArray |
60 | | - |
61 | | - ## Bounds check |
62 | | - lb, ub = check_init_bounds(prob) |
63 | | - lb, ub = check_init_bounds(prob) |
64 | | - prob = remake(prob; lb = lb, ub = ub) |
65 | | - |
66 | | - particles, _init_gbest = if lb === nothing || ub === nothing || |
67 | | - (all(isinf, lb) && all(isinf, ub)) |
68 | | - __init(prob, opt, GPUUnboundedSampler(), args...; kwargs...) |
69 | | - else |
70 | | - __init(prob, opt, sampler, args...; kwargs...) |
71 | | - 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) |
79 | | -end |
80 | | - |
81 | | -function SciMLBase.init( |
82 | | - prob::OptimizationProblem, opt::ParallelSyncPSOKernel, args...; sampler = GPUUniformSampler(), kwargs...) |
83 | | - @assert prob.u0 isa SArray |
84 | | - |
85 | | - ## Bounds check |
86 | | - lb, ub = check_init_bounds(prob) |
87 | | - lb, ub = check_init_bounds(prob) |
88 | | - prob = remake(prob; lb = lb, ub = ub) |
89 | | - |
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 |
96 | | - |
97 | | - return PSOCache{ |
98 | | - typeof(prob), typeof(opt), typeof(particles), typeof(init_gbest)}( |
99 | | - prob, opt, particles, init_gbest) |
100 | | -end |
101 | | - |
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 | | - |
126 | | -function SciMLBase.reinit!(cache::Union{PSOCache, HybridPSOCache}; kwargs...) |
127 | | - reinit_cache!(cache, cache.alg) |
128 | | -end |
129 | | - |
130 | | -function reinit_cache!(cache::PSOCache, opt::ParallelPSOKernel) |
131 | | - prob = cache.prob |
132 | | - backend = opt.backend |
133 | | - particles = cache.particles |
134 | | - |
135 | | - kernel! = PSOGPU.gpu_init_particles!(backend) |
136 | | - kernel!(particles, prob, opt, typeof(prob.u0); ndrange = opt.num_particles) |
137 | | - |
138 | | - best_particle = minimum(particles) |
139 | | - _init_gbest = SPSOGBest(best_particle.best_position, best_particle.best_cost) |
140 | | - |
141 | | - copyto!(cache.gbest, [_init_gbest]) |
142 | | - |
143 | | - return nothing |
144 | | -end |
145 | | - |
146 | | -function reinit_cache!(cache::PSOCache, opt::ParallelSyncPSOKernel) |
147 | | - prob = cache.prob |
148 | | - backend = opt.backend |
149 | | - particles = cache.particles |
150 | | - |
151 | | - kernel! = PSOGPU.gpu_init_particles!(backend) |
152 | | - kernel!(particles, prob, opt, typeof(prob.u0); ndrange = opt.num_particles) |
153 | | - |
154 | | - best_particle = minimum(particles) |
155 | | - |
156 | | - init_gbest = SPSOGBest(best_particle.best_position, best_particle.best_cost) |
157 | | - |
158 | | - cache.gbest = init_gbest |
159 | | - |
160 | | - return nothing |
161 | | -end |
162 | 4 |
|
163 | 5 | function SciMLBase.solve!( |
164 | 6 | cache::Union{PSOCache, HybridPSOCache}, args...; maxiters = 100, kwargs...) |
|
0 commit comments