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 <eoGenOp.h> 00022 #include <eoOp.h> 00023 #include <eoCloneOps.h> 00024 #include <eoPopulator.h> 00025 #include <eoOpContainer.h> 00026 00027 #include "PyEO.h" 00028 #include "def_abstract_functor.h" 00029 00030 using namespace boost::python; 00031 00032 class GenOpWrapper : public eoGenOp<PyEO> 00033 { 00034 public: 00035 00036 PyObject* self; 00037 GenOpWrapper(PyObject* p) : self(p) {} 00038 unsigned max_production(void) 00039 { 00040 return call_method<unsigned>(self,"max_production"); 00041 } 00042 std::string className() const 00043 { 00044 return "GenOpDerivative"; // never saw the use of className anyway 00045 } 00046 00047 void apply(eoPopulator<PyEO>& populator ) 00048 { 00049 boost::python::call_method<void>(self,"apply", boost::ref( populator ) ); 00050 } 00051 }; 00052 00053 class PopulatorWrapper : public eoPopulator<PyEO> 00054 { 00055 public: 00056 PyObject* self; 00057 PopulatorWrapper(PyObject* p, const eoPop<PyEO>& src, eoPop<PyEO>& dest) 00058 : eoPopulator<PyEO>(src, dest), self(p) 00059 { 00060 //throw std::runtime_error("abstract base class"); 00061 } 00062 00063 const PyEO& select() 00064 { 00065 return call_method<const PyEO&>(self,"select"); 00066 } 00067 }; 00068 00069 class MonOpWrapper : public eoMonOp<PyEO> 00070 { 00071 public: 00072 PyObject* self; 00073 MonOpWrapper(PyObject* p) : self(p) {} 00074 bool operator()(PyEO& _eo) 00075 { return boost::python::call_method<bool>(self, "__call__", boost::ref( _eo )); } 00076 }; 00077 class BinOpWrapper : public eoBinOp<PyEO> 00078 { 00079 public: 00080 PyObject* self; 00081 BinOpWrapper(PyObject* p) : self(p) {} 00082 bool operator()(PyEO& _eo, const PyEO& _eo2) 00083 { return boost::python::call_method<bool>(self, "__call__", boost::ref( _eo ), boost::ref(_eo2)); } 00084 }; 00085 class QuadOpWrapper : public eoQuadOp<PyEO> 00086 { 00087 public: 00088 PyObject* self; 00089 QuadOpWrapper(PyObject* p) : self(p) {} 00090 bool operator()(PyEO& _eo, PyEO& _eo2) 00091 { return boost::python::call_method<bool>(self, "__call__", boost::ref( _eo ), boost::ref(_eo2)); } 00092 }; 00093 00094 void geneticOps() 00095 { 00096 class_<eoPopulator<PyEO>, PopulatorWrapper, boost::noncopyable> 00097 ("eoPopulator", init<const eoPop<PyEO>&, eoPop<PyEO>&>() ) 00098 .def("select", &PopulatorWrapper::select, return_internal_reference<>() ) 00099 .def("get", &eoPopulator<PyEO>::operator*, return_internal_reference<>() ) 00100 .def("next", &eoPopulator<PyEO>::operator++, return_internal_reference<>() ) 00101 .def("insert", &eoPopulator<PyEO>::insert) 00102 .def("reserve", &eoPopulator<PyEO>::reserve) 00103 .def("source", &eoPopulator<PyEO>::source, return_internal_reference<>() ) 00104 .def("offspring", &eoPopulator<PyEO>::offspring, return_internal_reference<>() ) 00105 .def("tellp", &eoPopulator<PyEO>::tellp) 00106 .def("seekp", &eoPopulator<PyEO>::seekp) 00107 .def("exhausted", &eoPopulator<PyEO>::exhausted) 00108 ; 00109 00110 class_<eoSeqPopulator<PyEO>, bases<eoPopulator<PyEO> > > 00111 ("eoSeqPopulator", init<const eoPop<PyEO>&, eoPop<PyEO>&>() ) 00112 .def("select", &eoSeqPopulator<PyEO>::select, return_internal_reference<>() ) 00113 ; 00114 00115 class_<eoSelectivePopulator<PyEO>, bases<eoPopulator<PyEO> > > 00116 ("eoSelectivePopulator", init<const eoPop<PyEO>&, eoPop<PyEO>&, eoSelectOne<PyEO>& >() ) 00117 .def("select", &eoSeqPopulator<PyEO>::select, return_internal_reference<>() ) 00118 ; 00119 enum_<eoOp<PyEO>::OpType>("OpType") 00120 .value("unary", eoOp<PyEO>::unary) 00121 .value("binary", eoOp<PyEO>::binary) 00122 .value("quadratic", eoOp<PyEO>::quadratic) 00123 .value("general", eoOp<PyEO>::general) 00124 ; 00125 00126 class_<eoOp<PyEO> >("eoOp", init<eoOp<PyEO>::OpType>()) 00127 .def("getType", &eoOp<PyEO>::getType); 00128 00129 class_<eoMonOp<PyEO>, MonOpWrapper, bases<eoOp<PyEO> >, boost::noncopyable>("eoMonOp", init<>()) 00130 .def("__call__", &MonOpWrapper::operator(), "an example docstring"); 00131 class_<eoBinOp<PyEO>, BinOpWrapper, bases<eoOp<PyEO> >, boost::noncopyable>("eoBinOp", init<>()) 00132 .def("__call__", &BinOpWrapper::operator()); 00133 class_<eoQuadOp<PyEO>, QuadOpWrapper, bases<eoOp<PyEO> >, boost::noncopyable>("eoQuadOp", init<>()) 00134 .def("__call__", &QuadOpWrapper::operator()); 00135 00136 class_<eoGenOp<PyEO>, GenOpWrapper, bases<eoOp<PyEO> >, boost::noncopyable>("eoGenOp", init<>()) 00137 .def("max_production", &GenOpWrapper::max_production) 00138 .def("className", &GenOpWrapper::className) 00139 .def("apply", &GenOpWrapper::apply) 00140 .def("__call__", &eoGenOp<PyEO>::operator()) 00141 ; 00142 00143 class_<eoSequentialOp<PyEO>, bases<eoGenOp<PyEO> >, boost::noncopyable>("eoSequentialOp", init<>()) 00144 .def("add", &eoSequentialOp<PyEO>::add, WC1) 00145 .def("apply", &eoSequentialOp<PyEO>::apply) 00146 ; 00147 00148 class_<eoProportionalOp<PyEO>, bases<eoGenOp<PyEO> >, boost::noncopyable>("eoProportionalOp", init<>()) 00149 .def("add", &eoProportionalOp<PyEO>::add, WC1) 00150 .def("apply", &eoProportionalOp<PyEO>::apply) 00151 ; 00152 00153 /* Cloning */ 00154 class_<eoMonCloneOp<PyEO>, bases<eoMonOp<PyEO> > >("eoMonCloneOp").def("__call__", &eoMonCloneOp<PyEO>::operator()); 00155 class_<eoBinCloneOp<PyEO>, bases<eoBinOp<PyEO> > >("eoBinCloneOp").def("__call__", &eoBinCloneOp<PyEO>::operator()); 00156 class_<eoQuadCloneOp<PyEO>, bases<eoQuadOp<PyEO> > >("eoQuadCloneOp").def("__call__", &eoQuadCloneOp<PyEO>::operator()); 00157 00158 }