*** empty log message ***
This commit is contained in:
parent
a597ec6f8a
commit
01de9c4763
20 changed files with 93 additions and 40 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -18,16 +18,37 @@
|
|||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
|
||||
#include <eoPop.h>
|
||||
#include "PyEO.h"
|
||||
#include <eoPop.h>
|
||||
|
||||
#ifdef HAVE_SSTREAM
|
||||
#include <sstream>
|
||||
#else
|
||||
#include <strstream>
|
||||
#endif
|
||||
|
||||
typedef eoPop<PyEO>::iterator PopIt;
|
||||
typedef eoPop<PyEO>::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<int> 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<PyEO>& _pop, boost::python::tuple pickled)
|
||||
{
|
||||
int sz = extract<int>(pickled[0]);
|
||||
boost::python::list entries = list(pickled[1]);
|
||||
int sz = boost::python::extract<int>(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<PyEO>& pop, object key)
|
||||
PyEO& pop_getitem(eoPop<PyEO>& pop, boost::python::object key)
|
||||
{
|
||||
extract<int> x(key);
|
||||
boost::python::extract<int> x(key);
|
||||
if (!x.check())
|
||||
throw index_error("Slicing not allowed");
|
||||
|
||||
|
|
@ -132,15 +153,17 @@ PyEO& pop_getitem(eoPop<PyEO>& pop, object key)
|
|||
|
||||
if (static_cast<unsigned>(i) >= pop.size())
|
||||
{
|
||||
cerr << "throwing" << endl;
|
||||
throw index_error("Index out of bounds");
|
||||
}
|
||||
|
||||
cerr << "indexing " << i << endl;
|
||||
|
||||
return pop[i];
|
||||
}
|
||||
void pop_setitem(eoPop<PyEO>& pop, object key, PyEO& value)
|
||||
{
|
||||
|
||||
extract<int> x(key);
|
||||
void pop_setitem(eoPop<PyEO>& pop, boost::python::object key, PyEO& value)
|
||||
{
|
||||
boost::python::extract<int> 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<index_error>(&translate_index_error);
|
||||
using namespace boost::python;
|
||||
|
||||
boost::python::register_exception_translator<index_error>(&translate_index_error);
|
||||
|
||||
class_<PyEO>("EO")
|
||||
boost::python::class_<PyEO>("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<PyEO> >("eoPop", init<>() )
|
||||
boost::python::class_<eoPop<PyEO> >("eoPop", init<>() )
|
||||
.def( init< unsigned, eoInit<PyEO>& >()[with_custodian_and_ward<1,3>()] )
|
||||
.def("append", &eoPop<PyEO>::append)
|
||||
.def("__str__", to_string<eoPop<PyEO> >)
|
||||
|
|
|
|||
|
|
@ -21,15 +21,18 @@
|
|||
#ifndef PYEO_H
|
||||
#define PYEO_H
|
||||
|
||||
#include <EO.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <exception>
|
||||
#include <boost/python.hpp>
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
struct index_error { index_error(std::string w) : what(w) {}; std::string what; };
|
||||
#include <EO.h>
|
||||
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 <class T>
|
||||
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<double> x(object::operator[](i));
|
||||
boost::python::extract<double> 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<const char*>(str(getFitness()));
|
||||
result += boost::python::extract<const char*>(boost::python::str(getFitness()));
|
||||
result += ' ';
|
||||
result += extract<const char*>(str(genome));
|
||||
result += boost::python::extract<const char*>(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)
|
||||
|
|
|
|||
|
|
@ -27,6 +27,8 @@
|
|||
#include "PyEO.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
void abstract1()
|
||||
{
|
||||
/* Abstract Classes: overrideble from python */
|
||||
|
|
|
|||
|
|
@ -26,6 +26,8 @@
|
|||
#include "PyEO.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
void algos()
|
||||
{
|
||||
def_abstract_functor<eoAlgo<PyEO> >("eoAlgo");
|
||||
|
|
|
|||
|
|
@ -25,6 +25,8 @@
|
|||
#include "PyEO.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
#define DEF3(x, i1, i2) class_<x<PyEO>, bases<eoBreed<PyEO > > >(#x, \
|
||||
init<i1, i2 >()[with_custodian_and_ward<1,2,with_custodian_and_ward<1,3> >()])\
|
||||
.def("__call__", &eoBreed<PyEO>::operator())
|
||||
|
|
|
|||
|
|
@ -29,6 +29,8 @@
|
|||
#include "PyEO.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
#define DEF(x) class_<x<PyEO>, bases<eoContinue<PyEO > > >(#x).def("__call__", &eoContinue<PyEO>::operator())
|
||||
#define DEF2(x, i1) class_<x<PyEO>, bases<eoContinue<PyEO > > >(#x, init<i1>() ).def("__call__", &eoContinue<PyEO>::operator())
|
||||
#define DEF3(x, i1, i2) class_<x<PyEO>, bases<eoContinue<PyEO > > >(#x, init<i1, i2 >() ).def("__call__", &eoContinue<PyEO>::operator())
|
||||
|
|
|
|||
|
|
@ -27,6 +27,8 @@
|
|||
#include "PyEO.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
class GenOpWrapper : public eoGenOp<PyEO>
|
||||
{
|
||||
public:
|
||||
|
|
|
|||
|
|
@ -22,6 +22,8 @@
|
|||
#include "PyEO.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
#define DEF(x) class_<x<PyEO>, bases<eoMerge<PyEO > > >(#x).def("__call__", &eoMerge<PyEO>::operator())
|
||||
#define DEF2(x, i1) class_<x<PyEO>, bases<eoMerge<PyEO > > >(#x, init<i1>() ).def("__call__", &eoMerge<PyEO>::operator())
|
||||
#define DEF3(x, i1, i2) class_<x<PyEO>, bases<eoMerge<PyEO > > >(#x, init<i1, i2 >() ).def("__call__", &eoMerge<PyEO>::operator())
|
||||
|
|
|
|||
|
|
@ -22,6 +22,8 @@
|
|||
#include <utils/eoMonitor.h>
|
||||
#include "PyEO.h"
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
class MonitorWrapper : public eoMonitor
|
||||
{
|
||||
public:
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@
|
|||
|
||||
#include "PyEO.h"
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
struct Perf2WorthWrapper : public eoPerf2Worth<PyEO,double>
|
||||
{
|
||||
PyObject* self;
|
||||
|
|
|
|||
|
|
@ -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<std::string>(pickled[0]);
|
||||
std::string s = boost::python::extract<std::string>(pickled[0]);
|
||||
#ifdef HAVE_SSTREAM
|
||||
std::istringstream is(s);
|
||||
#else
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@
|
|||
#include <utils/eoRNG.h>
|
||||
#include <boost/python.hpp>
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
#ifdef HAVE_SSTREAM
|
||||
#include <sstream>
|
||||
#else
|
||||
|
|
|
|||
|
|
@ -22,6 +22,8 @@
|
|||
|
||||
#include "PyEO.h"
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
// unfortunately have to define it specially
|
||||
class eoReduceWrapper : public eoReduce<PyEO>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -27,6 +27,8 @@
|
|||
#include "PyEO.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
#define DEF(x) class_<x<PyEO>, bases<eoReplacement<PyEO > > >(#x).def("__call__", &eoReplacement<PyEO>::operator())
|
||||
#define DEF2(x, i1) class_<x<PyEO>, bases<eoReplacement<PyEO > > >(#x, init<i1>() ).def("__call__", &eoReplacement<PyEO>::operator())
|
||||
#define DEF3(x, i1, i2) class_<x<PyEO>, bases<eoReplacement<PyEO > > >(#x, \
|
||||
|
|
|
|||
|
|
@ -29,6 +29,8 @@
|
|||
#include "pickle.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
class eoSelectOneWrapper : public eoSelectOne<PyEO>
|
||||
{
|
||||
public:
|
||||
|
|
|
|||
|
|
@ -29,6 +29,8 @@
|
|||
#include "PyEO.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
#define DEF(x) class_<x<PyEO>, bases<eoSelect<PyEO > > >(#x).def("__call__", &eoSelect<PyEO>::operator())
|
||||
#define DEF2(x, i1) class_<x<PyEO>, bases<eoSelect<PyEO > > >(#x, init<i1>()[WC1] ).def("__call__", &eoSelect<PyEO>::operator())
|
||||
#define DEF3(x, i1, i2) class_<x<PyEO>, bases<eoSelect<PyEO > > >(#x, init<i1, i2 >()[WC1] ).def("__call__", &eoSelect<PyEO>::operator())
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@
|
|||
#include "PyEO.h"
|
||||
#include "valueParam.h"
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
class StatBaseWrapper : public eoStatBase<PyEO>
|
||||
{
|
||||
public:
|
||||
|
|
|
|||
|
|
@ -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():
|
||||
|
|
|
|||
|
|
@ -25,7 +25,6 @@
|
|||
#include "valueParam.h"
|
||||
#include <boost/python/detail/api_placeholder.hpp>
|
||||
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
class ParamWrapper : public eoParam
|
||||
|
|
|
|||
Reference in a new issue