ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkParticleSwarmOptimizerBase.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 
00019 #ifndef __itkParticleSwarmOptimizerBase_h
00020 #define __itkParticleSwarmOptimizerBase_h
00021 
00022 #include "itkSingleValuedNonLinearOptimizer.h"
00023 #include "itkMersenneTwisterRandomVariateGenerator.h"
00024 
00025 namespace itk
00026 {
00055 class ITK_EXPORT ParticleSwarmOptimizerBase :
00056   public SingleValuedNonLinearOptimizer
00057 {
00058 public:
00060   typedef ParticleSwarmOptimizerBase          Self;
00061   typedef SingleValuedNonLinearOptimizer      Superclass;
00062   typedef SmartPointer<Self>                  Pointer;
00063   typedef SmartPointer<const Self>            ConstPointer;
00064 
00066   itkTypeMacro( ParticleSwarmOptimizerBase, SingleValuedNonLinearOptimizer )
00067 
00068   typedef std::vector< std::pair<ParametersType::ValueType,
00069                                  ParametersType::ValueType> > ParameterBoundsType;
00070 
00071   struct ParticleData
00072   {
00073     ParametersType m_CurrentParameters;
00074     ParametersType m_CurrentVelocity;
00075     CostFunctionType::MeasureType m_CurrentValue;
00076     ParametersType m_BestParameters;
00077     CostFunctionType::MeasureType m_BestValue;
00078   };
00079 
00080   typedef std::vector<ParticleData>         SwarmType;
00081   typedef unsigned int                      NumberOfIterationsType;
00082   typedef unsigned int                      NumberOfParticlesType;
00083   typedef unsigned int                      NumberOfGenerationsType;
00084   typedef CostFunctionType::MeasureType     MeasureType;
00085   typedef ParametersType::ValueType         ValueType;
00086   typedef Statistics::MersenneTwisterRandomVariateGenerator
00087                                             RandomVariateGeneratorType;
00088 
00094   itkSetMacro( InitializeNormalDistribution, bool )
00095   itkGetMacro( InitializeNormalDistribution, bool )
00096   itkBooleanMacro( InitializeNormalDistribution )
00097 
00103   void SetInitialSwarm( const SwarmType &initialSwarm );
00104   void ClearSwarm();
00106 
00112   itkSetMacro( PrintSwarm, bool )
00113   itkGetMacro( PrintSwarm, bool )
00114   itkBooleanMacro( PrintSwarm )
00115 
00117   void StartOptimization( void );
00118 
00119 
00122   void SetNumberOfParticles( NumberOfParticlesType n );
00123   itkGetMacro( NumberOfParticles, NumberOfParticlesType )
00124 
00127   itkSetMacro( MaximalNumberOfIterations, NumberOfIterationsType )
00128   itkGetMacro( MaximalNumberOfIterations, NumberOfIterationsType )
00129 
00134   itkSetMacro( NumberOfGenerationsWithMinimalImprovement, NumberOfGenerationsType )
00135   itkGetMacro( NumberOfGenerationsWithMinimalImprovement, NumberOfGenerationsType )
00136 
00140   virtual void SetParameterBounds( ParameterBoundsType & bounds );
00141   void SetParameterBounds( std::pair<ParametersType::ValueType,
00142                            ParametersType::ValueType> &bounds,
00143                            unsigned int n );
00145 
00146   ParameterBoundsType GetParameterBounds() const;
00147 
00162   itkSetMacro( FunctionConvergenceTolerance, MeasureType )
00163   itkGetMacro( FunctionConvergenceTolerance, MeasureType )
00164 
00167   void SetParametersConvergenceTolerance( ValueType convergenceTolerance,
00168                                           unsigned int sz );
00169   itkSetMacro( ParametersConvergenceTolerance, ParametersType )
00170   itkGetMacro( ParametersConvergenceTolerance, ParametersType )
00171   itkGetMacro( PercentageParticlesConverged, double )
00172   itkSetMacro( PercentageParticlesConverged, double )
00173 
00177   itkSetMacro( Seed, RandomVariateGeneratorType::IntegerType)
00178   itkGetMacro( Seed, RandomVariateGeneratorType::IntegerType)
00179 
00183   itkSetMacro( UseSeed, bool )
00184   itkGetMacro( UseSeed, bool )
00185   itkBooleanMacro( UseSeed)
00186 
00190   MeasureType GetValue() const;
00191 
00193   virtual const std::string GetStopConditionDescription() const;
00194 
00199   void PrintSwarm( std::ostream& os, Indent indent ) const;
00200 
00201 protected:
00202   ParticleSwarmOptimizerBase();
00203   virtual ~ParticleSwarmOptimizerBase();
00204   void PrintSelf( std::ostream& os, Indent indent ) const;
00205   void PrintParamtersType(  const ParametersType& x, std::ostream& os ) const;
00206 
00209   virtual void UpdateSwarm() = 0;
00210 
00211   ParticleSwarmOptimizerBase( const Self& ); //purposely not implemented
00212   void operator=( const Self& );//purposely not implemented
00213 
00214   virtual void ValidateSettings();
00215 
00219   virtual void Initialize();
00220 
00221   void RandomInitialization();
00222   void FileInitialization();
00223 
00224   bool                                         m_PrintSwarm;
00225   std::ostringstream                           m_StopConditionDescription;
00226   bool                                         m_InitializeNormalDistribution;
00227   NumberOfParticlesType                        m_NumberOfParticles;
00228   NumberOfIterationsType                       m_MaximalNumberOfIterations;
00229   NumberOfGenerationsType                      m_NumberOfGenerationsWithMinimalImprovement;
00230   ParameterBoundsType                          m_ParameterBounds;
00231   ParametersType                               m_ParametersConvergenceTolerance;
00232   double                                       m_PercentageParticlesConverged;
00233   CostFunctionType::MeasureType                m_FunctionConvergenceTolerance;
00234   std::vector<ParticleData>                    m_Particles;
00235   CostFunctionType::MeasureType                m_FunctionBestValue;
00236   std::vector<MeasureType>                     m_FunctionBestValueMemory;
00237   ParametersType                               m_ParametersBestValue;
00238   NumberOfIterationsType                       m_IterationIndex;
00239   RandomVariateGeneratorType::IntegerType      m_Seed;
00240   bool                                         m_UseSeed;
00241 };
00242 } // end namespace itk
00243 
00244 #endif
00245