EvolvingObjects
|
00001 /* 00002 PyEO 00003 00004 Copyright (C) 2003 Maarten Keijzer 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 */ 00020 00021 #include <eoSelectOne.h> 00022 #include <eoDetTournamentSelect.h> 00023 #include <eoRandomSelect.h> 00024 #include <eoStochTournamentSelect.h> 00025 #include <eoTruncatedSelectOne.h> 00026 #include <eoSequentialSelect.h> 00027 00028 #include "PyEO.h" 00029 #include "pickle.h" 00030 #include "def_abstract_functor.h" 00031 00032 using namespace boost::python; 00033 00034 class eoSelectOneWrapper : public eoSelectOne<PyEO> 00035 { 00036 public: 00037 PyObject* self; 00038 eoSelectOneWrapper(PyObject* p) : self(p) {} 00039 const PyEO& operator()(const eoPop<PyEO>& pop) 00040 { 00041 return boost::python::call_method< const PyEO& >(self, "__call__", boost::ref(pop)); 00042 } 00043 }; 00044 00045 template <class Select> 00046 void add_select(std::string name) 00047 { 00048 class_<Select, bases<eoSelectOne<PyEO> > >(name.c_str(), init<>() ) 00049 .def("__call__", &Select::operator(), return_internal_reference<>() ) 00050 ; 00051 } 00052 00053 template <class Select, class Init> 00054 void add_select(std::string name, Init init) 00055 { 00056 class_<Select, bases<eoSelectOne<PyEO> > >(name.c_str(), init) 00057 .def("__call__", &Select::operator(), return_internal_reference<>() ) 00058 ; 00059 } 00060 00061 template <class Select, class Init1, class Init2> 00062 void add_select(std::string name, Init1 init1, Init2 init2) 00063 { 00064 class_<Select, bases<eoSelectOne<PyEO> > >(name.c_str(), init1) 00065 .def( init2 ) 00066 .def("__call__", &Select::operator(), return_internal_reference<>() ) 00067 .def("setup", &Select::setup); 00068 } 00069 00070 void selectOne() 00071 { 00072 /* Concrete classes */ 00073 00074 pickle(class_<eoHowMany>("eoHowMany", init<>()) 00075 .def( init<double>() ) 00076 .def( init<double, bool>() ) 00077 .def( init<int>() ) 00078 .def("__call__", &eoHowMany::operator()) 00079 .def("__neg__", &eoHowMany::operator-) 00080 ); 00081 00082 class_<eoSelectOne<PyEO>, eoSelectOneWrapper, boost::noncopyable>("eoSelectOne", init<>()) 00083 .def("__call__", &eoSelectOneWrapper::operator(), return_internal_reference<>() ) 00084 .def("setup", &eoSelectOne<PyEO>::setup); 00085 00086 /* SelectOne derived classes */ 00087 00088 add_select<eoDetTournamentSelect<PyEO> >("eoDetTournamentSelect", init<>(), init<unsigned>() ); 00089 add_select<eoStochTournamentSelect<PyEO> >("eoStochTournamentSelect", init<>(), init<double>() ); 00090 add_select<eoTruncatedSelectOne<PyEO> >("eoTruncatedSelectOne", 00091 init<eoSelectOne<PyEO>&, double>()[WC1], 00092 init<eoSelectOne<PyEO>&, eoHowMany >()[WC1] 00093 ); 00094 00095 // eoProportionalSelect is not feasible to implement at this point as fitness is not recognizable as a float 00096 // use eoDetTournament instead: with a t-size of 2 it is equivalent to eoProportional with linear scaling 00097 //add_select<eoProportionalSelect<PyEO> >("eoProportionalSelect", init<eoPop<PyEO>&>() ); 00098 00099 add_select<eoRandomSelect<PyEO> >("eoRandomSelect"); 00100 add_select<eoBestSelect<PyEO> >("eoBestSelect"); 00101 add_select<eoNoSelect<PyEO> >("eoNoSelect"); 00102 00103 add_select<eoSequentialSelect<PyEO> >("eoSequentialSelect", init<>(), init<bool>()); 00104 add_select<eoEliteSequentialSelect<PyEO> >("eoEliteSequentialSelect"); 00105 /* 00106 * eoSelectFromWorth.h:class eoSelectFromWorth : public eoSelectOne<EOT> 00107 */ 00108 }