This repository has been archived on 2026-03-28. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
eodev/eo/src/eoTranspose.h
gustavo 3fe0218a72
1999-01-29 12:23:55 +00:00

74 lines
1.9 KiB
C++

// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoTranspose.h
// (c) GeNeura Team, 1998
//-----------------------------------------------------------------------------
#ifndef _EOTRANSPOSE_h
#define _EOTRANSPOSE_h
#include <eoUniform.h>
#include <eoOp.h>
/** Transposition operator: interchanges the position of two genes
of an EO. These positions must be defined by an only index, that is,
EOT must subclass eo1d
*/
template <class EOT >
class eoTranspose: public eoMonOp<EOT> {
public:
///
eoTranspose()
: eoMonOp< EOT >( ){};
/// needed virtual dtor
virtual ~eoTranspose() {};
///
virtual void operator()( EOT& _eo ) const {
eoUniform<unsigned> uniform(0, _eo.length() );
unsigned pos1 = uniform(),
pos2 = uniform();
applyAt( _eo, pos1, pos2 );
}
/** @name Methods from eoObject
readFrom and printOn are directly inherited from eoOp
*/
//@{
/** Inherited from eoObject
@see eoObject
*/
virtual string className() const {return "eoTranspose";};
//@}
private:
#ifdef _MSC_VER
typedef EOT::Type Type;
#else
typedef typename EOT::Type Type;
#endif
/** applies operator to one gene in the EO
@param _eo victim of transposition
@param i, j positions of the genes that are going to be changed
@throw runtime_exception if the positions to write are incorrect
*/
virtual void applyAt( EOT& _eo, unsigned _i, unsigned _j) const {
try {
Type tmp = _eo.gene( _i );
_eo.gene( _i ) = _eo.gene( _j );
_eo.gene( _j ) = tmp;
} catch ( exception& _e ) {
string msg = _e.what();
msg += "Caught exception at eoTranspose";
throw runtime_error( msg.c_str() );
}
}
};
#endif