Differential Evolution C++ library
C:/dev/de/differentialevolution/population.hpp
00001 /*
00002  * Copyright (c) 2011 Adrian Michel
00003  * http://www.amichel.com
00004  *
00005  * Permission to use, copy, modify, distribute and sell this 
00006  * software and its documentation for any purpose is hereby 
00007  * granted without fee, provided that both the above copyright 
00008  * notice and this permission notice appear in all copies and in 
00009  * the supporting documentation. 
00010  *  
00011  * This library is distributed in the hope that it will be 
00012  * useful. However, Adrian Michel makes no representations about
00013  * the suitability of this software for any purpose.  It is 
00014  * provided "as is" without any express or implied warranty. 
00015  * 
00016  * Should you find this library useful, please email 
00017  * info@amichel.com with a link or other reference 
00018  * to your work. 
00019 */
00020 
00021 #ifndef DE_POPULATION_HPP_INCLUDED
00022 #define DE_POPULATION_HPP_INCLUDED
00023 
00024 // MS compatible compilers support #pragma once
00025 
00026 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
00027 #pragma once
00028 #endif
00029 
00030 #include "individual.hpp"
00031 
00032 
00033 namespace de
00034 {
00035 
00036 typedef std::vector< individual_ptr > population_base;
00037 
00043 class population : public population_base
00044 {
00045 public:
00055         population( size_t popSize, size_t varCount )
00056         : population_base( popSize )
00057         {
00058                 assert( popSize > 0 );
00059                 assert( varCount > 0 );
00060 
00061                 init(popSize, varCount );
00062         }
00063 
00078         population( size_t popSize, size_t varCount, constraints_ptr constraints )
00079         : population_base( popSize )
00080         {
00081                 assert( popSize > 0 );
00082                 assert( varCount > 0 );
00083 
00084                 init(popSize, varCount );
00085 
00086                 for( population::size_type i = 0; i < size(); ++i ) 
00087                         at( i )->init( constraints );
00088         }
00089 
00097         individual_ptr best( bool minimize ) const 
00098         {
00099                 population::size_type best( 0 );
00100 
00101                 for( population::size_type i = 0; i < size(); ++i ) 
00102                         best = at( i )->better_or_equal( at( best ), minimize )? i : best;
00103 
00104                 return at( best );
00105         }
00106 
00114         std::string to_string()
00115         {
00116                 std::string str;
00117                 for( population::size_type i = 0; i < size(); ++i ) 
00118                         str += at( i )->to_string();
00119 
00120                 return str;
00121         }
00122 
00123 private:
00124         void init( size_t popSize, size_t varCount )
00125         {
00126                 for( population_base::size_type i = 0; i < size(); ++i ) 
00127                         operator[]( i ) = boost::make_shared< individual >( varCount );
00128         }
00129 public:
00130 
00131 };
00132 
00133         typedef boost::shared_ptr< population > population_ptr;
00134 
00135 }
00136 
00137 #endif //DE_POPULATION_HPP_INCLUDED