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

📄 multivaluechromosome.h

📁 遗传算法的四个源程序和源代码
💻 H
📖 第 1 页 / 共 2 页
字号:

/*! \file MultiValueChromosome.h
    \brief This file contains declaration and implementation of multi-value chromosome classes (with and without support for arithmetic crossover operations).
*/

/*
 * 
 * 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_MULTI_VALUE_CHROMOSOME_H__
#define __GA_MULTI_VALUE_CHROMOSOME_H__

#include <vector>
#include "Chromosome.h"
#include "Platform.h"
#include "RepresentationInterfaces.h"
#include "DomainChromosome.h"

using namespace std;
using namespace Chromosome;
using namespace Chromosome::Representation;

namespace Chromosome
{
	namespace Representation
	{

		/// <summary>This class is used by <see cref="GaMultiValueChromosome" /> template class for extracting of single values from chromosome's code
		/// and other manipulation on chromosome's code.
		///
		/// 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>
		/// <param name="TYPE">type of value.</param>
		template <typename TYPE>
		class GaChromosomeValue : public GaCodeValue
		{

		private:

			/// <summary>Single value of chromosome's code.</summary>
			TYPE _value;

		public:

			/// <summary>Detailed description can be found in specification of <see cref="GaCodeValue::Initialize" />.</summary>
			virtual void GACALL Initialize() { }

			/// <summary>Detailed description can be found in specification of <see cref="GaCodeValue::FromBuffer" />.</summary>
			virtual void GACALL FromBuffer(const GaCodeValuesBuffer& buffer,
				int pos)
			{
				if( pos >= 0 && pos < ( buffer.GetPosition() / (int)sizeof( TYPE ) ) )
					_value = ( (TYPE*)buffer.GetBuffer() )[ pos ];
			}

			/// <summary>This constructor initializes value.</summary>
			/// <param name="value">user-defined value.</param>
			GaChromosomeValue(TYPE value) : _value(value) { }

			/// <summary>This constructor calls <see cref="Initialize" /> method.</summary>
			GaChromosomeValue() { Initialize(); }

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

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

		};// END CLASS DEFINITION GaChromosomeValue

		/// <summary>This class can be used for chromosomes which code has multiple values. Values can be of any type, but all values must have same type,
		/// they must use same value set and must obey requirements introduced by the value set. 
		///
		/// 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>
		/// <param name="TYPE">type of value in chromosome's code.</param>
		template <typename TYPE>
		class GaMultiValueChromosome : public GaDomainChromosome<TYPE>,
			public GaSwapableCode,
			public GaSizableCode,
			public GaMutableCode,
			public GaMultiValueCode
		{

		protected:

			/// <summary>Chromosome's code.</summary>
			vector<TYPE> _values;

			/// <summary>Backup copy chromosome's code before mutation if improving-only mutation flag is set in chromosome's parameters.</summary>
			vector<TYPE> _backup;

		public:

			/// <summary>More details are given in specification of <see cref="GaSizableCode::Remove" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual void GACALL Remove(int start,
				int size)
			{
				if( size > 0 && start >=0 && start < (int)_values.size() )
					_values.erase( _values.begin() + start, _values.begin() + ( start + size ) );
			}

			/// <summary>More details are given in specification of <see cref="GaSizableCode::Insert" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual void GACALL Insert(int start,
				GaCodeValue* data,
				int size)
			{
				if( !data || size <= 0 || start < 0 || start > (int)_values.size() )
					return;

				// insert at the end
				if( start == _values.size() )
				{
					for( int i = 0; i < size; i++ )
						_values.push_back( GetClosestValue( ( (GaChromosomeValue<TYPE>*)data )[ i ].GetValue() ) );
				}
				else
				{
					vector<TYPE> tmp;
					tmp.reserve( size );
					for( int i = 0; i < size; i++ )
						tmp.push_back( GetClosestValue( ( (GaChromosomeValue<TYPE>*)data )[ i ].GetValue() ) );

					_values.insert( _values.begin() + start, tmp.begin(), tmp.end() );
				}
			}

			/// <summary>If swapping two parts overlap, position of second part is moved after the end of first part.
			///
			/// More details are given in specification of <see cref="GaSwapableCode::Swap" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual void GACALL Swap(int start1,
				int size1,
				int start2,
				int size2)
			{
				// check params
				if( start1 < 0 || start1 >= (int)_values.size() || start2 < 0 || start2 >= (int)_values.size()
					|| size1 <= 0 || size2 <= 0 || !_values.size() || start1 == start2 )
					return;

				// reorder swapping points (start1 is first, start2 is second)
				if( start2 < start1 )
				{
					int t = start2;
					start2 = start1;
					start1 = t;

					t = size2;
					size2 = size1;
					size1 = t;
				}

				// prevent overlapping of swap sequences
				if( start1 + size1 >= start2 )
				{
					int old = start2;
					start2 = start1 + size1;
					size2 -= start2 - old;
				}

				// temp buffer for placing new code
				vector<TYPE> newCode;
				newCode.reserve( _values.size() );

				// copy values before first swapping sequence
				newCode.insert( newCode.begin(), _values.begin(), _values.begin() + start1 );
				// place second swapping sequence to new location
				newCode.insert( newCode.end(),
					_values.begin() + start2, _values.begin() + ( start2 + size2 ) );
				// copy values between two swapping sequence
				newCode.insert( newCode.end(), 
					_values.begin() + ( start1 + size1 ), _values.begin() + start2 );
				// place first swapping sequence to new location
				newCode.insert( newCode.end(),
					_values.begin() + start1, _values.begin() + ( start1 + size1 ) );
				// copy values after second swapping sequence
				newCode.insert( newCode.end(), 
					_values.begin() + ( start2 + size2 ),  _values.end() );

				// save new code
				_values = newCode;
			}

			/// <summary>More details are given in specification of <see cref="GaMutableCode::Flip" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual void GACALL Flip(int start,
				int size)
			{
				if( size <= 0 || start < 0 || start >= (int)_values.size() )
					return;

				typename vector<TYPE>::iterator it = _values.begin() + start;
				for( int i = 0; i < size; i++, it++ )
					*it = ( (GaChromosomeDomainBlock<TYPE>*)this->_configBlock )->_domain->GenerateRandom();
			}

			/// <summary>More details are given in specification of <see cref="GaMutableCode::Invert" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual void GACALL Invert(int start,
				int size)
			{
				if( size <= 0 || start < 0 || start >= (int)_values.size() )
					return;

				typename vector<TYPE>::iterator it = _values.begin() + start;
				for( int i = 0; i < size; i++, it++ )
					( (GaChromosomeDomainBlock<TYPE>*)this->_configBlock )->_domain->Inverse( *it, *it );
			}

			/// <summary>More details are given in specification of <see cref="GaMultiValueCode::MakeBuffer" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual GaCodeValuesBuffer* GACALL MakeBuffer(int size) const { return new GaCodeValuesBuffer( size * sizeof( TYPE ) ); }

			/// <summary>More details are given in specification of <see cref="GaMultiValueCode::FillBuffer" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual void GACALL FillBuffer(int pos,
				int size,
				GaCodeValuesBuffer& buffer) const
			{
				// check params
				if( size <= 0 || pos < 0 || pos >= (int)_values.size() )
					return;

				// get buffer params
				int s = pos + size < (int)_values.size() ? size : (int)_values.size() - pos;
				TYPE* b = (TYPE*)buffer.GetBuffer();
				int p = buffer.GetPosition() / sizeof( TYPE );

				// save to buffer
				for( int i = 0; i < s; i++ )
					b[ p + i ] = _values[ pos + i ];

				// move buffer pointer
				buffer.Move( s * sizeof( TYPE ) );
			}

			/// <summary>More details are given in specification of <see cref="GaMultiValueCode::FromBuffer" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual void GACALL FromBuffer(const GaCodeValuesBuffer& buffer)
			{
				_values.clear();

				// size of code
				int size = buffer.GetPosition() / sizeof( TYPE );
				_values.reserve( size );

				// copy from buffer to code
				for( int i = 0; i < size; i++ )
					_values.push_back( ( (TYPE*)buffer.GetBuffer() )[ i ] );
			}

			/// <summary>This constructor initializes chromosome with CCB and user-defined code.</summary>
			/// <param name="values">values of chromosome's code.</param>
			/// <param name="size">size of chromosome's code.</param>
			/// <param name="configBlock">pointer to CCB.</param>
			GaMultiValueChromosome(TYPE* values,
				int size,
				GaChromosomeDomainBlock<TYPE>* configBlock) : GaDomainChromosome<TYPE>(configBlock)
			{
				if( size > 0 && values )
				{
					_values.reserve( size );

⌨️ 快捷键说明

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