EvolvingObjects
eoConstrictedVariableWeightVelocity.h
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoConstrictedVariableWeightVelocity.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 EOCONSTRICTEDVARIABLEWEIGHTVELOCITY_H
00026 #define EOCONSTRICTEDVARIABLEWEIGHTVELOCITY_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 
00045 template < class POT > class eoConstrictedVariableWeightVelocity:public eoVelocity < POT >
00046 {
00047 
00048 public:
00049 
00050     /*
00051      * Each element for the velocity evaluation is expected to be of type VelocityType.
00052      */
00053     typedef typename POT::ParticleVelocityType VelocityType;
00054 
00066     eoConstrictedVariableWeightVelocity (eoTopology < POT > & _topology,
00067                                          const VelocityType & _coeff,
00068                                          eoWeightUpdater<VelocityType> & _weightUpdater,
00069                                          const VelocityType & _c1,
00070                                          const VelocityType & _c2 ,
00071                                          eoRealVectorBounds & _bounds,
00072                                          eoRealBoundModifier & _bndsModifier,
00073                                          eoRng & _gen = rng):
00074             topology(_topology),
00075             coeff(_coeff),
00076             weightUpdater(_weightUpdater),
00077             c1 (_c1),
00078             c2 (_c2),
00079             bounds(_bounds),
00080             bndsModifier(_bndsModifier),
00081             gen(_gen){}
00082 
00083 
00094     eoConstrictedVariableWeightVelocity (eoTopology < POT > & _topology,
00095                                          const VelocityType & _coeff,
00096                                          eoWeightUpdater<VelocityType> & _weightUpdater,
00097                                          const VelocityType & _c1,
00098                                          const VelocityType & _c2,
00099                                          eoRealVectorBounds & _bounds,
00100                                          eoRng & _gen = rng):
00101             topology(_topology),
00102             coeff(_coeff),
00103             weightUpdater(_weightUpdater),
00104             c1 (_c1),
00105             c2 (_c2),
00106             bounds(_bounds),
00107             bndsModifier(dummyModifier),
00108             gen(_gen){}
00109 
00110 
00119     eoConstrictedVariableWeightVelocity (eoTopology < POT > & _topology,
00120                                          const VelocityType & _coeff,
00121                                          eoWeightUpdater<VelocityType> & _weightUpdater,
00122                                          const VelocityType & _c1,
00123                                          const VelocityType & _c2,
00124                                          eoRng & _gen = rng):
00125             topology(_topology),
00126             coeff(_coeff),
00127             weightUpdater(_weightUpdater),
00128             c1 (_c1),
00129             c2 (_c2),
00130             bounds(*(new eoRealVectorNoBounds(0))),
00131             bndsModifier(dummyModifier),
00132             gen(_gen)
00133     {}
00134 
00145     void operator  () (POT & _po,unsigned _indice)
00146     {
00147         VelocityType r1;
00148         VelocityType r2;
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 
00156         // need to resize the bounds even if there are dummy because of "isBounded" call
00157         bounds.adjust_size(_po.size());
00158 
00159         // update the inertia weight
00160         weightUpdater(weight);
00161 
00162         // assign the new velocities
00163         for (unsigned j = 0; j < _po.size (); j++)
00164         {
00165             newVelocity= coeff * (weight * _po.velocities[j] + r1 * (_po.bestPositions[j] - _po[j]) +  r2 * (topology.best (_indice)[j] - _po[j]));
00166 
00167             /* modify the bounds */
00168             bndsModifier(bounds,j);
00169 
00170             /* check bounds */
00171             if (bounds.isMinBounded(j))
00172                 newVelocity=(VelocityType)std::max(newVelocity,bounds.minimum(j));
00173             if (bounds.isMaxBounded(j))
00174                 newVelocity=(VelocityType)std::min(newVelocity,bounds.maximum(j));
00175 
00176             _po.velocities[j]=newVelocity;
00177         }
00178     }
00179 
00183     void updateNeighborhood(POT & _po,unsigned _indice)
00184     {
00185         topology.updateNeighborhood(_po,_indice);
00186     }
00187 
00188 
00189 
00190 protected:
00191     eoTopology < POT > & topology;
00192 
00193     const VelocityType & coeff;   // the fixed constriction coefficient
00194     eoWeightUpdater<VelocityType> & weightUpdater;      // the updater used to make the weight evoluate
00195 
00196     const VelocityType & c1;    // learning factor 1
00197     const VelocityType  & c2;    // learning factor 2
00198 
00199     eoRealVectorBounds & bounds; // REAL bounds even if the velocity could be of another type.
00200     eoRealBoundModifier & bndsModifier;
00201 
00202     VelocityType weight;
00203     eoRng & gen;        // the random generator
00204 
00205     // If the bound modifier doesn't need to be used, use the dummy instance
00206     eoDummyRealBoundModifier dummyModifier;
00207 };
00208 
00209 #endif /*EOCONSTRICTEDVARIABLEWEIGHTVELOCITY_H*/
 All Classes Namespaces Files Functions Variables Typedefs Friends