?? gamultivaluechromosome.h
字號:
#ifndef __GA_MULTI_VALUE_CHROMOSOME_H__
#define __GA_MULTI_VALUE_CHROMOSOME_H__
#include <vector>
#include "..\GaChromosome.h"
#include "..\..\CallConvention.h"
#include "GaRepresentationInterfaces.h"
#include "GaDomainChromosome.h"
using namespace std;
using namespace Chromosome;
using namespace Chromosome::Representation;
namespace Chromosome
{
namespace Representation
{
// Represents single value from collection of multi-value chromosome
template <typename TYPE>
class GaChromosomeValue : public GaCodeValue
{
private:
// Value
TYPE _value;
public:
// Initialize data
virtual void GACALL Initialize() { }
// Gets value from buffer
virtual void GACALL FromBuffer(const GaCodeValuesBuffer& buffer,
int pos)
{
if( pos >= 0 && pos < ( buffer.GetPosition() / (int)sizeof( TYPE ) ) )
_value = ( (TYPE*)buffer.GetBuffer() )[ pos ];
}
// Initialize value
GaChromosomeValue(TYPE value) : _value(value) { };
// Initialize value with defaults
GaChromosomeValue()
{
Initialize();
}
// Returns value
TYPE GACALL GetValue() const
{
return _value;
}
// Sets value
void GACALL SetValue(TYPE value)
{
_value = value;
}
};// END CLASS DEFINITION GaChromosomeValue
// Representation of chromosome with multiple values
template <typename TYPE>
class GaMultiValueChromosome : public GaDomainChromosome<TYPE>,
public GaSwapableCode,
public GaSizableCode,
public GaMutableCode,
public GaMultiValueCode
{
protected:
// Collection of values
vector<TYPE> _values;
// Backup collection of values for improving mutations
vector<TYPE> _backup;
public:
// Removes part of chromosome's code
virtual void GACALL Remove(int start,
int size)
{
if( size > 0 && start >=0 && start < (int)_values.size() )
_values.erase( _values.begin() + start, _values.begin() + ( start + size ) );
}
// Inserts information int chromosome's code
virtual void GACALL Insert(int start,
GaCodeValue* data,
int size)
{
if( !data || size <= 0 || start < 0 || start > (int)_values.size() )
return;
// insert at the end
if( start == _values.size() )
{
for( int i = 0; i < size; i++ )
_values.push_back( GetClosestValue( ( (GaChromosomeValue<TYPE>*)data )[ i ].GetValue() ) );
}
else
{
vector<TYPE> tmp;
tmp.reserve( size );
for( int i = 0; i < size; i++ )
tmp.push_back( GetClosestValue( ( (GaChromosomeValue<TYPE>*)data )[ i ].GetValue() ) );
_values.insert( _values.begin() + start, tmp.begin(), tmp.end() );
}
}
// Swap parts of chromosome's code
virtual void GACALL Swap(int start1,
int size1,
int start2,
int size2)
{
// check params
if( start1 < 0 || start1 >= (int)_values.size() || start2 < 0 || start2 >= (int)_values.size()
|| size1 <= 0 || size2 <= 0 || !_values.size() || start1 == start2 )
return;
// reorder swapping points (start1 is first, start2 is second)
if( start2 < start1 )
{
int t = start2;
start2 = start1;
start1 = t;
t = size2;
size2 = size1;
size1 = t;
}
// prevent overlapping of swap sequences
if( start1 + size1 >= start2 )
{
int old = start2;
start2 = start1 + size1;
size2 -= start2 - old;
}
// temp buffer for placing new code
vector<TYPE> newCode;
newCode.reserve( _values.size() );
// copy values before first swapping sequence
newCode.insert( newCode.begin(), _values.begin(), _values.begin() + start1 );
// place second swapping sequence to new location
newCode.insert( newCode.end(),
_values.begin() + start2, _values.begin() + ( start2 + size2 ) );
// copy values between two swapping sequence
newCode.insert( newCode.end(),
_values.begin() + ( start1 + size1 ), _values.begin() + start2 );
// place first swapping sequence to new location
newCode.insert( newCode.end(),
_values.begin() + start1, _values.begin() + ( start1 + size1 ) );
// copy values after second swapping sequence
newCode.insert( newCode.end(),
_values.begin() + ( start2 + size2 ), _values.end() );
// save new code
_values = newCode;
}
// Randomly change the values of selected part of chromosome's code
virtual void GACALL Flip(int start,
int size)
{
if( size <= 0 || start < 0 || start >= (int)_values.size() )
return;
vector<TYPE>::iterator it = _values.begin() + start;
for( int i = 0; i < size; i++, it++ )
*it = ( (GaChromosomeDomainBlock<TYPE>*)_configBlock )->_domain->GenerateRandom();
}
// Inverts data in selected part of chromosome's code
virtual void GACALL Invert(int start,
int size)
{
if( size <= 0 || start < 0 || start >= (int)_values.size() )
return;
vector<TYPE>::iterator it = _values.begin() + start;
for( int i = 0; i < size; i++, it++ )
( (GaChromosomeDomainBlock<TYPE>*)_configBlock )->_domain->Inverse( *it, *it );
}
// Makes new buffer for manupulatin parts of chromosome's code
virtual GaCodeValuesBuffer* GACALL MakeBuffer(int size) const
{
return new GaCodeValuesBuffer( size * sizeof( TYPE ) );
}
// Fills buffer with part of chromosome's code
virtual void GACALL FillBuffer(int pos,
int size,
GaCodeValuesBuffer& buffer) const
{
// check params
if( size <= 0 || pos < 0 || pos >= (int)_values.size() )
return;
// get buffer params
int s = pos + size < (int)_values.size() ? size : (int)_values.size() - pos;
TYPE* b = (TYPE*)buffer.GetBuffer();
int p = buffer.GetPosition() / sizeof( TYPE );
// save to buffer
for( int i = 0; i < s; i++ )
b[ p + i ] = _values[ pos + i ];
// move buffer pointer
buffer.Move( s * sizeof( TYPE ) );
}
// Makes chromosome's code from buffer of values
virtual void GACALL FromBuffer(const GaCodeValuesBuffer& buffer)
{
_values.clear();
// size of code
int size = buffer.GetPosition() / sizeof( TYPE );
_values.reserve( size );
// copy from buffer to code
for( int i = 0; i < size; i++ )
_values.push_back( ( (TYPE*)buffer.GetBuffer() )[ i ] );
}
// Initialize chromosome
GaMultiValueChromosome(TYPE* values,
int size,
GaChromosomeDomainBlock<TYPE>* configBlock) : GaDomainChromosome(configBlock)
{
if( size > 0 && values )
{
_values.reserve( size );
for( int i = 0; i < size; i++ )
_values.push_back( GetClosestValue( values[ i ] ) );
}
}
// Initialize chromosome with default(random) values
GaMultiValueChromosome(int size,
GaChromosomeDomainBlock<TYPE>* configBlock) : GaDomainChromosome(configBlock)
{
_values.reserve( size );
for( int i = 0; i < size; i++ )
_values.push_back( ( (GaChromosomeDomainBlock<TYPE>*)_configBlock )->_domain->GenerateRandom() );
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -