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