Simplify configuration.

Remove support for (outdated) <strstream>, require <sstream>.
Require uint32_t for now, defined in stdint.h according to C99.
Some general cleanup and more documentation.
This commit is contained in:
kuepper 2005-09-28 21:49:26 +00:00
commit cf2a57dd88
46 changed files with 482 additions and 886 deletions

View file

@ -1,6 +1,6 @@
/*
PyEO
Copyright (C) 2003 Maarten Keijzer
This program is free software; you can redistribute it and/or modify
@ -18,35 +18,31 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sstream>
#include "PyEO.h"
#include <eoPop.h>
#ifdef HAVE_SSTREAM
#include <sstream>
#else
#include <strstream>
#endif
using namespace std;
//using namespace boost::python;
// static member, needs to be instantiated somewhere
std::vector<int> PyFitness::objective_info;
bool PyFitness::dominates(const PyFitness& oth) const
{
{
bool dom = false;
for (unsigned i = 0; i < nObjectives(); ++i)
{
int objective = objective_info[i];
if (objective == 0) // ignore
continue;
bool maxim = objective > 0;
double aval = maxim? (*this)[i] : -(*this)[i];
double bval = maxim? oth[i] : -oth[i];
@ -103,17 +99,9 @@ struct pyPop_pickle_suite : boost::python::pickle_suite
template <class T>
boost::python::str to_string(T& _p)
{
#ifdef HAVE_SSTREAM
std::ostringstream os;
_p.printOn(os);
return boost::python::str(os.str().c_str());
#else
std::ostrstream os;
_p.printOn(os);
os << ends;
std::string s(os.str());
return boost::python::str(s.c_str());
#endif
}
void pop_sort(eoPop<PyEO>& pop) { pop.sort(); }
@ -129,11 +117,11 @@ PyEO& pop_getitem(eoPop<PyEO>& pop, boost::python::object key)
boost::python::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];
@ -144,14 +132,14 @@ void pop_setitem(eoPop<PyEO>& pop, boost::python::object key, PyEO& value)
boost::python::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;
}
@ -179,7 +167,7 @@ BOOST_PYTHON_MODULE(PyEO)
using namespace boost::python;
boost::python::register_exception_translator<index_error>(&translate_index_error);
boost::python::class_<PyEO>("EO")
.add_property("fitness", &PyEO::getFitness, &PyEO::setFitness)
.add_property("genome", &PyEO::getGenome, &PyEO::setGenome)
@ -204,8 +192,8 @@ BOOST_PYTHON_MODULE(PyEO)
.def_pickle(pyPop_pickle_suite())
;
// Other definitions in different compilation units,
// Other definitions in different compilation units,
// this to avoid having g++ to choke on the load
random_numbers();
valueParam();

View file

@ -1,6 +1,6 @@
/*
PyEO
Copyright (C) 2003 Maarten Keijzer
This program is free software; you can redistribute it and/or modify
@ -24,30 +24,22 @@
#include <config.h>
#include <boost/python.hpp>
#ifdef HAVE_SSTREAM
#include <sstream>
#else
#include <strstream>
#endif
/** Implements pickle support for eoPersistent derivatives */
template <class T>
struct T_pickle_suite : boost::python::pickle_suite
{
static
static
std::string print_to_string(const T& t)
{
#ifdef HAVE_SSTREAM
std::ostringstream os;
#else
std::ostrstream os;
#endif
t.printOn(os);
os << std::ends;
return os.str();
}
static
boost::python::tuple getstate(const T& t)
{
@ -59,11 +51,7 @@ struct T_pickle_suite : boost::python::pickle_suite
void setstate(T& t, boost::python::tuple pickled)
{
std::string s = boost::python::extract<std::string>(pickled[0]);
#ifdef HAVE_SSTREAM
std::istringstream is(s);
#else
std::istrstream is(s.c_str(), s.size());
#endif
t.readFrom(is);
}
};

View file

@ -1,6 +1,6 @@
/*
PyEO
Copyright (C) 2003 Maarten Keijzer
This program is free software; you can redistribute it and/or modify
@ -23,12 +23,7 @@
using namespace boost::python;
#ifdef HAVE_SSTREAM
#include <sstream>
#else
#include <strstream>
#endif
#include <boost/python/detail/api_placeholder.hpp>
using namespace boost::python;
@ -43,11 +38,7 @@ double normal(eoRng& rng) { return rng.normal(); }
std::string rng_to_string(const eoRng& _rng)
{
#ifdef HAVE_SSTREAM
std::ostringstream os;
#else
std::ostrstream os;
#endif
_rng.printOn(os);
os << std::ends;
return os.str();
@ -56,11 +47,7 @@ std::string rng_to_string(const eoRng& _rng)
void rng_from_string(eoRng& _rng, std::string s)
{
#ifdef HAVE_SSTREAM
std::istringstream is(s);
#else
std::istrstream is(s.c_str(), s.size());
#endif
_rng.readFrom(is);
}
@ -91,7 +78,7 @@ int spin(eoRng& _rng, numeric::array values, double total)
}
double chance = _rng.uniform() * total;
int i = 0;
while (chance >= 0.0)
chance -= extract<double>(values[i++]);
@ -115,6 +102,6 @@ void random_numbers()
.def("roulette_wheel", spin)
.def_pickle(RNG_pickle_suite())
;
def("rng", get_rng, return_value_policy<reference_existing_object>());
}