Skip to content

Commit eb4b35b

Browse files
committed
Update tests and fix bugs
1 parent 38da82c commit eb4b35b

File tree

5 files changed

+121
-36
lines changed

5 files changed

+121
-36
lines changed

Project.toml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,4 @@ SimpleNonlinearSolve = "727e6d20-b764-4bd8-a329-72de5adea6c7"
2323
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
2424

2525
[compat]
26-
julia = "1.6"
27-
28-
[extras]
29-
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
30-
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
31-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
32-
33-
[targets]
34-
test = ["Test", "StaticArrays", "LinearAlgebra", "Optimization", "ForwardDiff"]
26+
julia = "1.6"

src/solve.jl

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ struct HybridPSOCache{TPc, TSp, TAlg}
1313
alg::TAlg
1414
end
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
4037
end
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
6155
end
6256

6357
function 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)
7779
end
7880

7981
function 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)
101100
end
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+
103126
function SciMLBase.reinit!(cache::Union{PSOCache, HybridPSOCache}; kwargs...)
104127
reinit_cache!(cache, cache.alg)
105128
end
@@ -170,6 +193,7 @@ function SciMLBase.solve!(
170193
cache.particles,
171194
opt,
172195
args...;
196+
maxiters,
173197
kwargs...)
174198
t1 = time()
175199

@@ -182,7 +206,7 @@ end
182206
function 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...)
186210
end
187211

188212
function SciMLBase.__solve(prob::OptimizationProblem,

src/utils.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ end
9797

9898
@inbounds particles[i] = SPSOParticle(
9999
position, velocity, cost, best_position, best_cost)
100-
101100
end
102101

103102
function init_particles!(particles, prob, opt, ::Type{T}) where {T <: SArray}

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
33
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
44
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
55
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
6+
QuasiMonteCarlo = "8a4e6c94-4038-4cdc-81c3-7e6ffdb2a71b"
67
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
78
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
89
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"

test/regression.jl

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using PSOGPU, StaticArrays, SciMLBase, Test, LinearAlgebra, Random, KernelAbstractions
2+
using QuasiMonteCarlo
23

34
@testset "Rosenbrock test dimension = $(N)" for N in 2:3
45

@@ -36,12 +37,26 @@ using PSOGPU, StaticArrays, SciMLBase, Test, LinearAlgebra, Random, KernelAbstra
3637

3738
@test sol.objective < 1e-4
3839

40+
sol = solve!(
41+
init(prob, ParallelPSOKernel(n_particles; backend = CPU());
42+
sampler = LatinHypercubeSample()),
43+
maxiters = 500)
44+
45+
@test sol.retcode == ReturnCode.Default
46+
3947
sol = solve(prob,
4048
ParallelPSOKernel(n_particles; backend = CPU()),
4149
maxiters = 500)
4250

4351
@test sol.objective < 1e-4
4452

53+
sol = solve!(
54+
init(prob, ParallelSyncPSOKernel(n_particles; backend = CPU());
55+
sampler = LatinHypercubeSample()),
56+
maxiters = 500)
57+
58+
@test sol.retcode == ReturnCode.Default
59+
4560
sol = solve(prob,
4661
ParallelSyncPSOKernel(n_particles; backend = CPU()),
4762
maxiters = 500)
@@ -66,12 +81,26 @@ using PSOGPU, StaticArrays, SciMLBase, Test, LinearAlgebra, Random, KernelAbstra
6681

6782
@test sol.objective < 1e-4
6883

84+
sol = solve!(
85+
init(prob, ParallelPSOKernel(n_particles; backend = CPU());
86+
sampler = LatinHypercubeSample()),
87+
maxiters = 500)
88+
89+
@test sol.retcode == ReturnCode.Default
90+
6991
sol = solve(prob,
7092
ParallelPSOKernel(n_particles; backend = CPU()),
7193
maxiters = 500)
7294

7395
@test sol.objective < 1e-4
7496

97+
sol = solve!(
98+
init(prob, ParallelSyncPSOKernel(n_particles; backend = CPU());
99+
sampler = LatinHypercubeSample()),
100+
maxiters = 500)
101+
102+
@test sol.retcode == ReturnCode.Default
103+
75104
sol = solve(prob,
76105
ParallelSyncPSOKernel(n_particles; backend = CPU()),
77106
maxiters = 500)
@@ -93,12 +122,24 @@ using PSOGPU, StaticArrays, SciMLBase, Test, LinearAlgebra, Random, KernelAbstra
93122

94123
@test sol.objective < 1e-4
95124

125+
sol = solve!(
126+
init(prob, ParallelPSOKernel(n_particles; backend = CPU());
127+
sampler = LatinHypercubeSample()),
128+
maxiters = 500)
129+
130+
@test sol.retcode == ReturnCode.Default
131+
96132
sol = solve(prob,
97133
ParallelPSOKernel(n_particles; backend = CPU()),
98134
maxiters = 500)
99135

100136
@test sol.objective < 1e-4
101137

138+
sol = solve!(
139+
init(prob, ParallelSyncPSOKernel(n_particles; backend = CPU());
140+
sampler = LatinHypercubeSample()),
141+
maxiters = 500)
142+
102143
sol = solve(prob,
103144
ParallelSyncPSOKernel(n_particles; backend = CPU()),
104145
maxiters = 500)
@@ -138,12 +179,26 @@ end
138179

139180
@test sol.objective < 2e-3
140181

182+
sol = solve!(
183+
init(prob, ParallelPSOKernel(n_particles; backend = CPU());
184+
sampler = LatinHypercubeSample()),
185+
maxiters = 2000)
186+
187+
@test sol.retcode == ReturnCode.Default
188+
141189
sol = solve(prob,
142190
ParallelPSOKernel(n_particles; backend = CPU()),
143191
maxiters = 2000)
144192

145193
@test sol.objective < 2e-2
146194

195+
sol = solve!(
196+
init(prob, ParallelSyncPSOKernel(n_particles; backend = CPU());
197+
sampler = LatinHypercubeSample()),
198+
maxiters = 2000)
199+
200+
@test sol.retcode == ReturnCode.Default
201+
147202
sol = solve(prob,
148203
ParallelSyncPSOKernel(n_particles; backend = CPU()),
149204
maxiters = 2000)
@@ -171,12 +226,26 @@ end
171226

172227
@test sol.objective < 2e-3
173228

229+
sol = solve!(
230+
init(prob, ParallelPSOKernel(n_particles; backend = CPU());
231+
sampler = LatinHypercubeSample()),
232+
maxiters = 1000)
233+
234+
@test sol.retcode == ReturnCode.Default
235+
174236
sol = solve(prob,
175237
ParallelPSOKernel(n_particles; backend = CPU()),
176238
maxiters = 1000)
177239

178240
@test sol.objective < 2e-3
179241

242+
sol = solve!(
243+
init(prob, ParallelSyncPSOKernel(n_particles; backend = CPU());
244+
sampler = LatinHypercubeSample()),
245+
maxiters = 2000)
246+
247+
@test sol.retcode == ReturnCode.Default
248+
180249
sol = solve(prob,
181250
ParallelSyncPSOKernel(n_particles; backend = CPU()),
182251
maxiters = 2000)

0 commit comments

Comments
 (0)