EvolvingObjects
eoExtendedVelocity.h
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoExtendedVelocity.h
00005 // (c) INRIA Dolphin 2008
00006 /*
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     Contact: thomas.legrand@inria.fr
00022  */
00023 //-----------------------------------------------------------------------------
00024 
00025 #ifndef eoExtendedVelocity_H
00026 #define eoExtendedVelocity_H
00027 
00028 //-----------------------------------------------------------------------------
00029 #include <eoFunctor.h>
00030 #include <utils/eoRNG.h>
00031 #include <eoPop.h>
00032 #include <utils/eoRealVectorBounds.h>
00033 #include <eoRealBoundModifier.h>
00034 #include <eoTopology.h>
00035 //-----------------------------------------------------------------------------
00036 
00037 
00047 template < class POT > class eoExtendedVelocity:public eoVelocity < POT >
00048 {
00049 
00050 public:
00051 
00052     /*
00053      * Each element for the velocity evaluation is expected to be of type VelocityType.
00054      */
00055     typedef typename POT::ParticleVelocityType VelocityType;
00056 
00068     eoExtendedVelocity (eoTopology < POT > & _topology,
00069                                         const VelocityType & _w,
00070                         const VelocityType & _c1,
00071                         const VelocityType & _c2,
00072                         const VelocityType & _c3,
00073                         eoRealVectorBounds & _bounds,
00074                         eoRealBoundModifier & _bndsModifier,
00075                         eoRng & _gen = rng):
00076             topology(_topology),
00077             omega (_w),
00078             c1 (_c1),
00079             c2 (_c2),
00080             c3 (_c3),
00081             bounds(_bounds),
00082             bndsModifier(_bndsModifier),
00083             gen(_gen){}
00084 
00085 
00096     eoExtendedVelocity (eoTopology < POT > & _topology,
00097                         const VelocityType & _w,
00098                         const VelocityType & _c1,
00099                         const VelocityType & _c2,
00100                         const VelocityType & _c3,
00101                         eoRealVectorBounds & _bounds,
00102                         eoRng & _gen = rng):
00103             topology(_topology),
00104             omega (_w),
00105             c1 (_c1),
00106             c2 (_c2),
00107             c3 (_c3),
00108             bounds(_bounds),
00109             bndsModifier(dummyModifier),
00110             gen(_gen){}
00111 
00112 
00121     eoExtendedVelocity (eoTopology < POT > & _topology,
00122                         const VelocityType & _w,
00123                         const VelocityType & _c1,
00124                         const VelocityType & _c2,
00125                         const VelocityType & _c3,
00126                         eoRng & _gen = rng):
00127             topology(_topology),
00128             omega (_w),
00129             c1 (_c1),
00130             c2 (_c2),
00131             c3 (_c3),
00132             bounds(*(new eoRealVectorNoBounds(0))),
00133             bndsModifier(dummyModifier),
00134             gen(_gen)
00135     {}
00136 
00137 
00144     void operator  () (POT & _po,unsigned _indice)
00145     {
00146         VelocityType r1;
00147         VelocityType r2;
00148                 VelocityType r3;
00149 
00150         VelocityType newVelocity;
00151 
00152         // cast the learning factors to VelocityType
00153         r1 =  (VelocityType) rng.uniform (1) * c1;
00154         r2 =  (VelocityType) rng.uniform (1) * c2;
00155                 r3 =  (VelocityType) rng.uniform (1) * c3;
00156 
00157         // need to resize the bounds even if there are dummy because of "isBounded" call
00158         bounds.adjust_size(_po.size());
00159 
00160         // assign the new velocities
00161         for (unsigned j = 0; j < _po.size (); j++)
00162         {
00163             newVelocity= omega *  _po.velocities[j]
00164                                         + r1 * (_po.bestPositions[j] - _po[j])
00165                                         + r2 * (topology.best (_indice)[j] - _po[j])
00166                                         + r3 * (topology.globalBest()[j] - _po[j]);
00167 
00168             /* check bounds */
00169             if (bounds.isMinBounded(j))
00170                 newVelocity=std::max(newVelocity,bounds.minimum(j));
00171             if (bounds.isMaxBounded(j))
00172                 newVelocity=std::min(newVelocity,bounds.maximum(j));
00173 
00174             _po.velocities[j]=newVelocity;
00175         }
00176     }
00177 
00181     void updateNeighborhood(POT & _po,unsigned _indice)
00182     {
00183         topology.updateNeighborhood(_po,_indice);
00184     }
00185 
00188 
00189         eoTopology<POT> & getTopology ()
00190         {
00191                 return topology;
00192         }
00193 
00194 protected:
00195     eoTopology < POT > & topology;
00196     const VelocityType & omega;  // social/cognitive coefficient
00197     const VelocityType & c1;
00198     const VelocityType & c2;  // social/cognitive coefficient
00199     const VelocityType & c3;  // social/cognitive coefficient
00200 
00201     eoRealVectorBounds bounds; // REAL bounds even if the velocity could be of another type.
00202         eoRealBoundModifier & bndsModifier;
00203 
00204         eoRng & gen; // the random generator
00205 
00206     // If the bound modifier doesn't need to be used, use the dummy instance
00207     eoDummyRealBoundModifier dummyModifier;
00208 };
00213 #endif /*eoExtendedVelocity_H */
 All Classes Namespaces Files Functions Variables Typedefs Friends