EvolvingObjects
eoCMABreed.h
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; fill-column: 80; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoCMABreed
00005 // (c) Maarten Keijzer 2005
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  */
00022 //-----------------------------------------------------------------------------
00023 
00024 
00025 #ifndef _EOCMABREED_H
00026 #define _EOCMABREED_H
00027 
00028 #include <eoBreed.h>
00029 #include <eoVector.h>
00030 #include <es/CMAState.h>
00031 
00032 #include <algorithm>
00033 
00035 template <class FitT>
00036 class eoCMABreed : public eoBreed< eoVector<FitT, double> > {
00037 
00038     eo::CMAState& state;
00039     unsigned lambda;
00040 
00041     typedef eoVector<FitT, double> EOT;
00042 
00043     public:
00044     eoCMABreed(eo::CMAState& state_, unsigned lambda_) : state(state_), lambda(lambda_) {}
00045 
00046     void operator()(const eoPop<EOT>& parents, eoPop<EOT>& offspring) {
00047 
00048         // two temporary arrays of pointers to store the sorted population
00049         std::vector<const EOT*> sorted(parents.size());
00050 
00051         // mu stores population as vector (instead of eoPop)
00052         std::vector<const std::vector<double>* > mu(parents.size());
00053 
00054         parents.sort(sorted);
00055         for (unsigned i = 0; i < sorted.size(); ++i) {
00056             mu[i] = static_cast< const std::vector<double>* >( sorted[i] );
00057         }
00058 
00059         // learn
00060         state.reestimate(mu, sorted[0]->fitness(), sorted.back()->fitness());
00061 
00062         if (!state.updateEigenSystem(10)) {
00063             std::cerr << "No good eigensystem found" << std::endl;
00064         }
00065 
00066         // generate
00067         offspring.resize(lambda);
00068 
00069         for (unsigned i = 0; i < lambda; ++i) {
00070             state.sample( static_cast< std::vector<double>& >( offspring[i] ));
00071             offspring[i].invalidate();
00072         }
00073 
00074     }
00075 };
00076 
00077 
00078 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends