diff --git a/eo/src/pyeo/Makefile b/eo/src/pyeo/Makefile index df0e09cb..7f48a895 100644 --- a/eo/src/pyeo/Makefile +++ b/eo/src/pyeo/Makefile @@ -30,7 +30,7 @@ CPPFLAGS = -Wall -O2 #-g #-O2 LDFLAGS = COMPILE = $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c LINK = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) -INC=-I/usr/include/python2.2 -I.. -ftemplate-depth-50 -I/usr/include/stlport +INC=-I/usr/include/python2.2 -I.. -ftemplate-depth-50 #-I/usr/include/stlport OBJECTS=eoFunctorStore.o PyEO.o abstract1.o algos.o \ random_numbers.o geneticOps.o selectOne.o continuators.o\ @@ -44,7 +44,7 @@ clean: rm PyEO/*.so *.o test/*.pyc PyEO/PyEO.so: $(OBJECTS) - $(LINK) -o PyEO/PyEO.so $(OBJECTS) -lboost_python -lpython2.2 -shared -lstlport + $(LINK) -o PyEO/PyEO.so $(OBJECTS) -lboost_python -lpython2.2 -shared #-lstlport eoFunctorStore.o: ../eoFunctorStore.h ../eoFunctorStore.cpp $(COMPILE) -o eoFunctorStore.o ../eoFunctorStore.cpp $(INC) diff --git a/eo/src/pyeo/PyEO.cpp b/eo/src/pyeo/PyEO.cpp index a22c6036..6a0a1394 100644 --- a/eo/src/pyeo/PyEO.cpp +++ b/eo/src/pyeo/PyEO.cpp @@ -18,16 +18,37 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ - -#include #include "PyEO.h" +#include + #ifdef HAVE_SSTREAM #include #else #include #endif +typedef eoPop::iterator PopIt; +typedef eoPop::const_iterator cPopIt; + +PopIt operator+(PopIt it, size_t a) +{ + return it + ptrdiff_t(a); +} +cPopIt operator+(cPopIt it, size_t a) +{ + return it + ptrdiff_t(a); +} +PopIt operator-(PopIt it, size_t a) +{ + return it - ptrdiff_t(a); +} +cPopIt operator-(cPopIt it, size_t a) +{ + return it - ptrdiff_t(a); +} + using namespace std; +//using namespace boost::python; // static member, needs to be instantiated somewhere std::vector PyFitness::objective_info; @@ -82,13 +103,13 @@ struct pyPop_pickle_suite : boost::python::pickle_suite for (unsigned i = 0; i != _pop.size(); ++i) entries.append( PyEO_pickle_suite::getstate(_pop[i]) ); - return boost::python::make_tuple(object(_pop.size()), entries); + return boost::python::make_tuple(boost::python::object(_pop.size()), entries); } static void setstate( eoPop& _pop, boost::python::tuple pickled) { - int sz = extract(pickled[0]); - boost::python::list entries = list(pickled[1]); + int sz = boost::python::extract(pickled[0]); + boost::python::list entries = boost::python::list(pickled[1]); _pop.resize(sz); for (unsigned i = 0; i != _pop.size(); ++i) { @@ -104,13 +125,13 @@ boost::python::str to_string(T& _p) #ifdef HAVE_SSTREAM std::ostringstream os; _p.printOn(os); - return str(os.str().c_str()); + return boost::python::str(os.str().c_str()); #else std::ostrstream os; _p.printOn(os); os << ends; std::string s(os.str()); - return str(s.c_str()); + return boost::python::str(s.c_str()); #endif } @@ -122,9 +143,9 @@ void translate_index_error(index_error const& e) PyErr_SetString(PyExc_IndexError, e.what.c_str()); } -PyEO& pop_getitem(eoPop& pop, object key) +PyEO& pop_getitem(eoPop& pop, boost::python::object key) { - extract x(key); + boost::python::extract x(key); if (!x.check()) throw index_error("Slicing not allowed"); @@ -132,15 +153,17 @@ PyEO& pop_getitem(eoPop& pop, object key) if (static_cast(i) >= pop.size()) { + cerr << "throwing" << endl; throw index_error("Index out of bounds"); } - + cerr << "indexing " << i << endl; + return pop[i]; } -void pop_setitem(eoPop& pop, object key, PyEO& value) -{ - extract x(key); +void pop_setitem(eoPop& pop, boost::python::object key, PyEO& value) +{ + boost::python::extract x(key); if (!x.check()) throw index_error("Slicing not allowed"); @@ -175,9 +198,11 @@ extern void statistics(); BOOST_PYTHON_MODULE(PyEO) { - register_exception_translator(&translate_index_error); + using namespace boost::python; + + boost::python::register_exception_translator(&translate_index_error); - class_("EO") + boost::python::class_("EO") .add_property("fitness", &PyEO::getFitness, &PyEO::setFitness) .add_property("genome", &PyEO::getGenome, &PyEO::setGenome) .def_pickle(PyEO_pickle_suite()) @@ -186,7 +211,7 @@ BOOST_PYTHON_MODULE(PyEO) .def("__str__", &PyEO::to_string) ; - class_ >("eoPop", init<>() ) + boost::python::class_ >("eoPop", init<>() ) .def( init< unsigned, eoInit& >()[with_custodian_and_ward<1,3>()] ) .def("append", &eoPop::append) .def("__str__", to_string >) diff --git a/eo/src/pyeo/PyEO.h b/eo/src/pyeo/PyEO.h index abca84b6..6cf3c003 100644 --- a/eo/src/pyeo/PyEO.h +++ b/eo/src/pyeo/PyEO.h @@ -21,15 +21,18 @@ #ifndef PYEO_H #define PYEO_H -#include #include #include +#include #include -using namespace boost::python; - -struct index_error { index_error(std::string w) : what(w) {}; std::string what; }; +#include +struct index_error : public std::exception { + index_error(std::string w) : what(w) {}; + virtual ~index_error() throw() {} + std::string what; +}; class PyFitness : public boost::python::object { @@ -37,10 +40,10 @@ class PyFitness : public boost::python::object typedef PyFitness fitness_traits; // it's its own traits class :-) - PyFitness() : object() {} + PyFitness() : boost::python::object() {} template - PyFitness(const T& o) : object(o) {} + PyFitness(const T& o) : boost::python::object(o) {} static unsigned nObjectives() { return objective_info.size(); } static double tol() { return 1e-6; } @@ -63,7 +66,7 @@ class PyFitness : public boost::python::object double operator[](int i) const { - extract x(object::operator[](i)); + boost::python::extract x(object::operator[](i)); if (!x.check()) throw std::runtime_error("PyFitness: does not contain doubles"); @@ -101,28 +104,28 @@ class PyFitness : public boost::python::object return other.operator<(*this); } - void printOn(std::ostream& os) const { const object& o = *this; boost::python::api::operator<<(os,o); } + void printOn(std::ostream& os) const { const boost::python::object& o = *this; boost::python::api::operator<<(os,o); } friend std::ostream& operator<<(std::ostream& os, const PyFitness& p) { p.printOn(os); return os; } - friend std::istream& operator>>(std::istream& is, PyFitness& p) { object o; is >> o; p = o; return is; } + friend std::istream& operator>>(std::istream& is, PyFitness& p) { boost::python::object o; is >> o; p = o; return is; } }; struct PyEO : public EO< PyFitness > { typedef PyFitness Fitness; - object getFitness() const { return invalid()? Fitness(): fitness(); } - void setFitness(object f) { if (f == Fitness()) invalidate(); else fitness(f); } + boost::python::object getFitness() const { return invalid()? Fitness(): fitness(); } + void setFitness(boost::python::object f) { if (f == Fitness()) invalidate(); else fitness(f); } - object getGenome() const { return genome; } - void setGenome(object g) { genome = g; } - object genome; + boost::python::object getGenome() const { return genome; } + void setGenome(boost::python::object g) { genome = g; } + boost::python::object genome; std::string to_string() const { std::string result; - result += extract(str(getFitness())); + result += boost::python::extract(boost::python::str(getFitness())); result += ' '; - result += extract(str(genome)); + result += boost::python::extract(boost::python::str(genome)); return result; } @@ -140,7 +143,7 @@ struct PyEO_pickle_suite : boost::python::pickle_suite static boost::python::tuple getstate(const PyEO& _eo) { - return make_tuple(_eo.getFitness(), _eo.genome); + return boost::python::make_tuple(_eo.getFitness(), _eo.genome); } static void setstate(PyEO& _eo, boost::python::tuple pickled) diff --git a/eo/src/pyeo/abstract1.cpp b/eo/src/pyeo/abstract1.cpp index 5f6c0713..8a443a04 100644 --- a/eo/src/pyeo/abstract1.cpp +++ b/eo/src/pyeo/abstract1.cpp @@ -27,6 +27,8 @@ #include "PyEO.h" #include "def_abstract_functor.h" +using namespace boost::python; + void abstract1() { /* Abstract Classes: overrideble from python */ diff --git a/eo/src/pyeo/algos.cpp b/eo/src/pyeo/algos.cpp index b8c69558..12feaf19 100644 --- a/eo/src/pyeo/algos.cpp +++ b/eo/src/pyeo/algos.cpp @@ -26,6 +26,8 @@ #include "PyEO.h" #include "def_abstract_functor.h" +using namespace boost::python; + void algos() { def_abstract_functor >("eoAlgo"); diff --git a/eo/src/pyeo/breeders.cpp b/eo/src/pyeo/breeders.cpp index 4610e947..30cf0994 100644 --- a/eo/src/pyeo/breeders.cpp +++ b/eo/src/pyeo/breeders.cpp @@ -25,6 +25,8 @@ #include "PyEO.h" #include "def_abstract_functor.h" +using namespace boost::python; + #define DEF3(x, i1, i2) class_, bases > >(#x, \ init()[with_custodian_and_ward<1,2,with_custodian_and_ward<1,3> >()])\ .def("__call__", &eoBreed::operator()) diff --git a/eo/src/pyeo/continuators.cpp b/eo/src/pyeo/continuators.cpp index 974f5d5f..4f1a76c5 100644 --- a/eo/src/pyeo/continuators.cpp +++ b/eo/src/pyeo/continuators.cpp @@ -29,6 +29,8 @@ #include "PyEO.h" #include "def_abstract_functor.h" +using namespace boost::python; + #define DEF(x) class_, bases > >(#x).def("__call__", &eoContinue::operator()) #define DEF2(x, i1) class_, bases > >(#x, init() ).def("__call__", &eoContinue::operator()) #define DEF3(x, i1, i2) class_, bases > >(#x, init() ).def("__call__", &eoContinue::operator()) diff --git a/eo/src/pyeo/geneticOps.cpp b/eo/src/pyeo/geneticOps.cpp index 777a0f5c..2665837b 100644 --- a/eo/src/pyeo/geneticOps.cpp +++ b/eo/src/pyeo/geneticOps.cpp @@ -27,6 +27,8 @@ #include "PyEO.h" #include "def_abstract_functor.h" +using namespace boost::python; + class GenOpWrapper : public eoGenOp { public: diff --git a/eo/src/pyeo/mergers.cpp b/eo/src/pyeo/mergers.cpp index 23f24b5a..fd2b3411 100644 --- a/eo/src/pyeo/mergers.cpp +++ b/eo/src/pyeo/mergers.cpp @@ -22,6 +22,8 @@ #include "PyEO.h" #include "def_abstract_functor.h" +using namespace boost::python; + #define DEF(x) class_, bases > >(#x).def("__call__", &eoMerge::operator()) #define DEF2(x, i1) class_, bases > >(#x, init() ).def("__call__", &eoMerge::operator()) #define DEF3(x, i1, i2) class_, bases > >(#x, init() ).def("__call__", &eoMerge::operator()) diff --git a/eo/src/pyeo/monitors.cpp b/eo/src/pyeo/monitors.cpp index 0b0e1734..8215128d 100644 --- a/eo/src/pyeo/monitors.cpp +++ b/eo/src/pyeo/monitors.cpp @@ -22,6 +22,8 @@ #include #include "PyEO.h" +using namespace boost::python; + class MonitorWrapper : public eoMonitor { public: diff --git a/eo/src/pyeo/perf2worth.cpp b/eo/src/pyeo/perf2worth.cpp index 6bf21614..3ca13996 100644 --- a/eo/src/pyeo/perf2worth.cpp +++ b/eo/src/pyeo/perf2worth.cpp @@ -21,6 +21,8 @@ #include "PyEO.h" +using namespace boost::python; + struct Perf2WorthWrapper : public eoPerf2Worth { PyObject* self; diff --git a/eo/src/pyeo/pickle.h b/eo/src/pyeo/pickle.h index 4dba587b..3d0bc4e0 100644 --- a/eo/src/pyeo/pickle.h +++ b/eo/src/pyeo/pickle.h @@ -56,7 +56,7 @@ struct T_pickle_suite : boost::python::pickle_suite static void setstate(T& t, boost::python::tuple pickled) { - std::string s = extract(pickled[0]); + std::string s = boost::python::extract(pickled[0]); #ifdef HAVE_SSTREAM std::istringstream is(s); #else diff --git a/eo/src/pyeo/random_numbers.cpp b/eo/src/pyeo/random_numbers.cpp index 77ebcdf2..a496a371 100644 --- a/eo/src/pyeo/random_numbers.cpp +++ b/eo/src/pyeo/random_numbers.cpp @@ -21,6 +21,8 @@ #include #include +using namespace boost::python; + #ifdef HAVE_SSTREAM #include #else diff --git a/eo/src/pyeo/reduce.cpp b/eo/src/pyeo/reduce.cpp index fa37e827..a23c15cd 100644 --- a/eo/src/pyeo/reduce.cpp +++ b/eo/src/pyeo/reduce.cpp @@ -22,6 +22,8 @@ #include "PyEO.h" +using namespace boost::python; + // 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 ecfcf284..9867a113 100644 --- a/eo/src/pyeo/replacement.cpp +++ b/eo/src/pyeo/replacement.cpp @@ -27,6 +27,8 @@ #include "PyEO.h" #include "def_abstract_functor.h" +using namespace boost::python; + #define DEF(x) class_, bases > >(#x).def("__call__", &eoReplacement::operator()) #define DEF2(x, i1) class_, bases > >(#x, init() ).def("__call__", &eoReplacement::operator()) #define DEF3(x, i1, i2) class_, bases > >(#x, \ diff --git a/eo/src/pyeo/selectOne.cpp b/eo/src/pyeo/selectOne.cpp index 877f0595..554a6e36 100644 --- a/eo/src/pyeo/selectOne.cpp +++ b/eo/src/pyeo/selectOne.cpp @@ -29,6 +29,8 @@ #include "pickle.h" #include "def_abstract_functor.h" +using namespace boost::python; + class eoSelectOneWrapper : public eoSelectOne { public: diff --git a/eo/src/pyeo/selectors.cpp b/eo/src/pyeo/selectors.cpp index 4d5cfae7..ba8d405d 100644 --- a/eo/src/pyeo/selectors.cpp +++ b/eo/src/pyeo/selectors.cpp @@ -29,6 +29,8 @@ #include "PyEO.h" #include "def_abstract_functor.h" +using namespace boost::python; + #define DEF(x) class_, bases > >(#x).def("__call__", &eoSelect::operator()) #define DEF2(x, i1) class_, bases > >(#x, init()[WC1] ).def("__call__", &eoSelect::operator()) #define DEF3(x, i1, i2) class_, bases > >(#x, init()[WC1] ).def("__call__", &eoSelect::operator()) diff --git a/eo/src/pyeo/statistics.cpp b/eo/src/pyeo/statistics.cpp index 8dacf46e..9c436ba5 100644 --- a/eo/src/pyeo/statistics.cpp +++ b/eo/src/pyeo/statistics.cpp @@ -3,6 +3,8 @@ #include "PyEO.h" #include "valueParam.h" +using namespace boost::python; + class StatBaseWrapper : public eoStatBase { public: diff --git a/eo/src/pyeo/test/test_breeders.py b/eo/src/pyeo/test/test_breeders.py index fb8f19b7..5d3fc760 100644 --- a/eo/src/pyeo/test/test_breeders.py +++ b/eo/src/pyeo/test/test_breeders.py @@ -7,8 +7,8 @@ class TestBreeders(unittest.TestCase): pop = eoPop(50, Init(20)) evaluate = EvalFunc() + print 'HERE' for indy in pop: evaluate(indy) - newpop = eoPop(); breed(pop,newpop) @@ -18,13 +18,11 @@ class TestBreeders(unittest.TestCase): print newpop.best() def testGeneralBreeder(self): - seq = eoSequentialOp(); seq.add(Crossover(), 0.7) seq.add(Mutate(), 0.1) breed = eoGeneralBreeder(eoDetTournamentSelect(3), seq) - self.runtest(breed) def suite(): diff --git a/eo/src/pyeo/valueParam.cpp b/eo/src/pyeo/valueParam.cpp index 86fd4fac..a9f5f310 100644 --- a/eo/src/pyeo/valueParam.cpp +++ b/eo/src/pyeo/valueParam.cpp @@ -25,7 +25,6 @@ #include "valueParam.h" #include - using namespace boost::python; class ParamWrapper : public eoParam