Skip to content

Commit 5ed8cd9

Browse files
committed
fixed shutdown methods and added test shutting down optimization from callback function
1 parent 0dd2182 commit 5ed8cd9

File tree

4 files changed

+287
-5
lines changed

4 files changed

+287
-5
lines changed

Manifest.toml

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
# This file is machine-generated - editing it directly is not advised
2+
3+
[[Arpack]]
4+
deps = ["Arpack_jll", "Libdl", "LinearAlgebra"]
5+
git-tree-sha1 = "2ff92b71ba1747c5fdd541f8fc87736d82f40ec9"
6+
uuid = "7d9fca2a-8960-54d3-9f78-7d1dccf2cb97"
7+
version = "0.4.0"
8+
9+
[[Arpack_jll]]
10+
deps = ["Libdl", "OpenBLAS_jll", "Pkg"]
11+
git-tree-sha1 = "e214a9b9bd1b4e1b4f15b22c0994862b66af7ff7"
12+
uuid = "68821587-b530-5797-8361-c406ea357684"
13+
version = "3.5.0+3"
14+
15+
[[Base64]]
16+
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
17+
18+
[[CPUTime]]
19+
git-tree-sha1 = "2dcc50ea6a0a1ef6440d6eecd0fe3813e5671f45"
20+
uuid = "a9c8d775-2e2e-55fc-8582-045d282d599e"
21+
version = "1.0.0"
22+
23+
[[Compat]]
24+
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
25+
git-tree-sha1 = "0198d18b28c093bef39872a22f1a897218a925f5"
26+
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
27+
version = "3.8.0"
28+
29+
[[CompilerSupportLibraries_jll]]
30+
deps = ["Libdl", "Pkg"]
31+
git-tree-sha1 = "7c4f882c41faa72118841185afc58a2eb00ef612"
32+
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"
33+
version = "0.3.3+0"
34+
35+
[[DataAPI]]
36+
git-tree-sha1 = "674b67f344687a88310213ddfa8a2b3c76cc4252"
37+
uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
38+
version = "1.1.0"
39+
40+
[[DataStructures]]
41+
deps = ["InteractiveUtils", "OrderedCollections"]
42+
git-tree-sha1 = "73eb18320fe3ba58790c8b8f6f89420f0a622773"
43+
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
44+
version = "0.17.11"
45+
46+
[[Dates]]
47+
deps = ["Printf"]
48+
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
49+
50+
[[DelimitedFiles]]
51+
deps = ["Mmap"]
52+
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"
53+
54+
[[Distributed]]
55+
deps = ["Random", "Serialization", "Sockets"]
56+
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"
57+
58+
[[Distributions]]
59+
deps = ["FillArrays", "LinearAlgebra", "PDMats", "Printf", "QuadGK", "Random", "SpecialFunctions", "Statistics", "StatsBase", "StatsFuns"]
60+
git-tree-sha1 = "c4ed10355637fcb0725dc6a27060f74df24f13cd"
61+
uuid = "31c24e10-a181-5473-b8eb-7969acd0382f"
62+
version = "0.23.2"
63+
64+
[[FillArrays]]
65+
deps = ["LinearAlgebra", "Random", "SparseArrays"]
66+
git-tree-sha1 = "51cc2f9bc4eb9c6c0e81ec2f779d1085583cc956"
67+
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
68+
version = "0.8.7"
69+
70+
[[HTTP]]
71+
deps = ["Base64", "Dates", "IniFile", "MbedTLS", "Sockets"]
72+
git-tree-sha1 = "cd60d9a575d3b70c026d7e714212fd4ecf86b4bb"
73+
uuid = "cd3eb016-35fb-5094-929b-558a96fad6f3"
74+
version = "0.8.13"
75+
76+
[[IniFile]]
77+
deps = ["Test"]
78+
git-tree-sha1 = "098e4d2c533924c921f9f9847274f2ad89e018b8"
79+
uuid = "83e8ac13-25f8-5344-8a64-a9f2b223428f"
80+
version = "0.5.0"
81+
82+
[[InteractiveUtils]]
83+
deps = ["Markdown"]
84+
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
85+
86+
[[JSON]]
87+
deps = ["Dates", "Mmap", "Parsers", "Unicode"]
88+
git-tree-sha1 = "b34d7cef7b337321e97d22242c3c2b91f476748e"
89+
uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
90+
version = "0.21.0"
91+
92+
[[LibGit2]]
93+
deps = ["Printf"]
94+
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"
95+
96+
[[Libdl]]
97+
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
98+
99+
[[LinearAlgebra]]
100+
deps = ["Libdl"]
101+
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
102+
103+
[[Logging]]
104+
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
105+
106+
[[Markdown]]
107+
deps = ["Base64"]
108+
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
109+
110+
[[MbedTLS]]
111+
deps = ["Dates", "MbedTLS_jll", "Random", "Sockets"]
112+
git-tree-sha1 = "a9e2221f06b42f56052f43ad7edecb01d0ef5ab4"
113+
uuid = "739be429-bea8-5141-9913-cc70e7f3736d"
114+
version = "1.0.1"
115+
116+
[[MbedTLS_jll]]
117+
deps = ["Libdl", "Pkg"]
118+
git-tree-sha1 = "066a4467008745eed36dad973ceb66405785a621"
119+
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"
120+
version = "2.16.0+1"
121+
122+
[[Missings]]
123+
deps = ["DataAPI"]
124+
git-tree-sha1 = "de0a5ce9e5289f27df672ffabef4d1e5861247d5"
125+
uuid = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"
126+
version = "0.4.3"
127+
128+
[[Mmap]]
129+
uuid = "a63ad114-7e13-5084-954f-fe012c677804"
130+
131+
[[OpenBLAS_jll]]
132+
deps = ["CompilerSupportLibraries_jll", "Libdl", "Pkg"]
133+
git-tree-sha1 = "2ee3e636e94b9fd95fa8364d5cba2e20dae16609"
134+
uuid = "4536629a-c528-5b80-bd46-f80d51c5b363"
135+
version = "0.3.9+2"
136+
137+
[[OpenSpecFun_jll]]
138+
deps = ["CompilerSupportLibraries_jll", "Libdl", "Pkg"]
139+
git-tree-sha1 = "d51c416559217d974a1113522d5919235ae67a87"
140+
uuid = "efe28fd5-8261-553b-a9e1-b2916fc3738e"
141+
version = "0.5.3+3"
142+
143+
[[OrderedCollections]]
144+
deps = ["Random", "Serialization", "Test"]
145+
git-tree-sha1 = "c4c13474d23c60d20a67b217f1d7f22a40edf8f1"
146+
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
147+
version = "1.1.0"
148+
149+
[[PDMats]]
150+
deps = ["Arpack", "LinearAlgebra", "SparseArrays", "SuiteSparse", "Test"]
151+
git-tree-sha1 = "2fc6f50ddd959e462f0a2dbc802ddf2a539c6e35"
152+
uuid = "90014a1f-27ba-587c-ab20-58faa44d9150"
153+
version = "0.9.12"
154+
155+
[[Parsers]]
156+
deps = ["Dates", "Test"]
157+
git-tree-sha1 = "75d07cb840c300084634b4991761886d0d762724"
158+
uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
159+
version = "1.0.1"
160+
161+
[[Pkg]]
162+
deps = ["Dates", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "UUIDs"]
163+
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
164+
165+
[[Printf]]
166+
deps = ["Unicode"]
167+
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
168+
169+
[[QuadGK]]
170+
deps = ["DataStructures", "LinearAlgebra"]
171+
git-tree-sha1 = "dc84e810393cfc6294248c9032a9cdacc14a3db4"
172+
uuid = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
173+
version = "2.3.1"
174+
175+
[[REPL]]
176+
deps = ["InteractiveUtils", "Markdown", "Sockets"]
177+
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
178+
179+
[[Random]]
180+
deps = ["Serialization"]
181+
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
182+
183+
[[Rmath]]
184+
deps = ["Random", "Rmath_jll"]
185+
git-tree-sha1 = "86c5647b565873641538d8f812c04e4c9dbeb370"
186+
uuid = "79098fc4-a85e-5d69-aa6a-4863f24498fa"
187+
version = "0.6.1"
188+
189+
[[Rmath_jll]]
190+
deps = ["Libdl", "Pkg"]
191+
git-tree-sha1 = "1660f8fefbf5ab9c67560513131d4e933012fc4b"
192+
uuid = "f50d1b31-88e8-58de-be2c-1cc44531875f"
193+
version = "0.2.2+0"
194+
195+
[[SHA]]
196+
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
197+
198+
[[Serialization]]
199+
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
200+
201+
[[SharedArrays]]
202+
deps = ["Distributed", "Mmap", "Random", "Serialization"]
203+
uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383"
204+
205+
[[Sockets]]
206+
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"
207+
208+
[[SortingAlgorithms]]
209+
deps = ["DataStructures", "Random", "Test"]
210+
git-tree-sha1 = "03f5898c9959f8115e30bc7226ada7d0df554ddd"
211+
uuid = "a2af1166-a08f-5f64-846c-94a0d3cef48c"
212+
version = "0.3.1"
213+
214+
[[SparseArrays]]
215+
deps = ["LinearAlgebra", "Random"]
216+
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
217+
218+
[[SpatialIndexing]]
219+
deps = ["Printf", "Random", "Test"]
220+
git-tree-sha1 = "7bc6fc5c363aae476ec2c8bc527aef104b90c779"
221+
uuid = "d4ead438-fe20-5cc5-a293-4fd39a41b74c"
222+
version = "0.1.2"
223+
224+
[[SpecialFunctions]]
225+
deps = ["OpenSpecFun_jll"]
226+
git-tree-sha1 = "e19b98acb182567bcb7b75bb5d9eedf3a3b5ec6c"
227+
uuid = "276daf66-3868-5448-9aa4-cd146d93841b"
228+
version = "0.10.0"
229+
230+
[[Statistics]]
231+
deps = ["LinearAlgebra", "SparseArrays"]
232+
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
233+
234+
[[StatsBase]]
235+
deps = ["DataAPI", "DataStructures", "LinearAlgebra", "Missings", "Printf", "Random", "SortingAlgorithms", "SparseArrays", "Statistics"]
236+
git-tree-sha1 = "a6102b1f364befdb05746f386b67c6b7e3262c45"
237+
uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
238+
version = "0.33.0"
239+
240+
[[StatsFuns]]
241+
deps = ["Rmath", "SpecialFunctions"]
242+
git-tree-sha1 = "f290ddd5fdedeadd10e961eb3f4d3340f09d030a"
243+
uuid = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
244+
version = "0.9.4"
245+
246+
[[SuiteSparse]]
247+
deps = ["Libdl", "LinearAlgebra", "Serialization", "SparseArrays"]
248+
uuid = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9"
249+
250+
[[Test]]
251+
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
252+
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
253+
254+
[[UUIDs]]
255+
deps = ["Random", "SHA"]
256+
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
257+
258+
[[Unicode]]
259+
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

src/opt_controller.jl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ mutable struct OptRunController{O<:Optimizer, E<:Evaluator}
4242
max_steps_without_fevals::Int # stop optimization if no func evals in this many steps (indicates a converged/degenerate search)
4343
max_steps_without_progress::Int # stop optimization if no improvement in this many steps (indicates a converged/degenerate search)
4444
max_time::Float64 # maximal time, 0 to ignore
45-
45+
4646
min_delta_fitness_tol::Float64 # minimal difference between current best fitness and second-best one
4747
fitness_tol::Float64 # minimal difference between current best fitness and the known optmimum
4848

@@ -61,6 +61,7 @@ mutable struct OptRunController{O<:Optimizer, E<:Evaluator}
6161
last_callback_time::Float64 # last time callback function was called
6262

6363
stop_reason::String # the reason for algorithm termination, empty if it's not terminated
64+
shutdown::Bool # shutdown optimization run iff true
6465
end
6566

6667
"""
@@ -90,7 +91,7 @@ function OptRunController(optimizer::O, evaluator::E, params) where {O<:Optimize
9091
:CallbackFunction, :CallbackInterval,
9192
:MaxSteps, :MaxFuncEvals, :MaxNumStepsWithoutFuncEvals, :MaxStepsWithoutProgress, :MaxTime,
9293
:MinDeltaFitnessTolerance, :FitnessTolerance]]...,
93-
0, 0, 0, 0, 0, 0, 0.0, 0.0, 0.0, -1.0, "")
94+
0, 0, 0, 0, 0, 0, 0.0, 0.0, 0.0, -1.0, "", false)
9495
end
9596

9697
# stepping optimizer has it's own evaluator, get a reference
@@ -188,6 +189,10 @@ function elapsed_time(ctrl::OptRunController)
188189
end
189190

190191
function check_stop_condition(ctrl::OptRunController)
192+
if ctrl.shutdown
193+
return "Run explicitly stopped via shutdown method"
194+
end
195+
191196
if ctrl.max_time > 0 && elapsed_time(ctrl) > ctrl.max_time
192197
return "Max time ($(ctrl.max_time) s) reached"
193198
end
@@ -270,12 +275,17 @@ setup_optimizer!(ctrl::OptRunController{<:SteppingOptimizer}) =
270275
setup_optimizer!(ctrl::OptRunController{<:AskTellOptimizer}) =
271276
setup!(ctrl.optimizer, ctrl.evaluator)
272277

273-
shutdown_optimizer!(ctrl::OptRunController{<:SteppingOptimizer}) =
278+
shutdown!(ctrl::OptRunController) = ctrl.shutdown = true
279+
280+
function shutdown_optimizer!(ctrl::OptRunController{<:SteppingOptimizer})
274281
shutdown!(ctrl.optimizer)
282+
shutdown!(ctrl)
283+
end
275284

276285
function shutdown_optimizer!(ctrl::OptRunController{<:AskTellOptimizer})
277286
shutdown!(ctrl.optimizer)
278287
shutdown!(ctrl.evaluator)
288+
shutdown!(ctrl)
279289
end
280290

281291
"""

src/optimizer.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ function setup!(o::SteppingOptimizer)
9191
end
9292

9393
function setup!(o::AskTellOptimizer, evaluator::Evaluator)
94-
# Do nothing, override if you need to setup prior to the optimization loop
94+
# Do nothing, override if optimizer needs specific setup prior to the optimization loop
9595
end
9696

9797
function shutdown!(o::Optimizer)
98-
# Do nothing, override if you need to setup prior to the optimization loop
98+
# Do nothing, override if optimizer needs specific shutdown
9999
end
100100

101101
shutdown!(o::SteppingOptimizer) =

test/test_toplevel_bboptimize.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,17 @@
234234
res = bboptimize(CallableObjective1(); SearchRange = [(-2.0, 3.0), (-4.0, 5.0)])
235235
@test best_candidate(res) zeros(2) atol = 1e-5
236236
end
237+
238+
@testset "explicit termination via shutdown method" begin
239+
cb = function (oc)
240+
if best_fitness(oc) < 50.0
241+
BlackBoxOptim.shutdown_optimizer!(oc)
242+
end
243+
end
244+
res = bboptimize(rosenbrock; SearchRange = (-5.0, 5.0), NumDimensions = 2,
245+
PopulationSize = 10, TraceMode = :silent, MaxFuncEvals = 1000,
246+
CallbackFunction = cb, CallbackInterval = 0.0)
247+
reason = BlackBoxOptim.stop_reason(res)
248+
@test occursin("stopped", reason) && occursin("shutdown method", reason)
249+
end
237250
end

0 commit comments

Comments
 (0)