📄 chromosome.h
字号:
/*! \file Chromosome.h
\brief This file contains declaration of interfaces and classes needed to implement behavior and representations chromosomes.
*/
/*
*
* website: http://www.coolsoft-sd.com/
* contact: support@coolsoft-sd.com
*
*/
/*
* Genetic Algorithm Library
* Copyright (C) 2007-2008 Coolsoft Software Development
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef __GA_CHROMOSOME_H__
#define __GA_CHROMOSOME_H__
#include "Platform.h"
#include "SmartPtr.h"
#include "Operation.h"
#include "SortedGroupType.h"
namespace Population
{
class GaPopulation;
} // Population
using namespace Common;
using namespace Population;
/// <summary>Contatins interfaces, classes and datatypes needed to implement behavior and representations chromosomes and genetic operations.</summary>
namespace Chromosome
{
/// <summary>This class is base class for all chromosomes' parameters.
/// Built-in genetic operations (crossover, mutation, fitness function and comparator) share their parameters with chromosomes parameters.
///
/// 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 GaChromosomeParams : public GaParameters
{
protected:
/// <summary>Mutation probability in interval (0, 1) of mutation operation being performed during coupling of selected parents.</summary>
float _mutationProbability;
/// <summary>Maximal number of affected values in chromosome�s code by mutation operation.</summary>
int _mutationSize;
/// <summary>This flag if set, instructs that only mutations which lead to improvement of fitness value should be accepted. If it isn't
/// set, then all mutation should be accepted.</summary>
bool _improvingOnlyMutations;
/// <summary>probability in interval (0, 1) of crossover operation being performed during coupling of selected parents.</summary>
float _crossoverProbability;
/// <summary>Number of crossover points between two parents when crossover operation is performed.</summary>
int _numberOfCrossoverPoints;
public:
/// <summary>This constructor initializes parameters with user-defined values.</summary>
/// <param name="mutationProbability">mutation probability in interval (0, 1).</param>
/// <param name="mutationSize">maximal number of affected values in chromosome's code by mutation operation.</param>
/// <param name="improvingOnlyMutations">state of improving only mutation flag.</param>
/// <param name="crossoverProbability">crossover probability in interval (0, 1).</param>
/// <param name="numberOfCrossoverPoints">number of crossover points.</param>
GaChromosomeParams(float mutationProbability,
int mutationSize,
bool improvingOnlyMutations,
float crossoverProbability,
int numberOfCrossoverPoints) : _mutationProbability(mutationProbability),
_mutationSize(mutationSize),
_improvingOnlyMutations(improvingOnlyMutations),
_crossoverProbability(crossoverProbability),
_numberOfCrossoverPoints(numberOfCrossoverPoints) { }
/// <summary>This constructor initializes parameters with default values.
/// <br>1. mutation probability: 3%
/// <br>2. mutation size: 1 (only one value is mutated)
/// <br>3. only improving mutations are accepted: yes
/// <br>4. crossover probability: 80%
/// <br>5. number of crossover points: 1
/// </summary>
GaChromosomeParams() : _mutationProbability(0.03f),
_mutationSize(1),
_improvingOnlyMutations(true),
_crossoverProbability(0.8f),
_numberOfCrossoverPoints(1) { }
/// <summary>More details are given in specification of <see cref="GaParameters::Clone" /> method.
///
/// This method is not thread-safe.</summary>
virtual GaParameters* GACALL Clone() const { return new GaChromosomeParams( *this ); }
/// <summary>SetMutationProbability method sets probability of mutation operation being performed during coupling of selected parents.
///
/// This method is not thread-safe.</summary>
/// <param name="probability">new probability of mutation operation. Value is in interval (0, 1).</param>
inline void GACALL SetMutationProbability(float probability) { _mutationProbability = probability; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns probability of mutation operation being performed during coupling of selected parents.</returns>
inline float GACALL GetMutationProbability() const { return _mutationProbability; }
/// <summary><c>SetMutationSize</c> method sets maximal number of affected values in chromosome's code by mutation operation.
///
/// This method is not thread-safe.</summary>
/// <param name="size">maximal number of affected values in chromosome's code.</param>
inline void GACALL SetMutationSize(int size) { _mutationSize = size; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns maximal number of affected values in chromosome's code by mutation operation. </returns>
inline int GetMutationSize() const { return _mutationSize; }
/// <summary><c>SetImprovingMutationsFlag</c> method sets improving-mutation flag.
///
/// This method is not thread-safe.</summary>
/// <param name="improvingOnly">if this parameter is set to <c>true</c>, then only the mutations which lead to improvement of fitness value
/// should be accepted. If this parameter is set to <c>false</c>, all mutations are accepted.</param>
inline void GACALL SetImprovingMutationsFlag(bool improvingOnly) { _improvingOnlyMutations = improvingOnly; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns <c>true</c> if only the mutations which lead to improvement of fitness value should be accepted.
/// If all mutation should be accepted, then this method return <c>false</c>.</returns>
inline bool GACALL GetImprovingMutationsFlag() const { return _improvingOnlyMutations; }
/// <summary> <c>SetCrossoverProbability</c> method sets probability of crossover operation being performed during coupling of selected parents.
///
/// This method is not thread-safe.</summary>
/// <param name="probability">new probability of crossover operation. Value is in interval (0, 1).</param>
inline void GACALL SetCrossoverProbability(float probability) { _crossoverProbability = probability; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns probability of crossover operation being performed during coupling of selected parents.</returns>
inline float GACALL GetCrossoverProbability() const { return _crossoverProbability; }
/// <summary><c>SetNumberOfCrossoverPoints</c> method sets number of crossover points.
///
/// This method is not thread-safe.</summary>
/// <param name="numberOfPoints">new number of crossover points.</param>
inline void GACALL SetNumberOfCrossoverPoints(int numberOfPoints) { _numberOfCrossoverPoints = numberOfPoints; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns number of crossover points.</returns>
inline int GACALL GetNumberOfCrossoverPoints() const { return _numberOfCrossoverPoints; }
};// END CLASS DEFINITION GaChromosomeParams
class GaChromosome;
/// <summary><c>GaChromosomePtr</c> type is instance of <see cref="GaSmartPtr" /> template class and represents smart pointer to a chromosome.
/// Detailed description can be found in specification of <see cref="GaSmartPtr" /> template class.</summary>
typedef GaSmartPtr<GaChromosome> GaChromosomePtr;
/// <summary><c>GaChromosomeConstPtr</c> type is instance of <see cref="GaSmartPtr" /> template class and represents smart pointer to a constant chromosome.
/// Detailed description can be found in specification of <see cref="GaSmartPtr" /> template class.</summary>
typedef GaSmartPtr<const GaChromosome> GaChromosomeConstPtr;
/// <summary><c>GaChromosome</c> is interface for all chromosomes types in the library. Algorithms, populations and genetic operations assume
/// that all chromosomes obey this interface. Genetic operations over chromosomes (crossover, mutation, fitness function) may require
/// additional interfaces to be implemented, but all other built-in parts of the library don't have additional assumptions.</summary>
class GaChromosome
{
public:
/// <summary>Because this is base class, virtual destructor must be defined in order to enable calling of right destructor
/// (destructor of inheriting class).</summary>
virtual ~GaChromosome() { };
/// <summary>This method performs crossover operation with two parents (<c>this</c> and <c>secondParent</c>) and 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 Crossover(GaChromosomePtr secondParent) const=0;
/// <summary>This method performs mutation over <c>this</c> chromosome.</summary>
virtual void GACALL Mutation()=0;
/// <summary>This method produces copy of chromosome. It can create exact copy or it can create chromosome with new code,
/// but with exact setup (parameters, operations, etc).</summary>
/// <param name="setupOnly">it this parameter is set to <c>true</c> only the setup of the chromosome is copied into new one,
/// and new chromosome code is created, otherwise the whole chromosome is copied (code and setup).</param>
/// <returns>Method returns smart pointer to newly created chromosome.</returns>
virtual GaChromosomePtr GACALL MakeCopy(bool setupOnly) const=0;
/// <summary>This method creates new chromosome with exact setup (parameters, operations, etc) of this chromosome.</summary>
/// <returns>Method returns smart pointer to newly created chromosome.</returns>
virtual GaChromosomePtr GACALL MakeNewFromPrototype() const=0;
/// <summary>This method returns fitness value of chromosome.</summary>
/// <returns>Method returns fitness value of chromosome.</returns>
virtual float GACALL GetFitness() const=0;
/// <summary>This method compares fitness values of two chromosomes (<c>this</c> and <c>c</c>).
/// NOTE: Comparison doesn't have to be arithmetical comparison.</summary>
/// <param name="c">the second chromosome for comparison with <c>this</c>.</param>
/// <returns>a) -1 if fitness value of <c>this</c> is lower then value of <c>c</c>.
/// <br>b) 0 if fitness values of both chromosomes are equal.
/// <br>c) 1 if fitness value of <c>this</c> is greater then value of <c>c</c>. </returns>
virtual int GACALL CompareFitnesses(GaChromosomePtr c) const=0;
/// <summary>This method compares fitness values of two chromosomes (<c>this</c> and <c>c</c>).
/// NOTE: Comparison doesn't have to be arithmetical comparison.</summary>
/// <param name="c">the second chromosome for comparison with <c>this</c>.</param>
/// <returns>a) -1 if fitness value of <c>this</c> is lower then value of <c>c</c>.
/// <br>b) 0 if fitness values of both chromosomes are equal.
/// <br>c) 1 if fitness value of <c>this</c> is greater then value of <c>c</c>. </returns>
virtual int GACALL CompareFitnesses(float c) const=0;
/// <summary>This method recalculates fitness value of the chromosome and stores it.</summary>
virtual void GACALL RefreshFitness()=0;
/// <summary>This method returns reference of chromosome's parameters.</summary>
/// <returns>Method returns reference of chromosome's parameters.</returns>
virtual const GaChromosomeParams& GACALL GetParameters() const=0;
/// <summary>This method sets pointer to new parameters of chromosome.</summary>
/// <param name="p">pointer to new parameters.</param>
virtual void GACALL SetParameters(GaChromosomeParams* p)=0;
/// <summary>This method returns size of chromosome.s code.</summary>
/// <returns>Method returns size of chromosome.s code.</returns>
virtual int GACALL GetCodeSize() const=0;
/// <summary>This operator copies setup and chromosome's code from <c>rhs</c>.</summary>
/// <param name="rhs">reference to chromosome which should be copied.</param>
/// <returns>Method returns reference to this.</returns>
virtual GaChromosome& GACALL operator =(const GaChromosome& rhs)=0;
/// <summary>This operator compares chromosomes' codes.</summary>
/// <param name="c">reference to chromosome which is compared to <c>this</c> chromosome.</param>
/// <returns>Method returns percent of similarity </returns>
virtual float GACALL operator ==(const GaChromosome& c) const=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -