Skip to content

Commit b593de3

Browse files
committed
Added option to turn off all logging
1 parent 7eb2371 commit b593de3

File tree

5 files changed

+120
-71
lines changed

5 files changed

+120
-71
lines changed

dfols/controller.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,14 @@ def able_to_do_restart(self):
9292

9393

9494
class Controller(object):
95-
def __init__(self, objfun, args, x0, r0, r0_nsamples, xl, xu, npt, rhobeg, rhoend, nf, nx, maxfun, params, scaling_changes):
95+
def __init__(self, objfun, args, x0, r0, r0_nsamples, xl, xu, npt, rhobeg, rhoend, nf, nx, maxfun, params,
96+
scaling_changes, do_logging):
97+
self.do_logging = do_logging
9698
self.objfun = objfun
9799
self.args = args
98100
self.maxfun = maxfun
99101
self.model = Model(npt, x0, r0, xl, xu, r0_nsamples, precondition=params("interpolation.precondition"),
100-
abs_tol = params("model.abs_tol"), rel_tol = params("model.rel_tol"))
102+
abs_tol = params("model.abs_tol"), rel_tol = params("model.rel_tol"), do_logging=do_logging)
101103
self.nf = nf
102104
self.nx = nx
103105
self.rhobeg = rhobeg
@@ -131,7 +133,8 @@ def npt(self):
131133
return self.model.npt()
132134

