eoVariableInertiaWeightedVelocity.h

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoVariableInertiaWeightedVelocity.h
00005 // (c) OPAC 2007
00006 /*
00007     Contact: paradiseo-help@lists.gforge.inria.fr
00008  */
00009 //-----------------------------------------------------------------------------
00010 
00011 #ifndef EOVARIABLEINERTIAWEIGHTEDVELOCITY_H
00012 #define EOVARIABLEINERTIAWEIGHTEDVELOCITY_H
00013 
00014 //-----------------------------------------------------------------------------
00015 #include <eoVelocity.h>
00016 #include <eoTopology.h>
00017 #include <eoWeightUpdater.h>
00018 #include <utils/eoRealVectorBounds.h>
00019 #include <eoRealBoundModifier.h>
00020 //-----------------------------------------------------------------------------
00021 
00022 
00023 
00029 template < class POT > class eoVariableInertiaWeightedVelocity:public eoVelocity < POT >
00030 {
00031 
00032 public:
00033 
00034     /*
00035      * Each element for the velocity evaluation is expected to be of type VelocityType.
00036      */
00037     typedef typename POT::ParticleVelocityType VelocityType;
00038 
00049     eoVariableInertiaWeightedVelocity (eoTopology < POT > & _topology,
00050                                        eoWeightUpdater<VelocityType> & _weightUpdater,
00051                                        const VelocityType & _c1,
00052                                        const VelocityType & _c2 ,
00053                                        eoRealVectorBounds & _bounds,
00054                                        eoRealBoundModifier & _bndsModifier,
00055                                        eoRng & _gen = rng):
00056             topology(_topology),
00057             weightUpdater(_weightUpdater),
00058             c1 (_c1),
00059             c2 (_c2),
00060             bounds(_bounds),
00061             bndsModifier(_bndsModifier),
00062             gen(_gen){}
00063 
00064 
00074     eoVariableInertiaWeightedVelocity (eoTopology < POT > & _topology,
00075                                        eoWeightUpdater<VelocityType> & _weightUpdater,
00076                                        const VelocityType & _c1,
00077                                        const VelocityType & _c2,
00078                                        eoRealVectorBounds & _bounds,
00079                                        eoRng & _gen = rng):
00080             topology(_topology),
00081             weightUpdater(_weightUpdater),
00082             c1 (_c1),
00083             c2 (_c2),
00084             bounds(_bounds),
00085             bndsModifier(dummyModifier),
00086             gen(_gen){}
00087 
00088 
00096     eoVariableInertiaWeightedVelocity (eoTopology < POT > & _topology,
00097                                        eoWeightUpdater<VelocityType> & _weightUpdater,
00098                                        const VelocityType & _c1,
00099                                        const VelocityType & _c2,
00100                                        eoRng & _gen = rng):
00101             topology(_topology),
00102             weightUpdater(_weightUpdater),
00103             c1 (_c1),
00104             c2 (_c2),
00105             bounds(*(new eoRealVectorNoBounds(0))),
00106             bndsModifier(dummyModifier),
00107             gen(_gen)
00108     {}
00109 
00120     void operator  () (POT & _po,unsigned _indice)
00121     {
00122         VelocityType r1;
00123         VelocityType r2;
00124 
00125         VelocityType newVelocity;
00126 
00127         // cast the learning factors to VelocityType
00128         r1 = (VelocityType) rng.uniform (1) * c1;
00129         r2 = (VelocityType) rng.uniform (1) * c2;
00130 
00131         // need to resize the bounds even if there are dummy because of "isBounded" call
00132         bounds.adjust_size(_po.size());
00133 
00134         // update the inertia weight
00135         weightUpdater(weight);
00136 
00137         // assign the new velocities
00138         for (unsigned j = 0; j < _po.size (); j++)
00139         {
00140             newVelocity= weight * _po.velocities[j] + r1 * (_po.bestPositions[j] - _po[j]) +  r2 * (topology.best (_indice)[j] - _po[j]);
00141 
00142             /* modify the bounds */
00143             bndsModifier(bounds,j);
00144 
00145             /* check bounds */
00146             if (bounds.isMinBounded(j))
00147                 newVelocity=(VelocityType)std::max(newVelocity,bounds.minimum(j));
00148             if (bounds.isMaxBounded(j))
00149                 newVelocity=(VelocityType)std::min(newVelocity,bounds.maximum(j));
00150 
00151             _po.velocities[j]=newVelocity;
00152         }
00153     }
00154 
00158     void updateNeighborhood(POT & _po,unsigned _indice)
00159     {
00160         topology.updateNeighborhood(_po,_indice);
00161     }
00162 
00163 
00164 
00165 protected:
00166     eoTopology < POT > & topology;
00167     eoWeightUpdater<VelocityType> & weightUpdater;      // the updater used to make the weight evoluate
00168     const VelocityType & c1;    // learning factor 1
00169     const VelocityType  & c2;    // learning factor 2
00170    
00171         eoRealVectorBounds & bounds; // REAL bounds even if the velocity could be of another type.
00172     eoRealBoundModifier & bndsModifier;
00173     
00174     VelocityType weight;
00175     eoRng & gen;        // the random generator  
00176 
00177     // If the bound modifier doesn't need to be used, use the dummy instance
00178     eoDummyRealBoundModifier dummyModifier;
00179 };
00180 
00181 #endif /*EOVARIABLEINERTIAWEIGHTEDVELOCITY_H*/
00182 

Generated on Fri Jun 22 10:17:02 2007 for EO-PSO by  doxygen 1.4.7