Skip to content

Commit c87ad37

Browse files
committed
Small code cleanup
1 parent b79e996 commit c87ad37

File tree

3 files changed

+13
-30
lines changed

3 files changed

+13
-30
lines changed

dfols/controller.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,8 @@ def get_new_direction_for_growing(self, step_length):
442442
return dirn * (step_length / LA.norm(dirn))
443443

444444
def evaluate_criticality_measure(self, params):
445-
# TODO: add comment for calculation of criticality measure, need h != None
445+
# Calculate criticality measure for regularized problems (h is not None)
446+
446447
# Build model for full least squares function
447448
gopt, H = self.model.build_full_model()
448449

@@ -452,19 +453,15 @@ def evaluate_criticality_measure(self, params):
452453
# gnew = gopt.copy()
453454
# crvmin = -1
454455
return np.inf
455-
##print("gopt", gopt)
456-
##print("H", H)
456+
457457
# NOTE: smaller params here to get more iterations in S-FISTA
458458
func_tol = params("func_tol.criticality_measure") * self.delta
459-
##print("function tolerance (criticality measure)", func_tol)
460459
if self.model.projections:
461460
d, gnew, crvmin = ctrsbox_sfista(self.model.xopt(abs_coordinates=True), gopt, np.zeros(H.shape), self.model.projections, 1,
462461
self.h, self.lh, self.prox_uh, argsh = self.argsh, argsprox=self.argsprox, func_tol=func_tol,
463462
max_iters=params("func_tol.max_iters"), d_max_iters=params("dykstra.max_iters"), d_tol=params("dykstra.d_tol"),
464463
scaling_changes=self.scaling_changes)
465464
else:
466-
# NOTE: alternative way if using trsbox
467-
# d, gnew, crvmin = trsbox(self.model.xopt(), gopt, H, self.model.sl, self.model.su, self.delta)
468465
proj = lambda x: pbox(x, self.model.sl, self.model.su)
469466
d, gnew, crvmin = ctrsbox_sfista(self.model.xopt(abs_coordinates=True), gopt, np.zeros(H.shape), [proj], 1,
470467
self.h, self.lh, self.prox_uh, argsh = self.argsh, argsprox=self.argsprox, func_tol=func_tol,
@@ -473,8 +470,6 @@ def evaluate_criticality_measure(self, params):
473470

474471
# Calculate criticality measure
475472
criticality_measure = self.h(remove_scaling(self.model.xopt(abs_coordinates=True), self.scaling_changes), *self.argsh) - model_value(gopt, np.zeros(H.shape), d, self.model.xopt(abs_coordinates=True), self.h, self.argsh, self.scaling_changes)
476-
##print("d (criticality measure): ", d)
477-
##print("model value (criticality measure): ", model_value(gopt, 2*H, d, self.model.xopt(abs_coordinates=True), self.h, self.argsh))
478473
return criticality_measure
479474

480475
def trust_region_step(self, params, criticality_measure=1e-2):
@@ -484,7 +479,6 @@ def trust_region_step(self, params, criticality_measure=1e-2):
484479
# QUESTION: c1 = min{1, 1/delta_max^2}, but choose c1=1here; choose maxhessian = max(||H||_2,1)
485480
# QUESTION: when criticality_measure = 0? choose max(criticality_measure,1)
486481
func_tol = (1-params("func_tol.tr_step")) * 1 * max(criticality_measure,1) * min(self.delta, max(criticality_measure,1) / max(np.linalg.norm(H, 2),1))
487-
##print("function tolerance (trust region step)", func_tol)
488482

489483
if self.h is None:
490484
if self.model.projections:
@@ -520,14 +514,12 @@ def trust_region_step(self, params, criticality_measure=1e-2):
520514
self.h, self.lh, self.prox_uh, argsh = self.argsh, argsprox=self.argsprox, func_tol=func_tol,
521515
max_iters=params("func_tol.max_iters"), d_max_iters=params("dykstra.max_iters"), d_tol=params("dykstra.d_tol"),
522516
scaling_changes=self.scaling_changes)
517+
523518
# NOTE: check sufficient decrease. If increase in the model, set zero step
524519
pred_reduction = self.h(remove_scaling(self.model.xopt(abs_coordinates=True), self.scaling_changes), *self.argsh) - model_value(gopt, H, d, self.model.xopt(abs_coordinates=True), self.h, self.argsh, self.scaling_changes)
525-
##("pred_reduction", pred_reduction)
526520
if pred_reduction < 0.0:
527-
##print("bad d", d)
528521
d = np.zeros(d.shape)
529-
##print("d (trust region step): ", d)
530-
##print("new point (after trust region step): ", d + self.model.xopt(abs_coordinates=True))
522+
531523
return d, gopt, H, gnew, crvmin
532524

533525
def geometry_step(self, knew, adelt, number_of_samples, params):
@@ -727,9 +719,7 @@ def calculate_ratio(self, x, current_iter, rvec_list, d, gopt, H):
727719
if len(self.model.projections) > 1: # if we are using multiple projections, only warn since likely due to constraint intersection
728720
exit_info = ExitInformation(EXIT_TR_INCREASE_WARNING, "Either multiple constraints are active or trust region step gave model increase")
729721
else:
730-
exit_info = ExitInformation(EXIT_TR_INCREASE_ERROR, "Either rust region step gave model increase")
731-
##print("actual reduction: ", actual_reduction)
732-
##print("pred reduction: ", pred_reduction)
722+
exit_info = ExitInformation(EXIT_TR_INCREASE_ERROR, "Trust region step gave model increase")
733723
ratio = actual_reduction / pred_reduction
734724
return ratio, exit_info
735725

dfols/solver.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -555,11 +555,7 @@ def solve_main(objfun, x0, argsf, xl, xu, projections, npt, rhobeg, rhoend, maxf
555555
break # quit
556556

557557
# Estimate f in order to compute 'actual reduction'
558-
##print("d before calcualte_ratio: ", d)
559-
##print("xopt before calcualte_ratio: ", control.model.xopt(abs_coordinates=True))
560-
# NOTE: be careful abour x in calculate_ratio
561558
ratio, exit_info = control.calculate_ratio(control.model.xopt(abs_coordinates=True), current_iter, rvec_list[:num_samples_run, :], d, gopt, H)
562-
##print("ratio: ", ratio)
563559
if exit_info is not None:
564560
if exit_info.able_to_do_restart() and params("restarts.use_restarts") and params(
565561
"restarts.use_soft_restarts"):
@@ -607,7 +603,6 @@ def solve_main(objfun, x0, argsf, xl, xu, projections, npt, rhobeg, rhoend, maxf
607603
params("tr_radius.gamma_inc_overline") * dnorm), 1.0e10)
608604
if params("logging.save_diagnostic_info"):
609605
diagnostic_info.update_iter_type(ITER_VERY_SUCCESSFUL)
610-
# QUESTION: previously: control.delta <= 1.5 * control.rho, why use 1.5 here?
611606
if control.delta <= 1.5 * control.rho: # cap trust region radius at rho
612607
control.delta = control.rho
613608

dfols/trust_region.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def ctrsbox_sfista(xopt, g, H, projections, delta, h, L_h, prox_uh, argsh=(), ar
100100
y = np.zeros(n)
101101
t = 1
102102
k_H = np.linalg.norm(H, 2) # SOLVED: use ||H||_2 <= ||H||_F, might be better than maxhessian
103-
# QUESTION: difference expression for u from MAX_LOP_ITERS
103+
# QUESTION: different expression for u from MAX_LOOP_ITERS
104104
#u = 2 * func_tol / (L_h * (L_h + sqrt(L_h * L_h + 2 * k_H * func_tol))) # smoothing parameter
105105
crvmin = -1.0
106106
# NOTE: iterataion depends on delta/func_tol
@@ -109,14 +109,11 @@ def ctrsbox_sfista(xopt, g, H, projections, delta, h, L_h, prox_uh, argsh=(), ar
109109
except ValueError:
110110
MAX_LOOP_ITERS = max_iters
111111
u = 2 * delta / (MAX_LOOP_ITERS * L_h) # smoothing parameter
112-
##print("smoothing parameter", u)
113-
##print("number of iterations", MAX_LOOP_ITERS)
114112

115113
def gradient_Fu(xopt, g, H, u, prox_uh, d):
116114
# Calculate gradient_Fu,
117-
# where Fu(d) := g(d) + h_u(d) and h_u(d) is a 1/u-smooth approximation of
118-
# h.
119-
# We assume that h is global Lipschitz continous with constant L_h,
115+
# where Fu(d) := g(d) + h_u(d) and h_u(d) is a 1/u-smooth approximation of h.
116+
# We assume that h is globally Lipschitz continous with constant L_h,
120117
# then we can let h_u(d) be the Moreau Envelope M_h_u(d) of h.
121118
# TODO: Add instruction to prox_uh
122119
# SOLVED: bug here, previous: g + H @ d + (d - prox_uh(xopt, u, d, *argsprox)) / u
@@ -151,9 +148,10 @@ def proj(d0):
151148
# SOLVED: (previously) make sfista decrease in each iteration (might have d = 0, criticality measure=0)
152149
# if model_value(g, H, d, xopt, h, *argsh) > model_value(g, H, prev_d, xopt, h, *argsh):
153150
# d = prev_d
154-
if model_value(g, H, d, xopt, h, *argsh, scaling_changes) < model_value_best:
155-
d_best = d
156-
model_value_best = model_value(g, H, d, xopt, h, *argsh, scaling_changes)
151+
new_model_value = model_value(g, H, d, xopt, h, *argsh, scaling_changes)
152+
if new_model_value < model_value_best:
153+
d_best = d.copy()
154+
model_value_best = new_model_value
157155

158156
# update true gradient
159157
# FIXME: gnew is the gradient of the smoothed version

0 commit comments

Comments
 (0)