EvolvingObjects
|
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- 00002 00003 //----------------------------------------------------------------------------- 00004 // eoStandardVelocity.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 EOSTANDARDVELOCITY_H 00027 #define EOSTANDARDVELOCITY_H 00028 00029 //----------------------------------------------------------------------------- 00030 #include <eoFunctor.h> 00031 #include <utils/eoRNG.h> 00032 #include <eoPop.h> 00033 #include <utils/eoRealVectorBounds.h> 00034 #include <eoRealBoundModifier.h> 00035 #include <eoTopology.h> 00036 //----------------------------------------------------------------------------- 00037 00038 00046 template < class POT > class eoStandardVelocity: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 eoStandardVelocity (eoTopology < POT > & _topology, 00067 const VelocityType & _w, 00068 const VelocityType & _c1, 00069 const VelocityType & _c2, 00070 eoRealVectorBounds & _bounds, 00071 eoRealBoundModifier & _bndsModifier, 00072 eoRng & _gen = rng): 00073 topology(_topology), 00074 omega (_w), 00075 c1 (_c1), 00076 c2 (_c2), 00077 bounds(_bounds), 00078 bndsModifier(_bndsModifier), 00079 gen(_gen){} 00080 00081 00091 eoStandardVelocity (eoTopology < POT > & _topology, 00092 const VelocityType & _w, 00093 const VelocityType & _c1, 00094 const VelocityType & _c2, 00095 eoRealVectorBounds & _bounds, 00096 eoRng & _gen = rng): 00097 topology(_topology), 00098 omega (_w), 00099 c1 (_c1), 00100 c2 (_c2), 00101 bounds(_bounds), 00102 bndsModifier(dummyModifier), 00103 gen(_gen){} 00104 00105 00113 eoStandardVelocity (eoTopology < POT > & _topology, 00114 const VelocityType & _w, 00115 const VelocityType & _c1, 00116 const VelocityType & _c2, 00117 eoRng & _gen = rng): 00118 topology(_topology), 00119 omega (_w), 00120 c1 (_c1), 00121 c2 (_c2), 00122 bounds(*(new eoRealVectorNoBounds(0))), 00123 bndsModifier(dummyModifier), 00124 gen(_gen) 00125 {} 00126 00127 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= omega * _po.velocities[j] + r1 * (_po.bestPositions[j] - _po[j]) + r2 * (topology.best (_indice)[j] - _po[j]); 00152 00153 /* check bounds */ 00154 if (bounds.isMinBounded(j)) 00155 newVelocity=std::max(newVelocity,bounds.minimum(j)); 00156 if (bounds.isMaxBounded(j)) 00157 newVelocity=std::min(newVelocity,bounds.maximum(j)); 00158 00159 _po.velocities[j]=newVelocity; 00160 } 00161 } 00162 00166 void updateNeighborhood(POT & _po,unsigned _indice) 00167 { 00168 topology.updateNeighborhood(_po,_indice); 00169 } 00170 00173 00174 eoTopology<POT> & getTopology () 00175 { 00176 return topology; 00177 } 00178 00179 protected: 00180 eoTopology < POT > & topology; 00181 const VelocityType & omega; // social/cognitive coefficient 00182 const VelocityType & c1; // social/cognitive coefficient 00183 const VelocityType & c2; // social/cognitive coefficient 00184 00185 eoRealVectorBounds bounds; // REAL bounds even if the velocity could be of another type. 00186 eoRealBoundModifier & bndsModifier; 00187 00188 eoRng & gen; // the random generator 00189 00190 // If the bound modifier doesn't need to be used, use the dummy instance 00191 eoDummyRealBoundModifier dummyModifier; 00192 }; 00193 00194 00195 #endif /*EOSTANDARDVELOCITY_H */