StoppingCriterion.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2015 Imperial College London
5  * Copyright 2013-2015 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MIRTK_StoppingCriterion_H
21 #define MIRTK_StoppingCriterion_H
22 
23 #include "mirtk/Object.h"
24 #include "mirtk/ObjectiveFunction.h"
25 
26 
27 namespace mirtk {
28 
29 
30 // Forward declaration of cyclic dependency
31 class LocalOptimizer;
32 
33 
34 /**
35  * Stopping criterion for iterative optimization
36  */
37 class StoppingCriterion : public Object
38 {
39  mirtkAbstractMacro(StoppingCriterion);
40 
41  // ---------------------------------------------------------------------------
42  // Attributes
43 
44  /// Optimizer to which this stopping criterion belongs to
45  mirtkPublicAggregateMacro(const LocalOptimizer, Optimizer);
46 
47  /// Objective function
48  mirtkPublicAggregateMacro(const ObjectiveFunction, Function);
49 
50  /// Copy attributes of this class from another instance
51  void CopyAttributes(const StoppingCriterion &);
52 
53  // ---------------------------------------------------------------------------
54  // Construction/Destruction
55 protected:
56 
57  /// Constructor
58  StoppingCriterion(const ObjectiveFunction * = NULL);
59 
60  /// Constructor
62 
63  /// Copy constructor
65 
66  /// Assignment operator
68 
69 public:
70 
71  /// Create new copy of this instance
72  virtual StoppingCriterion *New() const = 0;
73 
74  /// Destructor
75  virtual ~StoppingCriterion();
76 
77  /// Initialize stopping criterion after input and parameters are set
78  virtual void Initialize();
79 
80  // ---------------------------------------------------------------------------
81  // Evaluation
82 
83  /// Test stopping criterion
84  ///
85  /// The objective function must be up-to-date when this function is called.
86  /// This is usually the case anyway because the current objective function
87  /// value after a change of the parameters must be evaluated before this
88  /// function is called to be able to provide this value as argument.
89  ///
90  /// \note The objective function value may be infinite in case of a non-parametric
91  /// deformable surface model. In this case, stopping criteria are based
92  /// only on the current surface geometry or last node displacements.
93  /// Stopping criteria based on the objective function value should
94  /// never be fulfilled in this case and always return \c false.
95  ///
96  /// \param[in] iter Current number of iterations.
97  /// \param[in] value Objective function value at current iteration.
98  /// \param[in] delta Last change of objective function parameters.
99  ///
100  /// \returns Whether stopping criterion is fulfilled.
101  virtual bool Fulfilled(int iter, double value, const double *delta) = 0;
102 
103  // ---------------------------------------------------------------------------
104  // Logging
105 
106  /// Print current stopping criterion status / value
107  ///
108  /// This function must be called after Fulfilled, which should update any
109  /// cached values that are needed by this function to avoid a costly
110  /// reevaluation of the stopping criterion.
111  ///
112  /// \note The printed string is expected to be considerably short and must
113  /// not end with a newline or space characters.
114  virtual void Print(ostream &) const;
115 
116 };
117 
118 
119 } // namespace mirtk
120 
121 #endif // MIRTK_StoppingCriterion_H
virtual void Print(ostream &) const
virtual bool Fulfilled(int iter, double value, const double *delta)=0
StoppingCriterion & operator=(const StoppingCriterion &)
Assignment operator.
StoppingCriterion(const ObjectiveFunction *=NULL)
Constructor.
Definition: IOConfig.h:41
virtual ~StoppingCriterion()
Destructor.
virtual void Initialize()
Initialize stopping criterion after input and parameters are set.
virtual StoppingCriterion * New() const =0
Create new copy of this instance.