From c6b4f255d99b65341aba4074a2c8383f189a0821 Mon Sep 17 00:00:00 2001 From: maartenkeijzer Date: Tue, 7 Jan 2003 14:24:56 +0000 Subject: [PATCH] some bugfixes --- eo/src/pyeo/PyEO.h | 2 +- eo/src/pyeo/perf2worth.cpp | 56 +++++++++++++++++++++++++++++++++++-- eo/src/pyeo/test/test_mo.py | 46 ++++++++++++++++++++++++++---- 3 files changed, 96 insertions(+), 8 deletions(-) diff --git a/eo/src/pyeo/PyEO.h b/eo/src/pyeo/PyEO.h index dc5dd260..79fcee3f 100644 --- a/eo/src/pyeo/PyEO.h +++ b/eo/src/pyeo/PyEO.h @@ -46,7 +46,7 @@ class PyFitness : public boost::python::object static double tol() { return 1e-6; } static bool maximizing(int which) { return objective_info[which] > 0; } - static void setObjectivesSize(int sz) { objective_info.resize(sz); } + static void setObjectivesSize(int sz) { objective_info.resize(sz, 0); } static void setObjectivesValue(unsigned which, int value) { if (which >= objective_info.size()) diff --git a/eo/src/pyeo/perf2worth.cpp b/eo/src/pyeo/perf2worth.cpp index 77cbd70e..7c4da9af 100644 --- a/eo/src/pyeo/perf2worth.cpp +++ b/eo/src/pyeo/perf2worth.cpp @@ -21,9 +21,61 @@ #include "PyEO.h" -void perf2worth() // will have to rethink this +struct Perf2WorthWrapper : public eoPerf2Worth { - class_ >("eoNDSorting_II"); + PyObject* self; + Perf2WorthWrapper(PyObject* p) : self(p) {} + + void operator()( const eoPop& pop) + { + call_method(self, "__call__", boost::ref(pop)); + } + +}; + +numeric::array get_worths(eoPerf2Worth& p) +{ + std::vector& worths = p.value(); + list result; + + for (unsigned i = 0; i < worths.size(); ++i) + result.append(worths[i]); + + return numeric::array(result); +} + +struct CachedPerf2WorthWrapper : public eoPerf2WorthCached +{ + PyObject* self; + CachedPerf2WorthWrapper(PyObject* p) : self(p) {} + + void calculate_worths(const eoPop& pop) + { + call_method(self, "calculate_worths", boost::ref(pop)); + } +}; + +void perf2worth() +{ + numeric::array::set_module_and_type("Numeric", "ArrayType"); + + class_, Perf2WorthWrapper, boost::noncopyable>("eoPerf2Worth", init<>()) + .def("__call__", &Perf2WorthWrapper::operator()) + .def("sort_pop", &eoPerf2Worth::sort_pop) + .def("value", get_worths) + ; + + class_, CachedPerf2WorthWrapper, bases >, boost::noncopyable> + ("eoPerf2WorthCached", init<>()) + .def("__call__", &eoPerf2WorthCached::operator()) + .def("calculate_worths", &CachedPerf2WorthWrapper::calculate_worths) + ; + + //class_, bases > >("eoNoPerf2Worth") +// .def("__call__", &eoNoPerf2Worth::operator()); + + class_, bases > >("eoNDSorting_II") + .def("calculate_worths", &eoNDSorting_II::calculate_worths); } diff --git a/eo/src/pyeo/test/test_mo.py b/eo/src/pyeo/test/test_mo.py index 3cf43e67..11a02c03 100644 --- a/eo/src/pyeo/test/test_mo.py +++ b/eo/src/pyeo/test/test_mo.py @@ -3,10 +3,10 @@ import unittest class TestSGA(unittest.TestCase): - def testMO(self): + def testIndividuals(self): setObjectivesSize(2); setObjectivesValue(0,1); - setObjectivesValue(1,-1); + setObjectivesValue(1,1); eo1 = EO(); eo2 = EO(); @@ -14,10 +14,46 @@ class TestSGA(unittest.TestCase): eo1.fitness = [0,1]; eo2.fitness = [1,1]; - print dominates(eo1, eo2) + self.failUnlessEqual(dominates(eo1, eo2), 0) + self.failUnlessEqual(dominates(eo2, eo1), 1) + self.failUnlessEqual(dominates(eo2, eo2), 0) + setObjectivesValue(0,-1) - setObjectivesValue(1,1); - print dominates(eo1, eo2) + setObjectivesValue(1,-1); + + self.failUnlessEqual(dominates(eo1, eo2), 1) + self.failUnlessEqual(dominates(eo2, eo1), 0) + self.failUnlessEqual(dominates(eo2, eo2), 0) + + def testNDSorting(self): + + setObjectivesSize(2); + setObjectivesValue(0,-1) + setObjectivesValue(1,-1); + + pop = Pop() + pop.resize(6) + + pop[5].fitness = [0.15,0.87] + pop[4].fitness = [0.1,0.9] + pop[3].fitness = [0,1]; + pop[2].fitness = [1,0]; + pop[1].fitness = [1,1]; + pop[0].fitness = [2,1]; + + srt = eoNDSorting_II() + + srt(pop) + srt.sort_pop(pop) + + for indy in pop: + print indy.fitness + + + worths = srt.value() + print worths + print type(worths) + if __name__=='__main__': unittest.main()