ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkParticleSwarmOptimizerBase.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 
19 #ifndef itkParticleSwarmOptimizerBase_h
20 #define itkParticleSwarmOptimizerBase_h
21 
24 #include "ITKOptimizersExport.h"
25 
26 namespace itk
27 {
56 class ITKOptimizers_EXPORT ParticleSwarmOptimizerBase :
58 {
59 public:
60  ITK_DISALLOW_COPY_AND_ASSIGN(ParticleSwarmOptimizerBase);
61 
67 
70 
71  using ParameterBoundsType = std::vector< std::pair<ParametersType::ValueType,
72  ParametersType::ValueType> >;
73 
74  struct ParticleData
75  {
76  ParametersType m_CurrentParameters;
77  ParametersType m_CurrentVelocity;
79  ParametersType m_BestParameters;
81  };
82 
83  using SwarmType = std::vector<ParticleData>;
84  using NumberOfIterationsType = unsigned int;
85  using NumberOfParticlesType = unsigned int;
86  using NumberOfGenerationsType = unsigned int;
90 
96  itkSetMacro( InitializeNormalDistribution, bool )
97  itkGetMacro( InitializeNormalDistribution, bool )
98  itkBooleanMacro( InitializeNormalDistribution )
99 
105  void SetInitialSwarm( const SwarmType &initialSwarm );
106  void ClearSwarm();
108 
114  itkSetMacro( PrintSwarm, bool )
115  itkGetMacro( PrintSwarm, bool )
116  itkBooleanMacro( PrintSwarm )
117 
119  void StartOptimization() override;
120 
121 
124  void SetNumberOfParticles( NumberOfParticlesType n );
125  itkGetMacro( NumberOfParticles, NumberOfParticlesType )
126 
129  itkSetMacro( MaximalNumberOfIterations, NumberOfIterationsType )
130  itkGetMacro( MaximalNumberOfIterations, NumberOfIterationsType )
131 
136  itkSetMacro( NumberOfGenerationsWithMinimalImprovement, NumberOfGenerationsType )
137  itkGetMacro( NumberOfGenerationsWithMinimalImprovement, NumberOfGenerationsType )
138 
142  virtual void SetParameterBounds( ParameterBoundsType & bounds );
143  void SetParameterBounds( std::pair<ParametersType::ValueType,
144  ParametersType::ValueType> &bounds,
145  unsigned int n );
147 
148  ParameterBoundsType GetParameterBounds() const;
149 
164  itkSetMacro( FunctionConvergenceTolerance, MeasureType )
165  itkGetMacro( FunctionConvergenceTolerance, MeasureType )
166 
169  void SetParametersConvergenceTolerance( ValueType convergenceTolerance,
170  unsigned int sz );
171  itkSetMacro( ParametersConvergenceTolerance, ParametersType )
172  itkGetMacro( ParametersConvergenceTolerance, ParametersType )
173  itkGetMacro( PercentageParticlesConverged, double )
174  itkSetMacro( PercentageParticlesConverged, double )
175 
179  itkSetMacro( Seed, RandomVariateGeneratorType::IntegerType)
180  itkGetMacro( Seed, RandomVariateGeneratorType::IntegerType)
181 
185  itkSetMacro( UseSeed, bool )
186  itkGetMacro( UseSeed, bool )
187  itkBooleanMacro( UseSeed)
188 
192  MeasureType GetValue() const;
193 
195  const std::string GetStopConditionDescription() const override;
196 
201  void PrintSwarm( std::ostream& os, Indent indent ) const;
202 
203 protected:
205  ~ParticleSwarmOptimizerBase() override;
206  void PrintSelf( std::ostream& os, Indent indent ) const override;
207  void PrintParamtersType( const ParametersType& x, std::ostream& os ) const;
208 
211  virtual void UpdateSwarm() = 0;
212 
213  virtual void ValidateSettings();
214 
218  virtual void Initialize();
219 
220  void RandomInitialization();
221  void FileInitialization();
222 
223  bool m_PrintSwarm;
224  std::ostringstream m_StopConditionDescription;
225  bool m_InitializeNormalDistribution;
226  NumberOfParticlesType m_NumberOfParticles;
227  NumberOfIterationsType m_MaximalNumberOfIterations;
228  NumberOfGenerationsType m_NumberOfGenerationsWithMinimalImprovement;
229  ParameterBoundsType m_ParameterBounds;
230  ParametersType m_ParametersConvergenceTolerance;
231  double m_PercentageParticlesConverged;
232  CostFunctionType::MeasureType m_FunctionConvergenceTolerance;
233  std::vector<ParticleData> m_Particles;
234  CostFunctionType::MeasureType m_FunctionBestValue{0};
235  std::vector<MeasureType> m_FunctionBestValueMemory;
237  NumberOfIterationsType m_IterationIndex{0};
239  bool m_UseSeed;
240 };
241 } // end namespace itk
242 
243 #endif
This class is a base for the CostFunctions returning a single value.
Light weight base class for most itk classes.
std::vector< MeasureType > m_FunctionBestValueMemory
std::vector< std::pair< ParametersType::ValueType, ParametersType::ValueType > > ParameterBoundsType
This class is a base for the Optimization methods that optimize a single valued function.
RandomVariateGeneratorType::IntegerType m_Seed
CostFunctionType::MeasureType MeasureType
Control indentation during Print() invocation.
Definition: itkIndent.h:49
Abstract implementation of a Particle Swarm Optimization (PSO) algorithm.