/* PyEO Copyright (C) 2003 Maarten Keijzer This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include "PyEO.h" #include "def_abstract_functor.h" using namespace boost::python; class GenOpWrapper : public eoGenOp { public: PyObject* self; GenOpWrapper(PyObject* p) : self(p) {} unsigned max_production(void) { return call_method(self,"max_production"); } std::string className() const { return "GenOpDerivative"; // never saw the use of className anyway } void apply(eoPopulator& populator ) { boost::python::call_method(self,"apply", boost::ref( populator ) ); } }; class PopulatorWrapper : public eoPopulator { public: PyObject* self; PopulatorWrapper(PyObject* p, const eoPop& src, eoPop& dest) : eoPopulator(src, dest), self(p) { //throw std::runtime_error("abstract base class"); } const PyEO& select() { return call_method(self,"select"); } }; class MonOpWrapper : public eoMonOp { public: PyObject* self; MonOpWrapper(PyObject* p) : self(p) {} bool operator()(PyEO& _eo) { return boost::python::call_method(self, "__call__", boost::ref( _eo )); } }; class BinOpWrapper : public eoBinOp { public: PyObject* self; BinOpWrapper(PyObject* p) : self(p) {} bool operator()(PyEO& _eo, const PyEO& _eo2) { return boost::python::call_method(self, "__call__", boost::ref( _eo ), boost::ref(_eo2)); } }; class QuadOpWrapper : public eoQuadOp { public: PyObject* self; QuadOpWrapper(PyObject* p) : self(p) {} bool operator()(PyEO& _eo, PyEO& _eo2) { return boost::python::call_method(self, "__call__", boost::ref( _eo ), boost::ref(_eo2)); } }; void geneticOps() { class_, PopulatorWrapper, boost::noncopyable> ("eoPopulator", init&, eoPop&>() ) .def("select", &PopulatorWrapper::select, return_internal_reference<>() ) .def("get", &eoPopulator::operator*, return_internal_reference<>() ) .def("next", &eoPopulator::operator++, return_internal_reference<>() ) .def("insert", &eoPopulator::insert) .def("reserve", &eoPopulator::reserve) .def("source", &eoPopulator::source, return_internal_reference<>() ) .def("offspring", &eoPopulator::offspring, return_internal_reference<>() ) .def("tellp", &eoPopulator::tellp) .def("seekp", &eoPopulator::seekp) .def("exhausted", &eoPopulator::exhausted) ; class_, bases > > ("eoSeqPopulator", init&, eoPop&>() ) .def("select", &eoSeqPopulator::select, return_internal_reference<>() ) ; class_, bases > > ("eoSelectivePopulator", init&, eoPop&, eoSelectOne& >() ) .def("select", &eoSeqPopulator::select, return_internal_reference<>() ) ; enum_::OpType>("OpType") .value("unary", eoOp::unary) .value("binary", eoOp::binary) .value("quadratic", eoOp::quadratic) .value("general", eoOp::general) ; class_ >("eoOp", init::OpType>()) .def("getType", &eoOp::getType); class_, MonOpWrapper, bases >, boost::noncopyable>("eoMonOp", init<>()) .def("__call__", &MonOpWrapper::operator(), "an example docstring"); class_, BinOpWrapper, bases >, boost::noncopyable>("eoBinOp", init<>()) .def("__call__", &BinOpWrapper::operator()); class_, QuadOpWrapper, bases >, boost::noncopyable>("eoQuadOp", init<>()) .def("__call__", &QuadOpWrapper::operator()); class_, GenOpWrapper, bases >, boost::noncopyable>("eoGenOp", init<>()) .def("max_production", &GenOpWrapper::max_production) .def("className", &GenOpWrapper::className) .def("apply", &GenOpWrapper::apply) .def("__call__", &eoGenOp::operator()) ; class_, bases >, boost::noncopyable>("eoSequentialOp", init<>()) .def("add", &eoSequentialOp::add, WC1) .def("apply", &eoSequentialOp::apply) ; class_, bases >, boost::noncopyable>("eoProportionalOp", init<>()) .def("add", &eoProportionalOp::add, WC1) .def("apply", &eoProportionalOp::apply) ; /* Cloning */ class_, bases > >("eoMonCloneOp").def("__call__", &eoMonCloneOp::operator()); class_, bases > >("eoBinCloneOp").def("__call__", &eoBinCloneOp::operator()); class_, bases > >("eoQuadCloneOp").def("__call__", &eoQuadCloneOp::operator()); }