133135
def initialise_coordinate_directions(self, number_of_samples, num_directions, params):
134-
logging.debug("Initialising with coordinate directions")
136+
if self.do_logging:
137+
logging.debug("Initialising with coordinate directions")
135138
# self.model already has x0 evaluated, so only need to initialise the other points
136139
# num_directions = params("growing.ndirs_initial")
137140
assert self.model.num_pts <= (self.n() + 1) * (self.n() + 2) // 2, "prelim: must have npt <= (n+1)(n+2)/2"
@@ -204,7 +207,8 @@ def initialise_coordinate_directions(self, number_of_samples, num_directions, pa
204207
return None # return & continue
205208

206209
def initialise_random_directions(self, number_of_samples, num_directions, params):
207-
logging.debug("Initialising with random orthogonal directions")
210+
if self.do_logging:
211+
logging.debug("Initialising with random orthogonal directions")
208212
# self.model already has x0 evaluated, so only need to initialise the other points
209213
assert 1 <= num_directions < self.model.num_pts, "Initialisation: must have 1 <= ndirs_initial < npt"
210214

@@ -331,14 +335,12 @@ def trust_region_step(self):
331335
return d, gopt, H, gnew, crvmin
332336

333337
def geometry_step(self, knew, adelt, number_of_samples, params):
334-
logging.debug("Running geometry-fixing step")
338+
if self.do_logging:
339+
logging.debug("Running geometry-fixing step")
335340
try:
336341
c, g = self.model.lagrange_gradient(knew)
337342
# c = 1.0 if knew == self.model.kopt else 0.0 # based at xopt, just like d
338343
# Solve problem: bounds are sl <= xnew <= su, and ||xnew-xopt|| <= adelt
339-
logging.debug("xopt = %s" % str(self.model.xopt()))
340-
logging.debug("sl = %s" % str(self.model.sl))
341-
logging.debug("su = %s" % str(self.model.su))
342344
xnew = trsbox_geometry(self.model.xopt(), c, g, np.minimum(self.model.sl, 0.0), np.maximum(self.model.su, 0.0), adelt)
343345
except LA.LinAlgError:
344346
exit_info = ExitInformation(EXIT_LINALG_ERROR, "Singular matrix encountered in geometry step")
@@ -409,9 +411,11 @@ def evaluate_objective(self, x, number_of_samples, params):
409411
if not incremented_nx:
410412
self.nx += 1
411413
incremented_nx = True
412-
rvec_list[i, :], f_list[i] = eval_least_squares_objective(self.objfun, remove_scaling(x, self.scaling_changes), args=self.args, eval_num=self.nf, pt_num=self.nx,
414+
rvec_list[i, :], f_list[i] = eval_least_squares_objective(self.objfun, remove_scaling(x, self.scaling_changes),
415+
args=self.args, eval_num=self.nf, pt_num=self.nx,
413416
full_x_thresh=params("logging.n_to_print_whole_x_vector"),
414-
check_for_overflow=params("general.check_objfun_for_overflow"))
417+
check_for_overflow=params("general.check_objfun_for_overflow"),
418+
verbose=self.do_logging)
415419
num_samples_run += 1
416420

417421
# Check if the average value was below our threshold
@@ -524,11 +528,13 @@ def terminate_from_slow_iterations(self, current_iter, params):
524528
# Update counter of number of slow iterations
525529
if this_iter_slow:
526530
self.num_slow_iters += 1
527-
logging.info("Slow iteration (%g consecutive so far, max allowed %g)"
528-
% (self.num_slow_iters, params("slow.max_slow_iters")))
531+
if self.do_logging:
532+
logging.info("Slow iteration (%g consecutive so far, max allowed %g)"
533+
% (self.num_slow_iters, params("slow.max_slow_iters")))
529534
else:
530535
self.num_slow_iters = 0
531-
logging.debug("Non-slow iteration")
536+
if self.do_logging:
537+
logging.debug("Non-slow iteration")
532538
return this_iter_slow, self.num_slow_iters >= params("slow.max_slow_iters")
533539

534540
def soft_restart(self, number_of_samples, nruns_so_far, params, x_in_abs_coords_to_save=None, rvec_to_save=None,
@@ -556,7 +562,8 @@ def soft_restart(self, number_of_samples, nruns_so_far, params, x_in_abs_coords_
556562
self.model.save_point(self.model.xopt(abs_coordinates=True), self.model.ropt(),
557563
self.model.nsamples[self.model.kopt], x_in_abs_coords=True)
558564

559-
logging.info("Soft restart [currently, f = %g after %g function evals]" % (self.model.fopt(), self.nf))
565+
if self.do_logging:
566+
logging.info("Soft restart [currently, f = %g after %g function evals]" % (self.model.fopt(), self.nf))
560567
# Resetting method: reset delta and rho, then move the closest 'num_steps' points to xk to improve geometry
561568
# Note: closest points because we are suddenly increasing delta & rho, so we want to encourage spreading out points
562569
self.delta = self.rhobeg
@@ -607,7 +614,8 @@ def soft_restart(self, number_of_samples, nruns_so_far, params, x_in_abs_coords_
607614
for i in range(1, num_samples_run):
608615
self.model.add_new_sample(self.model.npt() - 1, rvec_extra=rvec_list[i, :])
609616

610-
logging.info("Soft restart: added %g new directions, npt is now %g" % (num_pts_to_add, self.model.npt()))
617+
if self.do_logging:
618+
logging.info("Soft restart: added %g new directions, npt is now %g" % (num_pts_to_add, self.model.npt()))
611619

612620
# Otherwise, we are doing a restart
613621
self.last_successful_iter = 0

dfols/model.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
__all__ = ['Model']
4242

4343
class Model(object):
44-
def __init__(self, npt, x0, r0, xl, xu, r0_nsamples, n=None, m=None, abs_tol=1e-12, rel_tol=1e-20, precondition=True):
44+
def __init__(self, npt, x0, r0, xl, xu, r0_nsamples, n=None, m=None, abs_tol=1e-12, rel_tol=1e-20, precondition=True,
45+
do_logging=True):
4546
if n is None:
4647
n = len(x0)
4748
if m is None:
@@ -51,6 +52,7 @@ def __init__(self, npt, x0, r0, xl, xu, r0_nsamples, n=None, m=None, abs_tol=1e-
5152
assert xl.shape == (n,), "xl has wrong shape (got %s, expect (%g,))" % (str(xl.shape), n)
5253
assert xu.shape == (n,), "xu has wrong shape (got %s, expect (%g,))" % (str(xu.shape), n)
5354
assert r0.shape == (m,), "r0 has wrong shape (got %s, expect (%g,))" % (str(r0.shape), m)
55+
self.do_logging = do_logging
5456
self.dim = n
5557
self.resid_dim = m
5658
self.num_pts = npt
@@ -298,7 +300,8 @@ def solve_geom_system(self, rhs):
298300
Qb = np.dot(self.Q.T, col_scale(rhs, self.left_scaling))
299301
return col_scale(LA.solve_triangular(self.R, Qb), self.right_scaling)
300302
else:
301-
logging.warning("model.solve_geom_system not using factorisation")
303+
if self.do_logging:
304+
logging.warning("model.solve_geom_system not using factorisation")
302305
W, left_scaling, right_scaling = self.interpolation_matrix()
303306
return col_scale(LA.lstsq(W, col_scale(rhs * left_scaling))[0], right_scaling)
304307

0 commit comments

Comments
 (0)