42
42
43
43
__all__ = ['solve' ]
44
44
45
+ module_logger = logging .getLogger (__name__ )
46
+
45
47
46
48
# A container for the results of the optimization routine
47
49
class OptimResults (object ):
@@ -149,11 +151,11 @@ def solve_main(objfun, x0, args, xl, xu, npt, rhobeg, rhoend, maxfun, nruns_so_f
149
151
num_directions = npt - 1
150
152
if params ("init.random_initial_directions" ):
151
153
if do_logging :
152
- logging .info ("Initialising (random directions)" )
154
+ module_logger .info ("Initialising (random directions)" )
153
155
exit_info = control .initialise_random_directions (number_of_samples , num_directions , params )
154
156
else :
155
157
if do_logging :
156
- logging .info ("Initialising (coordinate directions)" )
158
+ module_logger .info ("Initialising (coordinate directions)" )
157
159
exit_info = control .initialise_coordinate_directions (number_of_samples , num_directions , params )
158
160
if exit_info is not None :
159
161
x , f , gradmin , hessmin , nsamples = control .model .get_final_results ()
@@ -174,7 +176,7 @@ def solve_main(objfun, x0, args, xl, xu, npt, rhobeg, rhoend, maxfun, nruns_so_f
174
176
# ------------------------------------------
175
177
current_iter = - 1
176
178
if do_logging :
177
- logging .info ("Beginning main loop" )
179
+ module_logger .info ("Beginning main loop" )
178
180
if print_progress :
179
181
print ("{:^5}{:^7}{:^10}{:^10}{:^10}{:^10}{:^7}" .format ("Run" , "Iter" , "Obj" , "Grad" , "Delta" , "rho" , "Evals" ))
180
182
while True :
@@ -231,7 +233,7 @@ def solve_main(objfun, x0, args, xl, xu, npt, rhobeg, rhoend, maxfun, nruns_so_f
231
233
# Trust region step
232
234
d , gopt , H , gnew , crvmin = control .trust_region_step ()
233
235
if do_logging :
234
- logging .debug ("Trust region step is d = " + str (d ))
236
+ module_logger .debug ("Trust region step is d = " + str (d ))
235
237
xnew = control .model .xopt () + d
236
238
dnorm = min (LA .norm (d ), control .delta )
237
239
@@ -250,7 +252,7 @@ def solve_main(objfun, x0, args, xl, xu, npt, rhobeg, rhoend, maxfun, nruns_so_f
250
252
if dnorm < params ("general.safety_step_thresh" ) * control .rho :
251
253
# (start safety step)
252
254
if do_logging :
253
- logging .debug ("Safety step (main phase)" )
255
+ module_logger .debug ("Safety step (main phase)" )
254
256
255
257
if params ("logging.save_diagnostic_info" ):
256
258
diagnostic_info .update_ratio (np .nan )
@@ -295,12 +297,12 @@ def solve_main(objfun, x0, args, xl, xu, npt, rhobeg, rhoend, maxfun, nruns_so_f
295
297
# Reduce rho
296
298
control .reduce_rho (current_iter , params )
297
299
if do_logging :
298
- logging .info ("New rho = %g after %i function evaluations" % (control .rho , control .nf ))
300
+ module_logger .info ("New rho = %g after %i function evaluations" % (control .rho , control .nf ))
299
301
if control .n () < params ("logging.n_to_print_whole_x_vector" ):
300
- logging .debug ("Best so far: f = %.15g at x = " % (control .model .fopt ())
302
+ module_logger .debug ("Best so far: f = %.15g at x = " % (control .model .fopt ())
301
303
+ str (control .model .xopt (abs_coordinates = True )))
302
304
else :
303
- logging .debug ("Best so far: f = %.15g at x = [...]" % (control .model .fopt ()))
305
+ module_logger .debug ("Best so far: f = %.15g at x = [...]" % (control .model .fopt ()))
304
306
continue # next iteration
305
307
else :
306
308
# Quit on rho=rhoend
@@ -340,7 +342,7 @@ def solve_main(objfun, x0, args, xl, xu, npt, rhobeg, rhoend, maxfun, nruns_so_f
340
342
else :
341
343
# (start trust region step)
342
344
if do_logging :
343
- logging .debug ("Standard trust region step" )
345
+ module_logger .debug ("Standard trust region step" )
344
346
345
347
# Add chgJ and delta to restart auto-detect set
346
348
if params ("restarts.use_restarts" ) and params ("restarts.auto_detect" ):
@@ -420,26 +422,26 @@ def solve_main(objfun, x0, args, xl, xu, npt, rhobeg, rhoend, maxfun, nruns_so_f
420
422
421
423
# Update delta
422
424
if do_logging :
423
- logging .debug ("Ratio = %g" % ratio )
425
+ module_logger .debug ("Ratio = %g" % ratio )
424
426
if params ("logging.save_diagnostic_info" ):
425
427
diagnostic_info .update_ratio (ratio )
426
428
diagnostic_info .update_slow_iter (- 1 ) # n/a, unless otherwise update
427
429
if ratio < params ("tr_radius.eta1" ): # ratio < 0.1
428
430
control .delta = min (params ("tr_radius.gamma_dec" ) * control .delta , dnorm )
429
431
if params ("logging.save_diagnostic_info" ):
430
- # logging .info("Last eval was for unsuccessful step (ratio = %g)" % ratio)
432
+ # module_logger .info("Last eval was for unsuccessful step (ratio = %g)" % ratio)
431
433
diagnostic_info .update_iter_type (ITER_ACCEPTABLE_NO_GEOM if ratio > 0.0
432
434
else ITER_UNSUCCESSFUL_NO_GEOM ) # we flag geom update below
433
435
elif ratio <= params ("tr_radius.eta2" ): # 0.1 <= ratio <= 0.7
434
436
control .delta = max (params ("tr_radius.gamma_dec" ) * control .delta , dnorm )
435
437
if params ("logging.save_diagnostic_info" ):
436
- # logging .info("Last eval was for acceptable step (ratio = %g)" % ratio)
438
+ # module_logger .info("Last eval was for acceptable step (ratio = %g)" % ratio)
437
439
diagnostic_info .update_iter_type (ITER_SUCCESSFUL )
438
440
else : # (ratio > eta2 = 0.7)
439
441
control .delta = min (max (params ("tr_radius.gamma_inc" ) * control .delta ,
440
442
params ("tr_radius.gamma_inc_overline" ) * dnorm ), 1.0e10 )
441
443
if params ("logging.save_diagnostic_info" ):
442
- # logging .info("Last eval was for successful step (ratio = %g)" % ratio)
444
+ # module_logger .info("Last eval was for successful step (ratio = %g)" % ratio)
443
445
diagnostic_info .update_iter_type (ITER_VERY_SUCCESSFUL )
444
446
if control .delta <= 1.5 * control .rho : # cap trust region radius at rho
445
447
control .delta = control .rho
@@ -472,7 +474,7 @@ def solve_main(objfun, x0, args, xl, xu, npt, rhobeg, rhoend, maxfun, nruns_so_f
472
474
473
475
# Update point
474
476
if do_logging :
475
- logging .debug ("Updating with knew = %i" % knew )
477
+ module_logger .debug ("Updating with knew = %i" % knew )
476
478
control .model .change_point (knew , xnew , f_list [0 ]) # expect step, not absolute x
477
479
for i in range (1 , num_samples_run ):
478
480
control .model .add_new_sample (knew , f_extra = f_list [i ])
@@ -484,7 +486,7 @@ def solve_main(objfun, x0, args, xl, xu, npt, rhobeg, rhoend, maxfun, nruns_so_f
484
486
diagnostic_info .update_slow_iter (1 if this_iter_slow else 0 )
485
487
if should_terminate :
486
488
if do_logging :
487
- logging .info ("Slow iteration - terminating/restarting" )
489
+ module_logger .info ("Slow iteration - terminating/restarting" )
488
490
if params ("restarts.use_restarts" ) and params ("restarts.use_soft_restarts" ):
489
491
number_of_samples = max (nsamples (control .delta , control .rho , current_iter , nruns_so_far ), 1 )
490
492
exit_info = control .soft_restart (number_of_samples , nruns_so_far , params ,
@@ -539,7 +541,7 @@ def solve_main(objfun, x0, args, xl, xu, npt, rhobeg, rhoend, maxfun, nruns_so_f
539
541
slope2 , intercept2 , r_value2 , p_value2 , std_err2 = slope , intercept , r_value , p_value , std_err
540
542
541
543
if do_logging :
542
- logging .debug ("Iter %g: (slope, intercept, r_value) = (%g, %g, %g)" % (current_iter , slope , intercept , r_value ))
544
+ module_logger .debug ("Iter %g: (slope, intercept, r_value) = (%g, %g, %g)" % (current_iter , slope , intercept , r_value ))
543
545
if min (slope , slope2 ) > params ("restarts.auto_detect.min_chg_model_slope" ) \
544
546
and min (r_value , r_value2 ) > params ("restarts.auto_detect.min_correl" ):
545
547
# increasing trend, with at least some positive correlation
@@ -549,10 +551,10 @@ def solve_main(objfun, x0, args, xl, xu, npt, rhobeg, rhoend, maxfun, nruns_so_f
549
551
550
552
if do_restart and params ("restarts.use_soft_restarts" ):
551
553
if do_logging :
552
- logging .info ("Auto detection: need to do a restart" )
553
- logging .debug ("delta history = %s" % str (restart_auto_detect_delta ))
554
- logging .debug ("chg_grad history = %s" % str (restart_auto_detect_chg_grad ))
555
- logging .debug ("chg_hess history = %s" % str (restart_auto_detect_chg_hess ))
554
+ module_logger .info ("Auto detection: need to do a restart" )
555
+ module_logger .debug ("delta history = %s" % str (restart_auto_detect_delta ))
556
+ module_logger .debug ("chg_grad history = %s" % str (restart_auto_detect_chg_grad ))
557
+ module_logger .debug ("chg_hess history = %s" % str (restart_auto_detect_chg_hess ))
556
558
number_of_samples = max (nsamples (control .delta , control .rho , current_iter , nruns_so_far ), 1 )
557
559
exit_info = control .soft_restart (number_of_samples , nruns_so_far , params ,
558
560
x_in_abs_coords_to_save = None , f_to_save = None ,
@@ -570,15 +572,15 @@ def solve_main(objfun, x0, args, xl, xu, npt, rhobeg, rhoend, maxfun, nruns_so_f
570
572
continue # next iteration
571
573
elif do_restart :
572
574
if do_logging :
573
- logging .info ("Auto detection: need to do a restart" )
575
+ module_logger .info ("Auto detection: need to do a restart" )
574
576
exit_info = ExitInformation (EXIT_AUTO_DETECT_RESTART_WARNING , "Auto-detected restart" )
575
577
nruns_so_far += 1
576
578
break # quit
577
579
# If not doing restart, just continue as below (geom steps, etc.)
578
580
579
581
# Otherwise (ratio < eta1 = 0.1), check & fix geometry
580
582
if do_logging :
581
- logging .debug ("Checking and possibly improving geometry (unsuccessful step)" )
583
+ module_logger .debug ("Checking and possibly improving geometry (unsuccessful step)" )
582
584
distsq = max ((2.0 * control .delta ) ** 2 , (10.0 * control .rho ) ** 2 )
583
585
update_delta = False
584
586
number_of_samples = max (nsamples (control .delta , control .rho , current_iter , nruns_so_far ), 1 )
@@ -624,12 +626,12 @@ def solve_main(objfun, x0, args, xl, xu, npt, rhobeg, rhoend, maxfun, nruns_so_f
624
626
# Reduce rho
625
627
control .reduce_rho (current_iter , params )
626
628
if do_logging :
627
- logging .info ("New rho = %g after %i function evaluations" % (control .rho , control .nf ))
629
+ module_logger .info ("New rho = %g after %i function evaluations" % (control .rho , control .nf ))
628
630
if control .n () < params ("logging.n_to_print_whole_x_vector" ):
629
- logging .debug ("Best so far: f = %.15g at x = " % (control .model .fopt ())
631
+ module_logger .debug ("Best so far: f = %.15g at x = " % (control .model .fopt ())
630
632
+ str (control .model .xopt (abs_coordinates = True )))
631
633
else :
632
- logging .debug ("Best so far: f = %.15g at x = [...]" % (control .model .fopt ()))
634
+ module_logger .debug ("Best so far: f = %.15g at x = [...]" % (control .model .fopt ()))
633
635
continue # next iteration
634
636
else :
635
637
# Quit on rho=rhoend
@@ -658,8 +660,8 @@ def solve_main(objfun, x0, args, xl, xu, npt, rhobeg, rhoend, maxfun, nruns_so_f
658
660
# Quit & return the important information
659
661
x , f , gradmin , hessmin , nsamples = control .model .get_final_results ()
660
662
if do_logging :
661
- logging .debug ("At return from solver, number of function evals = %i" % nf )
662
- logging .debug ("Smallest objective value = %.15g at x = " % f + str (x ))
663
+ module_logger .debug ("At return from solver, number of function evals = %i" % nf )
664
+ module_logger .debug ("Smallest objective value = %.15g at x = " % f + str (x ))
663
665
return x , f , gradmin , hessmin , nsamples , control .nf , control .nx , nruns_so_far , exit_info , diagnostic_info
664
666
665
667
@@ -823,7 +825,7 @@ def solve(objfun, x0, args=(), bounds=None, npt=None, rhobeg=None, rhoend=1e-8,
823
825
if not reduction_last_run :
824
826
_rhobeg = _rhobeg * params ("restarts.rhobeg_scale_after_unsuccessful_restart" )
825
827
826
- logging .info ("Restarting from finish point (f = %g) after %g function evals; using rhobeg = %g and rhoend = %g"
828
+ module_logger .info ("Restarting from finish point (f = %g) after %g function evals; using rhobeg = %g and rhoend = %g"
827
829
% (fmin , nf , _rhobeg , _rhoend ))
828
830
if params ("restarts.hard.use_old_fk" ):
829
831
xmin2 , fmin2 , gradmin2 , hessmin2 , nsamples2 , nf , nx , nruns , exit_info , diagnostic_info = \
@@ -835,7 +837,7 @@ def solve(objfun, x0, args=(), bounds=None, npt=None, rhobeg=None, rhoend=1e-8,
835
837
diagnostic_info , scaling_changes , do_logging = do_logging , print_progress = print_progress )
836
838
837
839
if fmin2 < fmin or np .isnan (fmin ):
838
- logging .info ("Successful run with new f = %s compared to old f = %s" % (fmin2 , fmin ))
840
+ module_logger .info ("Successful run with new f = %s compared to old f = %s" % (fmin2 , fmin ))
839
841
last_successful_run = nruns
840
842
(xmin , fmin , nsamples_min ) = (xmin2 , fmin2 , nsamples2 )
841
843
if gradmin2 is not None : # may be None if finished during setup phase, in which case just use old gradient
@@ -844,7 +846,7 @@ def solve(objfun, x0, args=(), bounds=None, npt=None, rhobeg=None, rhoend=1e-8,
844
846
hessmin = hessmin2
845
847
reduction_last_run = True
846
848
else :
847
- logging .info ("Unsuccessful run with new f = %s compared to old f = %s" % (fmin2 , fmin ))
849
+ module_logger .info ("Unsuccessful run with new f = %s compared to old f = %s" % (fmin2 , fmin ))
848
850
reduction_last_run = False
849
851
total_unsuccessful_restarts += 1
850
852
@@ -868,7 +870,7 @@ def solve(objfun, x0, args=(), bounds=None, npt=None, rhobeg=None, rhoend=1e-8,
868
870
results .diagnostic_info = df
869
871
870
872
if do_logging :
871
- logging .info ("Did a total of %g run(s)" % nruns )
873
+ module_logger .info ("Did a total of %g run(s)" % nruns )
872
874
873
875
return results
874
876
0 commit comments