@@ -320,7 +320,7 @@ function optimize(f, l::Number, u::Number, initial_x::AbstractArray; autodiff =
320320 )
321321end
322322
323- optimize (
323+ function optimize (
324324 f,
325325 l:: Number ,
326326 u:: Number ,
@@ -329,16 +329,19 @@ optimize(
329329 opt:: Options = Options ();
330330 inplace:: Bool = true ,
331331 autodiff = :finite ,
332- ) = optimize (
333- f,
334- Fill (T (l), size (initial_x)... ),
335- Fill (T (u), size (initial_x)... ),
336- initial_x,
337- mo,
338- opt;
339- inplace,
340- autodiff,
341332)
333+ T = eltype (initial_x)
334+ optimize (
335+ f,
336+ Fill (T (l), size (initial_x)... ),
337+ Fill (T (u), size (initial_x)... ),
338+ initial_x,
339+ mo,
340+ opt;
341+ inplace,
342+ autodiff,
343+ )
344+ end
342345function optimize (
343346 f,
344347 l:: AbstractArray ,
@@ -414,7 +417,7 @@ function optimize(
414417 autodiff = :finite ,
415418)
416419 T= eltype (initial_x)
417- optimize (f, g, Fill (T (l), size (initial_x)... ), T .(u), initial_x, opt, inplace, autodiff)
420+ optimize (f, g, Fill (T (l), size (initial_x)... ), T .(u), initial_x, opt; inplace, autodiff)
418421end
419422
420423function optimize (
@@ -503,102 +506,161 @@ function optimize(
503506 fval_all = Vector {Vector{T}} ()
504507
505508 # Count the total number of outer iterations
506- iteration = 0
509+ iteration = 1
507510
508511 # define the function (dfbox) to optimize by the inner optimizer
509512
510513 xold = copy (x)
511- converged = false
512- local results, fval0, _x_converged, _f_converged, _g_converged
513- first = true
514- f_increased, stopped_by_time_limit, stopped_by_callback = false , false , false
515- stopped = false
516514 _time = time ()
515+ fval0 = dfbox. obj. F
516+
517+ # Optimize with current setting of mu
518+ if show_trace > 0
519+ header_string = " Fminbox iteration $iteration "
520+ println (header_string)
521+ println (" -" ^ length (header_string))
522+ print (" Calling inner optimizer with mu = " )
523+ show (IOContext (stdout , :compact => true ), " text/plain" , dfbox. mu)
524+ println (" \n " )
525+ println (" (numbers below include barrier contribution)" )
526+ end
517527
518- while ! converged && ! stopped && iteration < outer_iterations
519- fval0 = dfbox. obj. F
520- # Increment the number of steps we've had to perform
521- iteration += 1
522-
523- copyto! (xold, x)
524- # Optimize with current setting of mu
525- if show_trace > 0
526- header_string = " Fminbox iteration $iteration "
527- println (header_string)
528- println (" -" ^ length (header_string))
529- print (" Calling inner optimizer with mu = " )
530- show (IOContext (stdout , :compact => true ), " text/plain" , dfbox. mu)
531- println (" \n " )
532- println (" (numbers below include barrier contribution)" )
533- end
528+ # we need to update the +mu*barrier_grad part. Since we're using the
529+ # value_gradient! not !! as in initial_state, we won't make a superfluous
530+ # evaluation
531+ if ! (F. method isa NelderMead)
532+ value_gradient! (dfbox, x)
533+ reset! (_optimizer, state, dfbox, x)
534+ else
535+ value! (dfbox, x)
536+ end
537+ results = optimize (dfbox, x, _optimizer, options, state)
538+ stopped_by_callback = results. stopped_by. callback
539+ dfbox. obj. f_calls[1 ] = 0
540+ if hasfield (typeof (dfbox. obj), :df_calls )
541+ dfbox. obj. df_calls[1 ] = 0
542+ end
543+ if hasfield (typeof (dfbox. obj), :h_calls )
544+ dfbox. obj. h_calls[1 ] = 0
545+ end
546+ copyto! (x, minimizer (results))
547+ boxdist = Base. minimum (((xi, li, ui),) -> min (xi - li, ui - xi), zip (x, l, u)) # Base.minimum !== minimum
548+ if show_trace > 0
549+ println ()
550+ println (" Exiting inner optimizer with x = " , x)
551+ print (" Current distance to box: " )
552+ show (IOContext (stdout , :compact => true ), " text/plain" , boxdist)
553+ println ()
554+ println (" Decreasing barrier term μ.\n " )
555+ end
534556
535- # we need to update the +mu*barrier_grad part. Since we're using the
536- # value_gradient! not !! as in initial_state, we won't make a superfluous
537- # evaluation
557+ # Decrease mu
558+ dfbox. mu *= T (F. mufactor)
559+ # Test for convergence
560+ g = x .- min .(max .(x .- gradient (dfbox. obj), l), u)
561+ _x_converged, _f_converged, _g_converged, f_increased =
562+ assess_convergence (
563+ x,
564+ xold,
565+ minimum (results),
566+ fval0,
567+ g,
568+ options. outer_x_abstol,
569+ options. outer_x_reltol,
570+ options. outer_f_abstol,
571+ options. outer_f_reltol,
572+ options. outer_g_abstol,
573+ )
574+ converged =
575+ _x_converged ||
576+ _f_converged ||
577+ _g_converged ||
578+ stopped_by_callback
579+ _time = time ()
580+ stopped_by_time_limit = _time - t0 > options. time_limit
581+ stopped = stopped_by_time_limit
538582
539- if ! (F. method isa NelderMead)
540- value_gradient! (dfbox, x)
541- else
542- value! (dfbox, x)
543- end
544- if ! (F. method isa NelderMead && iteration == 1 )
583+ if f_increased && ! allow_outer_f_increases
584+ @warn (" f(x) increased: stopping optimization" )
585+ else
586+ while ! converged && ! stopped && iteration < outer_iterations
587+ fval0 = dfbox. obj. F
588+ # Increment the number of steps we've had to perform
589+ iteration += 1
590+
591+ copyto! (xold, x)
592+ # Optimize with current setting of mu
593+ if show_trace > 0
594+ header_string = " Fminbox iteration $iteration "
595+ println (header_string)
596+ println (" -" ^ length (header_string))
597+ print (" Calling inner optimizer with mu = " )
598+ show (IOContext (stdout , :compact => true ), " text/plain" , dfbox. mu)
599+ println (" \n " )
600+ println (" (numbers below include barrier contribution)" )
601+ end
602+
603+ # we need to update the +mu*barrier_grad part. Since we're using the
604+ # value_gradient! not !! as in initial_state, we won't make a superfluous
605+ # evaluation
606+
607+ if ! (F. method isa NelderMead)
608+ value_gradient! (dfbox, x)
609+ else
610+ value! (dfbox, x)
611+ end
545612 reset! (_optimizer, state, dfbox, x)
546- end
547- resultsnew = optimize (dfbox, x, _optimizer, options, state)
548- stopped_by_callback = resultsnew. stopped_by. callback
549- if first
550- results = resultsnew
551- first = false
552- else
613+ resultsnew = optimize (dfbox, x, _optimizer, options, state)
614+ stopped_by_callback = resultsnew. stopped_by. callback
553615 append! (results, resultsnew)
616+ dfbox. obj. f_calls[1 ] = 0
617+ if hasfield (typeof (dfbox. obj), :df_calls )
618+ dfbox. obj. df_calls[1 ] = 0
619+ end
620+ if hasfield (typeof (dfbox. obj), :h_calls )
621+ dfbox. obj. h_calls[1 ] = 0
622+ end
623+ copyto! (x, minimizer (results))
624+ boxdist = Base. minimum (((xi, li, ui),) -> min (xi - li, ui - xi), zip (x, l, u)) # Base.minimum !== minimum
625+ if show_trace > 0
626+ println ()
627+ println (" Exiting inner optimizer with x = " , x)
628+ print (" Current distance to box: " )
629+ show (IOContext (stdout , :compact => true ), " text/plain" , boxdist)
630+ println ()
631+ println (" Decreasing barrier term μ.\n " )
632+ end
633+
634+ # Decrease mu
635+ dfbox. mu *= T (F. mufactor)
636+ # Test for convergence
637+ g = x .- min .(max .(x .- gradient (dfbox. obj), l), u)
638+ _x_converged, _f_converged, _g_converged, f_increased =
639+ assess_convergence (
640+ x,
641+ xold,
642+ minimum (results),
643+ fval0,
644+ g,
645+ options. outer_x_abstol,
646+ options. outer_x_reltol,
647+ options. outer_f_abstol,
648+ options. outer_f_reltol,
649+ options. outer_g_abstol,
650+ )
651+ converged =
652+ _x_converged ||
653+ _f_converged ||
654+ _g_converged ||
655+ stopped_by_callback
656+ if f_increased && ! allow_outer_f_increases
657+ @warn (" f(x) increased: stopping optimization" )
658+ break
659+ end
660+ _time = time ()
661+ stopped_by_time_limit = _time - t0 > options. time_limit
662+ stopped = stopped_by_time_limit
554663 end
555- dfbox. obj. f_calls[1 ] = 0
556- if hasfield (typeof (dfbox. obj), :df_calls )
557- dfbox. obj. df_calls[1 ] = 0
558- end
559- if hasfield (typeof (dfbox. obj), :h_calls )
560- dfbox. obj. h_calls[1 ] = 0
561- end
562- copyto! (x, minimizer (results))
563- boxdist = Base. minimum (((xi, li, ui),) -> min (xi - li, ui - xi), zip (x, l, u)) # Base.minimum !== minimum
564- if show_trace > 0
565- println ()
566- println (" Exiting inner optimizer with x = " , x)
567- print (" Current distance to box: " )
568- show (IOContext (stdout , :compact => true ), " text/plain" , boxdist)
569- println ()
570- println (" Decreasing barrier term μ.\n " )
571- end
572-
573- # Decrease mu
574- dfbox. mu *= T (F. mufactor)
575- # Test for convergence
576- g = x .- min .(max .(x .- gradient (dfbox. obj), l), u)
577- _x_converged, _f_converged, _g_converged, f_increased =
578- assess_convergence (
579- x,
580- xold,
581- minimum (results),
582- fval0,
583- g,
584- options. outer_x_abstol,
585- options. outer_x_reltol,
586- options. outer_f_abstol,
587- options. outer_f_reltol,
588- options. outer_g_abstol,
589- )
590- converged =
591- _x_converged ||
592- _f_converged ||
593- _g_converged ||
594- stopped_by_callback
595- if f_increased && ! allow_outer_f_increases
596- @warn (" f(x) increased: stopping optimization" )
597- break
598- end
599- _time = time ()
600- stopped_by_time_limit = _time - t0 > options. time_limit ? true : false
601- stopped = stopped_by_time_limit
602664 end
603665
604666 stopped_by = (
0 commit comments