go home Home | Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages
itkQuasiNewtonLBFGSOptimizer.h
Go to the documentation of this file.
00001 /*======================================================================
00002 
00003   This file is part of the elastix software.
00004 
00005   Copyright (c) University Medical Center Utrecht. All rights reserved.
00006   See src/CopyrightElastix.txt or http://elastix.isi.uu.nl/legal.php for
00007   details.
00008 
00009      This software is distributed WITHOUT ANY WARRANTY; without even
00010      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00011      PURPOSE. See the above copyright notices for more information.
00012 
00013 ======================================================================*/
00014 
00015 #ifndef __itkQuasiNewtonLBFGSOptimizer_h
00016 #define __itkQuasiNewtonLBFGSOptimizer_h
00017 
00018 #include "itkScaledSingleValuedNonLinearOptimizer.h"
00019 #include "itkLineSearchOptimizer.h"
00020 #include <vector>
00021 
00022 namespace itk
00023 {
00054   class QuasiNewtonLBFGSOptimizer : public ScaledSingleValuedNonLinearOptimizer
00055   {
00056   public:
00057 
00058     typedef QuasiNewtonLBFGSOptimizer             Self;
00059     typedef ScaledSingleValuedNonLinearOptimizer  Superclass;
00060     typedef SmartPointer<Self>                    Pointer;
00061     typedef SmartPointer<const Self>              ConstPointer;
00062 
00063     itkNewMacro(Self);
00064     itkTypeMacro(QuasiNewtonLBFGSOptimizer, ScaledSingleValuedNonLinearOptimizer);
00065 
00066     typedef Superclass::ParametersType            ParametersType;
00067     typedef Superclass::DerivativeType            DerivativeType;
00068     typedef Superclass::CostFunctionType          CostFunctionType;
00069     typedef Superclass::ScaledCostFunctionType    ScaledCostFunctionType;
00070     typedef Superclass::MeasureType               MeasureType;
00071     typedef Superclass::ScalesType                ScalesType;
00072 
00073     typedef Array<double>                         RhoType;
00074     typedef std::vector<ParametersType>           SType;
00075     typedef std::vector<DerivativeType>           YType;
00076     typedef Array<double>                         DiagonalMatrixType;
00077     typedef LineSearchOptimizer                   LineSearchOptimizerType;
00078 
00079     typedef LineSearchOptimizerType::Pointer      LineSearchOptimizerPointer;
00080 
00081     typedef enum {
00082       MetricError,
00083       LineSearchError,
00084       MaximumNumberOfIterations,
00085       InvalidDiagonalMatrix,
00086       GradientMagnitudeTolerance,
00087       ZeroStep,
00088       Unknown }                                   StopConditionType;
00089 
00090     virtual void StartOptimization(void);
00091     virtual void ResumeOptimization(void);
00092     virtual void StopOptimization(void);
00093 
00095     itkGetConstMacro(CurrentIteration, unsigned long);
00096     itkGetConstMacro(CurrentValue, MeasureType);
00097     itkGetConstReferenceMacro(CurrentGradient, DerivativeType);
00098     itkGetConstMacro(InLineSearch, bool);
00099     itkGetConstReferenceMacro(StopCondition, StopConditionType);
00100     itkGetConstMacro(CurrentStepLength, double);
00101 
00103     itkSetObjectMacro(LineSearchOptimizer, LineSearchOptimizerType);
00104     itkGetObjectMacro(LineSearchOptimizer, LineSearchOptimizerType);
00105 
00107     itkGetConstMacro(MaximumNumberOfIterations, unsigned long);
00108     itkSetClampMacro(MaximumNumberOfIterations, unsigned long,
00109       1, NumericTraits<unsigned long>::max());
00110 
00116     itkGetConstMacro(GradientMagnitudeTolerance, double);
00117     itkSetMacro(GradientMagnitudeTolerance, double);
00118 
00122     itkSetClampMacro(Memory,unsigned int,0,NumericTraits<unsigned int>::max());
00123     itkGetConstMacro(Memory,unsigned int);
00124 
00125 
00126   protected:
00127     QuasiNewtonLBFGSOptimizer();
00128     virtual ~QuasiNewtonLBFGSOptimizer(){};
00129 
00130     void PrintSelf(std::ostream& os, Indent indent) const {};
00131 
00132     DerivativeType                m_CurrentGradient;
00133     MeasureType                   m_CurrentValue;
00134     unsigned long                 m_CurrentIteration;
00135     StopConditionType             m_StopCondition;
00136     bool                          m_Stop;
00137     double                        m_CurrentStepLength;
00138 
00140     bool                          m_InLineSearch;
00141 
00142     RhoType                       m_Rho;
00143     SType                         m_S;
00144     YType                         m_Y;
00145 
00146     unsigned int                  m_Point;
00147     unsigned int                  m_PreviousPoint;
00148     unsigned int                  m_Bound;
00149 
00150     itkSetMacro(InLineSearch, bool);
00151 
00156     virtual void ComputeDiagonalMatrix(DiagonalMatrixType & diag_H0);
00157 
00164     virtual void ComputeSearchDirection(
00165       const DerivativeType & gradient,
00166       ParametersType & searchDir);
00167 
00172     virtual void LineSearch(
00173       const ParametersType searchDir,
00174       double & step,
00175       ParametersType & x,
00176       MeasureType & f,
00177       DerivativeType & g );
00178 
00181     virtual void StoreCurrentPoint(
00182       const ParametersType & step,
00183       const DerivativeType & grad_dif);
00184 
00189     virtual bool TestConvergence(bool firstLineSearchDone);
00190 
00191   private:
00192     QuasiNewtonLBFGSOptimizer(const Self&); //purposely not implemented
00193     void operator=(const Self&); //purposely not implemented
00194 
00195     unsigned long                 m_MaximumNumberOfIterations;
00196     double                        m_GradientMagnitudeTolerance;
00197     LineSearchOptimizerPointer    m_LineSearchOptimizer;
00198     unsigned int                  m_Memory;
00199 
00200 
00201   }; // end class QuasiNewtonLBFGSOptimizer
00202 
00203 
00204 
00205 } // end namespace itk
00206 
00214 /*        LIMITED MEMORY BFGS METHOD FOR LARGE SCALE OPTIMIZATION */
00215 /*                          JORGE NOCEDAL */
00216 /*                        *** July 1990 *** */
00217 
00218 /*     This subroutine solves the unconstrained minimization problem */
00219 
00220 /*                      min F(x),    x= (x1,x2,...,xN), */
00221 
00222 /*      using the limited memory BFGS method. The routine is especially */
00223 /*      effective on problems involving a large number of variables. In */
00224 /*      a typical iteration of this method an approximation Hk to the */
00225 /*      inverse of the Hessian is obtained by applying M BFGS updates to */
00226 /*      a diagonal matrix Hk0, using information from the previous M steps.  */
00227 /*      The user specifies the number M, which determines the amount of */
00228 /*      storage required by the routine. The user may also provide the */
00229 /*      diagonal matrices Hk0 if not satisfied with the default choice. */
00230 /*      The algorithm is described in "On the limited memory BFGS method */
00231 /*      for large scale optimization", by D. Liu and J. Nocedal, */
00232 /*      Mathematical Programming B 45 (1989) 503-528. */
00233 
00234 /*      The user is required to calculate the function value F and its */
00235 /*      gradient G. In order to allow the user complete control over */
00236 /*      these computations, reverse  communication is used. The routine */
00237 /*      must be called repeatedly under the control of the parameter */
00238 /*      IFLAG. */
00239 
00240 /*      The steplength is determined at each iteration by means of the */
00241 /*      line search routine MCVSRCH, which is a slight modification of */
00242 /*      the routine CSRCH written by More' and Thuente. */
00243 
00244 /*      The calling statement is */
00245 
00246 /*          CALL LBFGS(N,M,X,F,G,DIAGCO,DIAG,IPRINT,EPS,XTOL,W,IFLAG) */
00247 
00248 /*      where */
00249 
00250 /*     N       is an INTEGER variable that must be set by the user to the */
00251 /*             number of variables. It is not altered by the routine. */
00252 /*             Restriction: N>0. */
00253 
00254 /*     M       is an INTEGER variable that must be set by the user to */
00255 /*             the number of corrections used in the BFGS update. It */
00256 /*             is not altered by the routine. Values of M less than 3 are */
00257 /*             not recommended; large values of M will result in excessive */
00258 /*             computing time. 3<= M <=7 is recommended. Restriction: M>0.  */
00259 
00260 /*     X       is a DOUBLE PRECISION array of length N. On initial entry */
00261 /*             it must be set by the user to the values of the initial */
00262 /*             estimate of the solution vector. On exit with IFLAG=0, it */
00263 /*             contains the values of the variables at the best point */
00264 /*             found (usually a solution). */
00265 
00266 /*     F       is a DOUBLE PRECISION variable. Before initial entry and on */
00267 /*             a re-entry with IFLAG=1, it must be set by the user to */
00268 /*             contain the value of the function F at the point X. */
00269 
00270 /*     G       is a DOUBLE PRECISION array of length N. Before initial */
00271 /*             entry and on a re-entry with IFLAG=1, it must be set by */
00272 /*             the user to contain the components of the gradient G at */
00273 /*             the point X. */
00274 
00275 /*     DIAGCO  is a LOGICAL variable that must be set to .TRUE. if the */
00276 /*             user  wishes to provide the diagonal matrix Hk0 at each */
00277 /*             iteration. Otherwise it should be set to .FALSE., in which */
00278 /*             case  LBFGS will use a default value described below. If */
00279 /*             DIAGCO is set to .TRUE. the routine will return at each */
00280 /*             iteration of the algorithm with IFLAG=2, and the diagonal */
00281 /*              matrix Hk0  must be provided in the array DIAG. */
00282 
00283 /*     DIAG    is a DOUBLE PRECISION array of length N. If DIAGCO=.TRUE., */
00284 /*             then on initial entry or on re-entry with IFLAG=2, DIAG */
00285 /*             it must be set by the user to contain the values of the */
00286 /*             diagonal matrix Hk0.  Restriction: all elements of DIAG */
00287 /*             must be positive. */
00288 
00289 /*     IPRINT  is an INTEGER array of length two which must be set by the */
00290 /*             user. */
00291 
00292 /*             IPRINT(1) specifies the frequency of the output: */
00293 /*                IPRINT(1) < 0 : no output is generated, */
00294 /*                IPRINT(1) = 0 : output only at first and last iteration, */
00295 /*                IPRINT(1) > 0 : output every IPRINT(1) iterations. */
00296 
00297 /*             IPRINT(2) specifies the type of output generated: */
00298 /*                IPRINT(2) = 0 : iteration count, number of function */
00299 /*                                evaluations, function value, norm of the */
00300 /*                                gradient, and steplength, */
00301 /*                IPRINT(2) = 1 : same as IPRINT(2)=0, plus vector of */
00302 /*                                variables and  gradient vector at the */
00303 /*                                initial point, */
00304 /*                IPRINT(2) = 2 : same as IPRINT(2)=1, plus vector of */
00305 /*                                variables, */
00306 /*                IPRINT(2) = 3 : same as IPRINT(2)=2, plus gradient vector.*/
00307 
00308 
00309 /*    EPS     is a positive DOUBLE PRECISION variable that must be set by */
00310 /*            the user, and determines the accuracy with which the solution*/
00311 /*            is to be found. The subroutine terminates when */
00312 
00313 /*                         ||G|| < EPS max(1,||X||), */
00314 
00315 /*            where ||.|| denotes the Euclidean norm. */
00316 
00317 /*    XTOL    is a  positive DOUBLE PRECISION variable that must be set by */
00318 /*            the user to an estimate of the machine precision (e.g. */
00319 /*            10**(-16) on a SUN station 3/60). The line search routine will*/
00320 /*            terminate if the relative width of the interval of uncertainty*/
00321 /*            is less than XTOL. */
00322 
00323 /*     W       is a DOUBLE PRECISION array of length N(2M+1)+2M used as */
00324 /*             workspace for LBFGS. This array must not be altered by the */
00325 /*             user. */
00326 
00327 /*    IFLAG   is an INTEGER variable that must be set to 0 on initial entry*/
00328 /*            to the subroutine. A return with IFLAG<0 indicates an error, */
00329 /*            and IFLAG=0 indicates that the routine has terminated without*/
00330 /*            detecting errors. On a return with IFLAG=1, the user must */
00331 /*            evaluate the function F and gradient G. On a return with */
00332 /*            IFLAG=2, the user must provide the diagonal matrix Hk0. */
00333 
00334 /*            The following negative values of IFLAG, detecting an error, */
00335 /*            are possible: */
00336 
00337 /*              IFLAG=-1  The line search routine MCSRCH failed. The */
00338 /*                        parameter INFO provides more detailed information */
00339 /*                        (see also the documentation of MCSRCH): */
00340 
00341 /*                       INFO = 0  IMPROPER INPUT PARAMETERS. */
00342 
00343 /*                       INFO = 2  RELATIVE WIDTH OF THE INTERVAL OF */
00344 /*                                 UNCERTAINTY IS AT MOST XTOL. */
00345 
00346 /*                       INFO = 3  MORE THAN 20 FUNCTION EVALUATIONS WERE */
00347 /*                                 REQUIRED AT THE PRESENT ITERATION. */
00348 
00349 /*                       INFO = 4  THE STEP IS TOO SMALL. */
00350 
00351 /*                       INFO = 5  THE STEP IS TOO LARGE. */
00352 
00353 /*                       INFO = 6  ROUNDING ERRORS PREVENT FURTHER PROGRESS.*/
00354 /*                                 THERE MAY NOT BE A STEP WHICH SATISFIES */
00355 /*                                 THE SUFFICIENT DECREASE AND CURVATURE */
00356 /*                                 CONDITIONS. TOLERANCES MAY BE TOO SMALL.  */
00357 
00358 /*             IFLAG=-2  The i-th diagonal element of the diagonal inverse */
00359 /*                       Hessian approximation, given in DIAG, is not */
00360 /*                       positive. */
00361 
00362 /*             IFLAG=-3  Improper input parameters for LBFGS (N or M are */
00363 /*                       not positive). */
00364 
00365 
00366 /*    ON THE DRIVER: */
00367 
00368 /*    The program that calls LBFGS must contain the declaration: */
00369 
00370 /*                       EXTERNAL LB2 */
00371 
00372 /*    LB2 is a BLOCK DATA that defines the default values of several */
00373 /*    parameters described in the COMMON section. */
00374 
00375 /*    COMMON: */
00376 
00377 /*     The subroutine contains one common area, which the user may wish to */
00378 /*    reference: */
00379 
00380 /* awf added stpawf */
00381 
00382 /*    MP  is an INTEGER variable with default value 6. It is used as the */
00383 /*        unit number for the printing of the monitoring information */
00384 /*        controlled by IPRINT. */
00385 
00386 /*    LP  is an INTEGER variable with default value 6. It is used as the */
00387 /*        unit number for the printing of error messages. This printing */
00388 /*        may be suppressed by setting LP to a non-positive value. */
00389 
00390 /*    GTOL is a DOUBLE PRECISION variable with default value 0.9, which */
00391 /*        controls the accuracy of the line search routine MCSRCH. If the */
00392 /*        function and gradient evaluations are inexpensive with respect */
00393 /*        to the cost of the iteration (which is sometimes the case when */
00394 /*        solving very large problems) it may be advantageous to set GTOL */
00395 /*        to a small value. A typical small value is 0.1.  Restriction: */
00396 /*        GTOL should be greater than 1.D-04. */
00397 
00398 /*    STPMIN and STPMAX are non-negative DOUBLE PRECISION variables which */
00399 /*        specify lower and uper bounds for the step in the line search.  */
00400 /*        Their default values are 1.D-20 and 1.D+20, respectively. These */
00401 /*        values need not be modified unless the exponents are too large */
00402 /*        for the machine being used, or unless the problem is extremely */
00403 /*        badly scaled (in which case the exponents should be increased).  */
00404 
00405 
00406 /*  MACHINE DEPENDENCIES */
00407 
00408 /*        The only variables that are machine-dependent are XTOL, */
00409 /*        STPMIN and STPMAX. */
00410 
00411 
00412 /*  GENERAL INFORMATION */
00413 
00414 /*    Other routines called directly:  DAXPY, DDOT, LB1, MCSRCH */
00415 
00416 /*    Input/Output  :  No input; diagnostic messages on unit MP and */
00417 /*                     error messages on unit LP. */
00418 
00419 
00420 /*    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
00421 
00422 
00423 #endif //#ifndef __itkQuasiNewtonLBFGSOptimizer_h
00424 


Generated on 11-05-2011 for elastix by doxygen 1.7.4 elastix logo