00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <sstream>
00022
00023 #include "PyEO.h"
00024 #include <eoPop.h>
00025
00026
00027 using namespace std;
00028
00029
00030
00031 std::vector<int> PyFitness::objective_info;
00032
00033 bool PyFitness::dominates(const PyFitness& oth) const
00034 {
00035 bool dom = false;
00036
00037 for (unsigned i = 0; i < nObjectives(); ++i)
00038 {
00039 int objective = objective_info[i];
00040
00041 if (objective == 0)
00042 continue;
00043
00044 bool maxim = objective > 0;
00045
00046 double aval = maxim? (*this)[i] : -(*this)[i];
00047 double bval = maxim? oth[i] : -oth[i];
00048
00049 if (fabs(aval - bval) > tol())
00050 {
00051 if (aval < bval)
00052 {
00053 return false;
00054 }
00055
00056 dom = true;
00057 }
00058
00059 }
00060
00061 return dom;
00062 }
00063
00064 bool dominates(const PyEO& a, const PyEO& b)
00065 {
00066 return PyFitness(a.fitness()).dominates(b.fitness());
00067 }
00068
00069 ostream& operator<<(ostream& os, const PyEO& _eo)
00070 {
00071 os << _eo.to_string();
00072 return os;
00073 }
00074
00075 struct pyPop_pickle_suite : boost::python::pickle_suite
00076 {
00077 static boost::python::tuple getstate(const eoPop<PyEO>& _pop)
00078 {
00079 boost::python::list entries;
00080 for (unsigned i = 0; i != _pop.size(); ++i)
00081 entries.append( PyEO_pickle_suite::getstate(_pop[i]) );
00082
00083 return boost::python::make_tuple(boost::python::object(_pop.size()), entries);
00084 }
00085
00086 static void setstate( eoPop<PyEO>& _pop, boost::python::tuple pickled)
00087 {
00088 int sz = boost::python::extract<int>(pickled[0]);
00089 boost::python::list entries = boost::python::list(pickled[1]);
00090 _pop.resize(sz);
00091 for (unsigned i = 0; i != _pop.size(); ++i)
00092 {
00093 PyEO_pickle_suite::setstate(_pop[i], boost::python::tuple(entries[i]) );
00094 }
00095 }
00096 };
00097
00098
00099 template <class T>
00100 boost::python::str to_string(T& _p)
00101 {
00102 std::ostringstream os;
00103 _p.printOn(os);
00104 return boost::python::str(os.str().c_str());
00105 }
00106
00107 void pop_sort(eoPop<PyEO>& pop) { pop.sort(); }
00108 void pop_shuffle(eoPop<PyEO>& pop) { pop.shuffle(); }
00109
00110 void translate_index_error(index_error const& e)
00111 {
00112 PyErr_SetString(PyExc_IndexError, e.what.c_str());
00113 }
00114
00115 PyEO& pop_getitem(eoPop<PyEO>& pop, boost::python::object key)
00116 {
00117 boost::python::extract<int> x(key);
00118 if (!x.check())
00119 throw index_error("Slicing not allowed");
00120
00121 int i = x();
00122
00123 if (static_cast<unsigned>(i) >= pop.size())
00124 {
00125 throw index_error("Index out of bounds");
00126 }
00127 return pop[i];
00128 }
00129
00130 void pop_setitem(eoPop<PyEO>& pop, boost::python::object key, PyEO& value)
00131 {
00132 boost::python::extract<int> x(key);
00133 if (!x.check())
00134 throw index_error("Slicing not allowed");
00135
00136 int i = x();
00137
00138 if (static_cast<unsigned>(i) >= pop.size())
00139 {
00140 throw index_error("Index out of bounds");
00141 }
00142
00143 pop[i] = value;
00144 }
00145
00146 void pop_push_back(eoPop<PyEO>& pop, PyEO& p) { pop.push_back(p); }
00147 void pop_resize( eoPop<PyEO>& pop, unsigned i) { pop.resize(i); }
00148 int pop_size( eoPop<PyEO>& pop) { return pop.size(); }
00149
00150 extern void abstract1();
00151 extern void algos();
00152 extern void random_numbers();
00153 extern void geneticOps();
00154 extern void selectOne();
00155 extern void continuators();
00156 extern void reduce();
00157 extern void replacement();
00158 extern void selectors();
00159 extern void breeders();
00160 extern void mergers();
00161 extern void valueParam();
00162 extern void perf2worth();
00163 extern void monitors();
00164 extern void statistics();
00165
00166 BOOST_PYTHON_MODULE(PyEO)
00167 {
00168 using namespace boost::python;
00169
00170 boost::python::register_exception_translator<index_error>(&translate_index_error);
00171
00172 boost::python::class_<PyEO>("EO")
00173 .add_property("fitness", &PyEO::getFitness, &PyEO::setFitness)
00174 .add_property("genome", &PyEO::getGenome, &PyEO::setGenome)
00175 .def_pickle(PyEO_pickle_suite())
00176 .def("invalidate", &PyEO::invalidate)
00177 .def("invalid", &PyEO::invalid)
00178 .def("__str__", &PyEO::to_string)
00179 ;
00180
00181 boost::python::class_<eoPop<PyEO> >("eoPop", init<>() )
00182 .def( init< unsigned, eoInit<PyEO>& >()[with_custodian_and_ward<1,3>()] )
00183 .def("append", &eoPop<PyEO>::append, "docstring?")
00184 .def("__str__", to_string<eoPop<PyEO> >)
00185 .def("__len__", pop_size)
00186 .def("sort", pop_sort )
00187 .def("shuffle", pop_shuffle)
00188 .def("__getitem__", pop_getitem, return_internal_reference<>() )
00189 .def("__setitem__", pop_setitem)
00190 .def("best", &eoPop<PyEO>::best_element, return_internal_reference<>() )
00191 .def("push_back", pop_push_back)
00192 .def("resize", pop_resize)
00193 .def_pickle(pyPop_pickle_suite())
00194 ;
00195
00196
00197
00198
00199 random_numbers();
00200 valueParam();
00201 abstract1();
00202 geneticOps();
00203 selectOne();
00204 selectors();
00205 perf2worth();
00206 monitors();
00207 statistics();
00208 continuators();
00209 reduce();
00210 replacement();
00211 breeders();
00212 mergers();
00213 algos();
00214
00215
00216 class_<PyFitness>("PyFitness");
00217
00218 def("nObjectives", &PyFitness::nObjectives);
00219 def("tol", &PyFitness::tol);
00220 def("maximizing", &PyFitness::maximizing);
00221 def("setObjectivesSize", &PyFitness::setObjectivesSize);
00222 def("setObjectivesValue", &PyFitness::setObjectivesValue);
00223 def("dominates", dominates);
00224 }
00225
00226
00227
00228 ostream & operator << ( ostream& _os, const eoPrintable& _o ) {
00229 _o.printOn(_os);
00230 return _os;
00231 };