📄 stopcriterias.h
字号:
/*! \file StopCriterias.h
\brief This file declares classes of classes that implement stop criterias and their parameters.
*/
/*
*
* 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_STOP_CRITERIAS_H__
#define __GA_STOP_CRITERIAS_H__
#include <math.h>
#include "Platform.h"
#include "AlgorithmOperations.h"
using namespace Algorithm;
namespace Algorithm
{
/// <summary>Contains implementation of stop criterias used by genetic algorithms.</summary>
namespace StopCriterias
{
/// <summary><c>GaGenerationCriteriaParams</c> class is used by <see cref="GaGenerationCriteria" /> class as parameters for the criteria.
/// It contains number of generation after which execution of algorithm should be stopped.
///
/// 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 GaGenerationCriteriaParams : public GaStopCriteriaParams
{
private:
/// <summary>Number of generations after which algorithm should stops its execution.</summary>
int _numberOfGenerations;
public:
/// <summary>This constructor initializes parameters with user-defined number of generations.</summary>
/// <param name="numberOfGeneration">number of generations after which algorithm should stops its execution.</param>
GaGenerationCriteriaParams(int numberOfGeneration) : _numberOfGenerations(numberOfGeneration) { }
/// <summary>This constructor initializes parameters with default values. Default number of generations is 1000.</summary>
GaGenerationCriteriaParams() : _numberOfGenerations(1000) { }
/// <summary>More details are given in specification of <see cref="GaParameters::Clone" /> method.</summary>
virtual GaParameters* GACALL Clone() const { return new GaGenerationCriteriaParams( *this ); }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns number of generations after which algorithm should stops its execution.</returns>
inline int GACALL GetNumberOfGeneration() const { return _numberOfGenerations; }
/// <summary><c>SetNumberOfGeneration</c> method sets number of generations after which algorithm should stops its execution.
///
/// This method is not thread-safe.</summary>
/// <param name="number">number of generations.</param>
inline void GACALL SetNumberOfGeneration(int number) { _numberOfGenerations = number; }
};// END CLASS DEFINITION GaGenerationCriteriaParams
/// <summary><c>GaGenerationCriteria</c> is used to stop genetic algorithm when it reaches specified number of generations.
/// This stop criteria uses <see cref="GaGenerationCriteriaParams" /> class as 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.
/// Because this genetic operation is stateless all public method are thread-safe.</summary>
class GaGenerationCriteria : public GaStopCriteria
{
public:
/// <summary>More details are given in specification of <see cref="GaStopCriteria::Evaluate" /> method.
///
/// This method is thread-safe.</summary>
GAL_API
virtual bool GACALL Evaluate(const GaAlgorithm& algorithm,
const GaStopCriteriaParams& parameters) const;
/// <summary>More details are given in specification of <see cref="GaOperation::MakeParameters" /> method.
///
/// This method is thread-safe.</summary>
/// <returns>Method makes new instance of <see cref="GaGenerationCriteriaParams" /> class.</returns>
virtual GaParameters* GACALL MakeParameters() const { return new GaGenerationCriteriaParams(); }
/// <summary>Valid parameters must have number of generations grater then 0.
///
/// More details are given in specification of <see cref="GaOperation::CheckParameters" /> method.
///
/// This method is thread-safe.</summary>
virtual bool GACALL CheckParameters(const GaParameters& parameters) const
{
return ( (const GaGenerationCriteriaParams&) parameters ).GetNumberOfGeneration() > 0;
}
};// END CLASS DEFINITION GaGenerationCriteria
/// <summary>This enumeration is used by stop criterias which are based on fitness values to specify type of comparison
/// of current and desired values.</summary>
enum GaFitnessCriteriaComparison
{
/// <summary>comparison should return <c>true</c> if current value is less then desired.</summary>
GFC_LESS_THEN = 0x1,
/// <summary>comparison should return <c>true</c> if current value is greater then desired value.</summary>
GFC_MORE_THEN = 0x2,
/// <summary>comparison should return <c>true</c> if current value and desired value are equal.</summary>
GFC_EQUALS_TO = 0x4,
/// <summary>comparison should return <c>true</c> if current value is less then or equals to desired value.</summary>
GFC_LESS_THEN_EQUALS_TO = GFC_LESS_THEN | GFC_EQUALS_TO,
/// <summary>comparison should return <c>true</c> if current value is greater then or equals to desired value.</summary>
GFC_MORE_THEN_EQUALS_TO = GFC_MORE_THEN | GFC_EQUALS_TO
};
/// <summary><c>GaFitnessCriteriaParams</c> class is used by <see cref="GaFitnessCriteria" /> class as parameters for the criteria.
/// It contains desired fitness value of specified type and it also specifies type of comparison which is used to compare desired and
/// current 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 GaFitnessCriteriaParams : public GaStopCriteriaParams
{
private:
/// <summary>Stores desired fitness value for stopping the algorithm.</summary>
float _fitnessLimit;
/// <summary>Type of comparison of desired value and current value. </summary>
GaFitnessCriteriaComparison _comparison;
/// <summary>Type of fitness values which are compared.</summary>
GaStatValueType _valueType;
public:
/// <summary>This constructor initializes parameters with user-defined values.</summary>
/// <param name="fitnessLimit">desired fitness value.</param>
/// <param name="comparison">type of comparison of desired and current fitness values.</param>
/// <param name="valueType">type of fitness values which are compared.</param>
GaFitnessCriteriaParams(float fitnessLimit,
GaFitnessCriteriaComparison comparison,
GaStatValueType valueType) : _fitnessLimit(fitnessLimit),
_comparison(comparison),
_valueType(valueType) { }
/// <summary>This constructor initializes parameters with default values. Default desired fitness value is 1, type of comparison is
/// <c>GFC_EQUALS_THEN</c> and type of fitness value is <c>GSV_BEST_FITNESS_SCALED</c>.</summary>
GaFitnessCriteriaParams() : _fitnessLimit(1),
_comparison(GFC_EQUALS_TO),
_valueType(GSV_BEST_FITNESS_SCALED) { }
/// <summary>More details are given in specification of <see cref="GaParameters::Clone" /> method.</summary>
virtual GaParameters* GACALL Clone() const { return new GaFitnessCriteriaParams( *this ); }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns desired fitness value.</returns>
inline float GACALL GetFitnessLimit() const { return _fitnessLimit; }
/// <summary><c>SetFitnessLimit</c> method sets desired fitness value.
///
/// This method is not thread-safe.</summary>
/// <param name="limit">desired fitness value.</param>
inline void GACALL SetFitnessLimit(float limit) { _fitnessLimit = limit; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns type of comparison of desired value and current value.</returns>
inline GaFitnessCriteriaComparison GACALL GetComparison() const { return _comparison; }
/// <summary><c>SetComparation</c> method sets type of comparison of desired value and current value.
///
/// This method is not thread-safe.</summary>
/// <param name="comparison">type of comparison.</param>
inline void GACALL SetComparison(GaFitnessCriteriaComparison comparison) { _comparison = comparison; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns type of fitness values which are compared.</returns>
inline GaStatValueType GACALL GetValueType() const { return _valueType; }
/// <summary><c>SetValueType</c> method sets type of fitness values which are compared.
///
/// This method is not thread-safe.</summary>
/// <param name="type">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -