some bugfixes
This commit is contained in:
parent
efafbb6f05
commit
c6b4f255d9
3 changed files with 96 additions and 8 deletions
|
|
@ -46,7 +46,7 @@ class PyFitness : public boost::python::object
|
||||||
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; }
|
||||||
|
|
||||||
static void setObjectivesSize(int sz) { objective_info.resize(sz); }
|
static void setObjectivesSize(int sz) { objective_info.resize(sz, 0); }
|
||||||
static void setObjectivesValue(unsigned which, int value)
|
static void setObjectivesValue(unsigned which, int value)
|
||||||
{
|
{
|
||||||
if (which >= objective_info.size())
|
if (which >= objective_info.size())
|
||||||
|
|
|
||||||
|
|
@ -21,9 +21,61 @@
|
||||||
|
|
||||||
#include "PyEO.h"
|
#include "PyEO.h"
|
||||||
|
|
||||||
void perf2worth() // will have to rethink this
|
struct Perf2WorthWrapper : public eoPerf2Worth<PyEO,double>
|
||||||
{
|
{
|
||||||
class_<eoNDSorting_II<PyEO> >("eoNDSorting_II");
|
PyObject* self;
|
||||||
|
Perf2WorthWrapper(PyObject* p) : self(p) {}
|
||||||
|
|
||||||
|
void operator()( const eoPop<PyEO>& pop)
|
||||||
|
{
|
||||||
|
call_method<void>(self, "__call__", boost::ref(pop));
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
numeric::array get_worths(eoPerf2Worth<PyEO, double>& p)
|
||||||
|
{
|
||||||
|
std::vector<double>& worths = p.value();
|
||||||
|
list result;
|
||||||
|
|
||||||
|
for (unsigned i = 0; i < worths.size(); ++i)
|
||||||
|
result.append(worths[i]);
|
||||||
|
|
||||||
|
return numeric::array(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct CachedPerf2WorthWrapper : public eoPerf2WorthCached<PyEO, double>
|
||||||
|
{
|
||||||
|
PyObject* self;
|
||||||
|
CachedPerf2WorthWrapper(PyObject* p) : self(p) {}
|
||||||
|
|
||||||
|
void calculate_worths(const eoPop<PyEO>& pop)
|
||||||
|
{
|
||||||
|
call_method<void>(self, "calculate_worths", boost::ref(pop));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void perf2worth()
|
||||||
|
{
|
||||||
|
numeric::array::set_module_and_type("Numeric", "ArrayType");
|
||||||
|
|
||||||
|
class_<eoPerf2Worth<PyEO, double>, Perf2WorthWrapper, boost::noncopyable>("eoPerf2Worth", init<>())
|
||||||
|
.def("__call__", &Perf2WorthWrapper::operator())
|
||||||
|
.def("sort_pop", &eoPerf2Worth<PyEO, double>::sort_pop)
|
||||||
|
.def("value", get_worths)
|
||||||
|
;
|
||||||
|
|
||||||
|
class_<eoPerf2WorthCached<PyEO, double>, CachedPerf2WorthWrapper, bases<eoPerf2Worth<PyEO, double> >, boost::noncopyable>
|
||||||
|
("eoPerf2WorthCached", init<>())
|
||||||
|
.def("__call__", &eoPerf2WorthCached<PyEO, double>::operator())
|
||||||
|
.def("calculate_worths", &CachedPerf2WorthWrapper::calculate_worths)
|
||||||
|
;
|
||||||
|
|
||||||
|
//class_<eoNoPerf2Worth<PyEO>, bases<eoPerf2Worth<PyEO, double> > >("eoNoPerf2Worth")
|
||||||
|
// .def("__call__", &eoNoPerf2Worth<PyEO>::operator());
|
||||||
|
|
||||||
|
class_<eoNDSorting_II<PyEO>, bases<eoPerf2WorthCached<PyEO, double> > >("eoNDSorting_II")
|
||||||
|
.def("calculate_worths", &eoNDSorting_II<PyEO>::calculate_worths);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@ import unittest
|
||||||
|
|
||||||
class TestSGA(unittest.TestCase):
|
class TestSGA(unittest.TestCase):
|
||||||
|
|
||||||
def testMO(self):
|
def testIndividuals(self):
|
||||||
setObjectivesSize(2);
|
setObjectivesSize(2);
|
||||||
setObjectivesValue(0,1);
|
setObjectivesValue(0,1);
|
||||||
setObjectivesValue(1,-1);
|
setObjectivesValue(1,1);
|
||||||
|
|
||||||
eo1 = EO();
|
eo1 = EO();
|
||||||
eo2 = EO();
|
eo2 = EO();
|
||||||
|
|
@ -14,10 +14,46 @@ class TestSGA(unittest.TestCase):
|
||||||
eo1.fitness = [0,1];
|
eo1.fitness = [0,1];
|
||||||
eo2.fitness = [1,1];
|
eo2.fitness = [1,1];
|
||||||
|
|
||||||
print dominates(eo1, eo2)
|
self.failUnlessEqual(dominates(eo1, eo2), 0)
|
||||||
|
self.failUnlessEqual(dominates(eo2, eo1), 1)
|
||||||
|
self.failUnlessEqual(dominates(eo2, eo2), 0)
|
||||||
|
|
||||||
setObjectivesValue(0,-1)
|
setObjectivesValue(0,-1)
|
||||||
setObjectivesValue(1,1);
|
setObjectivesValue(1,-1);
|
||||||
print dominates(eo1, eo2)
|
|
||||||
|
self.failUnlessEqual(dominates(eo1, eo2), 1)
|
||||||
|
self.failUnlessEqual(dominates(eo2, eo1), 0)
|
||||||
|
self.failUnlessEqual(dominates(eo2, eo2), 0)
|
||||||
|
|
||||||
|
def testNDSorting(self):
|
||||||
|
|
||||||
|
setObjectivesSize(2);
|
||||||
|
setObjectivesValue(0,-1)
|
||||||
|
setObjectivesValue(1,-1);
|
||||||
|
|
||||||
|
pop = Pop()
|
||||||
|
pop.resize(6)
|
||||||
|
|
||||||
|
pop[5].fitness = [0.15,0.87]
|
||||||
|
pop[4].fitness = [0.1,0.9]
|
||||||
|
pop[3].fitness = [0,1];
|
||||||
|
pop[2].fitness = [1,0];
|
||||||
|
pop[1].fitness = [1,1];
|
||||||
|
pop[0].fitness = [2,1];
|
||||||
|
|
||||||
|
srt = eoNDSorting_II()
|
||||||
|
|
||||||
|
srt(pop)
|
||||||
|
srt.sort_pop(pop)
|
||||||
|
|
||||||
|
for indy in pop:
|
||||||
|
print indy.fitness
|
||||||
|
|
||||||
|
|
||||||
|
worths = srt.value()
|
||||||
|
print worths
|
||||||
|
print type(worths)
|
||||||
|
|
||||||
|
|
||||||
if __name__=='__main__':
|
if __name__=='__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
Reference in a new issue