some reviews
This commit is contained in:
parent
729fa74b42
commit
96f618f88f
6 changed files with 508 additions and 554 deletions
272
eo/src/EO.h
272
eo/src/EO.h
|
|
@ -1,137 +1,135 @@
|
||||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// EO.h
|
// EO.h
|
||||||
// (c) GeNeura Team 1998
|
// (c) GeNeura Team 1998
|
||||||
/*
|
/*
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Lesser General Public
|
modify it under the terms of the GNU Lesser General Public
|
||||||
License as published by the Free Software Foundation; either
|
License as published by the Free Software Foundation; either
|
||||||
version 2 of the License, or (at your option) any later version.
|
version 2 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
This library is distributed in the hope that it will be useful,
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
Lesser General Public License for more details.
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
You should have received a copy of the GNU Lesser General Public
|
||||||
License along with this library; if not, write to the Free Software
|
License along with this library; if not, write to the Free Software
|
||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||||
*/
|
*/
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#ifndef EO_H
|
#ifndef EO_H
|
||||||
#define EO_H
|
#define EO_H
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#include <stdexcept> // runtime_error
|
#include <stdexcept> // runtime_error
|
||||||
#include <eoObject.h>
|
#include <eoObject.h> //
|
||||||
#include <eoPersistent.h>
|
#include <eoPersistent.h> //
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
/** EO is a base class for evolvable objects, that is, the subjects of evolution.
|
/** EO is a base class for evolvable objects, that is, the subjects of
|
||||||
EOs have only got a fitness, which at the same time needs to be only an object with the
|
evolution. EOs have only got a fitness, which at the same time needs to be
|
||||||
operation less than (<) defined. Fitness says how good is the object; evolution or change
|
only an object with the operation less than (<) defined. Fitness says how
|
||||||
of these objects is left to the genetic operators. A fitness less than another means a
|
good is the object; evolution or change of these objects is left to the
|
||||||
worse fitness, in whatever the context; thus, fitness is always maximized; although
|
genetic operators. A fitness less than another means a worse fitness, in
|
||||||
it can be minimized with a proper definition of the < operator.\\
|
whatever the context; thus, fitness is always maximized; although it can
|
||||||
The fitness object must have, besides an void ctor, a copy ctor.
|
be minimized with a proper definition of the < operator. The fitness
|
||||||
*/
|
object must have, besides an void ctor, a copy ctor.
|
||||||
template<class F> class EO: public eoObject, public eoPersistent
|
*/
|
||||||
{
|
template<class F> class EO: public eoObject, public eoPersistent
|
||||||
public:
|
{
|
||||||
typedef F Fitness;
|
public:
|
||||||
|
typedef F Fitness;
|
||||||
/** Default constructor.
|
|
||||||
Fitness must have a ctor which takes 0 as a value; we can not use void ctors here
|
/** Default constructor.
|
||||||
since default types like float have no void initializer. VC++ allows it, but gcc does not
|
Fitness must have a ctor which takes 0 as a value; we can not use void
|
||||||
*/
|
ctors here since default types like float have no void initializer.
|
||||||
EO(): repFitness(0), invalidFitness(true) {}
|
VC++ allows it, but gcc does not
|
||||||
|
*/
|
||||||
/** Ctor from stream.
|
EO(): repFitness(0), invalidFitness(true) {}
|
||||||
Fitness must have defined the lecture from an istream.
|
|
||||||
*/
|
/** Ctor from stream.
|
||||||
EO( istream& _is ) {
|
Fitness must have defined the lecture from an istream.
|
||||||
_is >> repFitness;
|
*/
|
||||||
invalidFitness = false;
|
EO( istream& _is ) { readFrom(_is); }
|
||||||
};
|
|
||||||
|
/// Virtual dtor
|
||||||
/// Copy ctor
|
virtual ~EO() {};
|
||||||
EO( const EO& _eo ): repFitness( _eo.repFitness ), invalidFitness( _eo.invalidFitness ) {};
|
|
||||||
|
/// Return fitness value.
|
||||||
/// Virtual dtor
|
Fitness fitness() const {
|
||||||
virtual ~EO() {};
|
if (invalid())
|
||||||
|
throw runtime_error("invalid fitness");
|
||||||
/// Return fitness value.
|
return repFitness;
|
||||||
Fitness fitness() const
|
}
|
||||||
{
|
|
||||||
if (invalid())
|
// Set fitness as invalid.
|
||||||
//throw runtime_error("invalid fitness");
|
void invalidate() { invalidFitness = true; }
|
||||||
cout << "invalid fitness" << endl;
|
|
||||||
return repFitness;
|
/** Set fitness. At the same time, validates it.
|
||||||
}
|
* @param _fitness New fitness value.
|
||||||
|
*/
|
||||||
// Set fitness as invalid.
|
void fitness(const Fitness& _fitness)
|
||||||
void invalidate() { invalidFitness = true; }
|
{
|
||||||
|
repFitness = _fitness;
|
||||||
/** Set fitness. At the same time, validates it.
|
invalidFitness = false;
|
||||||
* @param _fitness New fitness value.
|
}
|
||||||
*/
|
|
||||||
void fitness(const Fitness& _fitness)
|
/** Return true If fitness value is invalid, false otherwise.
|
||||||
{
|
* @return true If fitness is invalid.
|
||||||
repFitness = _fitness;
|
*/
|
||||||
invalidFitness = false;
|
bool invalid() const { return invalidFitness; }
|
||||||
}
|
|
||||||
|
/** Returns true if
|
||||||
/** Return true If fitness value is invalid, false otherwise.
|
@return true if the fitness is higher
|
||||||
* @return true If fitness is invalid.
|
*/
|
||||||
*/
|
bool operator<(const EO& _eo2) const { return fitness() < _eo2.fitness(); }
|
||||||
bool invalid() const { return invalidFitness; }
|
|
||||||
|
/// Methods inherited from eoObject
|
||||||
/** Returns true if
|
//@{
|
||||||
@return true if the fitness is higher
|
|
||||||
*/
|
/** Return the class id.
|
||||||
bool operator<(const EO& _eo2) const { return fitness() < _eo2.fitness();}
|
* @return the class name as a string
|
||||||
|
*/
|
||||||
/// Methods inherited from eoObject
|
virtual string className() const { return "EO"; }
|
||||||
//@{
|
|
||||||
|
/**
|
||||||
/** Return the class id.
|
* Read object.\\
|
||||||
* @return the class name as a string
|
* Calls base class, just in case that one had something to do.
|
||||||
*/
|
* The read and print methods should be compatible and have the same format.
|
||||||
virtual string className() const { return "EO"; };
|
* In principle, format is "plain": they just print a number
|
||||||
|
* @param _is a istream.
|
||||||
/**
|
* @throw runtime_exception If a valid object can't be read.
|
||||||
* Read object.\\
|
*/
|
||||||
* Calls base class, just in case that one had something to do. The read and print
|
virtual void readFrom(istream& _is) {
|
||||||
* methods should be compatible and have the same format. In principle, format is
|
_is >> repFitness;
|
||||||
* "plain": they just print a number
|
if (is)
|
||||||
* @param _is a istream.
|
invalidFitness = false;
|
||||||
* @throw runtime_exception If a valid object can't be read.
|
else
|
||||||
*/
|
throw runtime_error("EO(istream&): can't read valid eo from istream");
|
||||||
virtual void readFrom(istream& _is) {
|
}
|
||||||
_is >> repFitness;
|
|
||||||
invalidFitness = false;
|
/**
|
||||||
}
|
* Write object. Called printOn since it prints the object _on_ a stream.
|
||||||
|
* @param _os A ostream.
|
||||||
/**
|
*/
|
||||||
* Write object. It's called printOn since it prints the object _on_ a stream.
|
virtual void printOn(ostream& _os) const {
|
||||||
* @param _os A ostream.
|
_os << repFitness << endl;
|
||||||
*/
|
}
|
||||||
virtual void printOn(ostream& _os) const {
|
|
||||||
_os << repFitness << endl;
|
//@}
|
||||||
}
|
|
||||||
|
private:
|
||||||
//@}
|
Fitness repFitness; // value of fitness for this chromosome
|
||||||
|
bool invalidFitness; // true if the value of fitness is invalid
|
||||||
private:
|
};
|
||||||
Fitness repFitness; // value of fitness for this chromosome
|
|
||||||
bool invalidFitness; // true if the value of fitness is invalid
|
//-----------------------------------------------------------------------------
|
||||||
};
|
|
||||||
|
#endif EO_H
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#endif EO_H
|
|
||||||
|
|
|
||||||
|
|
@ -1,41 +0,0 @@
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// UException.h
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#ifndef _UEXCEPTION_H
|
|
||||||
#define _UEXCEPTION_H
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#if defined (__BCPLUSPLUS__)
|
|
||||||
#include <stdexcept>
|
|
||||||
#else
|
|
||||||
#include <exception>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Class UException
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
//@{
|
|
||||||
/**
|
|
||||||
* This class manages exceptions. It´s barely an extension of the standard exception,
|
|
||||||
* but it can be initialized with an STL string. Called UException (utils-exception)+
|
|
||||||
* to avoid conflicts with other classes.
|
|
||||||
*/
|
|
||||||
class UException: public exception {
|
|
||||||
public:
|
|
||||||
///
|
|
||||||
UException( const string& _msg ): msg( _msg ) { };
|
|
||||||
|
|
||||||
///
|
|
||||||
virtual const char* what() const { return msg.c_str(); };
|
|
||||||
|
|
||||||
private:
|
|
||||||
string msg;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
//@}
|
|
||||||
#endif
|
|
||||||
|
|
@ -34,14 +34,13 @@
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
/** @name eo1d class
|
/** @name eo1d class
|
||||||
* Randomly accesible evolvable object with one dimension, with
|
* Randomly accesible evolvable object with one dimension, with variable
|
||||||
variable length.
|
* length. Use this if you want to evolve "linear" things, like bitstrings,
|
||||||
* Use this if you want to evolve "linear" things, like bitstrings, or
|
* or floating-point arrays. If you don't, subclass directly from EO
|
||||||
floating-point arrays. If you don't, subclass directly from EO
|
* @see EO
|
||||||
* @see EO
|
* @author GeNeura
|
||||||
* @author GeNeura
|
* @version 0.2
|
||||||
* @version 0.2
|
*/
|
||||||
*/
|
|
||||||
|
|
||||||
//@{
|
//@{
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,333 +1,334 @@
|
||||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
/*
|
/*
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
File............: eo2dVector.h
|
File............: eo2dVector.h
|
||||||
Author..........: Geneura Team (this file: Victor Rivas, vrivas@ujaen.es)
|
Author..........: Geneura Team (this file: Victor Rivas, vrivas@ujaen.es)
|
||||||
Date............: 29-Sep-1999, at Fac. of Sciences, Univ. of Granada (Spain)
|
Date............: 29-Sep-1999, at Fac. of Sciences, Univ. of Granada (Spain)
|
||||||
Description.....: Implementation of a 2-dimensional chromosome usign STL
|
Description.....: Implementation of a 2-dimensional chromosome usign STL
|
||||||
vectors.
|
vectors.
|
||||||
|
|
||||||
================ Modif. 1 ================
|
================ Modif. 1 ================
|
||||||
Author........:
|
Author........:
|
||||||
Date..........:
|
Date..........:
|
||||||
Description...:
|
Description...:
|
||||||
|
|
||||||
QUEDA: Operador de asignación, lectura desde istream, escritura a ostream
|
QUEDA: Operador de asignación, lectura desde istream, escritura a ostream
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// eo2dVector.h
|
// eo2dVector.h
|
||||||
// (c) GeNeura Team, 1998
|
// (c) GeNeura Team, 1998
|
||||||
/*
|
/*
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Lesser General Public
|
modify it under the terms of the GNU Lesser General Public
|
||||||
License as published by the Free Software Foundation; either
|
License as published by the Free Software Foundation; either
|
||||||
version 2 of the License, or (at your option) any later version.
|
version 2 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
This library is distributed in the hope that it will be useful,
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
Lesser General Public License for more details.
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
You should have received a copy of the GNU Lesser General Public
|
||||||
License along with this library; if not, write to the Free Software
|
License along with this library; if not, write to the Free Software
|
||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||||
*/
|
*/
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
#ifndef _eo2dVector_H
|
#ifndef _eo2dVector_H
|
||||||
#define _eo2dVector_H
|
#define _eo2dVector_H
|
||||||
|
|
||||||
// STL libraries
|
// STL libraries
|
||||||
#include <vector> // For vector<int>
|
#include <vector> // For vector<int>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <strstream>
|
#include <strstream>
|
||||||
#include <iostream.h>
|
#include <iostream.h>
|
||||||
|
|
||||||
#include <eo2d.h>
|
#include <eo2d.h>
|
||||||
#include <eoRnd.h>
|
#include <eoRnd.h>
|
||||||
|
|
||||||
/** Adaptor that turns an STL vector of vectror into an EO
|
/** Adaptor that turns an STL vector of vectror into an EO
|
||||||
with the same gene type as the type with which
|
with the same gene type as the type with which
|
||||||
the vector of vector has been instantiated.
|
the vector of vector has been instantiated.
|
||||||
*/
|
*/
|
||||||
template <class T, class fitnessT>
|
template <class T, class fitnessT>
|
||||||
class eo2dVector: public eo2d<T, fitnessT>, public vector< vector<T> > {
|
class eo2dVector: public eo2d<T, fitnessT>, public vector< vector<T> > {
|
||||||
public:
|
public:
|
||||||
typedef T Type ;
|
typedef T Type ;
|
||||||
|
|
||||||
/** @name Canonical part of the objects: several ctors, copy ctor, \
|
/** @name Canonical part of the objects: several ctors, copy ctor, \
|
||||||
* dtor and assignment operator.
|
* dtor and assignment operator.
|
||||||
*/
|
*/
|
||||||
//@{
|
//@{
|
||||||
|
|
||||||
/** Ctor.
|
/** Ctor.
|
||||||
@param _rows Number of rows.
|
@param _rows Number of rows.
|
||||||
@param _cols Number of columns.
|
@param _cols Number of columns.
|
||||||
@param _val Common initial value
|
@param _val Common initial value
|
||||||
*/
|
*/
|
||||||
eo2dVector( const unsigned _rows = 0,
|
eo2dVector( const unsigned _rows = 0,
|
||||||
const unsigned _cols = 0,
|
const unsigned _cols = 0,
|
||||||
T _val = T() )
|
T _val = T() )
|
||||||
: eo2d<T, fitnessT>(), vector< vector<T> >( _rows, vector<T>( _cols, _val ) ){};
|
: eo2d<T, fitnessT>(), vector< vector<T> >( _rows, vector<T>( _cols, _val ) ){};
|
||||||
|
|
||||||
/** Ctor using a random number generator.
|
/** Ctor using a random number generator.
|
||||||
@param _rows Number of rows.
|
@param _rows Number of rows.
|
||||||
@param _cols Number of columns.
|
@param _cols Number of columns.
|
||||||
@param _rnd A random "T-type" generator, which returns a random value each time it´s called.
|
@param _rnd A random "T-type" generator, which returns a random value each time it´s called.
|
||||||
*/
|
*/
|
||||||
eo2dVector( const unsigned _rows,
|
eo2dVector( const unsigned _rows,
|
||||||
const unsigned _cols,
|
const unsigned _cols,
|
||||||
eoRnd<T>& _rnd );
|
eoRnd<T>& _rnd );
|
||||||
|
|
||||||
/** Ctor from a istream. The T class should accept reading from a istream. It doesn't read fitness,
|
/** Ctor from a istream. The T class should accept reading from a istream. It doesn't read fitness,
|
||||||
which is supposed to be dynamic and dependent on environment.
|
which is supposed to be dynamic and dependent on environment.
|
||||||
@param _is the input stream; should have all values in a single line, separated by whitespace
|
@param _is the input stream; should have all values in a single line, separated by whitespace
|
||||||
*/
|
*/
|
||||||
//eo2dVector( istream& _is);
|
//eo2dVector( istream& _is);
|
||||||
|
|
||||||
|
|
||||||
/// copy ctor
|
/// copy ctor
|
||||||
eo2dVector( const eo2dVector & _eo )
|
eo2dVector( const eo2dVector & _eo )
|
||||||
: eo2d<T, fitnessT>( _eo ), vector< vector<T> >( _eo ){ };
|
: eo2d<T, fitnessT>( _eo ), vector< vector<T> >( _eo ){ };
|
||||||
|
|
||||||
/// Assignment operator
|
/// Assignment operator
|
||||||
/*
|
/*
|
||||||
const eo2dVector& operator =( const eo2dVector & _eo ) {
|
const eo2dVector& operator =( const eo2dVector & _eo ) {
|
||||||
if ( this != &_eo ){
|
if ( this != &_eo ){
|
||||||
eo2d<T, fitnessT>::operator=( _eo );
|
eo2d<T, fitnessT>::operator=( _eo );
|
||||||
vector< <vector<T> >::operator=( _eo );
|
vector< <vector<T> >::operator=( _eo );
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
/// dtor
|
/// dtor
|
||||||
virtual ~eo2dVector() {};
|
virtual ~eo2dVector() {};
|
||||||
|
|
||||||
//@}
|
//@}
|
||||||
/** Reads and returns a copy of the gene in position _r,_c.\
|
/** Reads and returns a copy of the gene in position _r,_c.\
|
||||||
This implies that T must have a copy ctor .
|
This implies that T must have a copy ctor .
|
||||||
@param _r Index for rows. Must be an unsigned less than #numOfRows()#
|
@param _r Index for rows. Must be an unsigned less than #numOfRows()#
|
||||||
@param _c Index for columns. Must be an unsigned less than #numOfCols()#
|
@param _c Index for columns. Must be an unsigned less than #numOfCols()#
|
||||||
@return what's inside the gene, with the correct type
|
@return what's inside the gene, with the correct type
|
||||||
@exception out_of_range if _r >=numOfRows()
|
@exception out_of_range if _r >=numOfRows()
|
||||||
@exception out_of_range if _c >=numOfCols()
|
@exception out_of_range if _c >=numOfCols()
|
||||||
*/
|
*/
|
||||||
virtual T getGene( const unsigned _r,
|
virtual T getGene( const unsigned _r,
|
||||||
const unsigned _c ) const {
|
const unsigned _c ) const {
|
||||||
if ( _r >= numOfRows() ) {
|
if ( _r >= numOfRows() ) {
|
||||||
ostrstream msg;
|
ostrstream msg;
|
||||||
msg << "ERROR in eo2dVector::getGene: row out of range. "
|
msg << "ERROR in eo2dVector::getGene: row out of range. "
|
||||||
<< "It should be <" << numOfRows() << '\0' << endl;
|
<< "It should be <" << numOfRows() << '\0' << endl;
|
||||||
throw out_of_range( msg.str() );
|
throw out_of_range( msg.str() );
|
||||||
}
|
}
|
||||||
if ( _c >= numOfCols() ) {
|
if ( _c >= numOfCols() ) {
|
||||||
ostrstream msg;
|
ostrstream msg;
|
||||||
msg << "ERROR in eo2dVector::getGene: column out of range. "
|
msg << "ERROR in eo2dVector::getGene: column out of range. "
|
||||||
<< "It should be <" << numOfCols() << '\0' << endl;
|
<< "It should be <" << numOfCols() << '\0' << endl;
|
||||||
throw out_of_range( msg.str() );
|
throw out_of_range( msg.str() );
|
||||||
}
|
}
|
||||||
return (*this)[_r][_c];
|
return (*this)[_r][_c];
|
||||||
};
|
};
|
||||||
/** Overwrites the gene placed in position _r,_c with a
|
/** Overwrites the gene placed in position _r,_c with a
|
||||||
* new value. This means that the assignment operator
|
* new value. This means that the assignment operator
|
||||||
* for T must be defined .
|
* for T must be defined .
|
||||||
@param _r Index for rows. Must be an unsigned less than #numOfRows()#
|
@param _r Index for rows. Must be an unsigned less than #numOfRows()#
|
||||||
@param _c Index for columns. Must be an unsigned less than #numOfCols()#
|
@param _c Index for columns. Must be an unsigned less than #numOfCols()#
|
||||||
@return what's inside the gene, with the correct type
|
@return what's inside the gene, with the correct type
|
||||||
@exception out_of_range if _r >=numOfRows()
|
@exception out_of_range if _r >=numOfRows()
|
||||||
@exception out_of_range if _c >=numOfCols()
|
@exception out_of_range if _c >=numOfCols()
|
||||||
*/
|
*/
|
||||||
virtual void setGene( const unsigned _r,
|
virtual void setGene( const unsigned _r,
|
||||||
const unsigned _c,
|
const unsigned _c,
|
||||||
const T& _value ) {
|
const T& _value ) {
|
||||||
if ( _r >= numOfRows() ) {
|
if ( _r >= numOfRows() ) {
|
||||||
ostrstream msg;
|
ostrstream msg;
|
||||||
msg << "ERROR in eo2dVector::setGene: row out of range. "
|
msg << "ERROR in eo2dVector::setGene: row out of range. "
|
||||||
<< "It should be <" << numOfRows() << '\0' << endl;
|
<< "It should be <" << numOfRows() << '\0' << endl;
|
||||||
throw out_of_range( msg.str() );
|
throw out_of_range( msg.str() );
|
||||||
}
|
}
|
||||||
if ( _c >= numOfCols() ) {
|
if ( _c >= numOfCols() ) {
|
||||||
ostrstream msg;
|
ostrstream msg;
|
||||||
msg << "ERROR in eo2dVector::setGene: column out of range. "
|
msg << "ERROR in eo2dVector::setGene: column out of range. "
|
||||||
<< "It should be <" << numOfCols() << '\0' << endl;
|
<< "It should be <" << numOfCols() << '\0' << endl;
|
||||||
throw out_of_range( msg.str() );
|
throw out_of_range( msg.str() );
|
||||||
}
|
}
|
||||||
(*this)[_r][_c]=_value;
|
(*this)[_r][_c]=_value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** Inserts a row, moving the rest to the bottom.
|
/** Inserts a row, moving the rest to the bottom.
|
||||||
* If _r = numOfRows(), it insert it at the end.
|
* If _r = numOfRows(), it insert it at the end.
|
||||||
* Obviously, changes number of rows.
|
* Obviously, changes number of rows.
|
||||||
@param _r Position where the new row will be inserted.
|
@param _r Position where the new row will be inserted.
|
||||||
@param _val Vector containing the new values to be inserted.
|
@param _val Vector containing the new values to be inserted.
|
||||||
@exception invalid_argument If _val has not numOfCols() components.
|
@exception invalid_argument If _val has not numOfCols() components.
|
||||||
@exception out_of_range If _r is greater than numOfRows()
|
@exception out_of_range If _r is greater than numOfRows()
|
||||||
*/
|
*/
|
||||||
virtual void insertRow( const unsigned _r,
|
virtual void insertRow( const unsigned _r,
|
||||||
const vector<T>& _val ) {
|
const vector<T>& _val ) {
|
||||||
// Test errors.
|
// Test errors.
|
||||||
if ( _r > numOfRows() ) {
|
if ( _r > numOfRows() ) {
|
||||||
ostrstream msg;
|
ostrstream msg;
|
||||||
msg << "ERROR in eo2dVector::insertRow: row out of range. "
|
msg << "ERROR in eo2dVector::insertRow: row out of range. "
|
||||||
<< "It should be <=" << numOfRows() << '\0' << endl;
|
<< "It should be <=" << numOfRows() << '\0' << endl;
|
||||||
throw out_of_range( msg.str() );
|
throw out_of_range( msg.str() );
|
||||||
}
|
}
|
||||||
if ( _val.size() != numOfCols() ) {
|
if ( _val.size() != numOfCols() ) {
|
||||||
ostrstream msg;
|
ostrstream msg;
|
||||||
msg << "ERROR in eo2dVector::insertRow: "
|
msg << "ERROR in eo2dVector::insertRow: "
|
||||||
<< "Incorrect number of values to be added. "
|
<< "Incorrect number of values to be added. "
|
||||||
<< "It should be ==" << numOfCols() << '\0' << endl;
|
<< "It should be ==" << numOfCols() << '\0' << endl;
|
||||||
throw invalid_argument( msg.str() );
|
throw invalid_argument( msg.str() );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert the row.
|
// Insert the row.
|
||||||
vector< vector<T> >::iterator ite = begin()+_r;
|
vector< vector<T> >::iterator ite = begin()+_r;
|
||||||
insert( ite, _val );
|
insert( ite, _val );
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Eliminates the row at position _r; all the other genes will
|
/** Eliminates the row at position _r; all the other genes will
|
||||||
be shifted up.
|
be shifted up.
|
||||||
@param _r Number of he row to be deleted.
|
@param _r Number of he row to be deleted.
|
||||||
@exception out_of_range if _r >=numOfRows()
|
@exception out_of_range if _r >=numOfRows()
|
||||||
*/
|
*/
|
||||||
virtual void deleteRow( const unsigned _r ) {
|
virtual void deleteRow( const unsigned _r ) {
|
||||||
// Test error.
|
// Test error.
|
||||||
if ( _r >= numOfRows() ) {
|
if ( _r >= numOfRows() ) {
|
||||||
ostrstream msg;
|
ostrstream msg;
|
||||||
msg << "ERROR in eo2dVector::deleteRow: "
|
msg << "ERROR in eo2dVector::deleteRow: "
|
||||||
<< "Row out of range. "
|
<< "Row out of range. "
|
||||||
<< "It should be <" << numOfRows() << '\0' << endl;
|
<< "It should be <" << numOfRows() << '\0' << endl;
|
||||||
throw out_of_range( msg.str() );
|
throw out_of_range( msg.str() );
|
||||||
}
|
}
|
||||||
// Delete row.
|
// Delete row.
|
||||||
vector< vector<T> >::iterator ite = this->begin()+_r;
|
vector< vector<T> >::iterator ite = this->begin()+_r;
|
||||||
this->erase( ite );
|
this->erase( ite );
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Inserts a column, moving the rest to the right.
|
/** Inserts a column, moving the rest to the right.
|
||||||
* If _c = numOfCols(), it insert it at the end.
|
* If _c = numOfCols(), it insert it at the end.
|
||||||
* Obviously, changes number of cols.
|
* Obviously, changes number of cols.
|
||||||
@param _r Position where the new column will be inserted.
|
@param _r Position where the new column will be inserted.
|
||||||
@param _val Vector containing the new values to be inserted.
|
@param _val Vector containing the new values to be inserted.
|
||||||
@exception invalid_argument if _val has not numOfRows() components.
|
@exception invalid_argument if _val has not numOfRows() components.
|
||||||
*/
|
*/
|
||||||
virtual void insertCol( const unsigned _c,
|
virtual void insertCol( const unsigned _c,
|
||||||
const vector<T>& _val ) {
|
const vector<T>& _val ) {
|
||||||
// Test errors.
|
// Test errors.
|
||||||
if ( _c > numOfCols() ) {
|
if ( _c > numOfCols() ) {
|
||||||
ostrstream msg;
|
ostrstream msg;
|
||||||
msg << "ERROR in eo2dVector::insertCol: "
|
msg << "ERROR in eo2dVector::insertCol: "
|
||||||
<< "Column out of range. "
|
<< "Column out of range. "
|
||||||
<< "It should be >=" << numOfCols() << '\0' << endl;
|
<< "It should be >=" << numOfCols() << '\0' << endl;
|
||||||
throw out_of_range( msg.str() );
|
throw out_of_range( msg.str() );
|
||||||
}
|
}
|
||||||
if ( _val.size() != numOfRows() ) {
|
if ( _val.size() != numOfRows() ) {
|
||||||
ostrstream msg;
|
ostrstream msg;
|
||||||
msg << "ERROR in eo2dVector::insertCol: "
|
msg << "ERROR in eo2dVector::insertCol: "
|
||||||
<< "Incorrect number of values to be added. "
|
<< "Incorrect number of values to be added. "
|
||||||
<< "It should be ==" << numOfRows() << '\0' << endl;
|
<< "It should be ==" << numOfRows() << '\0' << endl;
|
||||||
throw invalid_argument( msg.str() );
|
throw invalid_argument( msg.str() );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert column.
|
// Insert column.
|
||||||
for( unsigned r=0; r<numOfRows(); ++r ) {
|
for( unsigned r=0; r<numOfRows(); ++r ) {
|
||||||
vector<vector<T> >::iterator it1 = begin()+r;
|
vector<vector<T> >::iterator it1 = begin()+r;
|
||||||
vector<T>::iterator it2 = (*it1).begin()+_c;
|
vector<T>::iterator it2 = (*it1).begin()+_c;
|
||||||
(*it1).insert( it2, _val[r] );
|
(*it1).insert( it2, _val[r] );
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Eliminates the column at position _c; all the other columns will
|
/** Eliminates the column at position _c; all the other columns will
|
||||||
be shifted left.
|
be shifted left.
|
||||||
@param _c Number of he column to be deleted.
|
@param _c Number of he column to be deleted.
|
||||||
@exception out_of_range if _c >=numOfCols()
|
@exception out_of_range if _c >=numOfCols()
|
||||||
*/
|
*/
|
||||||
virtual void deleteCol( const unsigned _c ) {
|
virtual void deleteCol( const unsigned _c ) {
|
||||||
// Test error.
|
// Test error.
|
||||||
if ( _c >= numOfCols() ) {
|
if ( _c >= numOfCols() ) {
|
||||||
ostrstream msg;
|
ostrstream msg;
|
||||||
msg << "ERROR in eo2dVector::deleteCol: "
|
msg << "ERROR in eo2dVector::deleteCol: "
|
||||||
<< "Column out of range. "
|
<< "Column out of range. "
|
||||||
<< "It should be <" << numOfCols() << '\0' << endl;
|
<< "It should be <" << numOfCols() << '\0' << endl;
|
||||||
throw out_of_range( msg.str() );
|
throw out_of_range( msg.str() );
|
||||||
}
|
}
|
||||||
// Delete columns.
|
// Delete columns.
|
||||||
for( unsigned r=0; r<numOfRows(); ++r ) {
|
for( unsigned r=0; r<numOfRows(); ++r ) {
|
||||||
vector<vector<T> >::iterator it1 = begin()+r;
|
vector<vector<T> >::iterator it1 = begin()+r;
|
||||||
vector<T>::iterator it2 = (*it1).begin()+_c;
|
vector<T>::iterator it2 = (*it1).begin()+_c;
|
||||||
(*it1).erase( it2 );
|
(*it1).erase( it2 );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Returns the number of rows in the eo2d
|
/// Returns the number of rows in the eo2d
|
||||||
virtual unsigned numOfRows() const {
|
virtual unsigned numOfRows() const {
|
||||||
return size();
|
return size();
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Returns the number of columns in the eo2d
|
/// Returns the number of columns in the eo2d
|
||||||
virtual unsigned numOfCols() const {
|
virtual unsigned numOfCols() const {
|
||||||
return begin()->size();
|
return begin()->size();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/** @name Methods from eoObject
|
/** @name Methods from eoObject
|
||||||
readFrom and printOn are directly inherited from eo1d
|
readFrom and printOn are directly inherited from eo1d
|
||||||
*/
|
*/
|
||||||
//@{
|
//@{
|
||||||
/** Inherited from eoObject
|
/** Inherited from eoObject
|
||||||
@see eoObject
|
@see eoObject
|
||||||
*/
|
*/
|
||||||
string className() const {return "eo2dVector";};
|
string className() const {return "eo2dVector";};
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
//____________________________ Some method implementation ____________________
|
//____________________________ Some method implementation ____________________
|
||||||
|
|
||||||
// Ctors_______________________________________________________________________
|
// Ctors_______________________________________________________________________
|
||||||
//_____________________________________________________________________________
|
//_____________________________________________________________________________
|
||||||
template <class T, class fitnessT>
|
template <class T, class fitnessT>
|
||||||
eo2dVector<T,fitnessT>::eo2dVector( const unsigned _rows,
|
eo2dVector<T,fitnessT>::eo2dVector( const unsigned _rows,
|
||||||
const unsigned _cols,
|
const unsigned _cols,
|
||||||
eoRnd<T>& _rnd )
|
eoRnd<T>& _rnd )
|
||||||
: eo2d<T, fitnessT>(), vector< vector<T> >( _rows, vector<T>( _cols, T() ) ){
|
: eo2d<T, fitnessT>(), vector< vector<T> >( _rows, vector<T>( _cols, T() ) ){
|
||||||
for ( vector< vector<T> >::iterator i = begin(); i != end(); ++i ) {
|
for ( vector< vector<T> >::iterator i = begin(); i != end(); ++i ) {
|
||||||
for( vector<T>::iterator j=(*i).begin(); j!= (*i).end(); ++j ) {
|
for( vector<T>::iterator j=(*i).begin(); j!= (*i).end(); ++j ) {
|
||||||
*j = _rnd();
|
*j = _rnd();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//_____________________________________________________________________________
|
//_____________________________________________________________________________
|
||||||
/*template <class T, class fitnessT>
|
/*template <class T, class fitnessT>
|
||||||
eoVector<T,fitnessT>::eoVector( istream& _is)
|
eoVector<T,fitnessT>::eoVector( istream& _is)
|
||||||
: eo1d<T, fitnessT>(), vector<T>( ){
|
: eo1d<T, fitnessT>(), vector<T>( ){
|
||||||
while (_is ) {
|
while (_is ) {
|
||||||
T tmp;
|
T tmp;
|
||||||
_is >> tmp;
|
_is >> tmp;
|
||||||
push_back( tmp );
|
push_back( tmp );
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//_____________________________________________________________________________
|
//_____________________________________________________________________________
|
||||||
template <class T, class fitnessT>
|
template <class T, class fitnessT>
|
||||||
ostream& operator<<( ostream& _os, const eo2dVector<T,fitnessT>& _eo) {
|
ostream& operator<<( ostream& _os, const eo2dVector<T,fitnessT>& _eo) {
|
||||||
for( unsigned i=0; i<_eo.numOfRows(); ++i ) {
|
for( unsigned i=0; i<_eo.numOfRows(); ++i ) {
|
||||||
for( unsigned j=0; j<_eo.numOfCols(); ++j ) {
|
for( unsigned j=0; j<_eo.numOfCols(); ++j ) {
|
||||||
_os << _eo.getGene( i,j ) << " ";
|
_os << _eo.getGene( i,j ) << " ";
|
||||||
}
|
}
|
||||||
_os << endl;
|
_os << endl;
|
||||||
}
|
}
|
||||||
return _os;
|
return _os;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,23 @@
|
||||||
//-----------------------------------------------------------------------------
|
/*
|
||||||
// eoBin.h
|
eoBin.h
|
||||||
//-----------------------------------------------------------------------------
|
(c) GeNeura Team 1998
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||||
|
*/
|
||||||
|
|
||||||
#ifndef eoBin_h
|
#ifndef eoBin_h
|
||||||
#define eoBin_h
|
#define eoBin_h
|
||||||
|
|
|
||||||
|
|
@ -40,28 +40,8 @@
|
||||||
#include <strstream>
|
#include <strstream>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
// include for exceptions
|
||||||
// Class UExceptions - probably useless ???
|
include <stdecept> // logic_error
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
* This class manages exceptions. It´s barely an extension of the standard except
|
|
||||||
ion,
|
|
||||||
* but it can be initialized with an STL string. Called UException (utils-except
|
|
||||||
ion)+
|
|
||||||
* to avoid conflicts with other classes.
|
|
||||||
*/
|
|
||||||
class UException: public exception {
|
|
||||||
public:
|
|
||||||
///
|
|
||||||
UException( const string& _msg ): msg( _msg ) { };
|
|
||||||
|
|
||||||
///
|
|
||||||
virtual const char* what() const { return msg.c_str(); };
|
|
||||||
|
|
||||||
private:
|
|
||||||
string msg;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// Class Param
|
// Class Param
|
||||||
|
|
@ -747,20 +727,20 @@ public:
|
||||||
/**
|
/**
|
||||||
* This class managges unknown argument exceptions.
|
* This class managges unknown argument exceptions.
|
||||||
*/
|
*/
|
||||||
class UnknownArg : public UException {
|
class UnknownArg : public logic_error {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
* @param _arg string to be shown when the exception occurs
|
* @param _arg string to be shown when the exception occurs
|
||||||
*/
|
*/
|
||||||
UnknownArg( const string& _arg): UException( "Invalid argument: "+_arg ) { };
|
UnknownArg( const string& _arg): logic_error( "Invalid argument: "+_arg ) { };
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class managges bad param types.
|
* This class managges bad param types.
|
||||||
*/
|
*/
|
||||||
class BadType : public UException {
|
class BadType : public logic_error {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -769,39 +749,39 @@ public:
|
||||||
* @param _value The value of the param
|
* @param _value The value of the param
|
||||||
*/
|
*/
|
||||||
BadType(const string& _param, const string& _value, const string& _correctType)
|
BadType(const string& _param, const string& _value, const string& _correctType)
|
||||||
: UException("The value '" + _value + "' assigned to the argument " + _param + " isn't a correct "+_correctType) { };
|
: logic_error("The value '" + _value + "' assigned to the argument " + _param + " isn't a correct "+_correctType) { };
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class managges exceptions produced when there isn't a value for a parameter.
|
* This class managges exceptions produced when there isn't a value for a parameter.
|
||||||
*/
|
*/
|
||||||
class MissingVal : public UException {
|
class MissingVal : public logic_error {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
* @param _param The param
|
* @param _param The param
|
||||||
*/
|
*/
|
||||||
MissingVal(const string& _param) : UException("Missing value for parameter " + _param) {};
|
MissingVal(const string& _param) : logic_error("Missing value for parameter " + _param) {};
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class managges exceptions produced when the user forgot a required parameter.
|
* This class managges exceptions produced when the user forgot a required parameter.
|
||||||
*/
|
*/
|
||||||
class MissingReqParam : public UException {
|
class MissingReqParam : public logic_error {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
* @param _shortName The param's short name
|
* @param _shortName The param's short name
|
||||||
*/
|
*/
|
||||||
MissingReqParam(const string& _shortName) : UException("Missing required parameter " + _shortName) {};
|
MissingReqParam(const string& _shortName) : logic_error("Missing required parameter " + _shortName) {};
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class managges exceptions du to < without a > in array value
|
* This class managges exceptions du to < without a > in array value
|
||||||
*/
|
*/
|
||||||
class BadArrayParam : public UException {
|
class BadArrayParam : public logic_error {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -810,7 +790,7 @@ public:
|
||||||
* @param _first_word The first word read after the "<"
|
* @param _first_word The first word read after the "<"
|
||||||
*/
|
*/
|
||||||
BadArrayParam(const string& _param, const string &_first_word) :
|
BadArrayParam(const string& _param, const string &_first_word) :
|
||||||
UException("Array parameter " + _param + ": No matching > (" + _first_word
|
logic_error("Array parameter " + _param + ": No matching > (" + _first_word
|
||||||
+ "... )") {};
|
+ "... )") {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Reference in a new issue