EvolvingObjects
eoVariableInertiaWeightedVelocity.h
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoVariableInertiaWeightedVelocity.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 EOVARIABLEINERTIAWEIGHTEDVELOCITY_H
00026 #define EOVARIABLEINERTIAWEIGHTEDVELOCITY_H
00027 
00028 //-----------------------------------------------------------------------------
00029 #include <eoVelocity.h>
00030 #include <eoTopology.h>
00031 #include <eoWeightUpdater.h>
00032 #include <utils/eoRealVectorBounds.h>
00033 #include <eoRealBoundModifier.h>
00034 //-----------------------------------------------------------------------------
00035 
00036 
00037 
00046 template < class POT > class eoVariableInertiaWeightedVelocity:public eoVelocity < POT >
00047 {
00048 
00049 public:
00050 
00051     /*
00052      * Each element for the velocity evaluation is expected to be of type VelocityType.
00053      */
00054     typedef typename POT::ParticleVelocityType VelocityType;
00055 
00066     eoVariableInertiaWeightedVelocity (eoTopology < POT > & _topology,
00067                                        eoWeightUpdater<VelocityType> & _weightUpdater,
00068                                        const VelocityType & _c1,
00069                                        const VelocityType & _c2 ,
00070                                        eoRealVectorBounds & _bounds,
00071                                        eoRealBoundModifier & _bndsModifier,
00072                                        eoRng & _gen = rng):
00073             topology(_topology),
00074             weightUpdater(_weightUpdater),
00075             c1 (_c1),
00076             c2 (_c2),
00077             bounds(_bounds),
00078             bndsModifier(_bndsModifier),
00079             gen(_gen){}
00080 
00081 
00091     eoVariableInertiaWeightedVelocity (eoTopology < POT > & _topology,
00092                                        eoWeightUpdater<VelocityType> & _weightUpdater,
00093                                        const VelocityType & _c1,
00094                                        const VelocityType & _c2,
00095                                        eoRealVectorBounds & _bounds,
00096                                        eoRng & _gen = rng):
00097             topology(_topology),
00098             weightUpdater(_weightUpdater),
00099             c1 (_c1),
00100             c2 (_c2),
00101             bounds(_bounds),
00102             bndsModifier(dummyModifier),
00103             gen(_gen){}
00104 
00105 
00113     eoVariableInertiaWeightedVelocity (eoTopology < POT > & _topology,
00114                                        eoWeightUpdater<VelocityType> & _weightUpdater,
00115                                        const VelocityType & _c1,
00116                                        const VelocityType & _c2,
00117                                        eoRng & _gen = rng):
00118             topology(_topology),
00119             weightUpdater(_weightUpdater),
00120             c1 (_c1),
00121             c2 (_c2),
00122             bounds(*(new eoRealVectorNoBounds(0))),
00123             bndsModifier(dummyModifier),
00124             gen(_gen)
00125     {}
00126 
00137     void operator  () (POT & _po,unsigned _indice)
00138     {
00139         VelocityType r1;
00140         VelocityType r2;
00141 
00142         VelocityType newVelocity;
00143 
00144         // cast the learning factors to VelocityType
00145         r1 = (VelocityType) rng.uniform (1) * c1;
00146         r2 = (VelocityType) rng.uniform (1) * c2;
00147 
00148         // need to resize the bounds even if there are dummy because of "isBounded" call
00149         bounds.adjust_size(_po.size());
00150 
00151         // update the inertia weight
00152         weightUpdater(weight);
00153 
00154         // assign the new velocities
00155         for (unsigned j = 0; j < _po.size (); j++)
00156         {
00157             newVelocity= weight * _po.velocities[j] + r1 * (_po.bestPositions[j] - _po[j]) +  r2 * (topology.best (_indice)[j] - _po[j]);
00158 
00159             /* modify the bounds */
00160             bndsModifier(bounds,j);
00161 
00162             /* check bounds */
00163             if (bounds.isMinBounded(j))
00164                 newVelocity=(VelocityType)std::max(newVelocity,bounds.minimum(j));
00165             if (bounds.isMaxBounded(j))
00166                 newVelocity=(VelocityType)std::min(newVelocity,bounds.maximum(j));
00167 
00168             _po.velocities[j]=newVelocity;
00169         }
00170     }
00171 
00175     void updateNeighborhood(POT & _po,unsigned _indice)
00176     {
00177         topology.updateNeighborhood(_po,_indice);
00178     }
00179 
00180 
00181 
00182 protected:
00183     eoTopology < POT > & topology;
00184     eoWeightUpdater<VelocityType> & weightUpdater;      // the updater used to make the weight evoluate
00185     const VelocityType & c1;    // learning factor 1
00186     const VelocityType  & c2;    // learning factor 2
00187 
00188         eoRealVectorBounds & bounds; // REAL bounds even if the velocity could be of another type.
00189     eoRealBoundModifier & bndsModifier;
00190 
00191     VelocityType weight;
00192     eoRng & gen;        // the random generator
00193 
00194     // If the bound modifier doesn't need to be used, use the dummy instance
00195     eoDummyRealBoundModifier dummyModifier;
00196 };
00197 
00198 #endif /*EOVARIABLEINERTIAWEIGHTEDVELOCITY_H*/
 All Classes Namespaces Files Functions Variables Typedefs Friends