📄 valuesets.h
字号:
GaRandom<T>* _randomGenerator;
public:
/// <summary>This constructor initializes value set with bounds and random generator.</summary>
/// <param name="values">bounds of interval of original values.</param>
/// <param name="invertedValues">bounds of interval of inverted values.</param>
/// <param name="randomGenerator">pointer to random generator.</param>
/// <param name="viceVersa">if this parameter is set to <c>true</c>, inverted values are treated as members of the set,
/// if it is set to <c>false</c>, inverted values are not treated as members of the set.</param>
GaIntervalValueSet(const GaValueIntervalBounds<T>& values,
const GaValueIntervalBounds<T>& invertedValues,
GaRandom<T>* randomGenerator,
bool viceVersa) : GaValueSet<T>(viceVersa),
_values(values),
_invertedValues(invertedValues),
_randomGenerator(randomGenerator) { }
/// <summary>This constructor initializes value set with random generator, but doesn't define bounds of intervals.</summary>
/// <param name="randomGenerator">pointer to random generator.</param>
GaIntervalValueSet(GaRandom<T>* randomGenerator) : _randomGenerator(randomGenerator) { }
/// <summary>More details are given in specification of <see cref="GaValueSet::GenerateRandom" /> method.
///
/// This method is not thread-safe.</summary>
virtual T GACALL GenerateRandom() const
{
return !this->_viceVersa || GaGlobalRandomBoolGenerator->Generate()
? _randomGenerator->Generate( _values.GetLowerBound(), _values.GetHigherBound() )
: _randomGenerator->Generate( _invertedValues.GetLowerBound(), _invertedValues.GetHigherBound() );
}
/// <summary>More details are given in specification of <see cref="GaValueSet::Inverse" /> method.
///
/// This method is not thread-safe.</summary>
virtual bool GACALL Inverse(const T& value,
T& inverted) const
{
if( _values.InBounds( value ) || ( this->_viceVersa && _invertedValues.InBounds( value ) ) )
{
inverted = _invertedValues.GetLowerBound() + _values.GetHigherBound() - value;
return true;
}
return false;
}
/// <summary>More details are given in specification of <see cref="GaValueSet::ClosestValue" /> method.
///
/// This method is not thread-safe.</summary>
virtual bool GACALL Belongs(const T& value) const { return _values.InBounds( value ) || ( this->_viceVersa && _invertedValues.InBounds( value ) ); }
/// <summary>More details are given in specification of <see cref="GaValueSet::ClosestValue" /> method.
///
/// This method is not thread-safe.</summary>
virtual const T& GACALL ClosestValue(const T& value) const
{
// value belongs to interval?
if( _values.InBounds( value ) || ( this->_viceVersa && _invertedValues.InBounds( value ) ) )
return value;
// array of bounds for finding the closest
const T* bounds[ 4 ];
bounds[ 0 ] = &_values.GetLowerBound();
bounds[ 1 ] = &_values.GetHigherBound();
bounds[ 2 ] = &_invertedValues.GetLowerBound();
bounds[ 3 ] = &_invertedValues.GetHigherBound();
// start of search
int i = this->_viceVersa ? 2 : 0;
int ret = i + 1;
T diff = value < *bounds[ ret ] ? *bounds[ ret ] - value : *bounds[ ret ] - value;
// find closes bounds
for( ; i >= 0; i-- )
{
// calculate distance
T d = value > *bounds[ i ] ? value - *bounds[ i ] : value - *bounds[ i ];
// closer
if( d < diff )
{
ret = i;
diff = d;
}
}
return *bounds[ ret ];
}
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns bounds of interval of original values.</returns>
inline const GaValueIntervalBounds<T>& GACALL GetValueBounds() const { return _values; }
/// <summary><c>SetValueBounds</c> method sets bounds of interval of original values.
///
/// This method is not thread-safe.</summary>
/// <param name="bounds">new bound of interval.</param>
inline void GACALL SetValueBounds(const GaValueIntervalBounds<T>& bounds) { _values = bounds; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns bounds of interval of inverted values.</returns>
inline const GaValueIntervalBounds<T>& GACALL GetInvertedValueBounds() const { return _invertedValues; }
/// <summary><c>SetInvertedValueBounds</c> method sets bounds of interval of inverted values.
///
/// This method is not thread-safe.</summary>
/// <param name="bounds">new bound of interval.</param>
inline void GACALL SetInvertedValueBounds(const GaValueIntervalBounds<T>& bounds) { _invertedValues = bounds; }
/// <summary><c>GetBounds</c> method returns bounds of both intervals.
///
/// This method is not thread-safe.</summary>
/// <param name="originals">reference to variable to which bounds of original values are stored.</param>
/// <param name="inverted">reference to variable to which bounds of inverted values are stored. </param>
inline void GACALL GetBounds(GaValueIntervalBounds<T>& originals,
GaValueIntervalBounds<T>& inverted) const
{
originals = _values;
inverted = _invertedValues;
}
/// <summary><c>SetBounds</c> method sets bounds of both intervals.
///
/// This method is not thread-safe.</summary>
/// <param name="originals">new bounds of interval of original values.</param>
/// <param name="inverted">new bounds of interval of inverted values.</param>
inline void GACALL SetBounds(const GaValueIntervalBounds<T>& originals,
const GaValueIntervalBounds<T>& inverted)
{
_values = originals;
_invertedValues = inverted;
}
};// END CLASS DEFINITION GaIntervalValueSet
/// <summary> This template represents value set which is consisted of multiple value sets. Value sets can be of any kind,
/// but must contains same type of data.
///
/// This class has built-in synchronizator so it is allowed to use <c>LOCK_OBJECT</c> and <c>LOCK_THIS_OBJECT</c> macros
/// with instances of this class, but no public or private methods are thread-safe.</summary>
/// <param name="T">type of data in value set.</param>
template <typename T>
class GaCombinedValueSet : public GaValueSet<T>
{
private:
/// <summary>Array of value sets.</summary>
vector<GaValueSet<T>*> _sets;
public:
/// <summary>This constructor initializes empty value set.</summary>
GaCombinedValueSet() { }
/// <summary>More details are given in specification of <see cref="GaValueSet::GenerateRandom" /> method.
///
/// This method is not thread-safe.</summary>
virtual T GACALL GenerateRandom() const
{
return _sets[ GaGlobalRandomIntegerGenerator->Generate( _sets.size() ) ]->GenerateRandom();
}
/// <summary>More details are given in specification of <see cref="GaValueSet::Inverse" /> method.
///
/// This method is not thread-safe.</summary>
virtual bool GACALL Inverse(const T& value,
T& inverted) const
{
for( typename vector<GaValueSet<T>*>::iterator it = _sets.begin(); it != _sets.end(); it++ )
{
if( (*it)->Inverse( value, inverted ) )
return true;
}
return false;
}
/// <summary>More details are given in specification of <see cref="GaValueSet::ClosestValue" /> method.
///
/// This method is not thread-safe.</summary>
virtual bool GACALL Belongs(const T& value) const
{
for( typename vector<GaValueSet<T>*>::iterator it = _sets.begin(); it != _sets.end(); it++ )
{
if( (*it)->Belongs( value ) )
return true;
}
return false;
}
/// <summary>More details are given in specification of <see cref="GaValueSet::ClosestValue" /> method.
///
/// This method is not thread-safe.</summary>
virtual const T& GACALL ClosestValue(const T& value) const
{
// random choose
T closest = _sets[ 0 ]->ClosestValue();
// find closest value
for( typename vector<GaValueSet<T>*>::iterator it = _sets.begin() + 1; it != _sets.end(); it++ )
{
// get closest value of current set
T t = ( *it )->ClosestValue();
// closer
if( value - t < closest )
closest = t;
}
return closest;
}
/// <summary><c>Add</c> method inserts new value set.
///
/// This method is not thread safe.</summary>
/// <param name="set">pointer to value set which is going to be added.</param>
void GACALL Add(GaValueSet<T>* set)
{
_sets.push_back(set);
}
/// <summary><c>Remove</c> method removes specified value set.
///
/// This method is not thread-safe.</summary>
/// <param name="set">pointer to value set which should be removed.</param>
void GACALL Remove(GaValueSet<T>* set)
{
for( typename vector<GaValueSet<T>*>::iterator it = _sets.begin(); it != _sets.end(); it )
{
if( *it == set )
{
_sets.erase( it );
return;
}
}
}
/// <summary><c>Remove</c> method removes value set at specified position.
///
/// This method is not thread-safe.</summary>
/// <param name="pos">position of the value set which is going to be removed.</param>
void GACALL Remove(int pos)
{
typename vector<GaValueSet<T>*>::iterator it = _sets.begin();
for( int i = 0; it != _sets.end(); it, i++ )
{
if( i == pos )
{
_sets.erase( it );
return;
}
}
}
/// <summary>This method is not thread safe.</summary>
/// <returns>Method returns number of value sets in this combined value set.</returns>
inline int GACALL GetNumberOfSets() const { return _sets.size(); }
/// <summary><c>operator []</c> returns reference to value set at specified position.
///
/// This operator is not thread-safe.</summary>
/// <param name="pos">position of value set.</param>
/// <returns>Operator returns reference to value set at specified position.</returns>
inline GaValueSet<T>& GACALL operator [](int pos) const { return *_sets[ pos ]; }
};// END CLASS DEFINITION GaCombinedValueSet
/// <summary>This template represents value set with no additional limitations, except limitation introduced by the type and
/// random generator used to generate values.
///
/// This class has built-in synchronizator so it is allowed to use <c>LOCK_OBJECT</c> and <c>LOCK_THIS_OBJECT</c> macros
/// with instances of this class, but no public or private methods are thread-safe.</summary>
/// <param name="T">type of data in value set. This type must support unary <c>operators -</c>.</param>
template <typename T>
class GaUnboundValueSet : public GaValueSet<T>
{
private:
/// <summary>Pointer to random generator which is used for generating of random values.</summary>
GaRandom<T>* _randomGenerator;
public:
/// <summary>This constructor initializes value set with random generator.</summary>
/// <param name="randomGenerator">pointer to random generator.</param>
GaUnboundValueSet(GaRandom<T>* randomGenerator) : _randomGenerator(randomGenerator) { }
/// <summary>More details are given in specification of <see cref="GaValueSet::GenerateRandom" /> method.
///
/// This method is not thread-safe.</summary>
virtual T GenerateRandom() const { return _randomGenerator->Generate(); }
/// <summary>More details are given in specification of <see cref="GaValueSet::Inverse" /> method.
///
/// This method is not thread-safe.</summary>
virtual bool Inverse(const T& value,
T& inverted) const
{
inverted = -value;
return true;
}
/// <summary>More details are given in specification of <see cref="GaValueSet::ClosestValue" /> method.
///
/// This method is not thread-safe.</summary>
virtual bool Belongs(const T& value) const { return true; }
/// <summary>More details are given in specification of <see cref="GaValueSet::ClosestValue" /> method.
///
/// This method is not thread-safe.</summary>
virtual const T& ClosestValue(const T& value) const { return value; }
};// END CLASS DEFINITION GaUnboundValueSet
} // Representation
} // Chromosome
#endif // __GA_VALUE_SETS_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -