eoFixedInertiaWeightedVelocity.h

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

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