eoConstrictedVelocity.h

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

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