next iteration

This commit is contained in:
maartenkeijzer 2003-01-14 09:37:45 +00:00
commit b5d1a95cf6
14 changed files with 94 additions and 57 deletions

View file

@ -173,7 +173,7 @@ BOOST_PYTHON_MODULE(PyEO)
; ;
class_<eoPop<PyEO> >("eoPop", init<>() ) class_<eoPop<PyEO> >("eoPop", init<>() )
.def( init< unsigned, eoInit<PyEO>& >() ) .def( init< unsigned, eoInit<PyEO>& >()[with_custodian_and_ward<1,3>()] )
.def("append", &eoPop<PyEO>::append) .def("append", &eoPop<PyEO>::append)
.def("__str__", to_string<eoPop<PyEO> >) .def("__str__", to_string<eoPop<PyEO> >)
.def("__len__", &eoPop<PyEO>::size) .def("__len__", &eoPop<PyEO>::size)

View file

@ -42,7 +42,6 @@ class PyFitness : public boost::python::object
template <class T> template <class T>
PyFitness(const T& o) : object(o) {} PyFitness(const T& o) : object(o) {}
static unsigned nObjectives() { return objective_info.size(); } static unsigned nObjectives() { return objective_info.size(); }
static double tol() { return 1e-6; } static double tol() { return 1e-6; }
static bool maximizing(int which) { return objective_info[which] > 0; } static bool maximizing(int which) { return objective_info[which] > 0; }

View file

@ -36,7 +36,18 @@ void algos()
eoQuadOp<PyEO>&, float, eoQuadOp<PyEO>&, float,
eoMonOp<PyEO>&, float, eoMonOp<PyEO>&, float,
eoEvalFunc<PyEO>&, eoEvalFunc<PyEO>&,
eoContinue<PyEO>&>()) eoContinue<PyEO>&>()
[
with_custodian_and_ward<1,2,
with_custodian_and_ward<1,3,
with_custodian_and_ward<1,5,
with_custodian_and_ward<1,7,
with_custodian_and_ward<1,8>
>
>
>
>()
])
.def("__call__", &eoSGA<PyEO>::operator()) .def("__call__", &eoSGA<PyEO>::operator())
; ;

View file

@ -25,9 +25,9 @@
#include "PyEO.h" #include "PyEO.h"
#include "def_abstract_functor.h" #include "def_abstract_functor.h"
#define DEF(x) class_<x<PyEO>, bases<eoBreed<PyEO > > >(#x).def("__call__", &eoBreed<PyEO>::operator()) #define DEF3(x, i1, i2) class_<x<PyEO>, bases<eoBreed<PyEO > > >(#x, \
#define DEF2(x, i1) class_<x<PyEO>, bases<eoBreed<PyEO > > >(#x, init<i1>() ).def("__call__", &eoBreed<PyEO>::operator()) init<i1, i2 >()[with_custodian_and_ward<1,2,with_custodian_and_ward<1,3> >()])\
#define DEF3(x, i1, i2) class_<x<PyEO>, bases<eoBreed<PyEO > > >(#x, init<i1, i2 >() ).def("__call__", &eoBreed<PyEO>::operator()) .def("__call__", &eoBreed<PyEO>::operator())
void breeders() void breeders()
{ {
@ -36,14 +36,14 @@ void breeders()
DEF3(eoSelectTransform, eoSelect<PyEO>&, eoTransform<PyEO>&); DEF3(eoSelectTransform, eoSelect<PyEO>&, eoTransform<PyEO>&);
DEF3(eoGeneralBreeder, eoSelectOne<PyEO>&, eoGenOp<PyEO>&) DEF3(eoGeneralBreeder, eoSelectOne<PyEO>&, eoGenOp<PyEO>&)
.def( init<eoSelectOne<PyEO>&, eoGenOp<PyEO>&, double>() ) .def( init<eoSelectOne<PyEO>&, eoGenOp<PyEO>&, double>()[WC2])
.def( init<eoSelectOne<PyEO>&, eoGenOp<PyEO>&, double, bool>() ) .def( init<eoSelectOne<PyEO>&, eoGenOp<PyEO>&, double, bool>()[WC2] )
.def( init<eoSelectOne<PyEO>&, eoGenOp<PyEO>&, eoHowMany>() ); .def( init<eoSelectOne<PyEO>&, eoGenOp<PyEO>&, eoHowMany>() );
DEF3(eoOneToOneBreeder, eoGenOp<PyEO>&, eoEvalFunc<PyEO>&) DEF3(eoOneToOneBreeder, eoGenOp<PyEO>&, eoEvalFunc<PyEO>&)
.def( init<eoGenOp<PyEO>&, eoEvalFunc<PyEO>&, double>() ) .def( init<eoGenOp<PyEO>&, eoEvalFunc<PyEO>&, double>()[WC2] )
.def( init<eoGenOp<PyEO>&, eoEvalFunc<PyEO>&, double, eoHowMany>() ); .def( init<eoGenOp<PyEO>&, eoEvalFunc<PyEO>&, double, eoHowMany>()[WC2] );
} }

