Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkOptimizerFactory.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkOptimizerFactory.h"
19 
20 #include <itkAmoebaOptimizer.h>
21 #include <itkConjugateGradientOptimizer.h>
22 #include <itkExhaustiveOptimizer.h>
23 #include <itkFRPROptimizer.h>
24 #include <itkGradientDescentOptimizer.h>
25 #include <itkLBFGSBOptimizer.h>
26 #include <itkLBFGSOptimizer.h>
27 #include <itkNormalVariateGenerator.h>
28 #include <itkOnePlusOneEvolutionaryOptimizer.h>
29 #include <itkPowellOptimizer.h>
30 #include <itkQuaternionRigidTransformGradientDescentOptimizer.h>
31 #include <itkRegularStepGradientDescentOptimizer.h>
32 #include <itkSPSAOptimizer.h>
33 #include <itkVersorRigid3DTransformOptimizer.h>
34 #include <itkVersorTransformOptimizer.h>
35 
36 namespace mitk
37 {
38  OptimizerFactory::OptimizerFactory() : m_OptimizerParameters(nullptr), m_NumberTransformParameters(16) {}
40  void OptimizerFactory::SetNumberOfTransformParameters(int numberTransformParameters)
41  {
42  m_NumberTransformParameters = numberTransformParameters;
43  }
44 
46  {
47  if (m_OptimizerParameters.IsNull())
48  {
49  MITK_ERROR << "No parameters set! Returning\n";
50  return nullptr;
51  }
52  int optimizer = m_OptimizerParameters->GetOptimizer();
54  {
56  OptimizerPointer->SetStepLength(m_OptimizerParameters->GetStepLengthExhaustive());
57  itk::ExhaustiveOptimizer::StepsType steps(m_NumberTransformParameters);
58  for (int i = 0; i < m_NumberTransformParameters; i++)
59  {
60  steps[i] = m_OptimizerParameters->GetNumberOfStepsExhaustive();
61  }
62 
63  OptimizerPointer->SetNumberOfSteps(steps);
64 
65  return OptimizerPointer.GetPointer();
66  }
68  {
70  OptimizerPointer->SetMaximize(m_OptimizerParameters->GetMaximize());
71  OptimizerPointer->SetLearningRate(m_OptimizerParameters->GetLearningRateGradientDescent());
72  OptimizerPointer->SetNumberOfIterations(m_OptimizerParameters->GetNumberOfIterationsGradientDescent());
73  return OptimizerPointer.GetPointer();
74  }
76  {
79  OptimizerPointer->SetMaximize(m_OptimizerParameters->GetMaximize());
80  OptimizerPointer->SetLearningRate(
81  m_OptimizerParameters->GetLearningRateQuaternionRigidTransformGradientDescent());
82  OptimizerPointer->SetNumberOfIterations(
83  m_OptimizerParameters->GetNumberOfIterationsQuaternionRigidTransformGradientDescent());
84  return OptimizerPointer.GetPointer();
85  }
86  else if (optimizer == OptimizerParameters::LBFGSBOPTIMIZER)
87  {
89  // Set up boundary conditions
90  itk::LBFGSBOptimizer::BoundValueType lower(12);
91  itk::LBFGSBOptimizer::BoundValueType upper(12);
92  itk::LBFGSBOptimizer::BoundSelectionType select(12);
93 
94  lower.Fill(-1);
95  upper.Fill(10);
96  select.Fill(2);
97 
98  OptimizerPointer->SetLowerBound(lower);
99  OptimizerPointer->SetUpperBound(upper);
100  OptimizerPointer->SetBoundSelection(select);
101  OptimizerPointer->SetCostFunctionConvergenceFactor(1e+1);
102  OptimizerPointer->SetMaximumNumberOfCorrections(5);
103  OptimizerPointer->SetProjectedGradientTolerance(1e-5);
104  OptimizerPointer->SetMaximumNumberOfEvaluations(500);
105  OptimizerPointer->SetMaximumNumberOfIterations(200);
106  return OptimizerPointer.GetPointer();
107  }
109  {
111  OptimizerPointer->SetMaximize(m_OptimizerParameters->GetMaximize());
113  generator->Initialize(12345);
114  OptimizerPointer->SetNormalVariateGenerator(generator);
115  OptimizerPointer->SetGrowthFactor(m_OptimizerParameters->GetGrowthFactorOnePlusOneEvolutionary());
116  OptimizerPointer->SetShrinkFactor(m_OptimizerParameters->GetShrinkFactorOnePlusOneEvolutionary());
117  OptimizerPointer->SetEpsilon(m_OptimizerParameters->GetEpsilonOnePlusOneEvolutionary());
118  OptimizerPointer->SetInitialRadius(m_OptimizerParameters->GetInitialRadiusOnePlusOneEvolutionary());
119  OptimizerPointer->SetMaximumIteration(m_OptimizerParameters->GetNumberOfIterationsOnePlusOneEvolutionary());
120  return OptimizerPointer.GetPointer();
121  }
122  else if (optimizer == OptimizerParameters::POWELLOPTIMIZER)
123  {
125  OptimizerPointer->SetMaximize(m_OptimizerParameters->GetMaximize());
126  OptimizerPointer->SetStepLength(m_OptimizerParameters->GetStepLengthPowell());
127  OptimizerPointer->SetStepTolerance(m_OptimizerParameters->GetStepTolerancePowell());
128  OptimizerPointer->SetValueTolerance(m_OptimizerParameters->GetValueTolerancePowell());
129  OptimizerPointer->SetMaximumIteration(m_OptimizerParameters->GetNumberOfIterationsPowell());
130  return OptimizerPointer.GetPointer();
131  }
132  else if (optimizer == OptimizerParameters::FRPROPTIMIZER)
133  {
135  OptimizerPointer->SetMaximize(m_OptimizerParameters->GetMaximize());
136  OptimizerPointer->SetStepLength(m_OptimizerParameters->GetStepLengthFRPR());
137  if (m_OptimizerParameters->GetFletchReevesFRPR())
138  {
139  OptimizerPointer->SetToFletchReeves();
140  }
141  else if (m_OptimizerParameters->GetPolakRibiereFRPR())
142  {
143  OptimizerPointer->SetToPolakRibiere();
144  }
145  OptimizerPointer->SetMaximumIteration(m_OptimizerParameters->GetNumberOfIterationsFRPR());
146  return OptimizerPointer.GetPointer();
147  }
149  {
152  OptimizerPointer->SetMaximize(m_OptimizerParameters->GetMaximize());
153  OptimizerPointer->SetGradientMagnitudeTolerance(
154  m_OptimizerParameters->GetGradientMagnitudeToleranceRegularStepGradientDescent());
155  OptimizerPointer->SetMinimumStepLength(m_OptimizerParameters->GetMinimumStepLengthRegularStepGradientDescent());
156  OptimizerPointer->SetMaximumStepLength(m_OptimizerParameters->GetMaximumStepLengthRegularStepGradientDescent());
157  OptimizerPointer->SetNumberOfIterations(m_OptimizerParameters->GetNumberOfIterationsRegularStepGradientDescent());
158  OptimizerPointer->SetRelaxationFactor(m_OptimizerParameters->GetRelaxationFactorRegularStepGradientDescent());
159  return OptimizerPointer.GetPointer();
160  }
162  {
164  OptimizerPointer->SetMaximize(m_OptimizerParameters->GetMaximize());
165  OptimizerPointer->SetGradientMagnitudeTolerance(
166  m_OptimizerParameters->GetGradientMagnitudeToleranceVersorRigid3DTransform());
167  OptimizerPointer->SetMinimumStepLength(m_OptimizerParameters->GetMinimumStepLengthVersorRigid3DTransform());
168  OptimizerPointer->SetMaximumStepLength(m_OptimizerParameters->GetMaximumStepLengthVersorRigid3DTransform());
169  OptimizerPointer->SetNumberOfIterations(m_OptimizerParameters->GetNumberOfIterationsVersorRigid3DTransform());
170  return OptimizerPointer.GetPointer();
171  }
172  else if (optimizer == OptimizerParameters::VERSORTRANSFORMOPTIMIZER)
173  {
175  OptimizerPointer->SetMaximize(m_OptimizerParameters->GetMaximize());
176  OptimizerPointer->SetGradientMagnitudeTolerance(
177  m_OptimizerParameters->GetGradientMagnitudeToleranceVersorTransform());
178  OptimizerPointer->SetMinimumStepLength(m_OptimizerParameters->GetMinimumStepLengthVersorTransform());
179  OptimizerPointer->SetMaximumStepLength(m_OptimizerParameters->GetMaximumStepLengthVersorTransform());
180  OptimizerPointer->SetNumberOfIterations(m_OptimizerParameters->GetNumberOfIterationsVersorTransform());
181  return OptimizerPointer.GetPointer();
182  }
183  else if (optimizer == OptimizerParameters::AMOEBAOPTIMIZER)
184  {
186  OptimizerPointer->SetMaximize(m_OptimizerParameters->GetMaximize());
187  OptimizerPointer->SetParametersConvergenceTolerance(
188  m_OptimizerParameters->GetParametersConvergenceToleranceAmoeba());
189  OptimizerPointer->SetFunctionConvergenceTolerance(m_OptimizerParameters->GetFunctionConvergenceToleranceAmoeba());
190  itk::Array<double> simplexDeltaAmoeba = m_OptimizerParameters->GetSimplexDeltaAmoeba();
191  OptimizerType::ParametersType simplexDelta(m_NumberTransformParameters);
192  for (int i = 0; i < m_NumberTransformParameters; i++)
193  {
194  simplexDelta[i] = simplexDeltaAmoeba[i];
195  }
196  OptimizerPointer->AutomaticInitialSimplexOff();
197  OptimizerPointer->SetInitialSimplexDelta(simplexDelta);
198  OptimizerPointer->SetMaximumNumberOfIterations(m_OptimizerParameters->GetNumberOfIterationsAmoeba());
199  return OptimizerPointer.GetPointer();
200  }
202  {
204  OptimizerPointer->SetMaximize(m_OptimizerParameters->GetMaximize());
205  /*typedef itk::ConjugateGradientOptimizer::InternalOptimizerType vnlOptimizerType;
206  vnlOptimizerType * vnlOptimizer = OptimizerPointer->GetOptimizer();
207  vnlOptimizer->set_f_tolerance( 1e-3 );
208  vnlOptimizer->set_g_tolerance( 1e-4 );
209  vnlOptimizer->set_x_tolerance( 1e-8 );
210  vnlOptimizer->set_epsilon_function( 1e-10 );
211  vnlOptimizer->set_max_function_evals( m_Iterations );
212  vnlOptimizer->set_check_derivatives( 3 );*/
213 
214  return OptimizerPointer.GetPointer();
215  }
216  else if (optimizer == OptimizerParameters::LBFGSOPTIMIZER)
217  {
219  OptimizerPointer->SetMaximize(m_OptimizerParameters->GetMaximize());
220  OptimizerPointer->SetGradientConvergenceTolerance(m_OptimizerParameters->GetGradientConvergenceToleranceLBFGS());
221  OptimizerPointer->SetLineSearchAccuracy(m_OptimizerParameters->GetLineSearchAccuracyLBFGS());
222  OptimizerPointer->SetDefaultStepLength(m_OptimizerParameters->GetDefaultStepLengthLBFGS());
223  OptimizerPointer->SetTrace(m_OptimizerParameters->GetTraceOnLBFGS());
224  OptimizerPointer->SetMaximumNumberOfFunctionEvaluations(m_OptimizerParameters->GetNumberOfIterationsLBFGS());
225  return OptimizerPointer.GetPointer();
226  }
227  else if (optimizer == OptimizerParameters::SPSAOPTIMIZER)
228  {
230  OptimizerPointer->SetMaximize(m_OptimizerParameters->GetMaximize());
231  OptimizerPointer->Seta(m_OptimizerParameters->GetaSPSA());
232  OptimizerPointer->SetA(m_OptimizerParameters->GetASPSA());
233  OptimizerPointer->SetAlpha(m_OptimizerParameters->GetAlphaSPSA());
234  OptimizerPointer->Setc(m_OptimizerParameters->GetcSPSA());
235  OptimizerPointer->SetGamma(m_OptimizerParameters->GetGammaSPSA());
236  OptimizerPointer->SetTolerance(m_OptimizerParameters->GetToleranceSPSA());
237  OptimizerPointer->SetStateOfConvergenceDecayRate(m_OptimizerParameters->GetStateOfConvergenceDecayRateSPSA());
238  OptimizerPointer->SetMinimumNumberOfIterations(m_OptimizerParameters->GetMinimumNumberOfIterationsSPSA());
239  OptimizerPointer->SetNumberOfPerturbations(m_OptimizerParameters->GetNumberOfPerturbationsSPSA());
240  OptimizerPointer->SetMaximumNumberOfIterations(m_OptimizerParameters->GetNumberOfIterationsSPSA());
241  return OptimizerPointer.GetPointer();
242  }
243  return nullptr;
244  }
245 } // end namespace
itk::SmartPointer< Self > Pointer
OptimizerType::Pointer GetOptimizer()
Returns the optimizer which then can be used in combination with a transform, a metric and an interpo...
#define MITK_ERROR
Definition: mitkLogMacros.h:24
DataCollection - Class to facilitate loading/accessing structured data.
void SetNumberOfTransformParameters(int numberTransformParameters)
Sets the number of transformParameters.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.