File: itkAmoebaOptimizerv4.h

package info (click to toggle)
insighttoolkit5 5.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704,384 kB
  • sloc: cpp: 783,592; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 464; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (168 lines) | stat: -rw-r--r-- 6,931 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         https://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#ifndef itkAmoebaOptimizerv4_h
#define itkAmoebaOptimizerv4_h

#include "itkSingleValuedNonLinearVnlOptimizerv4.h"
#include "vnl/algo/vnl_amoeba.h"
#include "ITKOptimizersv4Export.h"
#include <memory> // For unique_ptr.

namespace itk
{
/**
 * \class AmoebaOptimizerv4
 * \brief Wrap of the vnl_amoeba algorithm
 *
 * AmoebaOptimizerv4 is a wrapper around the vnl_amoeba algorithm which
 * is an implementation of the Nelder-Meade downhill simplex
 * problem. For most problems, it is a few times slower than a
 * Levenberg-Marquardt algorithm but does not require derivatives of
 * its cost function. It works by creating a simplex (n+1 points in
 * ND space). The cost function is evaluated at each corner of the
 * simplex.  The simplex is then modified (by reflecting a corner
 * about the opposite edge, by shrinking the entire simplex, by
 * contracting one edge of the simplex, or by expanding the simplex)
 * in searching for the minimum of the cost function.
 *
 * The methods AutomaticInitialSimplex() and SetInitialSimplexDelta()
 * control whether the optimizer defines the initial simplex
 * automatically (by constructing a very small simplex around the
 * initial position) or uses a user supplied simplex size.
 *
 * The method SetOptimizeWithRestarts() indicates that the amoeba algorithm
 * should be rerun after if converges. This heuristic increases the chances
 * of escaping from a local optimum. Each time the simplex is initialized with
 * the best solution obtained by the previous runs. The edge length is half of
 * that from the previous iteration. The heuristic is terminated if the total
 * number of iterations is greater-equal than the maximal number of iterations
 * (SetNumberOfIterations) or the difference between the current function
 * value and the best function value is less than a threshold
 * (SetFunctionConvergenceTolerance) and
 * max(|best_parameters_i - current_parameters_i|) is less than a threshold
 * (SetParametersConvergenceTolerance).
 *
 * \ingroup ITKOptimizersv4
 */
class ITKOptimizersv4_EXPORT AmoebaOptimizerv4 : public SingleValuedNonLinearVnlOptimizerv4
{
public:
  ITK_DISALLOW_COPY_AND_MOVE(AmoebaOptimizerv4);

  /** Standard "Self" type alias. */
  using Self = AmoebaOptimizerv4;
  using Superclass = SingleValuedNonLinearVnlOptimizerv4;
  using Pointer = SmartPointer<Self>;
  using ConstPointer = SmartPointer<const Self>;

  /** Method for creation through the object factory. */
  itkNewMacro(Self);

  /** \see LightObject::GetNameOfClass() */
  itkOverrideGetNameOfClassMacro(AmoebaOptimizerv4);

  /**  Parameters type.
   *  It defines a position in the optimization search space. */
  using ParametersType = Superclass::ParametersType;

  /** InternalParameters type alias. */
  using InternalParametersType = vnl_vector<double>;

  /** Start optimization with an initial value. */
  void
  StartOptimization(bool doOnlyInitialization = false) override;

  /** Plug in a Cost Function into the optimizer  */
  void
  SetMetric(MetricType * metric) override;

  /** Set/Get the mode which determines how the amoeba algorithm
   * defines the initial simplex.  Default is
   * AutomaticInitialSimplexOn. If AutomaticInitialSimplex is on, the
   * initial simplex is created with a default size. If
   * AutomaticInitialSimplex is off, then InitialSimplexDelta will be
   * used to define the initial simplex, setting the ith corner of the
   * simplex as [x0[0], x0[1], ..., x0[i]+InitialSimplexDelta[i], ...,
   * x0[d-1]]. */
  itkSetMacro(AutomaticInitialSimplex, bool);
  itkBooleanMacro(AutomaticInitialSimplex);
  itkGetConstMacro(AutomaticInitialSimplex, bool);

  /** Set/Get the mode that determines if we want to use multiple runs of the
   * Amoeba optimizer. If true, then the optimizer is rerun after it converges.
   * The additional runs are performed using a simplex initialized with the
   * best solution obtained by the previous runs. The edge length is half of
   * that from the previous iteration.
   */
  itkSetMacro(OptimizeWithRestarts, bool);
  itkBooleanMacro(OptimizeWithRestarts);
  itkGetConstMacro(OptimizeWithRestarts, bool);

  /** Set/Get the deltas that are used to define the initial simplex
   * when AutomaticInitialSimplex is off. */
  void
  SetInitialSimplexDelta(ParametersType initialSimplexDelta, bool automaticInitialSimplex = false);
  itkGetConstMacro(InitialSimplexDelta, ParametersType);

  /** The optimization algorithm will terminate when the simplex
   * diameter and the difference in cost function values at the corners of
   * the simplex falls below user specified thresholds. The simplex
   * diameter threshold is set via SetParametersConvergenceTolerance().*/
  itkSetMacro(ParametersConvergenceTolerance, double);
  itkGetConstMacro(ParametersConvergenceTolerance, double);

  /** The optimization algorithm will terminate when the simplex
   * diameter and the difference in cost function values at the corners of
   * the simplex falls below user specified thresholds. The cost function
   * convergence threshold is set via SetFunctionConvergenceTolerance().*/
  itkSetMacro(FunctionConvergenceTolerance, double);
  itkGetConstMacro(FunctionConvergenceTolerance, double);

  /** Report the reason for stopping. */
  const std::string
  GetStopConditionDescription() const override;

  /** Method for getting access to the internal optimizer. */
  vnl_amoeba *
  GetOptimizer() const;

protected:
  AmoebaOptimizerv4();
  ~AmoebaOptimizerv4() override;
  void
  PrintSelf(std::ostream & os, Indent indent) const override;

  using CostFunctionAdaptorType = Superclass::CostFunctionAdaptorType;

private:
  /**Check that the settings are valid. If not throw an exception.*/
  void
  ValidateSettings();

  ParametersType::ValueType   m_ParametersConvergenceTolerance{};
  MeasureType                 m_FunctionConvergenceTolerance{};
  bool                        m_AutomaticInitialSimplex{};
  ParametersType              m_InitialSimplexDelta{};
  bool                        m_OptimizeWithRestarts{};
  std::unique_ptr<vnl_amoeba> m_VnlOptimizer;

  std::ostringstream m_StopConditionDescription{};
};
} // end namespace itk

#endif