Added multi-objective support to PyEO (and changed its name to that)
This commit is contained in:
parent
3267a34191
commit
0a4fb55031
21 changed files with 60 additions and 328 deletions
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
pyeo
|
||||
PyEO
|
||||
|
||||
Copyright (C) 2003 Maarten Keijzer
|
||||
|
||||
|
|
@ -24,7 +24,7 @@
|
|||
#include <eoSGATransform.h>
|
||||
#include <eoPopEvalFunc.h>
|
||||
|
||||
#include "pyeo.h"
|
||||
#include "PyEO.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
void abstract1()
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
pyeo
|
||||
PyEO
|
||||
|
||||
Copyright (C) 2003 Maarten Keijzer
|
||||
|
||||
|
|
@ -23,7 +23,7 @@
|
|||
#include <eoDetSelect.h>
|
||||
#include <eoCellularEasyEA.h>
|
||||
|
||||
#include "pyeo.h"
|
||||
#include "PyEO.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
void algos()
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
pyeo
|
||||
PyEO
|
||||
|
||||
Copyright (C) 2003 Maarten Keijzer
|
||||
|
||||
|
|
@ -22,7 +22,7 @@
|
|||
#include <eoGeneralBreeder.h>
|
||||
#include <eoOneToOneBreeder.h>
|
||||
|
||||
#include "pyeo.h"
|
||||
#include "PyEO.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
#define DEF(x) class_<x<PyEO>, bases<eoBreed<PyEO > > >(#x).def("__call__", &eoBreed<PyEO>::operator())
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
pyeo
|
||||
PyEO
|
||||
|
||||
Copyright (C) 2003 Maarten Keijzer
|
||||
|
||||
|
|
@ -24,7 +24,7 @@
|
|||
#include <eoFitContinue.h>
|
||||
#include <eoSteadyFitContinue.h>
|
||||
|
||||
#include "pyeo.h"
|
||||
#include "PyEO.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
#define DEF(x) class_<x<PyEO>, bases<eoContinue<PyEO > > >(#x).def("__call__", &eoContinue<PyEO>::operator())
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
pyeo
|
||||
PyEO
|
||||
|
||||
Copyright (C) 2003 Maarten Keijzer
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
pyeo
|
||||
PyEO
|
||||
|
||||
Copyright (C) 2003 Maarten Keijzer
|
||||
|
||||
|
|
@ -24,7 +24,7 @@
|
|||
#include <eoPopulator.h>
|
||||
#include <eoOpContainer.h>
|
||||
|
||||
#include "pyeo.h"
|
||||
#include "PyEO.h"
|
||||
|
||||
class GenOpWrapper : public eoGenOp<PyEO>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
pyeo
|
||||
PyEO
|
||||
|
||||
Copyright (C) 2003 Maarten Keijzer
|
||||
|
||||
|
|
@ -19,7 +19,7 @@
|
|||
*/
|
||||
|
||||
#include <eoMerge.h>
|
||||
#include "pyeo.h"
|
||||
#include "PyEO.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
#define DEF(x) class_<x<PyEO>, bases<eoMerge<PyEO > > >(#x).def("__call__", &eoMerge<PyEO>::operator())
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
pyeo
|
||||
PyEO
|
||||
|
||||
Copyright (C) 2003 Maarten Keijzer
|
||||
|
||||
|
|
@ -19,7 +19,7 @@
|
|||
*/
|
||||
|
||||
#include <utils/eoMonitor.h>
|
||||
#include "pyeo.h"
|
||||
#include "PyEO.h"
|
||||
|
||||
void monitors()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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 <eoPerf2Worth.h>
|
||||
//#include <eoNDSorting.h>
|
||||
#include <eoNDSorting.h>
|
||||
|
||||
//#include "pyeo.h"
|
||||
#include "PyEO.h"
|
||||
|
||||
void perf2worth() // will have to rethink this
|
||||
{
|
||||
// class_<eoNDSorting_II<PyEO> >("eoNDSorting_II");
|
||||
class_<eoNDSorting_II<PyEO> >("eoNDSorting_II");
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
pyeo
|
||||
PyEO
|
||||
|
||||
Copyright (C) 2003 Maarten Keijzer
|
||||
|
||||
|
|
|
|||
|
|
@ -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 <eoPop.h>
|
||||
#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<PyEO>& _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<PyEO>& _pop, boost::python::tuple pickled)
|
||||
{
|
||||
int sz = extract<int>(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 <class T>
|
||||
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<PyEO>& pop) { pop.sort(); }
|
||||
void pop_shuffle(eoPop<PyEO>& 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<PyEO>& pop, object key)
|
||||
{
|
||||
extract<int> x(key);
|
||||
if (!x.check())
|
||||
throw index_error("Slicing not allowed");
|
||||
|
||||
int i = x();
|
||||
|
||||
if (static_cast<unsigned>(i) >= pop.size())
|
||||
{
|
||||
throw index_error("Index out of bounds");
|
||||
}
|
||||
|
||||
return pop[i];
|
||||
}
|
||||
void pop_setitem(eoPop<PyEO>& pop, object key, PyEO& value)
|
||||
{
|
||||
|
||||
extract<int> x(key);
|
||||
if (!x.check())
|
||||
throw index_error("Slicing not allowed");
|
||||
|
||||
int i = x();
|
||||
|
||||
if (static_cast<unsigned>(i) >= pop.size())
|
||||
{
|
||||
throw index_error("Index out of bounds");
|
||||
}
|
||||
|
||||
pop[i] = value;
|
||||
}
|
||||
|
||||
void pop_push_back(eoPop<PyEO>& pop, PyEO& p) { pop.push_back(p); }
|
||||
void pop_resize( eoPop<PyEO>& 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<index_error>(&translate_index_error);
|
||||
|
||||
class_<PyEO>("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_<eoPop<PyEO> >("Pop", init<>() )
|
||||
.def( init< unsigned, eoInit<PyEO>& >() )
|
||||
.def("append", &eoPop<PyEO>::append)
|
||||
.def("__str__", to_string<eoPop<PyEO> >)
|
||||
.def("__len__", &eoPop<PyEO>::size)
|
||||
.def("sort", pop_sort )
|
||||
.def("shuffle", pop_shuffle)
|
||||
.def("__getitem__", pop_getitem, return_internal_reference<>() )
|
||||
.def("__setitem__", pop_setitem)
|
||||
.def("best", &eoPop<PyEO>::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;
|
||||
};
|
||||
|
|
@ -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 <EO.h>
|
||||
#include <boost/python.hpp>
|
||||
|
||||
using namespace boost::python;
|
||||
/*
|
||||
class PyFitness : public boost::python::object
|
||||
{
|
||||
public :
|
||||
PyFitness() : object() {}
|
||||
|
||||
template <class T>
|
||||
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<const char*>(str(getFitness()));
|
||||
result += ' ';
|
||||
result += extract<const char*>(str(genome));
|
||||
return result;
|
||||
}
|
||||
|
||||
bool operator<(const PyEO& other) const { return EO<Fitness>::operator<(other); }
|
||||
bool operator>(const PyEO& other) const { return EO<Fitness>::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
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
pyeo
|
||||
PyEO
|
||||
|
||||
Copyright (C) 2003 Maarten Keijzer
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
pyeo
|
||||
PyEO
|
||||
|
||||
Copyright (C) 2003 Maarten Keijzer
|
||||
|
||||
|
|
@ -20,7 +20,7 @@
|
|||
|
||||
#include <eoReduce.h>
|
||||
|
||||
#include "pyeo.h"
|
||||
#include "PyEO.h"
|
||||
|
||||
// unfortunately have to define it specially
|
||||
class eoReduceWrapper : public eoReduce<PyEO>
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
pyeo
|
||||
PyEO
|
||||
|
||||
Copyright (C) 2003 Maarten Keijzer
|
||||
|
||||
|
|
@ -24,7 +24,7 @@
|
|||
#include <eoReduceMergeReduce.h>
|
||||
#include <eoMGGReplacement.h>
|
||||
|
||||
#include "pyeo.h"
|
||||
#include "PyEO.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
#define DEF(x) class_<x<PyEO>, bases<eoReplacement<PyEO > > >(#x).def("__call__", &eoReplacement<PyEO>::operator())
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
pyeo
|
||||
PyEO
|
||||
|
||||
Copyright (C) 2003 Maarten Keijzer
|
||||
|
||||
|
|
@ -25,7 +25,7 @@
|
|||
#include <eoTruncatedSelectOne.h>
|
||||
#include <eoSequentialSelect.h>
|
||||
|
||||
#include "pyeo.h"
|
||||
#include "PyEO.h"
|
||||
#include "pickle.h"
|
||||
|
||||
class eoSelectOneWrapper : public eoSelectOne<PyEO>
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
pyeo
|
||||
PyEO
|
||||
|
||||
Copyright (C) 2003 Maarten Keijzer
|
||||
|
||||
|
|
@ -26,7 +26,7 @@
|
|||
#include <eoTruncSelect.h>
|
||||
#include <eoTruncatedSelectMany.h>
|
||||
|
||||
#include "pyeo.h"
|
||||
#include "PyEO.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
#define DEF(x) class_<x<PyEO>, bases<eoSelect<PyEO > > >(#x).def("__call__", &eoSelect<PyEO>::operator())
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import sys
|
|||
sys.path.append('..')
|
||||
|
||||
print 'importing pyeo'
|
||||
from pyeo import *
|
||||
from PyEO import *
|
||||
print 'done'
|
||||
|
||||
from copy import copy
|
||||
|
|
|
|||
23
eo/src/pyeo/test/test_mo.py
Normal file
23
eo/src/pyeo/test/test_mo.py
Normal file
|
|
@ -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()
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
pyeo
|
||||
PyEO
|
||||
|
||||
Copyright (C) 2003 Maarten Keijzer
|
||||
|
||||
|
|
|
|||
Reference in a new issue