ITK  6.0.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  * https://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 
68  itkOverrideGetNameOfClassMacro(ParticleSwarmOptimizerBase);
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);
104  void
105  SetInitialSwarm(const SwarmType & initialSwarm);
106  void
107  ClearSwarm();
115  itkSetMacro(PrintSwarm, bool);
116  itkGetMacro(PrintSwarm, bool);
117  itkBooleanMacro(PrintSwarm);
121  void
122  StartOptimization() override;
123 
124 
127  void
128  SetNumberOfParticles(NumberOfParticlesType n);
129  itkGetMacro(NumberOfParticles, NumberOfParticlesType);
134  itkSetMacro(MaximalNumberOfIterations, NumberOfIterationsType);
135  itkGetMacro(MaximalNumberOfIterations, NumberOfIterationsType);
142  itkSetMacro(NumberOfGenerationsWithMinimalImprovement, NumberOfGenerationsType);
143  itkGetMacro(NumberOfGenerationsWithMinimalImprovement, NumberOfGenerationsType);
149  virtual void
150  SetParameterBounds(ParameterBoundsType & bounds);
151  void
152  SetParameterBounds(std::pair<ParametersType::ValueType, ParametersType::ValueType> & bounds, unsigned int n);
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);
188  itkSetMacro(Seed, RandomVariateGeneratorType::IntegerType);
189  itkGetMacro(Seed, RandomVariateGeneratorType::IntegerType);
195  itkSetMacro(UseSeed, bool);
196  itkGetMacro(UseSeed, bool);
197  itkBooleanMacro(UseSeed);
204  GetValue() const;
205 
207  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 
244  bool m_PrintSwarm{};
245  std::ostringstream m_StopConditionDescription{};
246  bool m_InitializeNormalDistribution{};
247  NumberOfParticlesType m_NumberOfParticles{};
248  NumberOfIterationsType m_MaximalNumberOfIterations{};
249  NumberOfGenerationsType m_NumberOfGenerationsWithMinimalImprovement{};
250  ParameterBoundsType m_ParameterBounds{};
251  ParametersType m_ParametersConvergenceTolerance{};
252  double m_PercentageParticlesConverged{};
253  CostFunctionType::MeasureType m_FunctionConvergenceTolerance{};
254  std::vector<ParticleData> m_Particles{};
255  CostFunctionType::MeasureType m_FunctionBestValue{ 0 };
256  std::vector<MeasureType> m_FunctionBestValueMemory{};
257  ParametersType m_ParametersBestValue{};
258  NumberOfIterationsType m_IterationIndex{ 0 };
260  bool m_UseSeed{};
261 };
262 } // end namespace itk
263 
264 #endif
itk::SingleValuedCostFunction::MeasureType
double MeasureType
Definition: itkSingleValuedCostFunction.h:50
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::ParticleData::m_CurrentVelocity
ParametersType m_CurrentVelocity
Definition: itkParticleSwarmOptimizerBase.h:75
itk::ParticleSwarmOptimizerBase::SwarmType
std::vector< ParticleData > SwarmType
Definition: itkParticleSwarmOptimizerBase.h:81
itk::ParticleSwarmOptimizerBase::MeasureType
CostFunctionType::MeasureType MeasureType
Definition: itkParticleSwarmOptimizerBase.h:85
itk::ParticleSwarmOptimizerBase::NumberOfParticlesType
unsigned int NumberOfParticlesType
Definition: itkParticleSwarmOptimizerBase.h:83
itk::OptimizerParameters< double >::ValueType
double ValueType
Definition: itkOptimizerParameters.h:40
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::ParticleSwarmOptimizerBase::ParticleData::m_CurrentParameters
ParametersType m_CurrentParameters
Definition: itkParticleSwarmOptimizerBase.h:74
itk::ParticleSwarmOptimizerBase::NumberOfIterationsType
unsigned int NumberOfIterationsType
Definition: itkParticleSwarmOptimizerBase.h:82
itk::ParticleSwarmOptimizerBase
Abstract implementation of a Particle Swarm Optimization (PSO) algorithm.
Definition: itkParticleSwarmOptimizerBase.h:56
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::ParticleSwarmOptimizerBase::ParticleData::m_CurrentValue
CostFunctionType::MeasureType m_CurrentValue
Definition: itkParticleSwarmOptimizerBase.h:76
itkMersenneTwisterRandomVariateGenerator.h
itk::Statistics::MersenneTwisterRandomVariateGenerator::IntegerType
uint32_t IntegerType
Definition: itkMersenneTwisterRandomVariateGenerator.h:136
itk::ParticleSwarmOptimizerBase::ValueType
ParametersType::ValueType ValueType
Definition: itkParticleSwarmOptimizerBase.h:86
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
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
itkSingleValuedNonLinearOptimizer.h
itk::ParticleSwarmOptimizerBase::ParticleData::m_BestParameters
ParametersType m_BestParameters
Definition: itkParticleSwarmOptimizerBase.h:77
itk::ParticleSwarmOptimizerBase::NumberOfGenerationsType
unsigned int NumberOfGenerationsType
Definition: itkParticleSwarmOptimizerBase.h:84