Skip to content

Commit

Permalink
Added error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
alexis-mignon committed May 1, 2012
1 parent d67c69e commit 8f1d309
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions lbfgs.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,42 @@ _LINE_SEARCH_ALGO = {
'strongwolfe' : LBFGS_LINESEARCH_BACKTRACKING_STRONG_WOLFE
}

_ERROR_MESSAGES = {
LBFGSERR_UNKNOWNERROR : "Unknown error." ,
LBFGSERR_LOGICERROR : "Logic error.",
LBFGSERR_OUTOFMEMORY : "Insufficient memory.",
LBFGSERR_CANCELED : "The minimization process has been canceled.",
LBFGSERR_INVALID_N : "Invalid number of variables specified.",
LBFGSERR_INVALID_N_SSE : "Invalid number of variables (for SSE) specified.",
LBFGSERR_INVALID_X_SSE : "The array x must be aligned to 16 (for SSE).",
LBFGSERR_INVALID_EPSILON : "Invalid parameter epsilon specified.",
LBFGSERR_INVALID_TESTPERIOD : "Invalid parameter past specified.",
LBFGSERR_INVALID_DELTA : "Invalid parameter delta specified.",
LBFGSERR_INVALID_LINESEARCH : "Invalid parameter linesearch specified.",
LBFGSERR_INVALID_MINSTEP : "Invalid parameter max_step specified.",
LBFGSERR_INVALID_MAXSTEP : "Invalid parameter max_step specified.",
LBFGSERR_INVALID_FTOL : "Invalid parameter ftol specified.",
LBFGSERR_INVALID_WOLFE : "Invalid parameter wolfe specified.",
LBFGSERR_INVALID_GTOL : "Invalid parameter gtol specified.",
LBFGSERR_INVALID_XTOL : "Invalid parameter xtol specified.",
LBFGSERR_INVALID_MAXLINESEARCH : "Invalid parameter max_linesearch specified.",
LBFGSERR_INVALID_ORTHANTWISE : "Invalid parameter orthantwise_c specified.",
LBFGSERR_INVALID_ORTHANTWISE_START : "Invalid parameter orthantwise_start specified.",
LBFGSERR_INVALID_ORTHANTWISE_END : "Invalid parameter orthantwise_end specified.",
LBFGSERR_OUTOFINTERVAL : "The line-search step went out of the interval of uncertainty.",
LBFGSERR_INCORRECT_TMINMAX : "A logic error occurred; alternatively, the interval of uncertainty became too small.",
LBFGSERR_ROUNDING_ERROR : "A rounding error occurred; alternatively, no line-search step satisfies the sufficient decrease and curvature conditions.",
LBFGSERR_MINIMUMSTEP : "The line-search step became smaller than min_step.",
LBFGSERR_MAXIMUMSTEP : "The line-search step became larger than max_step.",
LBFGSERR_MAXIMUMLINESEARCH : "The line-search routine reaches the maximum number of evaluations.",
LBFGSERR_MAXIMUMITERATION : "The algorithm routine reaches the maximum number of iterations.",
LBFGSERR_WIDTHTOOSMALL : "Relative width of the interval of uncertainty is at most xtol.",
LBFGSERR_INVALIDPARAMETERS : "A logic error (negative line-search step) occurred.",
LBFGSERR_INCREASEGRADIENT : "The current search direction increases the objective function value.",
}

class LBFGSError(Exception):
pass

cdef class LBFGS(object):
"""LBFGS algorithm, wrapped in a class to permit setting parameters"""
Expand Down Expand Up @@ -263,8 +299,7 @@ cdef class LBFGS(object):

if r == LBFGS_SUCCESS or r == LBFGS_ALREADY_MINIMIZED:
return x_final
# TODO handle errors

else: raise LBFGSError(_ERROR_MESSAGES[r])

def fmin_lbfgs(f, x0, progress=None, args=()):
"""Minimize a function using LBFGS or OWL-QN
Expand Down

0 comments on commit 8f1d309

Please sign in to comment.