📄 populationoperations.h
字号:
int* _parents;
/// <summary>Number of chromosomes which can be stored in this result set.</summary>
int _numberOfOffsprings;
/// <summary>Pointer to result set of selection operation. It contains chromosomes which are used as parents to produce offspring.</summary>
GaSelectionResultSet* _selection;
public:
/// <summary>This constructor initializes coupling result set with maximum number of offspring which can be stored and
/// selection result set which stores parents.</summary>
/// <param name="numberOfOffsprings">maximum number of offspring which this result set can store.</param>
/// <param name="selectionResults">pointer to selection results which stores parents.</param>
GAL_API
GaCouplingResultSet(int numberOfOffsprings,
GaSelectionResultSet* selectionResults);
/// <summary>Destructor frees memory of array which stores parents' indices and array of smart pointers to offspring.</summary>
GAL_API
virtual ~GaCouplingResultSet();
/// <summary><c>GetOffspringAt</c> method returns newly created chromosome and its parent's index at specified position in result set.
///
/// This method is not thread-safe.</summary>
/// <param name="pos">position of offspring chromosome.</param>
/// <param name="offspring">reference to smart pointer to which is stored pointer to new chromosome.</param>
/// <param name="parent">reference to variable to which is stored index of new chromosome's parent.</param>
/// <returns>Method returns <c>true</c> if chromosome exists at the position.</returns>
GAL_API
bool GACALL GetOffspringAt(int pos,
GaChromosomePtr& offspring,
int& parent) const;
/// <summary><c>SetOffspringAt</c> method sets smart pointer to new chromosome and index of its parent at specified position in result set.
/// It can be used to insert new chromosomes in result set.
///
/// This method is not thread-safe.</summary>
/// <param name="pos">position in result set which is changed.</param>
/// <param name="offspring">smart pointer to new chromosome which is inserted.</param>
/// <param name="parent">index of parent's chromosome.</param>
/// <returns>Method returns <c>true</c> if new chromosomes is inserted in result set.</returns>
GAL_API
bool GACALL SetOffspringAt(int pos,
GaChromosomePtr offspring,
int parent);
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns maximal number of offspring which this result can store.</returns>
inline int GACALL GetNumberOfOffsprings() const { return _numberOfOffsprings; }
/// <summary><c>SetNumberOOffspring</c> method changes size of arrays so they can store specified number of offspring.
///
/// This method is not thread-safe.</summary>
/// <param name="number">new number of chromosomes which can be stored in this result set.</param>
GAL_API
void GACALL SetNumberOfOffsprings(int number);
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns pointer to array of smart pointers to newly created chromosomes.</returns>
inline GaChromosomePtr* GACALL GetOffspringsBuffer() const { return _offsprings; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns pointer to array of parent's indices.</returns>
inline int* GACALL GetParentsBuffer() const { return _parents; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns reference to selection which stores parent chromosomes.</returns>
inline GaSelectionResultSet& GACALL GetSelectionResultSet() const { return *_selection; }
};// END CLASS DEFINITION GaCouplingResultSet
/// <summary>This class is interface for coupling operations which produce offspring from selected chromosomes.
///
/// This class has no built-in synchronizator, so <c>LOCK_OBJECT</c> and <c>LOCK_THIS_OBJECT</c> macros cannot be used with instances of this class.
/// No public or private methods are thread-safe.</summary>
class GaCouplingOperation : public GaOperation
{
public:
/// <summary><c>operator ()</c> performs coupling operation. Because this operation is parallelized, caller must specify ID its working thread,
/// and total number of working threads which are used.</summary>
/// <param name="population">reference to population which hosts selected chromosomes.</param>
/// <param name="output">reference to coupling result set which will store produced offspring.</param>
/// <param name="parameters">reference to parameters of replacement operation.</param>
/// <param name="workerId">ID of the worker (working thread) which called this operator.</param>
/// <param name="numberOfWorkers">number of workers (working threads).</param>
virtual void GACALL operator ()(const GaPopulation& population,
GaCouplingResultSet& output,
const GaCouplingParams& parameters,
int workerId,
int numberOfWorkers) const=0;
};// END CLASS DEFINITION GaCouplingOperation
/// <summary><c>GaCouplingPair</c> type is instance of <see cref="GaOperationParametersPair" /> template class and represents pair of
/// coupling operation and its parameters. Detailed description can be found in specification of
/// <see cref="GaOperationParametersPair" /> template class.</summary>
typedef GaOperationParametersPair<GaCouplingOperation, GaCouplingParams> GaCouplingPair;
/// <summary><c>GaCouplingCatalogue</c> type is instance of <see cref="GaCatalogue" /> template class and represents catalogue of coupling operations.
/// Detailed description can be found in specification of <see cref="GaCatalogue" /> template class.</summary>
typedef GaCatalogue<GaCouplingOperation> GaCouplingCatalogue;
/// <summary>This class is base for parameters of replacement operation.
///
/// This class has no built-in synchronizator, so <c>LOCK_OBJECT</c> and <c>LOCK_THIS_OBJECT</c> macros cannot be used with instances of this class.
/// No public or private methods are thread-safe.</summary>
class GaReplacementParams : public GaParameters
{
protected:
/// <summary>Number of chromosomes which should be replaced.</summary>
int _replacemetSize;
public:
/// <summary>This constructor initializes replacement parameters with user-defined replacement size.</summary>
/// <param name="replacementSize">number of chromosomes which should be replaced.</param>
GaReplacementParams(int replacementSize) { SetReplacementSize( replacementSize ); }
/// <summary>This constructor initializes replacement parameters with default values. Default replacement size is 2.</summary>
GaReplacementParams() : _replacemetSize(0) { }
/// <summary>More details are given in specification of <see cref="GaParameters::Clone" /> method.</summary>
virtual GaParameters* GACALL Clone() const { return new GaReplacementParams( *this ); }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns number of chromosomes which should be replaced.</returns>
inline int GACALL GetReplacementSize() const { return _replacemetSize; }
/// <summary><c>SetReplacementSize</c> method sets number of chromosomes which should be replaced.
///
/// This method is not thread-safe.</summary>
/// <param name="size">new number of chromosomes for replacement.</param>
inline void GACALL SetReplacementSize(int size) { _replacemetSize = size < 0 ? 0 : size; }
};// END CLASS DEFINITION GaReplacementParams
/// <summary>This class is interface for replacement operations which replace chromosomes of population with new chromosomes.</summary>
class GaReplacementOperation : public GaOperation
{
public:
// Replaces existing chromosomes with new ones based on passed parameters and selection results.
/// <summary><c>operator ()</c> performs replacement operation. </summary>
/// <param name="population">reference to population which hosts chromosomes.</param>
/// <param name="parameters">reference to parameters of replacement operation.</param>
/// <param name="newChromosomes">reference to result set of coupling operation.</param>
virtual void GACALL operator ()(GaPopulation& population,
const GaReplacementParams& parameters,
const GaCouplingResultSet& newChromosomes) const=0;
};// END CLASS DEFINITION GaReplacementOperation
/// <summary><c>GaReplacementPair</c> type is instance of <see cref="GaOperationParametersPair" /> template class and represents pair
/// of replacement operation and its parameters. Detailed description can be found in specification of
/// <see cref="GaOperationParametersPair" /> template class.</summary>
typedef GaOperationParametersPair<GaReplacementOperation, GaReplacementParams> GaReplacementPair;
/// <summary><c>GaReplacementCatalogue</c> type is instance of <see cref="GaCatalogue" /> template class and represents catalogue of replacement operations.
/// Detailed description can be found in specification of <see cref="GaCatalogue" /> template class.</summary>
typedef GaCatalogue<GaReplacementOperation> GaReplacementCatalogue;
/// <summary>This class is base for parameters of scaling operation.</summary>
class GaScalingParams : public GaParameters { };// END CLASS DEFINITION GaScalingParams
// Interface for scaling operation
/// <summary>This class is interface for scaling operations which transform chromosomes' fitness values.
///
/// This class has no built-in synchronizator, so <c>LOCK_OBJECT</c> and <c>LOCK_THIS_OBJECT</c> macros cannot be used with instances of this class.
/// No public or private methods are thread-safe.</summary>
class GaScalingOperation : public GaOperation
{
public:
/// <summary><c>operator ()</c> performs scaling operation and returns scaled fitness value.</summary>
/// <param name="chromosome">reference to chromosome which fitness is scaled.</param>
/// <param name="population">reference to population which hosts specified chromosome.</param>
/// <param name="parameters">reference to parameters of scaling operation.</param>
/// <returns>Method returns scaled fitness.</returns>
virtual float GACALL operator ()(const GaScaledChromosome& chromosome,
const GaPopulation& population,
const GaScalingParams& parameters) const=0;
/// <summary><c>IsRankingBased</c> method should return <c>true</c> if scaling of fitness value is based on ranking of chromosome in population.</summary>
/// <returns>Method returns <c>true</c> if scaling operation scales chromosome's fitness based on its ranking in population.
/// If ranking is not used in scaling method should return <c>false</c>.</returns>
virtual bool GACALL IsRankingBased() const=0;
/// <summary>NeedRescaling method checks current population for consistence of chromosomes' scaled fitness values.</summary>
/// <param name="population">reference to population which is checked.</param>
/// <param name="parameters">reference to parameters of scaling operation.</param>
/// <returns>Method returns <c>false</c> if all scaled fitness values of chromosomes are consistent.
/// If rescaling of some or all chromosomes in population should be done, method returns <c>true</c>.</returns>
virtual bool GACALL NeedRescaling(const GaPopulation& population,
const GaScalingParams& parameters) const=0;
};// END CLASS DEFINITION GaScalingOperation
/// <summary><c>GaScalingPair</c> type is instance of <see cref="GaOperationParametersPair" /> template class and represents pair of
/// scaling operation and its parameters. Detailed description can be found in specification of
/// <see cref="GaOperationParametersPair" /> template class.</summary>
typedef GaOperationParametersPair<GaScalingOperation, GaScalingParams> GaScalingPair;
/// <summary><c>GaScalingCatalogue</c> type is instance of <see cref="GaCatalogue" /> template class and represents catalogue of scaling operations.
/// Detailed description can be found in specification of <see cref="GaCatalogue" /> template class.</summary>
typedef GaCatalogue<GaScalingOperation> GaScalingCatalogue;
} // Population
#endif // __GA_POPULATION_OPERATIONS_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -