EvolvingObjects
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     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@lifl.fr
00022  */
00023 //-----------------------------------------------------------------------------
00024 
00025 #ifndef EOFIXEDINERTIAWEIGHTEDVELOCITY_H
00026 #define EOFIXEDINERTIAWEIGHTEDVELOCITY_H
00027 
00028 //-----------------------------------------------------------------------------
00029 #include <eoFunctor.h>
00030 #include <utils/eoRNG.h>
00031 #include <eoPop.h>
00032 #include <utils/eoRealVectorBounds.h>
00033 #include <eoTopology.h>
00034 //-----------------------------------------------------------------------------
00035 
00036 
00044 template < class POT > class eoFixedInertiaWeightedVelocity:public eoVelocity < POT >
00045 {
00046 
00047 public:
00048 
00049     /*
00050     * Each element for the velocity evaluation is expected to be of type VelocityType.
00051     */
00052     typedef typename POT::ParticleVelocityType VelocityType;
00053 
00064     eoFixedInertiaWeightedVelocity (eoTopology < POT > & _topology,
00065                                     const VelocityType & _weight,
00066                                     const VelocityType & _c1,
00067                                     const VelocityType & _c2 ,
00068                                     eoRealVectorBounds & _bounds,
00069                                     eoRealBoundModifier & _bndsModifier,
00070                                     eoRng & _gen = rng):
00071             topology(_topology),
00072             weight(_weight),
00073             c1 (_c1),
00074             c2 (_c2),
00075             bounds(_bounds),
00076             bndsModifier(_bndsModifier),
00077             gen(_gen){}
00078 
00079 
00089     eoFixedInertiaWeightedVelocity (eoTopology < POT > & _topology,
00090                                     const VelocityType & _weight,
00091                                     const VelocityType & _c1,
00092                                     const VelocityType & _c2,
00093                                     eoRealVectorBounds & _bounds,
00094                                     eoRng & _gen = rng):
00095             topology(_topology),
00096             weight(_weight),
00097             c1 (_c1),
00098             c2 (_c2),
00099             bounds(_bounds),
00100             bndsModifier(dummyModifier),
00101             gen(_gen){}
00102 
00103 
00111     eoFixedInertiaWeightedVelocity (eoTopology < POT > & _topology,
00112                                     const VelocityType & _weight,
00113                                     const VelocityType & _c1,
00114                                     const VelocityType & _c2,
00115                                     eoRng & _gen = rng):
00116             topology(_topology),
00117             weight(_weight),
00118             c1 (_c1),
00119             c2 (_c2),
00120             bounds(*(new eoRealVectorNoBounds(0))),
00121             bndsModifier(dummyModifier),
00122             gen(_gen)
00123     {}
00124 
00134     void operator  () (POT & _po,unsigned _indice)
00135     {
00136         VelocityType r1;
00137         VelocityType r2;
00138 
00139         VelocityType newVelocity;
00140 
00141         // cast the learning factors to VelocityType
00142         r1 = (VelocityType) rng.uniform (1) * c1;
00143         r2 = (VelocityType) rng.uniform (1) * c2;
00144 
00145         // need to resize the bounds even if there are dummy because of "isBounded" call
00146         bounds.adjust_size(_po.size());
00147 
00148         // assign the new velocities
00149         for (unsigned j = 0; j < _po.size (); j++)
00150         {
00151             newVelocity= weight *  _po.velocities[j] + r1 * (_po.bestPositions[j] - _po[j]) +  r2 * (topology.best (_indice)[j] - _po[j]);
00152 
00153             /* modify the bounds */
00154             bndsModifier(bounds,j);
00155 
00156             /* check bounds */
00157             if (bounds.isMinBounded(j))
00158                 newVelocity=(VelocityType)std::max(newVelocity,bounds.minimum(j));
00159             if (bounds.isMaxBounded(j))
00160                 newVelocity=(VelocityType)std::min(newVelocity,bounds.maximum(j));
00161 
00162             _po.velocities[j]=newVelocity;
00163         }
00164     }
00165 
00169     void updateNeighborhood(POT & _po,unsigned _indice)
00170     {
00171         topology.updateNeighborhood(_po,_indice);
00172     }
00173 
00174 
00175 
00176 protected:
00177     eoTopology < POT > & topology;
00178     const VelocityType & c1;    // learning factor 1
00179     const VelocityType  & c2;    // learning factor 2
00180     const VelocityType & weight;   // the fixed weight
00181     eoRng & gen;        // the random generator
00182 
00183     eoRealVectorBounds & bounds; // REAL bounds even if the velocity could be of another type.
00184     eoRealBoundModifier & bndsModifier;
00185 
00186     // If the bound modifier doesn't need to be used, use the dummy instance
00187     eoDummyRealBoundModifier dummyModifier;
00188 };
00189 
00190 
00191 #endif /*EOFIXEDINERTIAWEIGHTEDVELOCITY_H */
 All Classes Namespaces Files Functions Variables Typedefs Friends