EvolvingObjects
eoVelocityInit.h
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoVelocityInit.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 EOVELOCITYINIT_H
00026 #define EOVELOCITYINIT_H
00027 
00028 
00029 #include <algorithm>
00030 
00031 #include <eoOp.h>
00032 #include <eoSTLFunctor.h>
00033 #include <utils/eoRndGenerators.h>
00034 
00041 template < class POT > class eoVelocityInit:public eoInit < POT >
00042 {
00043 public:
00044     virtual std::string className (void) const
00045     {
00046         return "eoVelocityInit";
00047     }
00048 };
00049 
00050 
00054 template < class POT > class eoVelocityInitGenerator:public eoF < POT >
00055 {
00056 public:
00057 
00059     eoVelocityInitGenerator (eoVelocityInit < POT > &_init):init (_init){}
00060 
00061     virtual POT operator  () ()
00062     {
00063         POT p;
00064         init (p);
00065         return (p);
00066     }
00067 
00068 private:
00069     eoVelocityInit < POT > &init;
00070 };
00071 
00075 template < class POT > class eoVelocityInitFixedLength:public eoVelocityInit <
00076             POT >
00077 {
00078 public:
00079 
00080     typedef typename POT::ParticleVelocityType VelocityType;
00081 
00082     eoVelocityInitFixedLength (unsigned _combien,
00083                                eoRndGenerator < VelocityType >
00084                                &_generator):combien (_combien),
00085             generator (_generator)
00086     {
00087     }
00088 
00089     virtual void operator  () (POT & chrom)
00090     {
00091         chrom.resize (combien);
00092         std::generate (chrom.velocities.begin (), chrom.velocities.end (),
00093                        generator);
00094     }
00095 
00096 private:
00097     unsigned combien;
00099     eoSTLF < VelocityType > generator;
00100 };
00101 
00105 template < class POT > class eoVelocityInitVariableLength:public eoVelocityInit <
00106             POT >
00107 {
00108 public:
00109     typedef typename POT::ParticleVelocityType VelocityType;
00110 
00112     eoVelocityInitVariableLength (unsigned _minSize, unsigned _maxSize,
00113                                   eoVelocityInit < VelocityType >
00114                                   &_init):offset (_minSize),
00115             extent (_maxSize - _minSize), init (_init)
00116     {
00117         if (_minSize >= _maxSize)
00118             throw std::
00119             logic_error
00120             ("eoVelocityInitVariableLength: minSize larger or equal to maxSize");
00121     }
00122 
00123 
00124     virtual void operator  () (POT & _chrom)
00125     {
00126         _chrom.resizeVelocities (offset + rng.random (extent));
00127         typename std::vector < VelocityType >::iterator it;
00128         for (it = _chrom.velocities.begin (); it < _chrom.velocities.end (); it++)
00129             init (*it);
00130     }
00131 
00132     // accessor to the atom initializer (needed by operator constructs sometimes)
00133     eoInit < VelocityType > &atomInit ()
00134     {
00135         return init;
00136     }
00137 
00138 private:
00139     unsigned offset;
00140     unsigned extent;
00141     eoVelocityInit < VelocityType > &init;
00142 };
00143 
00144 #endif /*EOVELOCITYINIT_H */
00145 
 All Classes Namespaces Files Functions Variables Typedefs Friends