From 0a4fb5503110283a08787bfb361f7a5e30b75c84 Mon Sep 17 00:00:00 2001 From: maartenkeijzer Date: Mon, 6 Jan 2003 16:10:38 +0000 Subject: [PATCH] Added multi-objective support to PyEO (and changed its name to that) --- eo/src/pyeo/Makefile | 12 +- eo/src/pyeo/abstract1.cpp | 4 +- eo/src/pyeo/algos.cpp | 4 +- eo/src/pyeo/breeders.cpp | 4 +- eo/src/pyeo/continuators.cpp | 4 +- eo/src/pyeo/def_abstract_functor.h | 2 +- eo/src/pyeo/geneticOps.cpp | 4 +- eo/src/pyeo/mergers.cpp | 4 +- eo/src/pyeo/monitors.cpp | 4 +- eo/src/pyeo/perf2worth.cpp | 9 +- eo/src/pyeo/pickle.h | 2 +- eo/src/pyeo/pyeo.cpp | 173 ----------------------------- eo/src/pyeo/pyeo.h | 117 ------------------- eo/src/pyeo/random_numbers.cpp | 2 +- eo/src/pyeo/reduce.cpp | 4 +- eo/src/pyeo/replacement.cpp | 4 +- eo/src/pyeo/selectOne.cpp | 4 +- eo/src/pyeo/selectors.cpp | 4 +- eo/src/pyeo/test/maxone.py | 2 +- eo/src/pyeo/test/test_mo.py | 23 ++++ eo/src/pyeo/valueParam.cpp | 2 +- 21 files changed, 60 insertions(+), 328 deletions(-) delete mode 100644 eo/src/pyeo/pyeo.cpp delete mode 100644 eo/src/pyeo/pyeo.h create mode 100644 eo/src/pyeo/test/test_mo.py diff --git a/eo/src/pyeo/Makefile b/eo/src/pyeo/Makefile index ee1e4316..89ee85b1 100644 --- a/eo/src/pyeo/Makefile +++ b/eo/src/pyeo/Makefile @@ -30,25 +30,25 @@ CPPFLAGS = -Wall #-O2 LDFLAGS = COMPILE = $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c LINK = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) -INC=-I/usr/include/python2.2/ -I/usr/include/stlport -I.. -ftemplate-depth-50 +INC=-I/usr/include/python2.2 -I/usr/include/stlport -I.. -ftemplate-depth-50 -OBJECTS=eoFunctorStore.o pyeo.o abstract1.o algos.o \ +OBJECTS=eoFunctorStore.o PyEO.o abstract1.o algos.o \ random_numbers.o geneticOps.o selectOne.o continuators.o\ reduce.o replacement.o selectors.o breeders.o\ mergers.o valueParam.o perf2worth.o -all: pyeo.so +all: PyEO.so clean: rm *.so *.o test/*.pyc -pyeo.so: $(OBJECTS) - $(LINK) -o pyeo.so $(OBJECTS) -lboost_python -lpython2.2 -shared -lstlport +PyEO.so: $(OBJECTS) + $(LINK) -o PyEO.so $(OBJECTS) -lboost_python -lpython2.2 -shared -lstlport eoFunctorStore.o: ../eoFunctorStore.h ../eoFunctorStore.cpp $(COMPILE) -o eoFunctorStore.o ../eoFunctorStore.cpp $(INC) -%.o:%.cpp pyeo.h def_abstract_functor.h +%.o:%.cpp PyEO.h def_abstract_functor.h $(COMPILE) $< $(INC) diff --git a/eo/src/pyeo/abstract1.cpp b/eo/src/pyeo/abstract1.cpp index d35edefc..5f6c0713 100644 --- a/eo/src/pyeo/abstract1.cpp +++ b/eo/src/pyeo/abstract1.cpp @@ -1,5 +1,5 @@ /* - pyeo + PyEO Copyright (C) 2003 Maarten Keijzer @@ -24,7 +24,7 @@ #include #include -#include "pyeo.h" +#include "PyEO.h" #include "def_abstract_functor.h" void abstract1() diff --git a/eo/src/pyeo/algos.cpp b/eo/src/pyeo/algos.cpp index c375a6a0..baa137bb 100644 --- a/eo/src/pyeo/algos.cpp +++ b/eo/src/pyeo/algos.cpp @@ -1,5 +1,5 @@ /* - pyeo + PyEO Copyright (C) 2003 Maarten Keijzer @@ -23,7 +23,7 @@ #include #include -#include "pyeo.h" +#include "PyEO.h" #include "def_abstract_functor.h" void algos() diff --git a/eo/src/pyeo/breeders.cpp b/eo/src/pyeo/breeders.cpp index da2b5dcc..466286f5 100644 --- a/eo/src/pyeo/breeders.cpp +++ b/eo/src/pyeo/breeders.cpp @@ -1,5 +1,5 @@ /* - pyeo + PyEO Copyright (C) 2003 Maarten Keijzer @@ -22,7 +22,7 @@ #include #include -#include "pyeo.h" +#include "PyEO.h" #include "def_abstract_functor.h" #define DEF(x) class_, bases > >(#x).def("__call__", &eoBreed::operator()) diff --git a/eo/src/pyeo/continuators.cpp b/eo/src/pyeo/continuators.cpp index 8a9cb4a9..620ae420 100644 --- a/eo/src/pyeo/continuators.cpp +++ b/eo/src/pyeo/continuators.cpp @@ -1,5 +1,5 @@ /* - pyeo + PyEO Copyright (C) 2003 Maarten Keijzer @@ -24,7 +24,7 @@ #include #include -#include "pyeo.h" +#include "PyEO.h" #include "def_abstract_functor.h" #define DEF(x) class_, bases > >(#x).def("__call__", &eoContinue::operator()) diff --git a/eo/src/pyeo/def_abstract_functor.h b/eo/src/pyeo/def_abstract_functor.h index eaa4aa54..fe937952 100644 --- a/eo/src/pyeo/def_abstract_functor.h +++ b/eo/src/pyeo/def_abstract_functor.h @@ -1,5 +1,5 @@ /* - pyeo + PyEO Copyright (C) 2003 Maarten Keijzer diff --git a/eo/src/pyeo/geneticOps.cpp b/eo/src/pyeo/geneticOps.cpp index 85f5f84e..9e4379ff 100644 --- a/eo/src/pyeo/geneticOps.cpp +++ b/eo/src/pyeo/geneticOps.cpp @@ -1,5 +1,5 @@ /* - pyeo + PyEO Copyright (C) 2003 Maarten Keijzer @@ -24,7 +24,7 @@ #include #include -#include "pyeo.h" +#include "PyEO.h" class GenOpWrapper : public eoGenOp { diff --git a/eo/src/pyeo/mergers.cpp b/eo/src/pyeo/mergers.cpp index 1106b663..23f24b5a 100644 --- a/eo/src/pyeo/mergers.cpp +++ b/eo/src/pyeo/mergers.cpp @@ -1,5 +1,5 @@ /* - pyeo + PyEO Copyright (C) 2003 Maarten Keijzer @@ -19,7 +19,7 @@ */ #include -#include "pyeo.h" +#include "PyEO.h" #include "def_abstract_functor.h" #define DEF(x) class_, bases > >(#x).def("__call__", &eoMerge::operator()) diff --git a/eo/src/pyeo/monitors.cpp b/eo/src/pyeo/monitors.cpp index e15f091b..0a44521f 100644 --- a/eo/src/pyeo/monitors.cpp +++ b/eo/src/pyeo/monitors.cpp @@ -1,5 +1,5 @@ /* - pyeo + PyEO Copyright (C) 2003 Maarten Keijzer @@ -19,7 +19,7 @@ */ #include -#include "pyeo.h" +#include "PyEO.h" void monitors() { diff --git a/eo/src/pyeo/perf2worth.cpp b/eo/src/pyeo/perf2worth.cpp index 5ef8630d..77cbd70e 100644 --- a/eo/src/pyeo/perf2worth.cpp +++ b/eo/src/pyeo/perf2worth.cpp @@ -1,5 +1,5 @@ /* - pyeo + PyEO Copyright (C) 2003 Maarten Keijzer @@ -17,14 +17,13 @@ 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 "pyeo.h" +#include "PyEO.h" void perf2worth() // will have to rethink this { - // class_ >("eoNDSorting_II"); + class_ >("eoNDSorting_II"); } diff --git a/eo/src/pyeo/pickle.h b/eo/src/pyeo/pickle.h index 992f04a1..b5930626 100644 --- a/eo/src/pyeo/pickle.h +++ b/eo/src/pyeo/pickle.h @@ -1,5 +1,5 @@ /* - pyeo + PyEO Copyright (C) 2003 Maarten Keijzer diff --git a/eo/src/pyeo/pyeo.cpp b/eo/src/pyeo/pyeo.cpp deleted file mode 100644 index c7abcf18..00000000 --- a/eo/src/pyeo/pyeo.cpp +++ /dev/null @@ -1,173 +0,0 @@ -/* - 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 "pyeo.h" - -ostream& operator<<(ostream& os, const PyEO& _eo) -{ - os << _eo.to_string(); - return os; -} - -struct pyPop_pickle_suite : boost::python::pickle_suite -{ - static boost::python::tuple getstate(const eoPop& _pop) - { - boost::python::list entries; - for (unsigned i = 0; i != _pop.size(); ++i) - entries.append( PyEO_pickle_suite::getstate(_pop[i]) ); - - return make_tuple(object(_pop.size()), entries); - } - - static void setstate( eoPop& _pop, boost::python::tuple pickled) - { - int sz = extract(pickled[0]); - boost::python::list entries = pickled[1]; - _pop.resize(sz); - for (unsigned i = 0; i != _pop.size(); ++i) - PyEO_pickle_suite::setstate(_pop[i], tuple(entries[i]) ); - } -}; - - -template -boost::python::str to_string(T& _p) -{ - std::ostrstream os; - _p.printOn(os); - os << ends; - std::string s(os.str()); - return str(s.c_str()); -} - -void pop_sort(eoPop& pop) { pop.sort(); } -void pop_shuffle(eoPop& pop) { pop.shuffle(); } - -struct index_error { index_error(std::string w) : what(w) {}; std::string what; }; -void translate_index_error(index_error const& e) -{ - PyErr_SetString(PyExc_IndexError, e.what.c_str()); -} - -PyEO& pop_getitem(eoPop& pop, object key) -{ - extract x(key); - if (!x.check()) - throw index_error("Slicing not allowed"); - - int i = x(); - - if (static_cast(i) >= pop.size()) - { - throw index_error("Index out of bounds"); - } - - return pop[i]; -} -void pop_setitem(eoPop& pop, object key, PyEO& value) -{ - - extract x(key); - if (!x.check()) - throw index_error("Slicing not allowed"); - - int i = x(); - - if (static_cast(i) >= pop.size()) - { - throw index_error("Index out of bounds"); - } - - pop[i] = value; -} - -void pop_push_back(eoPop& pop, PyEO& p) { pop.push_back(p); } -void pop_resize( eoPop& pop, unsigned i) { pop.resize(i); } - -extern void abstract1(); -extern void algos(); -extern void random_numbers(); -extern void geneticOps(); -extern void selectOne(); -extern void continuators(); -extern void reduce(); -extern void replacement(); -extern void selectors(); -extern void breeders(); -extern void mergers(); -extern void valueParam(); -extern void perf2worth(); - -BOOST_PYTHON_MODULE(pyeo) -{ - register_exception_translator(&translate_index_error); - - class_("EO") - .add_property("fitness", &PyEO::getFitness, &PyEO::setFitness) - .add_property("genome", &PyEO::getGenome, &PyEO::setGenome) - .def_pickle(PyEO_pickle_suite()) - .def("invalidate", &PyEO::invalidate) - .def("invalid", &PyEO::invalid) - .def("__str__", &PyEO::to_string) - ; - - class_ >("Pop", init<>() ) - .def( init< unsigned, eoInit& >() ) - .def("append", &eoPop::append) - .def("__str__", to_string >) - .def("__len__", &eoPop::size) - .def("sort", pop_sort ) - .def("shuffle", pop_shuffle) - .def("__getitem__", pop_getitem, return_internal_reference<>() ) - .def("__setitem__", pop_setitem) - .def("best", &eoPop::best_element, return_internal_reference<>() ) - .def("push_back", pop_push_back) - .def("resize", pop_resize) - .def_pickle(pyPop_pickle_suite()) - ; - - - // Other definitions in different compilation units, - // this to avoid having g++ to choke on the load - random_numbers(); - valueParam(); - abstract1(); - geneticOps(); - selectOne(); - selectors(); - perf2worth(); - continuators(); - reduce(); - replacement(); - breeders(); - mergers(); - algos(); - -} - - -// to avoid having to build with libeo.a -ostream & operator << ( ostream& _os, const eoPrintable& _o ) { - _o.printOn(_os); - return _os; -}; diff --git a/eo/src/pyeo/pyeo.h b/eo/src/pyeo/pyeo.h deleted file mode 100644 index 101f6d45..00000000 --- a/eo/src/pyeo/pyeo.h +++ /dev/null @@ -1,117 +0,0 @@ -/* - 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 -*/ - -#ifndef PYEO_H -#define PYEO_H - -#include -#include - -using namespace boost::python; -/* -class PyFitness : public boost::python::object -{ - public : - PyFitness() : object() {} - - template - PyFitness(const T& o) : object(o) {} - - - static unsigned nObjectives() { return 1; } - static double tol() { return 1e-6; } - static bool maximizing(int which) { return true; } - - static bool dominates(const PyFitness& one, const PyFitness& oth) { return true; } // for the moment - - operator unsigned() const { return 1; } // for the moment - operator double() const { return 1; } // for the moment - - PyFitness operator[](int i) { return PyFitness(object::operator[](i)); } - - friend ostream& operator<<(ostream& os, const PyFitness& p) { const object& o = p; os << o; return os; } - friend istream& operator>>(istream& is, PyFitness& p) { object o; is >> o; p = o; return is; } - - typedef PyFitness AtomType; -}; - - - -object fabs(object obj) -{ - object zero(0.0); - if (obj < zero ) - return zero-obj; - return obj; -} - -object max(object a, object b) -{ - if (a < b) - return b; - return a; -} -*/ - -struct PyEO : public EO< object > -{ - typedef object Fitness; - - Fitness getFitness() const { return invalid()? Fitness(): fitness(); } - void setFitness(Fitness f) { if (f == Fitness()) invalidate(); else fitness(f); } - - object getGenome() const { return genome; } - void setGenome(object g) { genome = g; } - object genome; - - std::string to_string() const - { - std::string result; - result += extract(str(getFitness())); - result += ' '; - result += extract(str(genome)); - return result; - } - - bool operator<(const PyEO& other) const { return EO::operator<(other); } - bool operator>(const PyEO& other) const { return EO::operator>(other); } - -}; - -ostream& operator<<(ostream& os, const PyEO& _eo); - -struct PyEO_pickle_suite : boost::python::pickle_suite -{ - typedef PyEO::Fitness Fitness; - - static - boost::python::tuple getstate(const PyEO& _eo) - { - return make_tuple(_eo.getFitness(), _eo.genome); - } - static - void setstate(PyEO& _eo, boost::python::tuple pickled) - { - _eo.setFitness( Fitness(pickled[0]) ); - _eo.genome = pickled[1]; - } -}; - -#endif diff --git a/eo/src/pyeo/random_numbers.cpp b/eo/src/pyeo/random_numbers.cpp index 368ba058..0088b1c1 100644 --- a/eo/src/pyeo/random_numbers.cpp +++ b/eo/src/pyeo/random_numbers.cpp @@ -1,5 +1,5 @@ /* - pyeo + PyEO Copyright (C) 2003 Maarten Keijzer diff --git a/eo/src/pyeo/reduce.cpp b/eo/src/pyeo/reduce.cpp index 24910cac..fa37e827 100644 --- a/eo/src/pyeo/reduce.cpp +++ b/eo/src/pyeo/reduce.cpp @@ -1,5 +1,5 @@ /* - pyeo + PyEO Copyright (C) 2003 Maarten Keijzer @@ -20,7 +20,7 @@ #include -#include "pyeo.h" +#include "PyEO.h" // unfortunately have to define it specially class eoReduceWrapper : public eoReduce diff --git a/eo/src/pyeo/replacement.cpp b/eo/src/pyeo/replacement.cpp index bcf799cc..683aa233 100644 --- a/eo/src/pyeo/replacement.cpp +++ b/eo/src/pyeo/replacement.cpp @@ -1,5 +1,5 @@ /* - pyeo + PyEO Copyright (C) 2003 Maarten Keijzer @@ -24,7 +24,7 @@ #include #include -#include "pyeo.h" +#include "PyEO.h" #include "def_abstract_functor.h" #define DEF(x) class_, bases > >(#x).def("__call__", &eoReplacement::operator()) diff --git a/eo/src/pyeo/selectOne.cpp b/eo/src/pyeo/selectOne.cpp index 42bf0f96..3216a753 100644 --- a/eo/src/pyeo/selectOne.cpp +++ b/eo/src/pyeo/selectOne.cpp @@ -1,5 +1,5 @@ /* - pyeo + PyEO Copyright (C) 2003 Maarten Keijzer @@ -25,7 +25,7 @@ #include #include -#include "pyeo.h" +#include "PyEO.h" #include "pickle.h" class eoSelectOneWrapper : public eoSelectOne diff --git a/eo/src/pyeo/selectors.cpp b/eo/src/pyeo/selectors.cpp index aeb333f4..25be973b 100644 --- a/eo/src/pyeo/selectors.cpp +++ b/eo/src/pyeo/selectors.cpp @@ -1,5 +1,5 @@ /* - pyeo + PyEO Copyright (C) 2003 Maarten Keijzer @@ -26,7 +26,7 @@ #include #include -#include "pyeo.h" +#include "PyEO.h" #include "def_abstract_functor.h" #define DEF(x) class_, bases > >(#x).def("__call__", &eoSelect::operator()) diff --git a/eo/src/pyeo/test/maxone.py b/eo/src/pyeo/test/maxone.py index 6f1baefa..f95bc0a7 100644 --- a/eo/src/pyeo/test/maxone.py +++ b/eo/src/pyeo/test/maxone.py @@ -3,7 +3,7 @@ import sys sys.path.append('..') print 'importing pyeo' -from pyeo import * +from PyEO import * print 'done' from copy import copy diff --git a/eo/src/pyeo/test/test_mo.py b/eo/src/pyeo/test/test_mo.py new file mode 100644 index 00000000..3cf43e67 --- /dev/null +++ b/eo/src/pyeo/test/test_mo.py @@ -0,0 +1,23 @@ +from maxone import * +import unittest + +class TestSGA(unittest.TestCase): + + def testMO(self): + setObjectivesSize(2); + setObjectivesValue(0,1); + setObjectivesValue(1,-1); + + eo1 = EO(); + eo2 = EO(); + + eo1.fitness = [0,1]; + eo2.fitness = [1,1]; + + print dominates(eo1, eo2) + setObjectivesValue(0,-1) + setObjectivesValue(1,1); + print dominates(eo1, eo2) + +if __name__=='__main__': + unittest.main() diff --git a/eo/src/pyeo/valueParam.cpp b/eo/src/pyeo/valueParam.cpp index 2b7eb36f..5f4e2192 100644 --- a/eo/src/pyeo/valueParam.cpp +++ b/eo/src/pyeo/valueParam.cpp @@ -1,5 +1,5 @@ /* - pyeo + PyEO Copyright (C) 2003 Maarten Keijzer