Skip to content

Commit 3d9674b

Browse files
committed
Optionally turn off logging or turn on per-iteration info, drop random seed from example scripts (no longer needed by default)
1 parent 840e0d7 commit 3d9674b

File tree

6 files changed

+87
-59
lines changed

6 files changed

+87
-59
lines changed

examples/global_opt_demo.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ def freudenstein_roth(x):
2323
lower = np.array([-30.0, -30.0])
2424
upper = np.array([30.0, 30.0])
2525

26-
# Set random seed (for reproducibility)
27-
np.random.seed(0)
28-
2926
print("First run - search for local minimum only")
3027
print("")
3128
soln = pybobyqa.solve(freudenstein_roth, x0, maxfun=500, bounds=(lower, upper))

examples/rosenbrock_basic.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ def rosenbrock(x):
1010
# Define the starting point
1111
x0 = np.array([-1.2, 1.0])
1212

13-
# Set random seed (for reproducibility)
14-
np.random.seed(0)
15-
1613
# For optional extra output details
1714
# import logging
1815
# logging.basicConfig(level=logging.INFO, format='%(message)s')

examples/rosenbrock_bounds.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ def rosenbrock(x):
1010
# Define the starting point
1111
x0 = np.array([-1.2, 1.0])
1212

13-
# Set random seed (for reproducibility)
14-
np.random.seed(0)
15-
1613
# Define bound constraints (lower <= x <= upper)
1714
lower = np.array([-10.0, -10.0])
1815
upper = np.array([0.9, 0.85])

pybobyqa/controller.py

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

9393

