File: elxCMAEvolutionStrategy.hxx

package info (click to toggle)
elastix 5.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,480 kB
  • sloc: cpp: 68,403; lisp: 4,118; python: 1,013; xml: 182; sh: 177; makefile: 33
file content (300 lines) | stat: -rw-r--r-- 10,087 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*=========================================================================
 *
 *  Copyright UMC Utrecht and contributors
 *
 *  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
 *
 *        http://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 elxCMAEvolutionStrategy_hxx
#define elxCMAEvolutionStrategy_hxx

#include "elxCMAEvolutionStrategy.h"
#include <iomanip>
#include <string>
#include <vnl/vnl_math.h>

namespace elastix
{

/**
 * ***************** StartOptimization ************************
 */

template <class TElastix>
void
CMAEvolutionStrategy<TElastix>::StartOptimization()
{
  /** Check if the entered scales are correct and != [ 1 1 1 ...] */
  this->SetUseScales(false);
  const ScalesType & scales = this->GetScales();
  if (scales.GetSize() == this->GetInitialPosition().GetSize())
  {
    ScalesType unit_scales(scales.GetSize(), 1.0);
    if (scales != unit_scales)
    {
      /** only then: */
      this->SetUseScales(true);
    }
  }

  /** Call the superclass */
  this->Superclass1::StartOptimization();

} // end StartOptimization


/**
 * ***************** InitializeProgressVariables ************************
 */

template <class TElastix>
void
CMAEvolutionStrategy<TElastix>::InitializeProgressVariables()
{
  this->Superclass1::InitializeProgressVariables();

  /** Print some settings that can be automatically determined by the optimizer. */
  log::info(std::ostringstream{} << "The CMAEvolutionStrategy optimizer uses the following settings:\n"
                                 << "PopulationSize = " << this->GetPopulationSize() << "\n"
                                 << "NumberOfParents = " << this->GetNumberOfParents() << "\n"
                                 << "UseCovarianceMatrixAdaptation = " << this->GetUseCovarianceMatrixAdaptation()
                                 << "\n"
                                 << "UpdateBDPeriod = " << this->GetUpdateBDPeriod() << "\n");

} // end InitializeProgressVariables


/**
 * ***************** BeforeRegistration ***********************
 */

template <class TElastix>
void
CMAEvolutionStrategy<TElastix>::BeforeRegistration()
{
  /** Add target cells to xout[IterationInfo.*/
  this->AddTargetCellToIterationInfo("2:Metric");
  this->AddTargetCellToIterationInfo("3:StepLength");
  this->AddTargetCellToIterationInfo("4:||Step||");
  this->AddTargetCellToIterationInfo("5a:Sigma");
  this->AddTargetCellToIterationInfo("5b:MaximumD");
  this->AddTargetCellToIterationInfo("5c:MinimumD");

  /** Format the metric and stepsize as floats */
  this->GetIterationInfoAt("2:Metric") << std::showpoint << std::fixed;
  this->GetIterationInfoAt("3:StepLength") << std::showpoint << std::fixed;
  this->GetIterationInfoAt("4:||Step||") << std::showpoint << std::fixed;
  this->GetIterationInfoAt("5a:Sigma") << std::showpoint << std::fixed;
  this->GetIterationInfoAt("5b:MaximumD") << std::showpoint << std::fixed;
  this->GetIterationInfoAt("5c:MinimumD") << std::showpoint << std::fixed;

} // end BeforeRegistration


/**
 * ***************** BeforeEachResolution ***********************
 */

template <class TElastix>
void
CMAEvolutionStrategy<TElastix>::BeforeEachResolution()
{
  /** Get the current resolution level.*/
  unsigned int level = static_cast<unsigned int>(this->m_Registration->GetAsITKBaseType()->GetCurrentLevel());

  /** Set MaximumNumberOfIterations.*/
  unsigned int maximumNumberOfIterations = 500;
  this->m_Configuration->ReadParameter(
    maximumNumberOfIterations, "MaximumNumberOfIterations", this->GetComponentLabel(), level, 0);
  this->SetMaximumNumberOfIterations(maximumNumberOfIterations);

  /** Set the length of the initial step (InitialSigma). */
  double stepLength = 1.0;
  this->m_Configuration->ReadParameter(stepLength, "StepLength", this->GetComponentLabel(), level, 0);
  this->SetInitialSigma(stepLength);

  /** Set ValueTolerance */
  double valueTolerance = 0.00001;
  this->m_Configuration->ReadParameter(valueTolerance, "ValueTolerance", this->GetComponentLabel(), level, 0);
  this->SetValueTolerance(valueTolerance);

  /** Set PopulationSize */
  unsigned int populationSize = 0;
  this->m_Configuration->ReadParameter(populationSize, "PopulationSize", this->GetComponentLabel(), level, 0);
  this->SetPopulationSize(populationSize);

  /** Set NumberOfParents */
  unsigned int numberOfParents = 0;
  this->m_Configuration->ReadParameter(numberOfParents, "NumberOfParents", this->GetComponentLabel(), level, 0);
  this->SetNumberOfParents(numberOfParents);

  /** Set UseDecayingSigma */
  bool useDecayingSigma = false;
  this->m_Configuration->ReadParameter(useDecayingSigma, "UseDecayingSigma", this->GetComponentLabel(), level, 0);
  this->SetUseDecayingSigma(useDecayingSigma);

  /** Set SigmaDecayA */
  double sigmaDecayA = 50.0;
  this->m_Configuration->ReadParameter(sigmaDecayA, "SP_A", this->GetComponentLabel(), level, 0);
  this->SetSigmaDecayA(sigmaDecayA);

  /** Set SigmaDecayAlpha */
  double sigmaDecayAlpha = 0.602;
  this->m_Configuration->ReadParameter(sigmaDecayAlpha, "SP_alpha", this->GetComponentLabel(), level, 0);
  this->SetSigmaDecayAlpha(sigmaDecayAlpha);

  /** Set UseCovarianceMatrixAdaptation */
  bool useCovarianceMatrixAdaptation = true;
  this->m_Configuration->ReadParameter(
    useCovarianceMatrixAdaptation, "UseCovarianceMatrixAdaptation", this->GetComponentLabel(), level, 0);
  this->SetUseCovarianceMatrixAdaptation(useCovarianceMatrixAdaptation);

  /** Set RecombinationWeightsPreset */
  std::string recombinationWeightsPreset = "superlinear";
  this->m_Configuration->ReadParameter(
    recombinationWeightsPreset, "RecombinationWeightsPreset", this->GetComponentLabel(), level, 0);
  this->SetRecombinationWeightsPreset(recombinationWeightsPreset);

  /** Set UpdateBDPeriod */
  unsigned int updateBDPeriod = 0;
  this->m_Configuration->ReadParameter(updateBDPeriod, "UpdateBDPeriod", this->GetComponentLabel(), level, 0);
  this->SetUpdateBDPeriod(updateBDPeriod);

  /** Set PositionToleranceMin */
  double positionToleranceMin = 1e-8;
  this->m_Configuration->ReadParameter(
    positionToleranceMin, "PositionToleranceMin", this->GetComponentLabel(), level, 0);
  this->SetPositionToleranceMin(positionToleranceMin);

  /** Set PositionToleranceMax */
  double positionToleranceMax = 1e8;
  this->m_Configuration->ReadParameter(
    positionToleranceMax, "PositionToleranceMax", this->GetComponentLabel(), level, 0);
  this->SetPositionToleranceMax(positionToleranceMax);

  /** Set MaximumDeviation */
  double maximumDeviation = 10.0 * positionToleranceMax * stepLength;
  this->m_Configuration->ReadParameter(maximumDeviation, "MaximumDeviation", this->GetComponentLabel(), level, 0);
  this->SetMaximumDeviation(maximumDeviation);

  /** Set MinimumDeviation */
  double minimumDeviation = 0.0;
  this->m_Configuration->ReadParameter(minimumDeviation, "MinimumDeviation", this->GetComponentLabel(), level, 0);
  this->SetMinimumDeviation(minimumDeviation);

} // end BeforeEachResolution


/**
 * ***************** AfterEachIteration *************************
 */

template <class TElastix>
void
CMAEvolutionStrategy<TElastix>::AfterEachIteration()
{
  /** Print some information. */
  this->GetIterationInfoAt("2:Metric") << this->GetCurrentValue();
  this->GetIterationInfoAt("3:StepLength") << this->GetCurrentStepLength();
  this->GetIterationInfoAt("4:||Step||") << this->GetCurrentScaledStep().magnitude();
  this->GetIterationInfoAt("5a:Sigma") << this->GetCurrentSigma();
  this->GetIterationInfoAt("5b:MaximumD") << this->GetCurrentMaximumD();
  this->GetIterationInfoAt("5c:MinimumD") << this->GetCurrentMinimumD();

  /** Select new samples if desired. These
   * will be used in the next iteration */
  if (this->GetNewSamplesEveryIteration())
  {
    this->SelectNewSamples();
  }

} // end AfterEachIteration


/**
 * ***************** AfterEachResolution *************************
 */

template <class TElastix>
void
CMAEvolutionStrategy<TElastix>::AfterEachResolution()
{
  /**
    enum StopConditionType {
    MetricError,
    MaximumNumberOfIterations,
    PositionToleranceMin,
    PositionToleranceMax,
    ValueTolerance,
    ZeroStepLength,
    Unknown };  */

  std::string stopcondition;

  switch (this->GetStopCondition())
  {
    case MetricError:
      stopcondition = "Error in metric";
      break;

    case MaximumNumberOfIterations:
      stopcondition = "Maximum number of iterations has been reached";
      break;

    case PositionToleranceMin:
      stopcondition = "The minimum step length condition has been reached";
      break;

    case PositionToleranceMax:
      stopcondition = "The maximum step length condition has been reached";
      break;

    case ValueTolerance:
      stopcondition = "Almost no decrease in function value anymore";
      break;

    case ZeroStepLength:
      stopcondition = "The step length is 0";
      break;

    default:
      stopcondition = "Unknown";
      break;
  }

  /** Print the stopping condition */
  log::info(std::ostringstream{} << "Stopping condition: " << stopcondition << ".");

} // end AfterEachResolution


/**
 * ******************* AfterRegistration ************************
 */

template <class TElastix>
void
CMAEvolutionStrategy<TElastix>::AfterRegistration()
{
  /** Print the best metric value */

  double bestValue = this->GetCurrentValue();
  log::info(std::ostringstream{} << '\n' << "Final metric value  = " << bestValue);

} // end AfterRegistration


} // end namespace elastix

#endif // end #ifndef elxCMAEvolutionStrategy_hxx