View file

@ -50,14 +50,14 @@ void continuators()
.def("__call__", &eoGenContinue<PyEO>::operator()) .def("__call__", &eoGenContinue<PyEO>::operator())
; ;
class_<eoCombinedContinue<PyEO>, bases<eoContinue<PyEO> > >("eoCombinedContinue", init<eoContinue<PyEO>&>()) class_<eoCombinedContinue<PyEO>, bases<eoContinue<PyEO> > >("eoCombinedContinue", init<eoContinue<PyEO>&>()[WC1])
.def( init<eoContinue<PyEO>&, eoContinue<PyEO>& >() ) .def( init<eoContinue<PyEO>&, eoContinue<PyEO>& >()[WC2] )
.def("add", &eoCombinedContinue<PyEO>::add) .def("add", &eoCombinedContinue<PyEO>::add, WC1)
.def("__call__", &eoCombinedContinue<PyEO>::operator()) .def("__call__", &eoCombinedContinue<PyEO>::operator())
; ;
class_<eoEvalContinue<PyEO>, bases<eoContinue<PyEO> > >("eoEvalContinue", class_<eoEvalContinue<PyEO>, bases<eoContinue<PyEO> > >("eoEvalContinue",
init<eoEvalFuncCounter<PyEO>&, unsigned long>()) init<eoEvalFuncCounter<PyEO>&, unsigned long>()[WC1])
.def("__call__", &eoEvalContinue<PyEO>::operator()) .def("__call__", &eoEvalContinue<PyEO>::operator())
; ;
@ -76,11 +76,13 @@ void addSortedStat(eoCheckPoint<PyEO>& c, eoSortedStatBase<PyEO>& s) { c.add(s);
void add_checkpoint() void add_checkpoint()
{ {
class_<eoCheckPoint<PyEO>, bases< eoContinue<PyEO> > >("eoCheckPoint", class_<eoCheckPoint<PyEO>, bases< eoContinue<PyEO> > >("eoCheckPoint",
init<eoContinue<PyEO>&>())
.def("add", addContinue) init<eoContinue<PyEO>&> ()[with_custodian_and_ward<1,2>()]
)
.def("add", addContinue, with_custodian_and_ward<1,2>() )
.def("add", addMonitor, with_custodian_and_ward<1,2>() ) .def("add", addMonitor, with_custodian_and_ward<1,2>() )
.def("add", addStat) .def("add", addStat, with_custodian_and_ward<1,2>())
.def("add", addSortedStat) .def("add", addSortedStat, with_custodian_and_ward<1,2>())
.def("__call__", &eoCheckPoint<PyEO>::operator()) .def("__call__", &eoCheckPoint<PyEO>::operator())
; ;
} }

View file

@ -23,6 +23,10 @@
#include <eoFunctor.h> #include <eoFunctor.h>
// DEFINES for call
#define WC1 boost::python::with_custodian_and_ward<1,2>()
#define WC2 boost::python::with_custodian_and_ward<1,2, with_custodian_and_ward<1,3> >()
namespace eoutils { namespace eoutils {
using namespace boost::python; using namespace boost::python;

View file

@ -25,6 +25,7 @@
#include <eoOpContainer.h> #include <eoOpContainer.h>
#include "PyEO.h" #include "PyEO.h"
#include "def_abstract_functor.h"
class GenOpWrapper : public eoGenOp<PyEO> class GenOpWrapper : public eoGenOp<PyEO>
{ {
@ -138,12 +139,12 @@ void geneticOps()
; ;
class_<eoSequentialOp<PyEO>, bases<eoGenOp<PyEO> >, boost::noncopyable>("eoSequentialOp", init<>()) class_<eoSequentialOp<PyEO>, bases<eoGenOp<PyEO> >, boost::noncopyable>("eoSequentialOp", init<>())
.def("add", &eoSequentialOp<PyEO>::add) .def("add", &eoSequentialOp<PyEO>::add, WC1)
.def("apply", &eoSequentialOp<PyEO>::apply) .def("apply", &eoSequentialOp<PyEO>::apply)
; ;
class_<eoProportionalOp<PyEO>, bases<eoGenOp<PyEO> >, boost::noncopyable>("eoProportionalOp", init<>()) class_<eoProportionalOp<PyEO>, bases<eoGenOp<PyEO> >, boost::noncopyable>("eoProportionalOp", init<>())
.def("add", &eoProportionalOp<PyEO>::add) .def("add", &eoProportionalOp<PyEO>::add, WC1)
.def("apply", &eoProportionalOp<PyEO>::apply) .def("apply", &eoProportionalOp<PyEO>::apply)
; ;

View file

@ -22,6 +22,7 @@
#include <boost/python.hpp> #include <boost/python.hpp>
#include <strstream> #include <strstream>
#include <boost/python/detail/api_placeholder.hpp>
using namespace boost::python; using namespace boost::python;
@ -47,6 +48,26 @@ void rng_from_string(eoRng& _rng, std::string s)
_rng.readFrom(is); _rng.readFrom(is);
} }
int spin(eoRng& _rng, numeric::array values, double total)
{
if (total == 0.0)
{
unsigned sz = len(values);
for (unsigned i = 0; i < sz; ++i)
{
total += extract<double>(values[i]); //extract?
}
}
double chance = _rng.uniform() * total;
int i = 0;
while (chance >= 0.0)
chance -= extract<double>(values[i++]);
return --i;
}
void random_numbers() void random_numbers()
{ {
class_<eoRng, boost::noncopyable>("eoRng", init<uint32>()) class_<eoRng, boost::noncopyable>("eoRng", init<uint32>())
@ -60,6 +81,7 @@ void random_numbers()
.def("negexp", &eoRng::negexp) .def("negexp", &eoRng::negexp)
.def("to_string", rng_to_string) .def("to_string", rng_to_string)
.def("from_string", rng_from_string) .def("from_string", rng_from_string)
.def("roulette_wheel", spin)
; ;
def("rng", get_rng, return_value_policy<reference_existing_object>()); def("rng", get_rng, return_value_policy<reference_existing_object>());

View file

@ -29,7 +29,9 @@
#define DEF(x) class_<x<PyEO>, bases<eoReplacement<PyEO > > >(#x).def("__call__", &eoReplacement<PyEO>::operator()) #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 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, init<i1, i2 >() ).def("__call__", &eoReplacement<PyEO>::operator()) #define DEF3(x, i1, i2) class_<x<PyEO>, bases<eoReplacement<PyEO > > >(#x, \
init<i1, i2 >() [WC2])\
.def("__call__", &eoReplacement<PyEO>::operator())
void replacement() void replacement()
{ {
@ -37,7 +39,10 @@ void replacement()
// eoReplacement.h // eoReplacement.h
DEF(eoGenerationalReplacement); DEF(eoGenerationalReplacement);
DEF2(eoWeakElitistReplacement, eoReplacement<PyEO>& );
class_<eoWeakElitistReplacement<PyEO>, bases<eoReplacement<PyEO> > >
("eoWeakElitistReplacement",
init< eoReplacement<PyEO>& >()[WC1]);
// eoMergeReduce.h // eoMergeReduce.h
DEF3(eoMergeReduce, eoMerge<PyEO>&, eoReduce<PyEO>& ); DEF3(eoMergeReduce, eoMerge<PyEO>&, eoReduce<PyEO>& );
@ -52,10 +57,10 @@ void replacement()
DEF2(eoSSGAStochTournamentReplacement, double); DEF2(eoSSGAStochTournamentReplacement, double);
// eoReduceMergeReduce.h // eoReduceMergeReduce.h
class_<eoReduceMergeReduce<PyEO>, bases<eoReplacement<PyEO> > >("eoReplacement", //class_<eoReduceMergeReduce<PyEO>, bases<eoReplacement<PyEO> > >("eoReplacement",
init<eoHowMany, bool, eoHowMany, eoReduce<PyEO>&, // init<eoHowMany, bool, eoHowMany, eoReduce<PyEO>&,
eoHowMany, eoReduce<PyEO>&, eoReduce<PyEO>&>()) // eoHowMany, eoReduce<PyEO>&, eoReduce<PyEO>&>())
.def("__call__", &eoReplacement<PyEO>::operator()); // .def("__call__", &eoReplacement<PyEO>::operator());
//eoMGGReplacement //eoMGGReplacement
DEF(eoMGGReplacement) DEF(eoMGGReplacement)

View file

@ -27,6 +27,7 @@
#include "PyEO.h" #include "PyEO.h"
#include "pickle.h" #include "pickle.h"
#include "def_abstract_functor.h"
class eoSelectOneWrapper : public eoSelectOne<PyEO> class eoSelectOneWrapper : public eoSelectOne<PyEO>
{ {
@ -85,7 +86,7 @@ void selectOne()
add_select<eoDetTournamentSelect<PyEO> >("eoDetTournamentSelect", init<>(), init<unsigned>() ); add_select<eoDetTournamentSelect<PyEO> >("eoDetTournamentSelect", init<>(), init<unsigned>() );
add_select<eoStochTournamentSelect<PyEO> >("eoStochTournamentSelect", init<>(), init<double>() ); add_select<eoStochTournamentSelect<PyEO> >("eoStochTournamentSelect", init<>(), init<double>() );
add_select<eoTruncatedSelectOne<PyEO> >("eoTruncatedSelectOne", add_select<eoTruncatedSelectOne<PyEO> >("eoTruncatedSelectOne",
init<eoSelectOne<PyEO>&, double>(), init<eoSelectOne<PyEO>&, eoHowMany >() ); init<eoSelectOne<PyEO>&, double>()[WC1], init<eoSelectOne<PyEO>&, eoHowMany >()[WC1] );
// eoProportionalSelect is not feasible to implement at this point as fitness is not recognizable as a float // eoProportionalSelect is not feasible to implement at this point as fitness is not recognizable as a float
// use eoDetTournament instead: with a t-size of 2 it is equivalent to eoProportional with linear scaling // use eoDetTournament instead: with a t-size of 2 it is equivalent to eoProportional with linear scaling

View file

@ -30,8 +30,8 @@
#include "def_abstract_functor.h" #include "def_abstract_functor.h"
#define DEF(x) class_<x<PyEO>, bases<eoSelect<PyEO > > >(#x).def("__call__", &eoSelect<PyEO>::operator()) #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>() ).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 >() ).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())
void selectors() void selectors()
{ {
@ -39,21 +39,21 @@ void selectors()
DEF(eoDetSelect).def( init<double>() ).def( init<double, bool>() ); DEF(eoDetSelect).def( init<double>() ).def( init<double, bool>() );
DEF3(eoSelectMany, eoSelectOne<PyEO>&, double) DEF3(eoSelectMany, eoSelectOne<PyEO>&, double)
.def( init< eoSelectOne<PyEO>&, double, bool>() ) .def( init< eoSelectOne<PyEO>&, double, bool>()[WC1] )
.def( init< eoSelectOne<PyEO>&, eoHowMany>() ); .def( init< eoSelectOne<PyEO>&, eoHowMany>()[WC1] );
DEF2(eoSelectNumber, eoSelectOne<PyEO>&) DEF2(eoSelectNumber, eoSelectOne<PyEO>&)
.def( init< eoSelectOne<PyEO>&, unsigned>()); .def( init< eoSelectOne<PyEO>&, unsigned>()[WC1]);
DEF2(eoSelectPerc, eoSelectOne<PyEO>&) DEF2(eoSelectPerc, eoSelectOne<PyEO>&)
.def( init<eoSelectOne<PyEO>&, float>() ); .def( init<eoSelectOne<PyEO>&, float>()[WC1] );
DEF3(eoTruncSelect, eoSelectOne<PyEO>&, eoHowMany); DEF3(eoTruncSelect, eoSelectOne<PyEO>&, eoHowMany);
class_<eoTruncatedSelectMany<PyEO>, bases<eoSelect<PyEO> > >("eoTruncatedSelectMany", class_<eoTruncatedSelectMany<PyEO>, bases<eoSelect<PyEO> > >("eoTruncatedSelectMany",
init<eoSelectOne<PyEO>&, double, double> ()) init<eoSelectOne<PyEO>&, double, double>()[WC1])
.def(init<eoSelectOne<PyEO>&, double, double, bool> ()) .def(init<eoSelectOne<PyEO>&, double, double, bool> ()[WC1])
.def(init<eoSelectOne<PyEO>&, double, double, bool, bool> ()) .def(init<eoSelectOne<PyEO>&, double, double, bool, bool> ()[WC1])
.def(init<eoSelectOne<PyEO>&, eoHowMany, eoHowMany> ()); .def(init<eoSelectOne<PyEO>&, eoHowMany, eoHowMany> ()[WC1]);
} }

View file

@ -56,6 +56,4 @@ void statistics()
.def("lastCall", &eoSortedStatBase<PyEO>::lastCall) .def("lastCall", &eoSortedStatBase<PyEO>::lastCall)
.def("__call__", &SortedStatBaseWrapper::operator()) .def("__call__", &SortedStatBaseWrapper::operator())
; ;
} }

View file

@ -1,16 +1,12 @@
from maxone import * from maxone import *
import unittest import unittest
evaluate = EvalFunc() class TestBreeders(unittest.TestCase):
init = Init(20)
mutate = Mutate()
xover = Crossover()
class TestSGA(unittest.TestCase):
def runtest(self, breed): def runtest(self, breed):
pop = eoPop(50, init) pop = eoPop(50, Init(20))
evaluate = EvalFunc()
for indy in pop: evaluate(indy) for indy in pop: evaluate(indy)
newpop = eoPop(); newpop = eoPop();
@ -24,12 +20,10 @@ class TestSGA(unittest.TestCase):
def testGeneralBreeder(self): def testGeneralBreeder(self):
seq = eoSequentialOp(); seq = eoSequentialOp();
seq.add(xover, 0.7) seq.add(Crossover(), 0.7)
seq.add(mutate, 0.9) seq.add(Mutate(), 0.1)
sel = eoDetTournamentSelect(3) breed = eoGeneralBreeder(eoDetTournamentSelect(3), seq)
breed = eoGeneralBreeder(sel, seq)
self.runtest(breed) self.runtest(breed)

View file

@ -95,7 +95,8 @@ class NSGA_II(eoAlgo):
i += 1 i += 1
if i%100 == 0: if i%100 == 0:
pass #do_plot(pop) pass
do_plot(pop)
worths = self.perf2worth.getValue() worths = self.perf2worth.getValue()
@ -163,9 +164,8 @@ class TestNSGA_II(unittest.TestCase):
def testNSGA_II(self): def testNSGA_II(self):
init = MyInit();
evaluate = AnEval(); evaluate = AnEval();
pop = eoPop(25, init) pop = eoPop(25, MyInit())
for indy in pop: evaluate(indy) for indy in pop: evaluate(indy)
nsga = NSGA_II(50) nsga = NSGA_II(50)