Changed double linefeeds, will undo this if it doesn't work

This commit is contained in:
mac 2000-02-19 18:21:47 +00:00
commit 0d439f9f56
62 changed files with 7612 additions and 7555 deletions

View file

@ -1,238 +1,238 @@
#ifndef EO_PARSE_TREE_H
#define EO_PARSE_TREE_H
#include <list>
#include "EO.h"
#include "eoOp.h"
#include "eoInserter.h"
#include "eoIndiSelector.h"
#include "parse_tree.h"
#include "eoRnd.h"
using namespace gp_parse_tree;
using namespace std;
template <class FType, class Node>
class eoParseTree : public EO<FType>, public parse_tree<Node>
{
public :
typedef typename parse_tree<Node>::subtree Type;
eoParseTree(void) : EO<FType>(), parse_tree<Node>() {}
eoParseTree(unsigned _size, eoRnd<Type>& _rnd)
: EO<FType>(), parse_tree<Node>(_rnd())
{
pruneTree(_size);
}
void pruneTree(unsigned _size)
{
if (_size < 1)
return;
if (size() > _size)
{
Type* sub = &operator[](size() - 2); // prune tree
while (sub->size() > _size)
{
sub = &sub->operator[](0);
}
back() = *sub;
}
}
eoParseTree(std::istream& is) : EO<FType>(), parse_tree<Node>()
{
readFrom(is);
}
string className(void) const { return "eoParseTree"; }
void printOn(std::ostream& os) const
{
os << fitness() << ' ';
std::copy(ebegin(), eend(), ostream_iterator<Node>(os));
}
void readFrom(std::istream& is)
{
FType fit;
is >> fit;
fitness(fit);
std::copy(istream_iterator<Node>(is), istream_iterator<Node>(), back_inserter(*this));
}
};
template <class FType, class Node>
std::ostream& operator<<(std::ostream& os, const eoParseTree<FType, Node>& eot)
{
eot.printOn(os);
return os;
}
template <class FType, class Node>
std::istream& operator>>(std::istream& is, eoParseTree<FType, Node>& eot)
{
eot.readFrom(is);
return is;
}
template <class FType, class Node>
class eoGpDepthInitializer : public eoRnd< eoParseTree<FType, Node>::Type >
{
public :
typedef eoParseTree<FType, Node> EoType;
eoGpDepthInitializer(
unsigned _max_depth,
const vector<Node>& _initializor,
bool _grow = true)
:
eoRnd<EoType::Type>(),
max_depth(_max_depth),
initializor(_initializor),
grow(_grow)
{}
virtual string className() const { return "eoDepthInitializer"; };
EoType::Type operator()(void)
{
list<Node> sequence;
generate(sequence, max_depth);
parse_tree<Node> tree(sequence.begin(), sequence.end());
return tree.root();
}
void generate(list<Node>& sequence, int the_max, int last_terminal = -1)
{
if (last_terminal == -1)
{ // check where the last terminal in the sequence resides
vector<Node>::iterator it;
for (it = initializor.begin(); it != initializor.end(); ++it)
{
if (it->arity() > 1)
break;
}
last_terminal = it - initializor.begin();
}
if (the_max == 1)
{ // generate terminals only
vector<Node>::iterator it = initializor.begin() + rng.random(last_terminal);
sequence.push_front(*it);
return;
}
vector<Node>::iterator what_it;
if (grow)
{
what_it = initializor.begin() + rng.random(initializor.size());
}
else // full
{
what_it = initializor.begin() + last_terminal + rng.random(initializor.size() - last_terminal);
}
sequence.push_front(*what_it);
for (int i = 0; i < what_it->arity(); ++i)
generate(sequence, the_max - 1, last_terminal);
}
private :
unsigned max_depth;
std::vector<Node> initializor;
bool grow;
};
template<class FType, class Node>
class eoSubtreeXOver: public eoGeneralOp< eoParseTree<FType, Node> > {
public:
typedef eoParseTree<FType, Node> EoType;
eoSubtreeXOver( unsigned _max_length)
: eoGeneralOp<EoType>(), max_length(_max_length) {};
virtual string className() const { return "eoSubtreeXOver"; };
/// Dtor
virtual ~eoSubtreeXOver () {};
void operator()(eoIndiSelector<EoType>& _source, eoInserter<EoType>& _sink ) const
{
EoType eo1 = _source.select();
const EoType& eo2 = _source.select();
int i = rng.random(eo1.size());
int j = rng.random(eo2.size());
eo1[i] = eo2[j]; // insert subtree
eo1.pruneTree(max_length);
eo1.invalidate();
_sink.insert(eo1);
}
unsigned max_length;
};
template<class FType, class Node>
class eoBranchMutation: public eoGeneralOp< eoParseTree<FType, Node> >
{
public:
typedef eoParseTree<FType, Node> EoType;
eoBranchMutation(eoRnd<EoType::Type>& _init, unsigned _max_length)
: eoGeneralOp<EoType>(), max_length(_max_length), initializer(_init)
{};
virtual string className() const { return "eoBranchMutation"; };
/// Dtor
virtual ~eoBranchMutation() {};
void operator()(eoIndiSelector<EoType>& _source, eoInserter<EoType>& _sink ) const
{
EoType eo1 = _source.select();
int i = rng.random(eo1.size());
EoType eo2(eo1[i].size(), initializer); // create random other to cross with
eo1[i] = eo2.back(); // insert subtree
eo1.pruneTree(max_length);
eo1.invalidate();
_sink.insert(eo1);
}
private :
unsigned max_length;
eoRnd<EoType::Type>& initializer;
};
#endif
#ifndef EO_PARSE_TREE_H
#define EO_PARSE_TREE_H
#include <list>
#include "EO.h"
#include "eoOp.h"
#include "eoInserter.h"
#include "eoIndiSelector.h"
#include "parse_tree.h"
#include "eoRnd.h"
using namespace gp_parse_tree;
using namespace std;
template <class FType, class Node>
class eoParseTree : public EO<FType>, public parse_tree<Node>
{
public :
typedef typename parse_tree<Node>::subtree Type;
eoParseTree(void) : EO<FType>(), parse_tree<Node>() {}
eoParseTree(unsigned _size, eoRnd<Type>& _rnd)
: EO<FType>(), parse_tree<Node>(_rnd())
{
pruneTree(_size);
}
void pruneTree(unsigned _size)
{
if (_size < 1)
return;
if (size() > _size)
{
Type* sub = &operator[](size() - 2); // prune tree
while (sub->size() > _size)
{
sub = &sub->operator[](0);
}
back() = *sub;
}
}
eoParseTree(std::istream& is) : EO<FType>(), parse_tree<Node>()
{
readFrom(is);
}
string className(void) const { return "eoParseTree"; }
void printOn(std::ostream& os) const
{
os << fitness() << ' ';
std::copy(ebegin(), eend(), ostream_iterator<Node>(os));
}
void readFrom(std::istream& is)
{
FType fit;
is >> fit;
fitness(fit);
std::copy(istream_iterator<Node>(is), istream_iterator<Node>(), back_inserter(*this));
}
};
template <class FType, class Node>
std::ostream& operator<<(std::ostream& os, const eoParseTree<FType, Node>& eot)
{
eot.printOn(os);
return os;
}
template <class FType, class Node>
std::istream& operator>>(std::istream& is, eoParseTree<FType, Node>& eot)
{
eot.readFrom(is);
return is;
}
template <class FType, class Node>
class eoGpDepthInitializer : public eoRnd< eoParseTree<FType, Node>::Type >
{
public :
typedef eoParseTree<FType, Node> EoType;
eoGpDepthInitializer(
unsigned _max_depth,
const vector<Node>& _initializor,
bool _grow = true)
:
eoRnd<EoType::Type>(),
max_depth(_max_depth),
initializor(_initializor),
grow(_grow)
{}
virtual string className() const { return "eoDepthInitializer"; };
EoType::Type operator()(void)
{
list<Node> sequence;
generate(sequence, max_depth);
parse_tree<Node> tree(sequence.begin(), sequence.end());
return tree.root();
}
void generate(list<Node>& sequence, int the_max, int last_terminal = -1)
{
if (last_terminal == -1)
{ // check where the last terminal in the sequence resides
vector<Node>::iterator it;
for (it = initializor.begin(); it != initializor.end(); ++it)
{
if (it->arity() > 1)
break;
}
last_terminal = it - initializor.begin();
}
if (the_max == 1)
{ // generate terminals only
vector<Node>::iterator it = initializor.begin() + rng.random(last_terminal);
sequence.push_front(*it);
return;
}
vector<Node>::iterator what_it;
if (grow)
{
what_it = initializor.begin() + rng.random(initializor.size());
}
else // full
{
what_it = initializor.begin() + last_terminal + rng.random(initializor.size() - last_terminal);
}
sequence.push_front(*what_it);
for (int i = 0; i < what_it->arity(); ++i)
generate(sequence, the_max - 1, last_terminal);
}
private :
unsigned max_depth;
std::vector<Node> initializor;
bool grow;
};
template<class FType, class Node>
class eoSubtreeXOver: public eoGeneralOp< eoParseTree<FType, Node> > {
public:
typedef eoParseTree<FType, Node> EoType;
eoSubtreeXOver( unsigned _max_length)
: eoGeneralOp<EoType>(), max_length(_max_length) {};
virtual string className() const { return "eoSubtreeXOver"; };
/// Dtor
virtual ~eoSubtreeXOver () {};
void operator()(eoIndiSelector<EoType>& _source, eoInserter<EoType>& _sink ) const
{
EoType eo1 = _source.select();
const EoType& eo2 = _source.select();
int i = rng.random(eo1.size());
int j = rng.random(eo2.size());
eo1[i] = eo2[j]; // insert subtree
eo1.pruneTree(max_length);
eo1.invalidate();
_sink.insert(eo1);
}
unsigned max_length;
};
template<class FType, class Node>
class eoBranchMutation: public eoGeneralOp< eoParseTree<FType, Node> >
{
public:
typedef eoParseTree<FType, Node> EoType;
eoBranchMutation(eoRnd<EoType::Type>& _init, unsigned _max_length)
: eoGeneralOp<EoType>(), max_length(_max_length), initializer(_init)
{};
virtual string className() const { return "eoBranchMutation"; };
/// Dtor
virtual ~eoBranchMutation() {};
void operator()(eoIndiSelector<EoType>& _source, eoInserter<EoType>& _sink ) const
{
EoType eo1 = _source.select();
int i = rng.random(eo1.size());
EoType eo2(eo1[i].size(), initializer); // create random other to cross with
eo1[i] = eo2.back(); // insert subtree
eo1.pruneTree(max_length);
eo1.invalidate();
_sink.insert(eo1);
}
private :
unsigned max_length;
eoRnd<EoType::Type>& initializer;
};
#endif

