📄 chromosome.h
字号:
/// <summary>This method handles probability of crossover. It decides if crossover is going to be preformed.
/// If crossover is going to be performed this method calls <see cref="PerformeCrossover" />.
///
/// This method is not thread-safe.</summary>
/// <param name="secondParent">smart pointer to second parent.</param>
/// <returns>Method returns smart pointer to newly created offspring.</returns>
GAL_API
virtual GaChromosomePtr GACALL Crossover(GaChromosomePtr secondParent) const;
/// <summary>This method is not-thread safe.</summary>
/// <returns>Method returns fitness value of chromosome.</returns>
virtual float GACALL GetFitness() const { return _fitness; }
/// <summary>This method only extracts fitness value from <c>c</c>, and delegate responsibility to <see cref="ComapreFitnesses(float)" /> method.
/// More details are given in specification of <see cref="GaChromosome::CompareFitnesses" /> of interface.
///
/// This method is not thread-safe.</summary>
virtual int GACALL CompareFitnesses(GaChromosomePtr c) const { return CompareFitnesses( c->GetFitness() ); }
/// <summary>More details are given in specification of <see cref="GaChromosome::CompareFitnesses" /> of interface.
///
/// This method is not thread-safe.</summary>
virtual int GACALL CompareFitnesses(float c) const=0;
/// <summary><c>RefreshFitness</c> method recalculates fitness value of the chromosome and stores it.
///
/// This method is not thread safe.</summary>
virtual void GACALL RefreshFitness() { _fitness = CalculateFitness(); }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns reference of chromosome's parameters.</returns>
virtual const GaChromosomeParams& GACALL GetParameters() const { return *_configBlock->_parameters; }
/// <summary><c>SetParameters</c> method sets pointer to new parameters of chromosome.
///
/// This method is not thread-safe.</summary>
/// <param name="p">pointer to new parameters.</param>
virtual void GACALL SetParameters(GaChromosomeParams* p) { _configBlock->_parameters = p; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>This method returns reference to CCB.</returns>
virtual const GaChromosomeParamsBlock& GACALL GetConfigBlock() const { return *_configBlock; }
/// <summary><c>SetConfigBlock</c> method sets pointer to new CCB.
///
/// This method is not thread-safe.</summary>
/// <param name="block">pointer to new CCB.</param>
virtual void GACALL SetConfigBlock(GaChromosomeParamsBlock* block) { _configBlock = block; }
/// <summary><c>operator =</c> copies setup and chromosome's code from <c>rhs</c>.
///
/// This operator is not thread-safe.</summary>
/// <param name="rhs">reference to chromosome which is copied.</param>
/// <returns>Method returns reference to this.</returns>
virtual GaChromosome& GACALL operator =(const GaChromosome& rhs)
{
// copy data
_fitness = ( (const GaDefaultChromosome&)rhs )._fitness;
_configBlock = ( (const GaDefaultChromosome&)rhs )._configBlock;
return *this;
}
protected:
/// <summary>It is called when mutation should be performed over chromosome.</summary>
virtual void GACALL PerformMutation()=0;
/// <summary>. It is called when crossover should be performed over two chromosomes to produce offspring.</summary>
/// <param name="secondParent">smart pointer to second parent.</param>
/// <returns>Method returns smart pointer to newly created offspring.</returns>
virtual GaChromosomePtr GACALL PerformCrossover(GaChromosomePtr secondParent) const=0;
/// <summary>This method called before performing mutation if improving-only mutation flag is set. It should save chromosome's code
/// so it can be restored if mutation produce chromosome with lower fitness value.</summary>
virtual void GACALL PreapareForMutation()=0;
/// <summary>This method is called after mutation is performed if improving-only mutation flag is set and mutation improved fitness value.
/// The method can delete backed up chromosome's code which was made by <see cref="PreapareForMutation" /> method.</summary>
virtual void GACALL AcceptMutation()=0;
/// <summary>This method is called after mutation is performed if improving-only mutation flag is set and mutation degraded fitness value.
/// The method restores old chromosome's code which was backed up with <see cref="PreapareForMutation" /> method.</summary>
virtual void GACALL RejectMutation()=0;
};// END CLASS DEFINITION GaDefaultChromosome
class GaCrossoverOperation;
class GaMutationOperation;
class GaFitnessOperation;
class GaDynamicOperationChromosome;
/// <summary>This class is CCB for chromosomes which use extern genetic operations. This CCB stores pointer to those operations.
///
/// 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 GaChromosomeOperationsBlock : public GaChromosomeParamsBlock
{
friend class GaDynamicOperationChromosome;
protected:
/// <summary>Pointer to extern fitness operation.</summary>
GaFitnessOperation* _fitnessOperation;
/// <summary>Pointer to extern fitness comparator.</summary>
GaFitnessComparator* _fitnessComparator;
/// <summary>Pointer to extern mutation operation.</summary>
GaMutationOperation* _mutationOperation;
/// <summary>Pointer to extern crossover operation.</summary>
GaCrossoverOperation* _crossoverOperation;
public:
/// <summary>This constructor initializes CCB with pointer to chromosomes' parameters and extern genetic operations.</summary>
/// <param name="crossoverOperation">pointer to extern crossover operation.</param>
/// <param name="mutationOperation">pointer to extern mutation operation.</param>
/// <param name="fitnessOperation">pointer to extern fitness operation.</param>
/// <param name="fitnessComparator">pointer to extern fitness comparator.</param>
/// <param name="parameters">pointer to chromosomes� parameters.</param>
GaChromosomeOperationsBlock(GaCrossoverOperation* crossoverOperation,
GaMutationOperation* mutationOperation,
GaFitnessOperation* fitnessOperation,
GaFitnessComparator* fitnessComparator,
GaChromosomeParams* parameters) : GaChromosomeParamsBlock(parameters),
_crossoverOperation(crossoverOperation),
_mutationOperation(mutationOperation),
_fitnessOperation(fitnessOperation),
_fitnessComparator(fitnessComparator) { }
/// <summary>This is copy constructor. The constructor doesn't create copy of parameters' and operations' objects,
/// it only copies pointer to chromosomes' parameters and operations.</summary>
/// <param name="rhs">reference to CCB which is copied.</param>
GaChromosomeOperationsBlock(const GaChromosomeOperationsBlock& rhs) : GaChromosomeParamsBlock(rhs),
_crossoverOperation(rhs._crossoverOperation),
_mutationOperation(rhs._mutationOperation),
_fitnessOperation(rhs._fitnessOperation),
_fitnessComparator(rhs._fitnessComparator) { }
/// <summary>This constructor initializes empty CCB.</summary>
GaChromosomeOperationsBlock() : _crossoverOperation(NULL),
_mutationOperation(NULL),
_fitnessOperation(NULL),
_fitnessComparator(NULL) { }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns reference to extern crossover operation.</returns>
inline const GaCrossoverOperation& GACALL GetCrossoverOperation() const { return *_crossoverOperation; }
/// <summary><c>SetCrossoverOperation</c> sets pointer to extern crossover operation.
///
/// This method is not thread-safe.</summary>
/// <param name="operation"></param>
inline void GACALL SetCrossoverOperation(GaCrossoverOperation* operation) { _crossoverOperation = operation; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns reference to extern mutation operation.</returns>
inline const GaMutationOperation& GACALL GetMutationOperation() const { return *_mutationOperation; }
/// <summary><c>SetMutationOperation</c> sets pointer to extern mutation operation.
///
/// This method is not thread-safe.</summary>
/// <param name="operation">pointer to extern mutation operation.</param>
inline void GACALL SetMutationOperation(GaMutationOperation* operation) { _mutationOperation = operation; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns reference to extern fitness operation.</returns>
inline const GaFitnessOperation& GACALL GetFitnessOperation() const { return *_fitnessOperation; }
/// <summary><c>SetFitnessOperation</c> sets pointer to extern fitness operation.
///
/// This method is not thread-safe.</summary>
/// <param name="operation">pointer to extern fitness operation.</param>
inline void GACALL SetFitnessOperation(GaFitnessOperation* operation) { _fitnessOperation = operation; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns reference to extern fitness comparator.</returns>
inline const GaFitnessComparator& GACALL GetFitnessComparator() const { return *_fitnessComparator; }
/// <summary>SetFitnessComparator sets pointer to extern fitness comparator.
///
/// This method is not thread-safe.</summary>
/// <param name="comparator">pointer to extern fitness comparator.</param>
inline void GACALL SetFitnessComparator(GaFitnessComparator* comparator) { _fitnessComparator = comparator; }
};// END CLASS DEFINITION GaChromosomeOperationsBlock
/// <summary>This class should be base for chromosomes which use extern genetic operations.
///
/// 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 GaDynamicOperationChromosome : public GaDefaultChromosome
{
public:
/// <summary>This constructor initializes chromosome with CCB.</summary>
/// <param name="configBlock">pointer to CCB.</param>
GaDynamicOperationChromosome(GaChromosomeOperationsBlock* configBlock) : GaDefaultChromosome( configBlock ) { }
/// <summary>Copy constructor.</summary>
/// <param name="c">reference to chromosome which is copied.</param>
/// <param name="setupOnly">if this parameter is <c>true</c>, only pointer to CCB is copied. If this parameter is <c>false</c>,
/// chromosome's data and CCB is copied.</param>
GaDynamicOperationChromosome(const GaDynamicOperationChromosome& c,
bool setupOnly) : GaDefaultChromosome(c, setupOnly) { }
// Compares fitnesses of two chromosomes. Returns:
// a, -1 if the fitness value of this chromosome is lower then the given value
// b. 0 if the fitness values of both chromosomes are equal
// c. 1 if the fitness value of this chromosome is greater then the given value
/// <summary>CompareFitnesses method delegates control to extern fitness comparator specified in CCB.
///
/// For more information see specification of <see cref="GaChromosome::CompareFitnesses" /> method.
///
/// This method is not thread-safe.</summary>
GAL_API
virtual int GACALL CompareFitnesses(float c) const;
protected:
/// <summary><c>PerformCrossover</c> method delegates control to extern crossover operation specified in CCB.
///
/// For more information see specification of <see cref="GaDefaultChromosome::PerformCrossover" /> method.
///
/// This method is not thread-safe.</summary>
GAL_API
virtual GaChromosomePtr GACALL PerformCrossover(GaChromosomePtr secondParent) const;
/// <summary><c>PerformMutation</c> method delegates control to extern mutation operation specified in CCB.
///
/// For more information see specification of <see cref="GaDefaultChromosome::PerformMutation" /> method.
///
/// This method is not thread-safe.</summary>
GAL_API
virtual void GACALL PerformMutation();
/// <summary><c>CalculateFitness</c> method delegates control to extern fitness operation specified in CCB.
///
/// For more information see specification of <see cref="GaDefaultChromosome::CalculateFitness" /> method.
///
/// This method is not thread-safe.</summary>
GAL_API
virtual float GACALL CalculateFitness() const;
};// END CLASS DEFINITION GaDynamicOperationChromosome
} // Chromosome
#endif // __GA_CHROMOSOME_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -