ANIMA  4.0
animaNLOPTOptimizers.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <itkVector.h>
4 #include <itkMatrix.h>
5 #include <itkSingleValuedNonLinearOptimizer.h>
6 
7 #include <nlopt.hpp>
8 #include <AnimaOptimizersExport.h>
9 
11 
12 namespace anima
13 {
94  class ANIMAOPTIMIZERS_EXPORT NLOPTOptimizers:
95  public itk::SingleValuedNonLinearOptimizer
96  {
97  public:
98 
100  typedef SingleValuedNonLinearOptimizer Superclass;
101  typedef itk::SmartPointer<Self> Pointer;
102  typedef itk::SmartPointer<const Self> ConstPointer;
104  typedef SingleValuedNonLinearOptimizer::ParametersType ParametersType;
108  itkNewMacro(Self)
109 
110 
111  itkTypeMacro(NLOPTOptimizers, SingleValuedNonLinearOptimizer)
112 
113 
114  typedef itk::SingleValuedCostFunction CostFunctionType;
115  typedef CostFunctionType::Pointer CostFunctionPointer;
116 
118  itkSetMacro(Algorithm, nlopt_algorithm)
119  itkGetConstReferenceMacro(Algorithm, nlopt_algorithm)
120 
122  itkSetMacro(ErrorCode, nlopt_result)
123  itkGetConstReferenceMacro(ErrorCode, nlopt_result)
124 
125  std::string GetErrorCodeDescription() const;
126 
127  bool isSuccessful() const;
128 
129 
131  itkGetConstReferenceMacro(CurrentCost, MeasureType)
133  MeasureType GetValue() const {return this->GetCurrentCost();}
134 
135  void SetLowerBoundParameters( const ParametersType& p );
136  itkGetConstReferenceMacro(LowerBoundParameters, ParametersType)
137 
138  void SetUpperBoundParameters( const ParametersType& p );
139  itkGetConstReferenceMacro(UpperBoundParameters, ParametersType)
140 
141 
142  itkSetMacro(Maximize, bool)
143  itkGetConstReferenceMacro(Maximize, bool)
144 
145 
161  void SetStopVal(double stopval) {m_StopValSet=true; m_StopVal=stopval;}
162  itkGetConstReferenceMacro(StopVal, double)
163 
164 
182  itkSetMacro(FTolRel, double)
183  itkGetConstReferenceMacro(FTolRel, double)
184 
185 
200  itkSetMacro(FTolAbs, double)
201  itkGetConstReferenceMacro(FTolAbs, double)
202 
203 
221  itkSetMacro(XTolRel, double)
222  itkGetConstReferenceMacro(XTolRel, double)
223 
224 
240  itkSetMacro(XTolAbs, double)
241  itkGetConstReferenceMacro(XTolAbs, double)
242 
243 
259  itkSetMacro(MaxEval, int)
260  itkGetConstReferenceMacro(MaxEval, int)
261 
262 
278  itkSetMacro(MaxTime, double)
279  itkGetConstReferenceMacro(MaxTime, double)
280 
281 
283  itkSetMacro(VectorStorageSize, int)
284  itkGetConstReferenceMacro(VectorStorageSize, int)
285 
286 
288  itkSetMacro(PopulationSize, int)
289  itkGetConstReferenceMacro(PopulationSize, int)
290 
291 
292  itkSetMacro(LocalOptimizer, nlopt_algorithm)
293  itkGetConstReferenceMacro(LocalOptimizer, nlopt_algorithm)
294 
295  void StartOptimization() ITK_OVERRIDE;
296 
298  void StopOptimization() {nlopt_force_stop(m_NloptOptions);}
299 
300  itkGetMacro(VerboseLevel, unsigned int)
301  itkSetMacro(VerboseLevel, unsigned int)
302 
303  void AddInequalityConstraint(ConstraintsFunctionType *constraint);
304  void ClearInequalityConstraints();
305  void AddEqualityConstraint(ConstraintsFunctionType *constraint);
306  void ClearEqualityConstraints();
307 
308  protected:
309  NLOPTOptimizers();
311  virtual ~NLOPTOptimizers();
312  void PrintSelf(std::ostream& os, itk::Indent indent) const ITK_OVERRIDE;
313  static double NloptFunctionWrapper(unsigned n, const double *x, double *grad, void *data);
314  itkSetMacro(CurrentCost, double)
315 
316  private:
317  nlopt_opt m_NloptOptions;
318  nlopt_opt m_NloptLocalOptions;
319 
320  nlopt_algorithm m_Algorithm;
321  nlopt_algorithm m_LocalOptimizer;
322 
323  nlopt_result m_ErrorCode;
324 
325  ParametersType m_LowerBoundParameters;
326  ParametersType m_UpperBoundParameters;
327 
328  bool m_Maximize;
329  bool m_ForceStop;
330  bool m_StopValSet;
331  double m_StopVal;
332  double m_FTolRel;
333  double m_FTolAbs;
334  double m_XTolRel;
335  double m_XTolAbs;
336  int m_MaxEval;
337  double m_MaxTime;
338  int m_VectorStorageSize;
339  int m_PopulationSize;
340 
341  unsigned int m_VerboseLevel;
342 
344  MeasureType m_CurrentCost;
345 
346  std::vector<ConstraintsFunctionType::Pointer> m_InequalityConstraints;
347  std::vector<ConstraintsFunctionType::Pointer> m_EqualityConstraints;
348 
349  }; // end of class
350 
351 } // end of namespace anima
itk::SmartPointer< Self > Pointer
anima::NLOPTParametersConstraintFunction ConstraintsFunctionType
Implements an ITK wrapper for the NLOPT library.
STL namespace.
SingleValuedNonLinearOptimizer Superclass
itk::SmartPointer< const Self > ConstPointer
CostFunctionType::Pointer CostFunctionPointer
itk::SingleValuedCostFunction CostFunctionType
SingleValuedNonLinearOptimizer::ParametersType ParametersType