📄 populationoperations.h
字号:
/*! \file PopulationOperations.h
\brief This file declares interfaces and classes needed to implement genetic operations which are performed over population.
*/
/*
*
* 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_POPULATION_OPERATIONS_H__
#define __GA_POPULATION_OPERATIONS_H__
#include "Platform.h"
#include "Operation.h"
#include "Catalogue.h"
#include "Chromosome.h"
#include "SortedGroup.h"
using namespace Common;
using namespace Chromosome;
using namespace Population;
namespace Population
{
/// <summary>This class is base for parameters of selection 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 GaSelectionParams : public GaParameters
{
protected:
/// <summary>Number of chromosomes which should be selected.</summary>
int _selectionSize;
public:
/// <summary>This constructor initializes selection parameters with user-defined replacement size.</summary>
/// <param name="selectionSize">number of chromosomes which should be selected.</param>
GaSelectionParams(int selectionSize) { SetSelectionSize( selectionSize ); }
/// <summary>This constructor initializes selection parameters with default values. Default selection size is 2.</summary>
GaSelectionParams() : _selectionSize(0) { }
/// <summary>More details are given in specification of <see cref="GaParameters::Clone" /> method.</summary>
virtual GaParameters* GACALL Clone() const { return new GaSelectionParams( *this ); }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns number of chromosomes which should be selected.</returns>
inline int GACALL GetSelectionSize() const { return _selectionSize; }
/// <summary><c>SetSelectionSize</c> method sets number of chromosomes which should be selected.
///
/// This method is not thread-safe.</summary>
/// <param name="size">new number of chromosomes for selection.</param>
inline void GACALL SetSelectionSize(int size) { _selectionSize = size < 0 ? 0 : size; }
};// END CLASS DEFINITION GaSelectionParams
/// <summary>This class is used as storage for selection operation (result set). It uses sorted group to store indices of 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 GaSelectionResultSet
{
protected:
/// <summary>Pointer to sorted group which contains indices of selected chromosomes.</summary>
GaSortedGroup* _selectedGroup;
public:
/// <summary>This constructor initializes sorted group of result set which stores selected chromosomes.</summary>
/// <param name="selectionSize">number of chromosomes which can be stored by sorted group.</param>
/// <param name="population">pointer to population on which selection is performed.</param>
GAL_API
GaSelectionResultSet(int selectionSize,
const GaPopulation* population);
/// <summary>Destructor frees memory used by sorted group which stores selected chromosomes.</summary>
~GaSelectionResultSet() { delete _selectedGroup; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns reference to sorted group which stores selected chromosomes.</returns>
inline GaSortedGroup& GACALL SelectedGroup() const { return *_selectedGroup; }
/// <summary><c>GetAt</c> method returns smart pointer to chromosomes at given position in result set.
///
/// This method is not thread safe.</summary>
/// <param name="pos">position of chromosomes in result set.</param>
/// <returns>Method returns smart pointer to chromosome at given position in set.</returns>
inline GaChromosomePtr GACALL GetAt(int pos) const { return _selectedGroup->GetChromosomeAt( pos ); }
/// <summary><c>operator []</c> returns smart pointer to chromosomes at given position in result set.
///
/// This operator is not thread safe.</summary>
/// <param name="pos">position of chromosomes in result set.</param>
/// <returns>Operator returns smart pointer to chromosome at given position in set.</returns>
inline GaChromosomePtr GACALL operator [](int pos) const { return _selectedGroup->GetChromosomeAt( pos ); }
};// END CLASS DEFINITION GaSelectionResultSet
/// <summary>This class is interface for selection operation which selects chromosomes from population which
/// are going to be used as parents in production of new 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 GaSelectionOperation : public GaOperation
{
public:
/// <summary><c>operator ()</c> performs replacement operation.</summary>
/// <param name="population">reference to population from which chromosomes are selected.</param>
/// <param name="parameters">reference to parameters of selection operation.</param>
/// <param name="result">pointer to result set which is used to store selected chromosomes.</param>
virtual void GACALL operator ()(const GaPopulation& population,
const GaSelectionParams& parameters,
GaSelectionResultSet& result) const=0;
/// <summary><c>MakeResultSet</c> method makes result set used by selection operation for storing chromosomes indices.
/// Caller is responsible for memory allocated by this method for result set object.
///
/// This method is thread-safe.</summary>
/// <param name="population">pointer to population to which result set is bound.</param>
/// <param name="parameters">reference to parameters of selection operation.</param>
/// <returns>Method returns pointer to newly created result set.</returns>
virtual GaSelectionResultSet* GACALL MakeResultSet(GaPopulation* population,
const GaSelectionParams& parameters) const { return new GaSelectionResultSet( parameters.GetSelectionSize(), population ); }
};// END CLASS DEFINITION GaSelectionOperation
/// <summary><c>GaSelectionPair</c> type is instance of <see cref="GaOperationParametersPair" /> template class and represents pair of
/// selection operation and its parameters. Detailed description can be found in specification of
/// <see cref="GaOperationParametersPair" /> template class.</summary>
typedef GaOperationParametersPair<GaSelectionOperation, GaSelectionParams> GaSelectionPair;
/// <summary><c>GaSelectionCatalogue</c> type is instance of <see cref="GaCatalogue" /> template class and represents catalogue of selection operations.
/// Detailed description can be found in specification of <see cref="GaCatalogue" /> template class.</summary>
typedef GaCatalogue<GaSelectionOperation> GaSelectionCatalogue;
/// <summary>This class is base for parameters of coupling 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 GaCouplingParams : public GaParameters
{
private:
/// <summary>Number of chromosomes which should be produces.</summary>
int _numberOfOffsprings;
public:
/// <summary>This constructor initializes coupling parameters with user-defined number of produced offspring size.</summary>
/// <param name="numberOfOffsprings">number of chromosomes which should be produced.</param>
GaCouplingParams(int numberOfOffsprings) { SetNumberOfOffsprings( numberOfOffsprings ); }
/// <summary>This constructor initializes coupling parameters with default values. Default number of produced offspring is 2.</summary>
GaCouplingParams() : _numberOfOffsprings(0) { }
/// <summary>More details are given in specification of <see cref="GaParameters::Clone" /> method.</summary>
virtual GaParameters* GACALL Clone() const { return new GaCouplingParams( *this ); }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns number of chromosomes which should be produced.</returns>
inline int GACALL GetNumberOfOffsprings() const { return _numberOfOffsprings; }
/// <summary><c>SetNumberOfOffsprings</c> method sets number of chromosomes which should be produced.
///
/// This method is not thread-safe.</summary>
/// <param name="number">new number of chromosomes which should be produced.</param>
inline void GACALL SetNumberOfOffsprings(int number) { _numberOfOffsprings = number < 0 ? 0 : number; }
};// END CLASS DEFINITION GaCouplingParams
/// <summary>This class is used as storage for coupling operation (result set). Result set contains pointer to selection result set which
/// stores parent chromosomes. It also contains array of newly produced chromosomes and array of their parents' indices.
///
/// 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 GaCouplingResultSet
{
private:
/// <summary>Pointer to array of smart pointers which are point to newly produced offspring.</summary>
GaChromosomePtr* _offsprings;
/// <summary>Array of parents' indices of newly created offspring.</summary>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -