next iteration
This commit is contained in:
parent
01727c5a5d
commit
b5d1a95cf6
14 changed files with 94 additions and 57 deletions
|
|
@ -173,7 +173,7 @@ BOOST_PYTHON_MODULE(PyEO)
|
|||
;
|
||||
|
||||
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("__str__", to_string<eoPop<PyEO> >)
|
||||
.def("__len__", &eoPop<PyEO>::size)
|
||||
|
|
|
|||
|
|
@ -41,7 +41,6 @@ class PyFitness : public boost::python::object
|
|||
|
||||
template <class T>
|
||||
PyFitness(const T& o) : object(o) {}
|
||||
|
||||
|
||||
static unsigned nObjectives() { return objective_info.size(); }
|
||||
static double tol() { return 1e-6; }
|
||||
|
|
@ -117,7 +116,7 @@ struct PyEO : public EO< PyFitness >
|
|||
object getGenome() const { return genome; }
|
||||
void setGenome(object g) { genome = g; }
|
||||
object genome;
|
||||
|
||||
|
||||
std::string to_string() const
|
||||
{
|
||||
std::string result;
|
||||
|
|
|
|||
|
|
@ -36,7 +36,18 @@ void algos()
|
|||
eoQuadOp<PyEO>&, float,
|
||||
eoMonOp<PyEO>&, float,
|
||||
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())
|
||||
;
|
||||
|
||||
|
|
|
|||
|
|
@ -25,9 +25,9 @@
|
|||
#include "PyEO.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
#define DEF(x) class_<x<PyEO>, bases<eoBreed<PyEO > > >(#x).def("__call__", &eoBreed<PyEO>::operator())
|
||||
#define DEF2(x, i1) class_<x<PyEO>, bases<eoBreed<PyEO > > >(#x, init<i1>() ).def("__call__", &eoBreed<PyEO>::operator())
|
||||
#define DEF3(x, i1, i2) class_<x<PyEO>, bases<eoBreed<PyEO > > >(#x, init<i1, i2 >() ).def("__call__", &eoBreed<PyEO>::operator())
|
||||
#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())
|
||||
|
||||
void breeders()
|
||||
{
|
||||
|
|
@ -36,14 +36,14 @@ void breeders()
|
|||
DEF3(eoSelectTransform, eoSelect<PyEO>&, eoTransform<PyEO>&);
|
||||
|
||||
DEF3(eoGeneralBreeder, eoSelectOne<PyEO>&, eoGenOp<PyEO>&)
|
||||
.def( init<eoSelectOne<PyEO>&, eoGenOp<PyEO>&, double>() )
|
||||
.def( init<eoSelectOne<PyEO>&, eoGenOp<PyEO>&, double, bool>() )
|
||||
.def( init<eoSelectOne<PyEO>&, eoGenOp<PyEO>&, double>()[WC2])
|
||||
.def( init<eoSelectOne<PyEO>&, eoGenOp<PyEO>&, double, bool>()[WC2] )
|
||||
.def( init<eoSelectOne<PyEO>&, eoGenOp<PyEO>&, eoHowMany>() );
|
||||
|
||||
|
||||
DEF3(eoOneToOneBreeder, eoGenOp<PyEO>&, eoEvalFunc<PyEO>&)
|
||||
.def( init<eoGenOp<PyEO>&, eoEvalFunc<PyEO>&, double>() )
|
||||
.def( init<eoGenOp<PyEO>&, eoEvalFunc<PyEO>&, double, eoHowMany>() );
|
||||
.def( init<eoGenOp<PyEO>&, eoEvalFunc<PyEO>&, double>()[WC2] )
|
||||
.def( init<eoGenOp<PyEO>&, eoEvalFunc<PyEO>&, double, eoHowMany>()[WC2] );
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -50,14 +50,14 @@ void continuators()
|
|||
.def("__call__", &eoGenContinue<PyEO>::operator())
|
||||
;
|
||||
|
||||
class_<eoCombinedContinue<PyEO>, bases<eoContinue<PyEO> > >("eoCombinedContinue", init<eoContinue<PyEO>&>())
|
||||
.def( init<eoContinue<PyEO>&, eoContinue<PyEO>& >() )
|
||||
.def("add", &eoCombinedContinue<PyEO>::add)
|
||||
class_<eoCombinedContinue<PyEO>, bases<eoContinue<PyEO> > >("eoCombinedContinue", init<eoContinue<PyEO>&>()[WC1])
|
||||
.def( init<eoContinue<PyEO>&, eoContinue<PyEO>& >()[WC2] )
|
||||
.def("add", &eoCombinedContinue<PyEO>::add, WC1)
|
||||
.def("__call__", &eoCombinedContinue<PyEO>::operator())
|
||||
;
|
||||
|
||||
class_<eoEvalContinue<PyEO>, bases<eoContinue<PyEO> > >("eoEvalContinue",
|
||||
init<eoEvalFuncCounter<PyEO>&, unsigned long>())
|
||||
init<eoEvalFuncCounter<PyEO>&, unsigned long>()[WC1])
|
||||
.def("__call__", &eoEvalContinue<PyEO>::operator())
|
||||
;
|
||||
|
||||
|
|
@ -76,11 +76,13 @@ void addSortedStat(eoCheckPoint<PyEO>& c, eoSortedStatBase<PyEO>& s) { c.add(s);
|
|||
void add_checkpoint()
|
||||
{
|
||||
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", addStat)
|
||||
.def("add", addSortedStat)
|
||||
.def("add", addStat, with_custodian_and_ward<1,2>())
|
||||
.def("add", addSortedStat, with_custodian_and_ward<1,2>())
|
||||
.def("__call__", &eoCheckPoint<PyEO>::operator())
|
||||
;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,10 @@
|
|||
|
||||
#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 {
|
||||
|
||||
using namespace boost::python;
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@
|
|||
#include <eoOpContainer.h>
|
||||
|
||||
#include "PyEO.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
class GenOpWrapper : public eoGenOp<PyEO>
|
||||
{
|
||||
|
|
@ -138,12 +139,12 @@ void geneticOps()
|
|||
;
|
||||
|
||||
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)
|
||||
;
|
||||
|
||||
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)
|
||||
;
|
||||
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@
|
|||
#include <boost/python.hpp>
|
||||
|
||||
#include <strstream>
|
||||
#include <boost/python/detail/api_placeholder.hpp>
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
|
|
@ -47,6 +48,26 @@ void rng_from_string(eoRng& _rng, std::string s)
|
|||
_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()
|
||||
{
|
||||
class_<eoRng, boost::noncopyable>("eoRng", init<uint32>())
|
||||
|
|
@ -60,6 +81,7 @@ void random_numbers()
|
|||
.def("negexp", &eoRng::negexp)
|
||||
.def("to_string", rng_to_string)
|
||||
.def("from_string", rng_from_string)
|
||||
.def("roulette_wheel", spin)
|
||||
;
|
||||
|
||||
def("rng", get_rng, return_value_policy<reference_existing_object>());
|
||||
|
|
|
|||
|
|
@ -29,7 +29,9 @@
|
|||
|
||||
#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, 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()
|
||||
{
|
||||
|
|
@ -37,8 +39,11 @@ void replacement()
|
|||
|
||||
// eoReplacement.h
|
||||
DEF(eoGenerationalReplacement);
|
||||
DEF2(eoWeakElitistReplacement, eoReplacement<PyEO>& );
|
||||
|
||||
|
||||
class_<eoWeakElitistReplacement<PyEO>, bases<eoReplacement<PyEO> > >
|
||||
("eoWeakElitistReplacement",
|
||||
init< eoReplacement<PyEO>& >()[WC1]);
|
||||
|
||||
// eoMergeReduce.h
|
||||
DEF3(eoMergeReduce, eoMerge<PyEO>&, eoReduce<PyEO>& );
|
||||
DEF(eoPlusReplacement);
|
||||
|
|
@ -52,10 +57,10 @@ void replacement()
|
|||
DEF2(eoSSGAStochTournamentReplacement, double);
|
||||
|
||||
// eoReduceMergeReduce.h
|
||||
class_<eoReduceMergeReduce<PyEO>, bases<eoReplacement<PyEO> > >("eoReplacement",
|
||||
init<eoHowMany, bool, eoHowMany, eoReduce<PyEO>&,
|
||||
eoHowMany, eoReduce<PyEO>&, eoReduce<PyEO>&>())
|
||||
.def("__call__", &eoReplacement<PyEO>::operator());
|
||||
//class_<eoReduceMergeReduce<PyEO>, bases<eoReplacement<PyEO> > >("eoReplacement",
|
||||
// init<eoHowMany, bool, eoHowMany, eoReduce<PyEO>&,
|
||||
// eoHowMany, eoReduce<PyEO>&, eoReduce<PyEO>&>())
|
||||
// .def("__call__", &eoReplacement<PyEO>::operator());
|
||||
|
||||
//eoMGGReplacement
|
||||
DEF(eoMGGReplacement)
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@
|
|||
|
||||
#include "PyEO.h"
|
||||
#include "pickle.h"
|
||||
#include "def_abstract_functor.h"
|
||||
|
||||
class eoSelectOneWrapper : public eoSelectOne<PyEO>
|
||||
{
|
||||
|
|
@ -85,7 +86,7 @@ void selectOne()
|
|||
add_select<eoDetTournamentSelect<PyEO> >("eoDetTournamentSelect", init<>(), init<unsigned>() );
|
||||
add_select<eoStochTournamentSelect<PyEO> >("eoStochTournamentSelect", init<>(), init<double>() );
|
||||
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
|
||||
// use eoDetTournament instead: with a t-size of 2 it is equivalent to eoProportional with linear scaling
|
||||
|
|
|
|||
|
|
@ -30,8 +30,8 @@
|
|||
#include "def_abstract_functor.h"
|
||||
|
||||
#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 DEF3(x, i1, i2) class_<x<PyEO>, bases<eoSelect<PyEO > > >(#x, init<i1, i2 >() ).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())
|
||||
|
||||
void selectors()
|
||||
{
|
||||
|
|
@ -39,21 +39,21 @@ void selectors()
|
|||
|
||||
DEF(eoDetSelect).def( init<double>() ).def( init<double, bool>() );
|
||||
DEF3(eoSelectMany, eoSelectOne<PyEO>&, double)
|
||||
.def( init< eoSelectOne<PyEO>&, double, bool>() )
|
||||
.def( init< eoSelectOne<PyEO>&, eoHowMany>() );
|
||||
.def( init< eoSelectOne<PyEO>&, double, bool>()[WC1] )
|
||||
.def( init< eoSelectOne<PyEO>&, eoHowMany>()[WC1] );
|
||||
|
||||
DEF2(eoSelectNumber, eoSelectOne<PyEO>&)
|
||||
.def( init< eoSelectOne<PyEO>&, unsigned>());
|
||||
.def( init< eoSelectOne<PyEO>&, unsigned>()[WC1]);
|
||||
|
||||
DEF2(eoSelectPerc, eoSelectOne<PyEO>&)
|
||||
.def( init<eoSelectOne<PyEO>&, float>() );
|
||||
.def( init<eoSelectOne<PyEO>&, float>()[WC1] );
|
||||
|
||||
DEF3(eoTruncSelect, eoSelectOne<PyEO>&, eoHowMany);
|
||||
|
||||
class_<eoTruncatedSelectMany<PyEO>, bases<eoSelect<PyEO> > >("eoTruncatedSelectMany",
|
||||
init<eoSelectOne<PyEO>&, double, double> ())
|
||||
.def(init<eoSelectOne<PyEO>&, double, double, bool> ())
|
||||
.def(init<eoSelectOne<PyEO>&, double, double, bool, bool> ())
|
||||
.def(init<eoSelectOne<PyEO>&, eoHowMany, eoHowMany> ());
|
||||
init<eoSelectOne<PyEO>&, double, double>()[WC1])
|
||||
.def(init<eoSelectOne<PyEO>&, double, double, bool> ()[WC1])
|
||||
.def(init<eoSelectOne<PyEO>&, double, double, bool, bool> ()[WC1])
|
||||
.def(init<eoSelectOne<PyEO>&, eoHowMany, eoHowMany> ()[WC1]);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ const PyEO& popview_getitem(const std::vector<const PyEO*>& pop, int it)
|
|||
|
||||
return *pop[item];
|
||||
}
|
||||
|
||||
|
||||
void statistics()
|
||||
{
|
||||
class_<eoStatBase<PyEO>, StatBaseWrapper, boost::noncopyable>
|
||||
|
|
@ -56,6 +56,4 @@ void statistics()
|
|||
.def("lastCall", &eoSortedStatBase<PyEO>::lastCall)
|
||||
.def("__call__", &SortedStatBaseWrapper::operator())
|
||||
;
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,12 @@
|
|||
from maxone import *
|
||||
import unittest
|
||||
|
||||
evaluate = EvalFunc()
|
||||
init = Init(20)
|
||||
mutate = Mutate()
|
||||
xover = Crossover()
|
||||
|
||||
class TestSGA(unittest.TestCase):
|
||||
class TestBreeders(unittest.TestCase):
|
||||
|
||||
def runtest(self, breed):
|
||||
|
||||
pop = eoPop(50, init)
|
||||
pop = eoPop(50, Init(20))
|
||||
evaluate = EvalFunc()
|
||||
for indy in pop: evaluate(indy)
|
||||
|
||||
newpop = eoPop();
|
||||
|
|
@ -24,12 +20,10 @@ class TestSGA(unittest.TestCase):
|
|||
def testGeneralBreeder(self):
|
||||
|
||||
seq = eoSequentialOp();
|
||||
seq.add(xover, 0.7)
|
||||
seq.add(mutate, 0.9)
|
||||
seq.add(Crossover(), 0.7)
|
||||
seq.add(Mutate(), 0.1)
|
||||
|
||||
sel = eoDetTournamentSelect(3)
|
||||
|
||||
breed = eoGeneralBreeder(sel, seq)
|
||||
breed = eoGeneralBreeder(eoDetTournamentSelect(3), seq)
|
||||
|
||||
self.runtest(breed)
|
||||
|
||||
|
|
|
|||
|
|
@ -95,7 +95,8 @@ class NSGA_II(eoAlgo):
|
|||
|
||||
i += 1
|
||||
if i%100 == 0:
|
||||
pass #do_plot(pop)
|
||||
pass
|
||||
do_plot(pop)
|
||||
|
||||
worths = self.perf2worth.getValue()
|
||||
|
||||
|
|
@ -163,9 +164,8 @@ class TestNSGA_II(unittest.TestCase):
|
|||
|
||||
def testNSGA_II(self):
|
||||
|
||||
init = MyInit();
|
||||
evaluate = AnEval();
|
||||
pop = eoPop(25, init)
|
||||
pop = eoPop(25, MyInit())
|
||||
for indy in pop: evaluate(indy)
|
||||
|
||||
nsga = NSGA_II(50)
|
||||
|
|
|
|||
Reference in a new issue