?? gamultivaluechromosome.h
字號:
// Initialize empty chromosome
GaMultiValueChromosome(GaChromosomeDomainBlock<TYPE>* configBlock) : GaDomainChromosome(configBlock)
{ }
// Copy constructor
GaMultiValueChromosome(const GaMultiValueChromosome<TYPE>& c,
bool setupOnly) : GaDomainChromosome(c, setupOnly)
{
// copy code to new chromosome
if( !setupOnly )
_values = c._values;
}
// Make new chromosome which is the exact copy of this chromosome
virtual GaChromosomePtr GACALL MakeCopy(bool setupOnly) const
{
return GaSmartStorage<GaChromosome>::MakeInstance(
new GaMultiValueChromosome( *this, setupOnly ) );
}
// Make new chromosome of this type but with random characteristic
virtual GaChromosomePtr GACALL MakeNewFromPrototype() const
{
// make chromosome with exact setup
GaChromosomePtr newPtr = MakeCopy( true );
GaMultiValueChromosome* newChromosome = dynamic_cast<GaMultiValueChromosome*>( &( *newPtr ) );
// generate random chromosome code
if( _values.size() )
{
vector<TYPE>& collection = newChromosome->_values;
collection.reserve( _values.size() );
for( int i = 0; i < (int)_values.size(); i++ )
collection.push_back( ( (GaChromosomeDomainBlock<TYPE>*)_configBlock )->_domain->GenerateRandom() );
}
return newPtr;
}
// Returns size of chromosome's code
virtual int GACALL GetCodeSize() const
{
return (int)_values.size();
}
// Returns value at given position in collection
TYPE GACALL GetAt(int pos) const
{
return _values[ pos ];
}
// Sets value at given position in collection
void GACALL SetAt(TYPE value,
int pos)
{
_values[ pos ] = GetClosestValue( value );
}
// Returns collection of values which represents chromosome's code
const vector<TYPE>& GACALL GetCode() const
{
return _values;
}
// Copy data and setup from given source chromosome
virtual GaChromosome& GACALL operator =(const GaChromosome& rhs)
{
const vector<TYPE>& c = dynamic_cast<const GaMultiValueChromosome&>( rhs )._values;
_values = c;
return GaDefaultChromosome::operator =( rhs );
}
// Compares two chromosomes and returns how much are they simular in percent
virtual float GACALL operator ==(const GaChromosome& c) const
{
const vector<TYPE>& b = dynamic_cast<const GaMultiValueChromosome&>( c )._values;
int s = (int)b.size();
int sim = 0, t = s + (int)_values.size();
// get shorter chromosome
if( s > (int)_values.size() )
s = (int)_values.size();
// compare codes
for( int i = 0; i < s; i++ )
{
if( b[ i ] == _values[ i ] )
sim += 2;
}
return ( (float)sim / t ) * 100;
}
protected:
// Saves current chromosome's code before mutation
virtual void GACALL PreapareForMutation()
{
_backup.insert( _backup.begin(), _values.begin(), _values.end() );
}
// Accepts mutation and deletes backuped code
virtual void GACALL AcceptMutation()
{
_backup.clear();
}
// Rejects mutation and restores backuped chromosome's code
virtual void GACALL RejectMutation()
{
_values.clear();
_values = _backup;
_backup.clear();
}
};// END CLASS DEFINITION GaMultiValueChromosome
// Representation of chromosome with multiple values and defined arithmetic operations
template <typename TYPE>
class GaMVArithmeticChromosome : public GaMultiValueChromosome<TYPE>,
public GaArithmeticalCode
{
public:
// Returns new chromosome with code whic is made by adding values in two givne chromosomes' codes
GaChromosomePtr GACALL operator +(const GaArithmeticalCode& rhs) const
{
GaChromosomePtr newPtr = MakeCopy( true );
GaMVArithmeticChromosome* newChromosome =
dynamic_cast<GaMVArithmeticChromosome*>( &( *newPtr ) );
vector<TYPE> b = dynamic_cast<const GaMVArithmeticChromosome&>( rhs )._values;
// size of new chromosome
int s = (int)( b.size() <= _values.size() ? b.size() : _values.size() );
vector<TYPE>& res = newChromosome->_values;
res.reserve( s );
// make new code
for( int i = 0; i < s; i++ )
res.push_back( GetClosestValue( _values[ i ] + b[ i ] ) );
return newPtr;
}
// Returns new chromosome with code whic is made by substracting values in two givne chromosomes' codes
GaChromosomePtr GACALL operator -(const GaArithmeticalCode& rhs) const
{
GaChromosomePtr newPtr = MakeCopy( true );
GaMVArithmeticChromosome* newChromosome =
dynamic_cast<GaMVArithmeticChromosome*>( &( *newPtr ) );
vector<TYPE> b = dynamic_cast<const GaMVArithmeticChromosome&>( rhs )._values;
// size of new chromosome
int s = (int)( b.size() <= _values.size() ? b.size() : _values.size() );
vector<TYPE>& res = newChromosome->_values;
res.reserve( s );
// make new code
for( int i = 0; i < s; i++ )
res.push_back( GetClosestValue( _values[ i ] - b[ i ] ) );
return newPtr;
}
// Returns new chromosome with code whic is made by multiplying values in two givne chromosomes' codes
GaChromosomePtr GACALL operator *(const GaArithmeticalCode& rhs) const
{
GaChromosomePtr newPtr = MakeCopy( true );
GaMVArithmeticChromosome* newChromosome =
dynamic_cast<GaMVArithmeticChromosome*>( &( *newPtr ) );
vector<TYPE> b = dynamic_cast<const GaMVArithmeticChromosome&>( rhs )._values;
// size of new chromosome
int s = (int)( b.size() <= _values.size() ? b.size() : _values.size() );
vector<TYPE>& res = newChromosome->_values;
res.reserve( s );
// make new code
for( int i = 0; i < s; i++ )
res.push_back( GetClosestValue( _values[ i ] * b[ i ] ) );
return newPtr;
}
// Returns new chromosome with code whic is made by dividing values in two givne chromosomes' codes
GaChromosomePtr GACALL operator /(const GaArithmeticalCode& rhs) const
{
GaChromosomePtr newPtr = MakeCopy( true );
GaMVArithmeticChromosome* newChromosome =
dynamic_cast<GaMVArithmeticChromosome*>( &( *newPtr ) );
vector<TYPE> b = dynamic_cast<const GaMVArithmeticChromosome&>( rhs )._values;
// size of new chromosome
int s = (int)( b.size() <= _values.size() ? b.size() : _values.size() );
vector<TYPE>& res = newChromosome->_values;
res.reserve( s );
// make new code
for( int i = 0; i < s; i++ )
res.push_back( GetClosestValue( _values[ i ] / b[ i ] ) );
return newPtr;
}
// Returns new chromosome with code whic is midpoint between two givne chromosomes
GaChromosomePtr GACALL Midpoint(const GaArithmeticalCode& c) const
{
GaChromosomePtr newPtr = MakeCopy( true );
GaMVArithmeticChromosome* newChromosome =
dynamic_cast<GaMVArithmeticChromosome*>( &( *newPtr ) );
vector<TYPE> b = dynamic_cast<const GaMVArithmeticChromosome&>( c )._values;
// size of new chromosome
int s = (int)( b.size() <= _values.size() ? b.size() : _values.size() );
vector<TYPE>& res = newChromosome->_values;
res.reserve( s );
// make new code
for( int i = 0; i < s; i++ )
res.push_back( GetClosestValue( ( _values[ i ] + b[ i ] ) / 2 ) );
return newPtr;
}
// Initialize chromosome
GaMVArithmeticChromosome(TYPE* values,
int size,
GaChromosomeDomainBlock<TYPE>* configBlock) :
GaMultiValueChromosome(values, size, configBlock) { }
// Initialize chromosome with default(random) values
GaMVArithmeticChromosome(int size,
GaChromosomeDomainBlock<TYPE>* configBlock) : GaMultiValueChromosome(size, configBlock) { }
// Initialize empty chromosome
GaMVArithmeticChromosome(GaChromosomeDomainBlock<TYPE>* configBlock) :
GaMultiValueChromosome(configBlock) { }
// Copy constructor
GaMVArithmeticChromosome(const GaMVArithmeticChromosome<TYPE>& c,
bool setupOnly) : GaMultiValueChromosome(c, setupOnly) { }
// Make new chromosome which is the exact copy of this chromosome
virtual GaChromosomePtr GACALL MakeCopy(bool setupOnly) const
{
return GaSmartStorage<GaChromosome>::
MakeInstance( new GaMVArithmeticChromosome( *this, setupOnly ) );
}
};// END CLASS DEFINITION GaMVArithmeticChromosome
} // Representation
} // Chromosome
#endif // __GA_MULTI_VALUE_CHROMOSOME_H__
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -