Skip to content

Commit

Permalink
Merge pull request #25 from fides-dev/develop
Browse files Browse the repository at this point in the history
Release 0.3.1
  • Loading branch information
FFroehlich authored Mar 19, 2021
2 parents fdf90de + 26e2b68 commit eab4e22
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
1 change: 1 addition & 0 deletions fides/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class ExitFlag(int, enum.Enum):
MAXTIME = -2 #: Expected to reach maximum allowed time in next iteration
NOT_FINITE = -3 #: Encountered non-finite fval/grad/hess
EXCEEDED_BOUNDARY = -4 #: Exceeded specified boundaries
DELTA_TOO_SMALL = -5 #: Trust Region Radius too small to proceed
FTOL = 1 #: Converged according to fval difference
XTOL = 2 #: Converged according to x difference
GTOL = 3 #: Converged according to gradient norm
9 changes: 7 additions & 2 deletions fides/hessian_approximation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from typing import Optional
import numpy as np
from numpy.linalg import norm


class HessianApproximation:
Expand Down Expand Up @@ -73,7 +74,11 @@ class SR1(HessianApproximation):
"""
def update(self, s, y):
z = y - self._hess.dot(s)
self._hess += np.outer(z, z.T)/z.T.dot(s)
d = z.T.dot(s)

# [NocedalWright2006] (6.26) reject if update degenerate
if np.abs(d) >= 1e-8 * norm(s) * norm(z):
self._hess += np.outer(z, z.T)/d


class BFGS(HessianApproximation):
Expand Down Expand Up @@ -136,7 +141,7 @@ def __init__(self,

def init_mat(self, dim: int):
if self.switch_iteration is None:
self.switch_iteration = 5*dim
self.switch_iteration = 2*dim
self.hessian_update.init_mat(dim)

def update(self, s, y):
Expand Down
24 changes: 18 additions & 6 deletions fides/minimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def minimize(self, x0: np.ndarray):
step = \
trust_region(
self.x, self.grad, self.hess, scaling,
self.delta, dv, theta, self.lb, self.ub,
self.delta_iter, dv, theta, self.lb, self.ub,
subspace_dim=self.get_option(Options.SUBSPACE_DIM),
stepback_strategy=self.get_option(Options.STEPBACK_STRAT),
refine_stepback=self.get_option(Options.REFINE_STEPBACK),
Expand Down Expand Up @@ -359,7 +359,7 @@ def update_tr_radius(self,
qpval = 0.5 * stepsx.dot(dv * np.abs(grad) * stepsx)
self.tr_ratio = (fval + qpval - self.fval) / step.qpval

interior_solution = nsx < self.delta * 0.9
interior_solution = nsx < self.delta_iter * 0.9

# values as proposed in algorithm 4.1 in Nocedal & Wright
if self.tr_ratio >= self.get_option(Options.ETA) \
Expand Down Expand Up @@ -395,7 +395,8 @@ def check_convergence(self, fval, x, grad) -> None:
grtol = self.get_option(Options.GRTOL)
gnorm = norm(grad)

if np.isclose(fval, self.fval, atol=fatol, rtol=frtol):
if self.delta <= self.delta_iter and \
np.abs(fval - self.fval) < fatol + frtol*np.abs(self.fval):
self.exitflag = ExitFlag.FTOL
self.logger.warning(
'Stopping as function difference '
Expand All @@ -404,11 +405,13 @@ def check_convergence(self, fval, x, grad) -> None:
)
converged = True

elif np.isclose(x, self.x, atol=xatol, rtol=xrtol).all():
elif self.tr_ratio > 0.0 \
and norm(x - self.x) < xatol + xrtol*norm(self.x):
self.exitflag = ExitFlag.XTOL
self.logger.warning(
'Stopping as step was smaller than specified tolerances ('
f'atol={xatol:.2E}, rtol={xrtol:.2E})'
'Stopping as norm of step '
f'{norm(x - self.x)} was smaller than specified '
f'tolerances (atol={xatol:.2E}, rtol={xrtol:.2E})'
)
converged = True

Expand Down Expand Up @@ -463,6 +466,15 @@ def check_continue(self) -> bool:
)
return False

if self.delta < np.spacing(1):
self.exitflag = ExitFlag.DELTA_TOO_SMALL
self.logger.warning(
f'Stopping as trust region radius {self.delta:.2E} is '
f'smaller '
'than machine precision.'
)
return False

return True

def make_non_degenerate(self, eps=1e2 * np.spacing(1)) -> None:
Expand Down
2 changes: 1 addition & 1 deletion fides/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.3.0"
__version__ = "0.3.1"

0 comments on commit eab4e22

Please sign in to comment.