EvolvingObjects
|
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- 00002 00003 //----------------------------------------------------------------------------- 00004 // eoEasyPSO.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 _EOEASYPSO_H 00026 #define _EOEASYPSO_H 00027 00028 //----------------------------------------------------------------------------- 00029 #include <eoContinue.h> 00030 #include <eoPSO.h> 00031 #include <eoVelocity.h> 00032 #include <eoFlight.h> 00033 //----------------------------------------------------------------------------- 00034 00048 template < class POT > class eoEasyPSO:public eoPSO < POT > 00049 { 00050 public: 00051 00060 eoEasyPSO ( 00061 eoInitializerBase <POT> &_init, 00062 eoContinue < POT > &_continuator, 00063 eoEvalFunc < POT > &_eval, 00064 eoVelocity < POT > &_velocity, 00065 eoFlight < POT > &_flight): 00066 init(_init), 00067 continuator (_continuator), 00068 eval (_eval), 00069 velocity (_velocity), 00070 flight (_flight) 00071 {} 00072 00073 00080 eoEasyPSO ( 00081 eoInitializerBase <POT> &_init, 00082 eoContinue < POT > &_continuator, 00083 eoEvalFunc < POT > &_eval, 00084 eoVelocity < POT > &_velocity): 00085 init(_init), 00086 continuator (_continuator), 00087 eval (_eval), 00088 velocity (_velocity), 00089 flight (dummyFlight) 00090 {} 00091 00092 00093 /* Constructor without eoInitializerBase. Assume the initialization is done before running the algorithm 00094 * @param _continuator - An eoContinue that manages the stopping criterion and the checkpointing system 00095 * @param _eval - An eoEvalFunc: the evaluation performer 00096 * @param _velocity - An eoVelocity that defines how to compute the velocities 00097 * @param _flight - An eoFlight that defines how to make the particle flying: that means how 00098 * to modify the positions according to the velocities 00099 */ 00100 eoEasyPSO ( 00101 eoContinue < POT > &_continuator, 00102 eoEvalFunc < POT > &_eval, 00103 eoVelocity < POT > &_velocity, 00104 eoFlight < POT > &_flight): 00105 init(dummyInit), 00106 continuator (_continuator), 00107 eval (_eval), 00108 velocity (_velocity), 00109 flight (_flight) 00110 {} 00111 00112 00118 eoEasyPSO ( 00119 eoContinue < POT > &_continuator, 00120 eoEvalFunc < POT > &_eval, 00121 eoVelocity < POT > &_velocity): 00122 init(dummyInit), 00123 continuator (_continuator), 00124 eval (_eval), 00125 velocity (_velocity), 00126 flight (dummyFlight) 00127 {} 00128 00130 virtual void operator () (eoPop < POT > &_pop) 00131 { 00132 try 00133 { 00134 // initializes the topology, velocity, best particle(s) 00135 init(); 00136 do 00137 { 00138 // loop over all the particles for the current iteration 00139 for (unsigned idx = 0; idx < _pop.size (); idx++) 00140 { 00141 // perform velocity evaluation 00142 velocity (_pop[idx],idx); 00143 00144 // apply the flight 00145 flight (_pop[idx]); 00146 00147 // evaluate the position 00148 eval (_pop[idx]); 00149 00150 // update the topology (particle and local/global best(s)) 00151 velocity.updateNeighborhood(_pop[idx],idx); 00152 } 00153 00154 } 00155 while (continuator (_pop)); 00156 00157 } 00158 catch (std::exception & e) 00159 { 00160 std::string s = e.what (); 00161 s.append (" in eoEasyPSO"); 00162 throw std::runtime_error (s); 00163 } 00164 00165 } 00166 00167 protected: 00168 eoInitializerBase <POT> &init; 00169 eoContinue < POT > &continuator; 00170 eoEvalFunc < POT > &eval; 00171 eoVelocity < POT > &velocity; 00172 eoFlight < POT > &flight; 00173 00174 // if the flight does not need to be used, use the dummy flight instance 00175 class eoDummyFlight:public eoFlight < POT > 00176 { 00177 public: 00178 eoDummyFlight () {} 00179 void operator () (POT & _po) {} 00180 }dummyFlight; 00181 00182 // if the initializer does not need to be used, use the dummy one instead 00183 class eoDummyInitializer:public eoInitializerBase < POT > 00184 { 00185 public: 00186 eoDummyInitializer () {} 00187 void operator () (POT & _po) {} 00188 }dummyInit; 00189 00190 }; 00196 #endif /*_EOEASYPSO_H*/