EvolvingObjects
perf2worth.cpp
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 <eoNDSorting.h>
00022 
00023 #include "PyEO.h"
00024 
00025 using namespace boost::python;
00026 
00027 struct Perf2WorthWrapper : public eoPerf2Worth<PyEO,double>
00028 {
00029     PyObject* self;
00030     Perf2WorthWrapper(PyObject* p) : self(p) {}
00031 
00032     void operator()( const eoPop<PyEO>& pop)
00033     {
00034         call_method<void>(self, "__call__", boost::ref(pop));
00035     }
00036 };
00037 
00038 numeric::array get_worths(eoPerf2Worth<PyEO, double>& p)
00039 {
00040     std::vector<double>& worths = p.value();
00041     list result;
00042 
00043     for (unsigned i = 0; i < worths.size(); ++i)
00044         result.append(worths[i]);
00045 
00046     return numeric::array(result);
00047 }
00048 
00049 struct CachedPerf2WorthWrapper : public eoPerf2WorthCached<PyEO, double>
00050 {
00051     PyObject* self;
00052     CachedPerf2WorthWrapper(PyObject* p) : self(p) {}
00053 
00054     void calculate_worths(const eoPop<PyEO>& pop)
00055     {
00056         call_method<void>(self, "calculate_worths", boost::ref(pop));
00057     }
00058 };
00059 
00060 void perf2worth()
00061 {
00062     //numeric::array::set_module_and_type("Numeric", "ArrayType");
00063 
00064     class_<eoPerf2Worth<PyEO, double>,
00065         Perf2WorthWrapper,
00066         bases< eoValueParam<std::vector<double> > >,
00067         boost::noncopyable>("eoPerf2Worth", init<>())
00068 
00069         .def("__call__", &Perf2WorthWrapper::operator())
00070         .def("sort_pop", &eoPerf2Worth<PyEO, double>::sort_pop)
00071     //.def("value", get_worths)
00072         ;
00073 
00074     class_<eoPerf2WorthCached<PyEO, double>,
00075         CachedPerf2WorthWrapper,
00076         bases<eoPerf2Worth<PyEO, double> >,
00077         boost::noncopyable>("eoPerf2WorthCached", init<>())
00078 
00079         .def("__call__", &eoPerf2WorthCached<PyEO, double>::operator())
00080         .def("calculate_worths", &CachedPerf2WorthWrapper::calculate_worths)
00081         ;
00082 
00083     //class_<eoNoPerf2Worth<PyEO>, bases<eoPerf2Worth<PyEO, double> > >("eoNoPerf2Worth")
00084     //  .def("__call__", &eoNoPerf2Worth<PyEO>::operator());
00085 
00086     class_<eoNDSorting_II<PyEO>,
00087         bases<eoPerf2WorthCached<PyEO, double> > >("eoNDSorting_II")
00088         .def("calculate_worths", &eoNDSorting_II<PyEO>::calculate_worths);
00089 }
 All Classes Namespaces Files Functions Variables Typedefs Friends