ITK  5.3.0
Insight Toolkit
itkParticleSwarmOptimizerBase.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 {
57 {
58 public:
59  ITK_DISALLOW_COPY_AND_MOVE(ParticleSwarmOptimizerBase);
60 
66 
69 
70  using ParameterBoundsType = std::vector<std::pair<ParametersType::ValueType, ParametersType::ValueType>>;
71 
72  struct ParticleData
73  {
74  ParametersType m_CurrentParameters;
75  ParametersType m_CurrentVelocity;
77  ParametersType m_BestParameters;
79  };
80 
81  using SwarmType = std::vector<ParticleData>;
82  using NumberOfIterationsType = unsigned int;
83  using NumberOfParticlesType = unsigned int;
84  using NumberOfGenerationsType = unsigned int;
88 
94  itkSetMacro(InitializeNormalDistribution, bool);
95  itkGetMacro(InitializeNormalDistribution, bool);
96  itkBooleanMacro(InitializeNormalDistribution);
98 
104  void
105  SetInitialSwarm(const SwarmType & initialSwarm);
106  void
107  ClearSwarm();
109 
115  itkSetMacro(PrintSwarm, bool);
116  itkGetMacro(PrintSwarm, bool);
117  itkBooleanMacro(PrintSwarm);
119 
121  void
122  StartOptimization() override;
123 
124 
127  void
128  SetNumberOfParticles(NumberOfParticlesType n);
129  itkGetMacro(NumberOfParticles, NumberOfParticlesType);
131 
134  itkSetMacro(MaximalNumberOfIterations, NumberOfIterationsType);
135  itkGetMacro(MaximalNumberOfIterations, NumberOfIterationsType);
137 
142  itkSetMacro(NumberOfGenerationsWithMinimalImprovement, NumberOfGenerationsType);
143  itkGetMacro(NumberOfGenerationsWithMinimalImprovement, NumberOfGenerationsType);
145 
149  virtual void
150  SetParameterBounds(ParameterBoundsType & bounds);
151  void
152  SetParameterBounds(std::pair<ParametersType::ValueType, ParametersType::ValueType> & bounds, unsigned int n);
154 
156  GetParameterBounds() const;
157 
172  itkSetMacro(FunctionConvergenceTolerance, MeasureType);
173  itkGetMacro(FunctionConvergenceTolerance, MeasureType);
174 
177  void
178  SetParametersConvergenceTolerance(ValueType convergenceTolerance, unsigned int sz);
179  itkSetMacro(ParametersConvergenceTolerance, ParametersType);
180  itkGetMacro(ParametersConvergenceTolerance, ParametersType);
181  itkGetMacro(PercentageParticlesConverged, double);
182  itkSetMacro(PercentageParticlesConverged, double);
184 
188  itkSetMacro(Seed, RandomVariateGeneratorType::IntegerType);
189  itkGetMacro(Seed, RandomVariateGeneratorType::IntegerType);
191 
195  itkSetMacro(UseSeed, bool);
196  itkGetMacro(UseSeed, bool);
197  itkBooleanMacro(UseSeed);
199 
204  GetValue() const;
205 
207  const std::string
208  GetStopConditionDescription() const override;
209 
214  void
215  PrintSwarm(std::ostream & os, Indent indent) const;
216 
217 protected:
219  ~ParticleSwarmOptimizerBase() override;
220  void
221  PrintSelf(std::ostream & os, Indent indent) const override;
222  void
223  PrintParamtersType(const ParametersType & x, std::ostream & os) const;
224 
227  virtual void
228  UpdateSwarm() = 0;
229 
230  virtual void
231  ValidateSettings();
232 
236  virtual void
237  Initialize();
238 
239  void
240  RandomInitialization();
241  void
242  FileInitialization();
243 
245  std::ostringstream m_StopConditionDescription;
254  std::vector<ParticleData> m_Particles;
255  CostFunctionType::MeasureType m_FunctionBestValue{ 0 };
256  std::vector<MeasureType> m_FunctionBestValueMemory;
258  NumberOfIterationsType m_IterationIndex{ 0 };
260  bool m_UseSeed;
261 };
262 } // end namespace itk
263 
264 #endif
itk::ParticleSwarmOptimizerBase::m_FunctionConvergenceTolerance
CostFunctionType::MeasureType m_FunctionConvergenceTolerance
Definition: itkParticleSwarmOptimizerBase.h:253
itk::SingleValuedCostFunction::MeasureType
double MeasureType
Definition: itkSingleValuedCostFunction.h:50
itk::OptimizerParameters< double >
itk::Statistics::MersenneTwisterRandomVariateGenerator
MersenneTwisterRandom random variate generator.
Definition: itkMersenneTwisterRandomVariateGenerator.h:127
itk::ParticleSwarmOptimizerBase::ParticleData
Definition: itkParticleSwarmOptimizerBase.h:72
itk::SingleValuedNonLinearOptimizer
This class is a base for the Optimization methods that optimize a single valued function.
Definition: itkSingleValuedNonLinearOptimizer.h:35
itk::ParticleSwarmOptimizerBase::m_NumberOfParticles
NumberOfParticlesType m_NumberOfParticles
Definition: itkParticleSwarmOptimizerBase.h:247
itk::ParticleSwarmOptimizerBase::ParticleData::m_CurrentVelocity
ParametersType m_CurrentVelocity
Definition: itkParticleSwarmOptimizerBase.h:75
itk::ParticleSwarmOptimizerBase::m_InitializeNormalDistribution
bool m_InitializeNormalDistribution
Definition: itkParticleSwarmOptimizerBase.h:246
itk::ParticleSwarmOptimizerBase::m_FunctionBestValueMemory
std::vector< MeasureType > m_FunctionBestValueMemory
Definition: itkParticleSwarmOptimizerBase.h:256
itk::ParticleSwarmOptimizerBase::SwarmType
std::vector< ParticleData > SwarmType
Definition: itkParticleSwarmOptimizerBase.h:81
itk::ParticleSwarmOptimizerBase::MeasureType
CostFunctionType::MeasureType MeasureType
Definition: itkParticleSwarmOptimizerBase.h:85
itk::ParticleSwarmOptimizerBase::m_ParameterBounds
ParameterBoundsType m_ParameterBounds
Definition: itkParticleSwarmOptimizerBase.h:250
itk::ParticleSwarmOptimizerBase::NumberOfParticlesType
unsigned int NumberOfParticlesType
Definition: itkParticleSwarmOptimizerBase.h:83
itk::ParticleSwarmOptimizerBase::m_MaximalNumberOfIterations
NumberOfIterationsType m_MaximalNumberOfIterations
Definition: itkParticleSwarmOptimizerBase.h:248
itk::OptimizerParameters< double >::ValueType
double ValueType
Definition: itkOptimizerParameters.h:40
itk::ParticleSwarmOptimizerBase::m_NumberOfGenerationsWithMinimalImprovement
NumberOfGenerationsType m_NumberOfGenerationsWithMinimalImprovement
Definition: itkParticleSwarmOptimizerBase.h:249
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::ParticleSwarmOptimizerBase::m_ParametersConvergenceTolerance
ParametersType m_ParametersConvergenceTolerance
Definition: itkParticleSwarmOptimizerBase.h:251
itk::ParticleSwarmOptimizerBase::ParticleData::m_CurrentParameters
ParametersType m_CurrentParameters
Definition: itkParticleSwarmOptimizerBase.h:74
itk::ParticleSwarmOptimizerBase::NumberOfIterationsType
unsigned int NumberOfIterationsType
Definition: itkParticleSwarmOptimizerBase.h:82
itk::ParticleSwarmOptimizerBase::m_PercentageParticlesConverged
double m_PercentageParticlesConverged
Definition: itkParticleSwarmOptimizerBase.h:252
itk::ParticleSwarmOptimizerBase
Abstract implementation of a Particle Swarm Optimization (PSO) algorithm.
Definition: itkParticleSwarmOptimizerBase.h:56
itk::ParticleSwarmOptimizerBase::m_UseSeed
bool m_UseSeed
Definition: itkParticleSwarmOptimizerBase.h:260
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::ParticleSwarmOptimizerBase::m_ParametersBestValue
ParametersType m_ParametersBestValue
Definition: itkParticleSwarmOptimizerBase.h:257
itk::ParticleSwarmOptimizerBase::ParticleData::m_CurrentValue
CostFunctionType::MeasureType m_CurrentValue
Definition: itkParticleSwarmOptimizerBase.h:76
itkMersenneTwisterRandomVariateGenerator.h
itk::ParticleSwarmOptimizerBase::m_PrintSwarm
bool m_PrintSwarm
Definition: itkParticleSwarmOptimizerBase.h:244
itk::Statistics::MersenneTwisterRandomVariateGenerator::IntegerType
uint32_t IntegerType
Definition: itkMersenneTwisterRandomVariateGenerator.h:136
itk::ParticleSwarmOptimizerBase::ValueType
ParametersType::ValueType ValueType
Definition: itkParticleSwarmOptimizerBase.h:86
itk::ParticleSwarmOptimizerBase::m_Seed
RandomVariateGeneratorType::IntegerType m_Seed
Definition: itkParticleSwarmOptimizerBase.h:259
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ParticleSwarmOptimizerBase::ParticleData::m_BestValue
CostFunctionType::MeasureType m_BestValue
Definition: itkParticleSwarmOptimizerBase.h:78
itk::ParticleSwarmOptimizerBase::ParameterBoundsType
std::vector< std::pair< ParametersType::ValueType, ParametersType::ValueType > > ParameterBoundsType
Definition: itkParticleSwarmOptimizerBase.h:70
itk::ParticleSwarmOptimizerBase::m_Particles
std::vector< ParticleData > m_Particles
Definition: itkParticleSwarmOptimizerBase.h:254
itkSingleValuedNonLinearOptimizer.h
itk::ParticleSwarmOptimizerBase::ParticleData::m_BestParameters
ParametersType m_BestParameters
Definition: itkParticleSwarmOptimizerBase.h:77
itk::ParticleSwarmOptimizerBase::m_StopConditionDescription
std::ostringstream m_StopConditionDescription
Definition: itkParticleSwarmOptimizerBase.h:245
itk::ParticleSwarmOptimizerBase::NumberOfGenerationsType
unsigned int NumberOfGenerationsType
Definition: itkParticleSwarmOptimizerBase.h:84