View file

@ -1,228 +1,228 @@
#ifndef node_pool_h
#define node_pool_h
class MemPool
{
public :
MemPool(unsigned int sz) : esize(sz<sizeof(Link)? sizeof(Link) : sz) {}
~MemPool()
{
Chunk* n = chunks;
while(n)
{
Chunk* p = n;
n = n->next;
delete p;
}
}
void* allocate()
{
if (head == 0) grow();
Link* p = head;
head = p->next;
return static_cast<void*>(p);
}
void deallocate(void* b)
{
Link* p = static_cast<Link*>(b);
p->next = head;
head = p;
}
private :
void grow()
{
Chunk* n = new Chunk;
n->next = chunks;
chunks = n;
const int nelem = Chunk::size/esize;
char* start = n->mem;
char* last = &start[(nelem-1)*esize];
for (char* p = start; p < last; p += esize)
{
reinterpret_cast<Link*>(p)->next =
reinterpret_cast<Link*>(p + esize);
}
reinterpret_cast<Link*>(last)->next = 0;
head = reinterpret_cast<Link*>(start);
}
struct Link
{
Link* next;
};
struct Chunk
{
enum {size = 8 * 1024 - 16};
Chunk* next;
char mem[size];
};
Chunk* chunks;
const unsigned int esize;
Link* head;
};
template<class T>
class Node_alloc
{
public :
Node_alloc() {};
T* allocate(void)
{
T* t = static_cast<T*>(mem.allocate());
t = new (t) T;
//t->T(); // call constructor;
return t;
}
void deallocate(T* t)
{
t->~T(); // call destructor
mem.deallocate(static_cast<void*>(t));
}
private :
static MemPool mem;
};
template <class T>
class Standard_alloc
{
public :
Standard_alloc() {}
T* allocate(size_t arity = 1)
{
if (arity == 0)
return 0;
return new T [arity];
}
void deallocate(T* t, size_t arity = 1)
{
if (arity == 0)
return ;
delete [] t;
}
};
template <class T>
class Standard_Node_alloc
{
public :
Standard_Node_alloc() {}
T* allocate(void)
{
return new T;// [arity];
}
void deallocate(T* t)
{
delete t;
}
};
template <class T>
class Tree_alloc
{
public :
Tree_alloc() {}
T* allocate(size_t arity)
{
T* t;
switch(arity)
{
case 0 : return 0;
case 1 :
{
t = static_cast<T*>(mem1.allocate());
new (t) T;
break;
}
case 2 :
{
t = static_cast<T*>(mem2.allocate());
new (t) T;
new (&t[1]) T;
break;
}
case 3 :
{
t = static_cast<T*>(mem3.allocate());
new (t) T;
new (&t[1]) T;
new (&t[2]) T;
break;
}
default :
{
return new T[arity];
}
}
return t;
}
void deallocate(T* t, size_t arity)
{
switch(arity)
{
case 0: return;
case 3 :
{
t[2].~T(); t[1].~T(); t[0].~T();
mem3.deallocate(static_cast<void*>(t));
return;
}
case 2 :
{
t[1].~T(); t[0].~T();
mem2.deallocate(static_cast<void*>(t));
return;
}
case 1 :
{
t[0].~T();
mem1.deallocate(static_cast<void*>(t));
return;
}
default :
{
delete [] t;
return;
}
}
}
private :
static MemPool mem1;
static MemPool mem2;
static MemPool mem3;
};
// static (non thread_safe) memory pools
template <class T> MemPool Node_alloc<T>::mem = sizeof(T);
template <class T> MemPool Tree_alloc<T>::mem1 = sizeof(T);
template <class T> MemPool Tree_alloc<T>::mem2 = sizeof(T) * 2;
template <class T> MemPool Tree_alloc<T>::mem3 = sizeof(T) * 3;
#endif
#ifndef node_pool_h
#define node_pool_h
class MemPool
{
public :
MemPool(unsigned int sz) : esize(sz<sizeof(Link)? sizeof(Link) : sz) {}
~MemPool()
{
Chunk* n = chunks;
while(n)
{
Chunk* p = n;
n = n->next;
delete p;
}
}
void* allocate()
{
if (head == 0) grow();
Link* p = head;
head = p->next;
return static_cast<void*>(p);
}
void deallocate(void* b)
{
Link* p = static_cast<Link*>(b);
p->next = head;
head = p;
}
private :
void grow()
{
Chunk* n = new Chunk;
n->next = chunks;
chunks = n;
const int nelem = Chunk::size/esize;
char* start = n->mem;
char* last = &start[(nelem-1)*esize];
for (char* p = start; p < last; p += esize)
{
reinterpret_cast<Link*>(p)->next =
reinterpret_cast<Link*>(p + esize);
}
reinterpret_cast<Link*>(last)->next = 0;
head = reinterpret_cast<Link*>(start);
}
struct Link
{
Link* next;
};
struct Chunk
{
enum {size = 8 * 1024 - 16};
Chunk* next;
char mem[size];
};
Chunk* chunks;
const unsigned int esize;
Link* head;
};
template<class T>
class Node_alloc
{
public :
Node_alloc() {};
T* allocate(void)
{
T* t = static_cast<T*>(mem.allocate());
t = new (t) T;
//t->T(); // call constructor;
return t;
}
void deallocate(T* t)
{
t->~T(); // call destructor
mem.deallocate(static_cast<void*>(t));
}
private :
static MemPool mem;
};
template <class T>
class Standard_alloc
{
public :
Standard_alloc() {}
T* allocate(size_t arity = 1)
{
if (arity == 0)
return 0;
return new T [arity];
}
void deallocate(T* t, size_t arity = 1)
{
if (arity == 0)
return ;
delete [] t;
}
};
template <class T>
class Standard_Node_alloc
{
public :
Standard_Node_alloc() {}
T* allocate(void)
{
return new T;// [arity];
}
void deallocate(T* t)
{
delete t;
}
};
template <class T>
class Tree_alloc
{
public :
Tree_alloc() {}
T* allocate(size_t arity)
{
T* t;
switch(arity)
{
case 0 : return 0;
case 1 :
{
t = static_cast<T*>(mem1.allocate());
new (t) T;
break;
}
case 2 :
{
t = static_cast<T*>(mem2.allocate());
new (t) T;
new (&t[1]) T;
break;
}
case 3 :
{
t = static_cast<T*>(mem3.allocate());
new (t) T;
new (&t[1]) T;
new (&t[2]) T;
break;
}
default :
{
return new T[arity];
}
}
return t;
}
void deallocate(T* t, size_t arity)
{
switch(arity)
{
case 0: return;
case 3 :
{
t[2].~T(); t[1].~T(); t[0].~T();
mem3.deallocate(static_cast<void*>(t));
return;
}
case 2 :
{
t[1].~T(); t[0].~T();
mem2.deallocate(static_cast<void*>(t));
return;
}
case 1 :
{
t[0].~T();
mem1.deallocate(static_cast<void*>(t));
return;
}
default :
{
delete [] t;
return;
}
}
}
private :
static MemPool mem1;
static MemPool mem2;
static MemPool mem3;
};
// static (non thread_safe) memory pools
template <class T> MemPool Node_alloc<T>::mem = sizeof(T);
template <class T> MemPool Tree_alloc<T>::mem1 = sizeof(T);
template <class T> MemPool Tree_alloc<T>::mem2 = sizeof(T) * 2;
template <class T> MemPool Tree_alloc<T>::mem3 = sizeof(T) * 3;
#endif

File diff suppressed because it is too large Load diff