9494
class Controller(object):
95-
def __init__(self, objfun, x0, args, f0, f0_nsamples, xl, xu, npt, rhobeg, rhoend, nf, nx, maxfun, params, scaling_changes):
95+
def __init__(self, objfun, x0, args, f0, f0_nsamples, xl, xu, npt, rhobeg, rhoend, nf, nx, maxfun, params, scaling_changes, do_logging=True):
9696
self.objfun = objfun
9797
self.maxfun = maxfun
9898
self.args = args
9999
self.model = Model(npt, x0, f0, xl, xu, f0_nsamples, abs_tol=params("model.abs_tol"),
100-
precondition=params("interpolation.precondition"))
100+
precondition=params("interpolation.precondition"), do_logging=do_logging)
101101
self.nf = nf
102102
self.nx = nx
103103
self.rhobeg = rhobeg
@@ -121,6 +121,7 @@ def __init__(self, objfun, x0, args, f0, f0_nsamples, xl, xu, npt, rhobeg, rhoen
121121
self.total_unsuccessful_restarts = 0
122122
self.last_run_fopt = f0
123123
self.scaling_changes = scaling_changes
124+
self.do_logging = do_logging
124125

125126
def n(self):
126127
return self.model.n()
@@ -129,7 +130,8 @@ def npt(self):
129130
return self.model.npt()
130131

131132
def initialise_coordinate_directions(self, number_of_samples, num_directions, params):
132-
logging.debug("Initialising with coordinate directions")
133+
if self.do_logging:
134+
logging.debug("Initialising with coordinate directions")
133135
# self.model already has x0 evaluated, so only need to initialise the other points
134136
# num_directions = params("growing.ndirs_initial")
135137
assert self.model.num_pts <= (self.n() + 1) * (self.n() + 2) // 2, "prelim: must have npt <= (n+1)(n+2)/2"
@@ -202,7 +204,8 @@ def initialise_coordinate_directions(self, number_of_samples, num_directions, pa
202204
return None # return & continue
203205

204206
def initialise_random_directions(self, number_of_samples, num_directions, params):
205-
logging.debug("Initialising with random orthogonal directions")
207+
if self.do_logging:
208+
logging.debug("Initialising with random orthogonal directions")
206209
# self.model already has x0 evaluated, so only need to initialise the other points
207210
# num_directions = params("growing.ndirs_initial")
208211
assert 1 <= num_directions < self.model.num_pts, "Initialisation: must have 1 <= ndirs_initial < npt"
@@ -270,7 +273,8 @@ def trust_region_step(self):
270273
return d, gopt, H, gnew, crvmin
271274

272275
def geometry_step(self, knew, adelt, number_of_samples, params):
273-
logging.debug("Running geometry-fixing step")
276+
if self.do_logging:
277+
logging.debug("Running geometry-fixing step")
274278
try:
275279
c, g, H = self.model.lagrange_polynomial(knew) # based at xopt
276280
# Solve problem: bounds are sl <= xnew <= su, and ||xnew-xopt|| <= adelt
@@ -344,7 +348,8 @@ def evaluate_objective(self, x, number_of_samples, params):
344348
incremented_nx = True
345349
f_list[i] = eval_objective(self.objfun, remove_scaling(x, self.scaling_changes), self.args, eval_num=self.nf, pt_num=self.nx,
346350
full_x_thresh=params("logging.n_to_print_whole_x_vector"),
347-
check_for_overflow=params("general.check_objfun_for_overflow"))
351+
check_for_overflow=params("general.check_objfun_for_overflow"),
352+
verbose=self.do_logging)
348353
num_samples_run += 1
349354

350355
# Check if the average value was below our threshold
@@ -453,11 +458,13 @@ def terminate_from_slow_iterations(self, current_iter, params):
453458
# Update counter of number of slow iterations
454459
if this_iter_slow:
455460
self.num_slow_iters += 1
456-
logging.info("Slow iteration (%g consecutive so far, max allowed %g)"
461+
if self.do_logging:
462+
logging.info("Slow iteration (%g consecutive so far, max allowed %g)"
457463
% (self.num_slow_iters, params("slow.max_slow_iters")))
458464
else:
459465
self.num_slow_iters = 0
460-
logging.debug("Non-slow iteration")
466+
if self.do_logging:
467+
logging.debug("Non-slow iteration")
461468
return this_iter_slow, self.num_slow_iters >= params("slow.max_slow_iters")
462469

463470
def soft_restart(self, number_of_samples, nruns_so_far, params, x_in_abs_coords_to_save=None, f_to_save=None,
@@ -491,7 +498,8 @@ def soft_restart(self, number_of_samples, nruns_so_far, params, x_in_abs_coords_
491498
self.model.save_point(self.model.xopt(abs_coordinates=True), self.model.fopt(),
492499
self.model.nsamples[self.model.kopt], x_in_abs_coords=True)
493500

494-
logging.info("Soft restart [currently, f = %g after %g function evals]" % (self.model.fopt(), self.nf))
501+
if self.do_logging:
502+
logging.info("Soft restart [currently, f = %g after %g function evals]" % (self.model.fopt(), self.nf))
495503
# Resetting method: reset delta and rho, then move the closest 'num_steps' points to xk to improve geometry
496504
# Note: closest points because we are suddenly increasing delta & rho, so we want to encourage spreading out points
497505
self.delta = self.rhobeg

pybobyqa/model.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343

4444
class Model(object):
45-
def __init__(self, npt, x0, f0, xl, xu, f0_nsamples, n=None, abs_tol=-1e20, precondition=True):
45+
def __init__(self, npt, x0, f0, xl, xu, f0_nsamples, n=None, abs_tol=-1e20, precondition=True, do_logging=True):
4646
if n is None:
4747
n = len(x0)
4848
assert npt >= n + 1, "Require npt >= n+1 for quadratic models"
@@ -52,6 +52,7 @@ def __init__(self, npt, x0, f0, xl, xu, f0_nsamples, n=None, abs_tol=-1e20, prec
5252
assert xu.shape == (n,), "xu has wrong shape (got %s, expect (%g,))" % (str(xu.shape), n)
5353
self.dim = n
5454
self.num_pts = npt
55+
self.do_logging = do_logging
5556

5657
# Initialise to blank some useful stuff
5758
# Interpolation points
@@ -258,7 +259,8 @@ def solve_system(self, rhs):
258259
# Solve A(original)\rhs
259260
return col_scale(LA.lu_solve((self.lu, self.piv), col_scale(rhs, self.left_scaling)), self.right_scaling)
260261
else:
261-
logging.warning("model.solve_system not using factorisation")
262+
if self.do_logging:
263+
logging.warning("model.solve_system not using factorisation")
262264
A, left_scaling, right_scaling = self.interpolation_matrix()
263265
return col_scale(LA.solve(A, col_scale(rhs, left_scaling)), right_scaling)
264266

0 commit comments

Comments
 (0)