eoStandardVelocity.h

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoStandardVelocity.h
00005 // (c) OPAC 2007
00006 /*
00007     Contact: paradiseo-help@lists.gforge.inria.fr
00008  */
00009 //-----------------------------------------------------------------------------
00010 
00011 #ifndef EOSTANDARDVELOCITY_H
00012 #define EOSTANDARDVELOCITY_H
00013 
00014 //-----------------------------------------------------------------------------
00015 #include <eoFunctor.h>
00016 #include <utils/eoRNG.h>
00017 #include <eoPop.h>
00018 #include <utils/eoRealVectorBounds.h>
00019 #include <eoRealBoundModifier.h>
00020 #include <eoTopology.h>
00021 //-----------------------------------------------------------------------------
00022 
00023 
00028 template < class POT > class eoStandardVelocity: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 
00047     eoStandardVelocity (eoTopology < POT > & _topology,
00048                         const VelocityType & _c1,
00049                         const VelocityType & _c2 ,
00050                         eoRealVectorBounds & _bounds,
00051                         eoRealBoundModifier & _bndsModifier,
00052                         eoRng & _gen = rng):
00053             topology(_topology),
00054             c1 (_c1),
00055             c2 (_c2),
00056             bounds(_bounds),
00057             bndsModifier(_bndsModifier),
00058             gen(_gen){}
00059 
00060 
00069     eoStandardVelocity (eoTopology < POT > & _topology,
00070                         const VelocityType & _c1,
00071                         const VelocityType & _c2,
00072                         eoRealVectorBounds & _bounds,
00073                         eoRng & _gen = rng):
00074             topology(_topology),
00075             c1 (_c1),
00076             c2 (_c2),
00077             bounds(_bounds),
00078             bndsModifier(dummyModifier),
00079             gen(_gen){}
00080 
00081 
00088     eoStandardVelocity (eoTopology < POT > & _topology,
00089                         const VelocityType & _c1,
00090                         const VelocityType & _c2,
00091                         eoRng & _gen = rng):
00092             topology(_topology),
00093             c1 (_c1),
00094             c2 (_c2),
00095             bounds(*(new eoRealVectorNoBounds(0))),
00096             bndsModifier(dummyModifier),
00097             gen(_gen)
00098     {}
00099 
00100 
00107     void operator  () (POT & _po,unsigned _indice)
00108     {
00109         VelocityType r1;
00110         VelocityType r2;
00111 
00112         VelocityType newVelocity;
00113 
00114         // cast the learning factors to VelocityType
00115         r1 =  (VelocityType) rng.uniform (1) * c1;
00116         r2 =  (VelocityType) rng.uniform (1) * c2;
00117 
00118         // need to resize the bounds even if there are dummy because of "isBounded" call
00119         bounds.adjust_size(_po.size());
00120 
00121         // assign the new velocities
00122         for (unsigned j = 0; j < _po.size (); j++)
00123         {
00124             newVelocity=  _po.velocities[j] + r1 * (_po.bestPositions[j] - _po[j]) +  r2 * (topology.best (_indice)[j] - _po[j]);
00125 
00126             /* check bounds */
00127             if (bounds.isMinBounded(j))
00128                 newVelocity=std::max(newVelocity,bounds.minimum(j));
00129             if (bounds.isMaxBounded(j))
00130                 newVelocity=std::min(newVelocity,bounds.maximum(j));
00131 
00132             _po.velocities[j]=newVelocity;
00133         }
00134     }
00135 
00139     void updateNeighborhood(POT & _po,unsigned _indice)
00140     {
00141         topology.updateNeighborhood(_po,_indice);
00142     }
00143 
00144 
00145 protected:
00146     eoTopology < POT > & topology;
00147     const VelocityType & c1;  // learning factor 1
00148     const VelocityType &  c2;  // learning factor 2
00149   
00150     eoRealVectorBounds bounds; // REAL bounds even if the velocity could be of another type.
00151         eoRealBoundModifier & bndsModifier;
00152 
00153         eoRng & gen; // the random generator
00154         
00155     // If the bound modifier doesn't need to be used, use the dummy instance
00156     eoDummyRealBoundModifier dummyModifier;
00157 };
00158 
00159 
00160 #endif /*EOSTANDARDVELOCITY_H */
00161 

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