NifTK  16.4.1 - 0798f20
CMIC's Translational Medical Imaging Platform
itkUCLPowellOptimizer.h
Go to the documentation of this file.
1 /*=============================================================================
2 
3  NifTK: A software platform for medical image computing.
4 
5  Copyright (c) University College London (UCL). All rights reserved.
6 
7  This software is distributed WITHOUT ANY WARRANTY; without even
8  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9  PURPOSE.
10 
11  See LICENSE.txt in the top level directory for details.
12 
13 =============================================================================*/
14 
15 #ifndef itkUCLPowellOptimizer_h
16 #define itkUCLPowellOptimizer_h
17 
18 #include <iostream>
19 
20 #include <NifTKConfigure.h>
22 
23 #include <itkVector.h>
24 #include <itkMatrix.h>
25 #include <itkSingleValuedNonLinearOptimizer.h>
26 #include <itkSimilarityMeasure.h>
27 
28 namespace itk
29 {
30 
65  public SingleValuedNonLinearOptimizer
66 {
67 public:
70  typedef SingleValuedNonLinearOptimizer Superclass;
71  typedef SmartPointer<Self> Pointer;
72  typedef SmartPointer<const Self> ConstPointer;
73 
74  typedef SingleValuedNonLinearOptimizer::ParametersType
76 
78  itkNewMacro(Self);
79 
81  itkTypeMacro(UCLPowellOptimizer, SingleValuedNonLinearOptimizer );
82 
84  typedef SingleValuedCostFunction CostFunctionType;
85  typedef CostFunctionType::Pointer CostFunctionPointer;
86 
88  itkSetMacro( Maximize, bool );
89  itkGetConstReferenceMacro( Maximize, bool );
90 
92  itkSetMacro( MaximumIteration, unsigned int );
93  itkGetConstReferenceMacro( MaximumIteration, unsigned int );
94 
96  itkSetMacro(MaximumLineIteration, unsigned int);
97  itkGetConstMacro(MaximumLineIteration, unsigned int);
98 
101  itkSetMacro( StepLength, double );
102  itkGetConstReferenceMacro( StepLength, double );
103 
106  itkSetMacro( StepTolerance, double );
107  itkGetConstReferenceMacro( StepTolerance, double );
108 
112  itkSetMacro( ValueTolerance, double );
113  itkGetConstReferenceMacro( ValueTolerance, double );
114 
116  itkGetConstReferenceMacro( CurrentCost, MeasureType );
117  MeasureType GetValue() const { return this->GetCurrentCost(); }
118 
120  itkGetConstReferenceMacro( CurrentIteration, unsigned int);
121 
123  itkGetConstReferenceMacro( CurrentLineIteration, unsigned int);
124 
126  void StartOptimization() override;
127 
132  { m_Stop = true; }
133 
134  itkGetMacro(CatchGetValueException, bool);
135  itkSetMacro(CatchGetValueException, bool);
136 
137  itkGetMacro(MetricWorstPossibleValue, double);
138  itkSetMacro(MetricWorstPossibleValue, double);
139 
140  itkGetMacro(ParameterTolerance, double);
141  itkSetMacro(ParameterTolerance, double);
142 
143  const std::string GetStopConditionDescription() const override;
144 
145 protected:
148  virtual ~UCLPowellOptimizer();
149  void PrintSelf(std::ostream& os, Indent indent) const override;
150 
151  itkSetMacro(CurrentCost, double);
152 
155  inline void SetLine(const ParametersType & origin,
156  const vnl_vector<double> & direction)
157  {
158  for(unsigned int i=0; i<m_SpaceDimension; i++)
159  {
160  m_LineOrigin[i] = origin[i];
161  // all in the same scale in the optimisation.
162  // m_LineDirection[i] = direction[i] / this->GetScales()[i];
163  m_LineDirection[i] = direction[i];
164  }
165  }
166 
170  inline double GetLineValue(double x) const
171  {
172  UCLPowellOptimizer::ParametersType tempCoord( m_SpaceDimension );
173  return this->GetLineValue(x, tempCoord);
174  }
175 
176  double GetLineValue(double x, ParametersType & tempCoord) const;
177 
180  inline void SetCurrentLinePoint(double x, double fx)
181  {
182  for(unsigned int i=0; i<m_SpaceDimension; i++)
183  {
184  this->m_CurrentPosition[i] = this->m_LineOrigin[i] + x * this->m_LineDirection[i];
185  }
186  if(m_Maximize)
187  {
188  this->SetCurrentCost(-fx);
189  }
190  else
191  {
192  this->SetCurrentCost(fx);
193  }
194  this->Modified();
195  }
196 
199  inline void Swap(double *a, double *b) const
200  {
201  double tf;
202  tf = *a;
203  *a = *b;
204  *b = tf;
205  }
206 
209  inline void Shift(double *a, double *b, double *c, double d) const
210  {
211  *a = *b;
212  *b = *c;
213  *c = d;
214  }
215 
225  inline void LineBracket(double * x1, double * x2, double * x3, double * f1, double * f2, double * f3)
226  {
227  UCLPowellOptimizer::ParametersType tempCoord( m_SpaceDimension );
228  this->LineBracket( x1, x2, x3, f1, f2, f3, tempCoord);
229  }
230 
231  virtual void LineBracket(double *ax, double *bx, double *cx,
232  double *fa, double *fb, double *fc,
233  ParametersType & tempCoord);
234 
240  inline virtual void BracketedLineOptimize(double ax, double bx, double cx,
241  double fa, double functionValueOfb, double fc,
242  double * extX, double * extVal)
243  {
244  UCLPowellOptimizer::ParametersType tempCoord( m_SpaceDimension );
245  this->BracketedLineOptimize( ax, bx, cx, fa, functionValueOfb, fc, extX, extVal, tempCoord);
246  }
247 
248  virtual void BracketedLineOptimize(double ax, double bx, double cx,
249  double fa, double fb, double fc,
250  double * extX, double * extVal,
251  ParametersType & tempCoord);
252 
253  itkGetMacro(SpaceDimension, unsigned int);
254  void SetSpaceDimension( unsigned int dim )
255  {
256  this->m_SpaceDimension = dim;
257  this->m_LineDirection.set_size( dim );
258  this->m_LineOrigin.set_size( dim );
259  this->m_CurrentPosition.set_size( dim );
260  this->Modified();
261  }
262 
263  itkSetMacro(CurrentIteration, unsigned int);
264 
265  itkGetMacro(Stop, bool);
266  itkSetMacro(Stop, bool);
267 
272  {
273  for(unsigned int i=0; i<m_SpaceDimension; i++)
274  {
275  lastP[i] = lastP[i]/this->GetScales()[i];
276  p[i] = p[i]/this->GetScales()[i];
277  }
278  // Need to template this over the image types later.
280  typedef SimilarityMeasure<FloatImageType, FloatImageType> SimilarityMeasureType;
281 
282  return dynamic_cast<SimilarityMeasureType*>(this->m_CostFunction.GetPointer())->GetMeasureOfParameterChange(lastP, p);
283  }
284 
285 private:
286  unsigned int m_SpaceDimension;
287 
289  unsigned int m_CurrentIteration;
290  unsigned int m_CurrentLineIteration;
291 
293  unsigned int m_MaximumIteration;
294  unsigned int m_MaximumLineIteration;
295 
296  bool m_CatchGetValueException;
297  double m_MetricWorstPossibleValue;
298 
300  bool m_Maximize;
301 
303  double m_StepLength;
304  double m_StepTolerance;
305 
306  ParametersType m_LineOrigin;
307  vnl_vector<double> m_LineDirection;
308 
309  double m_ValueTolerance;
310 
311  // Parameters tolerance.
312  double m_ParameterTolerance;
313 
315  MeasureType m_CurrentCost;
316 
321  bool m_Stop;
322 
323  std::ostringstream m_StopConditionDescription;
324 }; // end of class
325 
326 } // end of namespace itk
327 
328 #endif
double GetLineValue(double x) const
Definition: itkUCLPowellOptimizer.h:170
Abstract base class, implementing TemplateMethod [2] for similarity measures.
Definition: itkSimilarityMeasure.h:56
void SetCurrentLinePoint(double x, double fx)
Definition: itkUCLPowellOptimizer.h:180
const GLfloat * c
Definition: glew.h:14144
void LineBracket(double *x1, double *x2, double *x3, double *f1, double *f2, double *f3)
Definition: itkUCLPowellOptimizer.h:225
SmartPointer< const Self > ConstPointer
Definition: itkUCLPowellOptimizer.h:72
#define NIFTKITK_WINEXPORT
Definition: niftkITKWin32ExportHeader.h:28
Definition: niftkITKAffineResampleImage.cxx:74
SingleValuedCostFunction CostFunctionType
Definition: itkUCLPowellOptimizer.h:81
void SetSpaceDimension(unsigned int dim)
Definition: itkUCLPowellOptimizer.h:254
SmartPointer< Self > Pointer
Definition: itkUCLPowellOptimizer.h:71
double GetMeasureOfParameterChange(ParametersType lastP, ParametersType p)
Definition: itkUCLPowellOptimizer.h:271
void Swap(double *a, double *b) const
Definition: itkUCLPowellOptimizer.h:199
GLdouble GLdouble GLdouble b
Definition: glew.h:7885
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:8272
GLfloat GLfloat p
Definition: glew.h:14169
SingleValuedNonLinearOptimizer::ParametersType ParametersType
Definition: itkUCLPowellOptimizer.h:75
void Shift(double *a, double *b, double *c, double d) const
Definition: itkUCLPowellOptimizer.h:209
void SetLine(const ParametersType &origin, const vnl_vector< double > &direction)
Definition: itkUCLPowellOptimizer.h:155
GLint GLint GLint GLint GLint x
Definition: glew.h:1236
void StopOptimization()
Definition: itkUCLPowellOptimizer.h:131
virtual void BracketedLineOptimize(double ax, double bx, double cx, double fa, double functionValueOfb, double fc, double *extX, double *extVal)
Definition: itkUCLPowellOptimizer.h:240
UCLPowellOptimizer Self
Definition: itkUCLPowellOptimizer.h:69
Simultaneous itk::Image and itk::Array (and hence vnl_vector) class.
MeasureType GetValue() const
Definition: itkUCLPowellOptimizer.h:117
SingleValuedNonLinearOptimizer Superclass
Definition: itkUCLPowellOptimizer.h:70
itk::Image< float, 3 > FloatImageType
Definition: niftkKNDoubleWindowBSI.cxx:56
Definition: itkUCLPowellOptimizer.h:64
GLsizei const GLcharARB ** string
Definition: glew.h:5194
CostFunctionType::Pointer CostFunctionPointer
Definition: itkUCLPowellOptimizer.h:85