⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 valuesets.h

📁 遗传算法做图像的模式匹配
💻 H
📖 第 1 页 / 共 3 页
字号:

/*! \file ValueSets.h
    \brief This file declares and implements value sets classes.
*/

/*
 * 
 * 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_VALUE_SETS_H__
#define __GA_VALUE_SETS_H__

#include <vector>
#include "Platform.h"
#include "GlobalRandomGenerator.h"
#include "Threading.h"

using namespace std;

namespace Chromosome
{
	namespace Representation
	{

		/// <summary>This template class is base class for all value set. Value sets store values which can be found in chromosomes' codes. 
		/// 
		/// This class has built-in synchronizator so it is allowed to use <c>LOCK_OBJECT</c> and <c>LOCK_THIS_OBJECT</c> macros
		/// with instances of this class, but no public or private methods are thread-safe.</summary>
		/// <param name="T">type of data in value set.</param>
		template <typename T>
		class GaValueSet
		{
			DEFINE_SYNC_CLASS

		protected:

			/// <summary>This attribute indicates that membership of original sets of values and inverted set of values are treated equally.
			/// If this attribute is set to <c>true</c>, inverted values are treated as members of the set, if it is set to <c>false</c>,
			/// inverted values are not treated as members of the set.</summary>
			bool _viceVersa;

		public:

			/// <summary>This constructor initializes value set with user-defined treatment of inverted values.</summary>
			/// <param name="viceVersa">if this parameter is set to <c>true</c>, inverted values are treated as members of the set,
			/// if it is set to <c>false</c>, inverted values are not treated as members of the set.</param>
			GaValueSet(bool viceVersa) : _viceVersa(viceVersa) { }

			/// <summary>This constructor initializes value set with inverted values as members of the set.</summary>
			GaValueSet() : _viceVersa(true) { }

			/// <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 ~GaValueSet() { }

			/// <summary>This method returns randomly chosen value from the set.</summary>
			/// <returns>Method returns randomly chosen value from the set.</returns>
			virtual T GACALL GenerateRandom() const=0;

			/// <summary><c>Inverse</c> method finds inverted value and stores it into inverted.</summary>
			/// <param name="value">value which should be inverted.</param>
			/// <param name="inverted">reference to variable to which inverted value is going to be stored.</param>
			/// <returns>Method returns <c>true</c> if inversion is successful.
			/// If specified value is not in set or it cannot be inverted it returns <c>false</c>.</returns>
			virtual bool GACALL Inverse(const T& value,
				T& inverted) const=0;

			/// <summary><c>Belongs</c> method checks membership of the value in this value set.</summary>
			/// <param name="value">value which is checked.</param>
			/// <returns>Method returns <c>true</c> if the value is member of the set.</returns>
			virtual bool GACALL Belongs(const T& value) const=0;

			/// <summary><c>ClosestValue</c> method returns nearest value which can be found in value set to specified value.</summary>
			/// <param name="value">value which nearest neighbor should be found.</param>
			/// <returns>Method returns reference to nearest value in value set to specified value.</returns>
			virtual const T& GACALL ClosestValue(const T& value) const=0;

			/// <summary>This method is not thread-safe.</summary>
			/// <returns>Method returns <c>true</c> if both, original values and inverted values, treated as members of the set, or returns <c>false</c>,
			/// if inverted values are not treated as members.</returns>
			inline bool GACALL GetViceVersa() const { return _viceVersa; }

			/// <summary><c>SetViceVersa</c> method sets treatment of inverted values.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="viceVersa">if this parameter is set to <c>true</c>, inverted values are treated as members of the set, if it is set to <c>false</c>,
			/// inverted values are not treated as members of the set.</param>
			inline void GACALL SetViceVersa(bool viceVersa) { _viceVersa = viceVersa; }

		};// END CLASS DEFINITION GaValueSet

		/// <summary>This template represents value set with only one value and its counterpart (inverted value).
		/// 
		/// This class has built-in synchronizator so it is allowed to use <c>LOCK_OBJECT</c> and <c>LOCK_THIS_OBJECT</c> macros
		/// with instances of this class, but no public or private methods are thread-safe.</summary>
		/// <param name="T">type of data in value set.</param>
		template <typename T>
		class GaSingleValueSet : public GaValueSet<T>
		{

		private:

			/// <summary>Original value of the set.</summary>
			T _value;

			/// <summary><see cref="_value" />'s counterpart (inverted value) in the set.</summary>
			T _invertedValue;

		public:

			/// <summary>This constructor initializes value set with value and its counterpart.</summary>
			/// <param name="value">original value.</param>
			/// <param name="invertedValue">inverted value.</param>
			/// <param name="viceVersa">if this parameter is set to <c>true</c>, inverted values are treated as members of the set,
			/// if it is set to <c>false</c>, inverted values are not treated as members of the set.</param>
			GaSingleValueSet(const T& value,
				const T& invertedValue,
				bool viceVersa) : GaValueSet<T>(viceVersa),
				_value(value),
				_invertedValue(invertedValue) { }

			/// <summary>More details are given in specification of <see cref="GaValueSet::GenerateRandom" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual T GACALL GenerateRandom() const { return !this->_viceVersa || GaGlobalRandomBoolGenerator->Generate() ? this->_value : _invertedValue; }

			/// <summary>More details are given in specification of <see cref="GaValueSet::Inverse" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual bool GACALL Inverse(const T& value,
				T& inverted) const
			{
				if( value == _value )
				{
					inverted = _invertedValue;
					return true;
				}

				if( this->_viceVersa && value == _invertedValue )
				{
					inverted = _value;
					return true;
				}

				return false;
			}

			/// <summary>More details are given in specification of <see cref="GaValueSet::Belongs" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual bool GACALL Belongs(const T& value) const { return value == _value || ( this->_viceVersa && value == _invertedValue ); }

			/// <summary>More details are given in specification of <see cref="GaValueSet::ClosestValue" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual const T& GACALL ClosestValue(const T& value) const { return _value; }

			/// <summary>This method is not thread-safe.</summary>
			/// <returns>Method returns original value.</returns>
			inline const T& GACALL GetValue() const { return _value; }

			/// <summary>This method is not thread-safe.</summary>
			/// <returns>Method returns inverted value of the original.</returns>
			inline const T& GACALL GetInvertedValue() const { return _invertedValue; }

			/// <summary><c>GetValue</c> method returns original value and its counterpart.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="original">reference to variable to which original value will be stored.</param>
			/// <param name="inverted">reference to variable to which inverted value will be stored.</param>
			inline void GACALL GetValue(T& original,
				T& inverted)
			{
				original = _value;
				inverted = _invertedValue;
			}

			/// <summary><c>SetValue</c> method sets original value.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="value">new original value.</param>
			inline void GACALL SetValue(const T& value) { _value = value; }

			/// <summary><c>SetValue</c> method sets original value and its counterpart.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="original">new original value.</param>
			/// <param name="inverted">new inverted value.</param>
			inline void GACALL SetValue(const T& original,
				const T& inverted)
			{
				_value = original;
				_invertedValue = inverted;
			}

			/// <summary><c>SetValue</c> method sets inverted value.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="value">new inverted value.</param>
			inline void GACALL SetInvertedValue(const T& value) { _invertedValue = value; }

		};// END CLASS DEFINITION GaSingleValueSet

		/// <summary>This template represents value set with multiple values and their counterpart (inverted values).
		/// 
		/// This class has built-in synchronizator so it is allowed to use <c>LOCK_OBJECT</c> and <c>LOCK_THIS_OBJECT</c> macros
		/// with instances of this class, but no public or private methods are thread-safe.</summary>
		/// <param name="T">type of data in value set.</param>
		template <typename T>
		class GaMultiValueSet : public GaValueSet<T>
		{

		private:

			/// <summary>Array of original values.</summary>
			vector<T> _values;

			/// <summary>array of counterparts of values in <c>_values</c> (inverted values).</summary>
			vector<T> _invertedValues;

		public:

			/// <summary>This constructor initializes empty value set with user-defined treatment of inverted values.</summary>
			/// <param name="viceVersa">if this parameter is set to <c>true</c>, inverted values are treated as members of the set,
			/// if it is set to <c>false</c>, inverted values are not treated as members of the set.</param>
			GaMultiValueSet(bool viceVersa) : GaValueSet<T>( viceVersa ) { }

			/// <summary>This constructor initializes empty value set with inverted values as members of the set.</summary>
			GaMultiValueSet() { }

			/// <summary>More details are given in specification of <see cref="GaValueSet::GenerateRandom" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual T GACALL GenerateRandom() const
			{
				return !this->_viceVersa || GaGlobalRandomBoolGenerator->Generate()
					? _values[ GaGlobalRandomIntegerGenerator->Generate( (int)_values.size() ) ]
					: _invertedValues[ GaGlobalRandomIntegerGenerator->Generate( (int)_invertedValues.size() ) ];
			}

			/// <summary>More details are given in specification of <see cref="GaValueSet::Inverse" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual bool GACALL Inverse(const T& value,
				T& inverted) const
			{
				int pos = 0;
				for( typename vector<T>::const_iterator it = _values.begin(); it != _values.end(); it++, pos++ )
				{
					if( value == *it )
					{
						inverted = _invertedValues[ pos ];
						return true;
					}
				}

				if( this->_viceVersa )
				{
					pos = 0;
					for( typename vector<T>::const_iterator it = _invertedValues.begin(); it != _invertedValues.end(); it++, pos++ )
					{
						if( value == *it )
						{
							inverted = _values[ pos ];
							return true;
						}
					}
				}

				return false;
			}

			/// <summary>More details are given in specification of <see cref="GaValueSet::Belongs" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual bool GACALL Belongs(const T& value) const
			{
				for( typename vector<T>::const_iterator it = _values.begin(); it != _values.end(); it++ )
				{
					if( value == *it )
						return true;
				}

				if( this->_viceVersa )
				{
					for( typename vector<T>::const_iterator it = _invertedValues.begin(); it != _invertedValues.end(); it++ )
					{
						if( value == *it )
							return true;
					}
				}

				return false;
			}

			/// <summary>More details are given in specification of <see cref="GaValueSet::ClosestValue" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual const T& GACALL ClosestValue(const T& value) const
			{
				const T* closest = &_values[ 0 ];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -