@@ -220,15 +220,17 @@ type OptController{O<:Optimizer, P<:OptimizationProblem}
220220 optimizer:: O # optimization algorithm
221221 problem:: P # opt problem
222222 parameters:: ParamsDictChain
223- runcontrollers:: Vector{Any }
223+ runcontrollers:: Vector{OptRunController{O} }
224224end
225225
226226function OptController {O<:Optimizer, P<:OptimizationProblem} (optimizer:: O , problem:: P ,
227227 params:: ParamsDictChain )
228- OptController {O,P} (optimizer, problem, params, Any [])
228+ OptController {O, P} (optimizer, problem, params, OptRunController{O} [])
229229end
230230
231+ problem (oc:: OptController ) = oc. problem
231232numruns (oc:: OptController ) = length (oc. runcontrollers)
233+ lastrun (oc:: OptController ) = oc. runcontrollers[end ]
232234
233235function update_parameters! {O<:Optimizer, P<:OptimizationProblem} (oc:: OptController{O,P} ,
234236 parameters:: Associative = @compat (Dict {Any,Any} ()))
@@ -239,7 +241,7 @@ function update_parameters!{O<:Optimizer, P<:OptimizationProblem}(oc::OptControl
239241 # been setup.
240242 for k in keys (parameters)
241243 if k ∉ [:MaxTime , :MaxSteps , :MaxFuncEvals , :ShowTrace ]
242- throw (" It is currently not supported to change parameters that can affect the original opt problem or optimizer (here: $(k) )" )
244+ throw (ArgumentError ( " It is currently not supported to change parameters that can affect the original opt problem or optimizer (here: $(k) )" ) )
243245 end
244246 end
245247
@@ -250,7 +252,14 @@ function update_parameters!{O<:Optimizer, P<:OptimizationProblem}(oc::OptControl
250252 end
251253end
252254
253- # Start a new optimization run, possibly with new parameters.
255+ function init_rng! (parameters:: Parameters )
256+ if parameters[:RandomizeRngSeed ]
257+ parameters[:RngSeed ] = rand (1 : 1_000_000 )
258+ srand (parameters[:RngSeed ])
259+ end
260+ end
261+
262+ # Start a new optimization run, possibly with new parameters and report on results.
254263function run! {O<:Optimizer, P<:OptimizationProblem} (oc:: OptController{O,P} )
255264 ctrl = OptRunController (oc. optimizer, oc. problem, oc. parameters)
256265 push! (oc. runcontrollers, ctrl)
@@ -260,49 +269,50 @@ function run!{O<:Optimizer, P<:OptimizationProblem}(oc::OptController{O,P})
260269 init_rng! (oc. parameters)
261270 end
262271
263- run_optimization (ctrl, oc. parameters)
264- end
265-
266- function init_rng! (parameters:: Parameters )
267- if parameters[:RandomizeRngSeed ]
268- parameters[:RngSeed ] = rand (1 : 1_000_000 )
269- srand (parameters[:RngSeed ])
270- end
271- end
272-
273- # Run one optimization run and report on results.
274- function run_optimization (ctrl:: OptRunController , params:: Associative )
275- # Run the optimization. Try to return something sensible
276- # even if interrupted with Ctrl-C.
277272 try
278273 run! (ctrl)
279274 show_report (ctrl)
280275
281- if params [:SaveFitnessTraceToCsv ]
276+ if oc . parameters [:SaveFitnessTraceToCsv ]
282277 write_results (ctrl)
283278 end
284279
285- return make_opt_result (ctrl, params )
280+ return make_opt_results (ctrl, oc )
286281 catch ex
287282 # If it was a ctrl-c interrupt we try to make a result and return it...
288283 if isa (ex, InterruptException)
289- return make_opt_result (ctrl, params)
284+ warn (" Optimization interrupted, recovering intermediate results..." )
285+ return make_opt_results (ctrl, oc)
290286 else
291287 rethrow (ex)
292288 end
293289 end
294290end
295291
296- function make_opt_result (ctrl:: OptRunController , params:: Associative )
297- SingleObjectiveOptimizationResults {Vector{Float64},Float64} (
298- string (params[:Method ]),
299- best_candidate (ctrl),
292+ make_opt_results {O<:Optimizer} (ctrl:: OptRunController{O} , oc:: OptController{O} ) =
293+ SimpleOptimizationResults {fitness_type(problem(ctrl)), Individual} (
294+ string (oc. parameters[:Method ]),
300295 best_fitness (ctrl),
296+ best_candidate (ctrl),
301297 stop_reason (ctrl),
302298 num_steps (ctrl),
303299 start_time (ctrl),
304300 elapsed_time (ctrl),
305- params ,
301+ oc . parameters ,
306302 num_func_evals (ctrl)
307303 )
308- end
304+
305+ make_opt_results {O<:PopulationOptimizer} (ctrl:: OptRunController{O} , oc:: OptController{O} ) =
306+ PopulationOptimizationResults{fitness_type (problem (ctrl)), Individual,
307+ typeof (population (ctrl. optimizer))}(
308+ string (oc. parameters[:Method ]),
309+ best_fitness (ctrl),
310+ best_candidate (ctrl),
311+ stop_reason (ctrl),
312+ num_steps (ctrl),
313+ start_time (ctrl),
314+ elapsed_time (ctrl),
315+ oc. parameters,
316+ num_func_evals (ctrl),
317+ population (ctrl. optimizer)
318+ )
0 commit comments