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

📄 binarychromosome.h

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

/*! \file BinaryChromosome.h
    \brief This file contains declaration of binary chromosome and its parameters 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_BINARY_CHROMOSOME_H__
#define __GA_BINARY_CHROMOSOME_H__

#include "Platform.h"
#include "Chromosome.h"
#include "RepresentationInterfaces.h"

using namespace Chromosome;
using namespace Chromosome::Representation;

namespace Chromosome
{
	namespace Representation
	{

		/// <summary>This class is used by <see cref="GaBinaryChromosome" /> 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>
		class GaBit : public GaCodeValue
		{

		private:

			/// <summary>This attribute stores state of the bit.</summary>
			bool _bitState;

		public:

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

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

			/// <summary>This constructor initializes bit with user-defined state.</summary>
			/// <param name="state">state of the bit.</param>
			GaBit(bool state) : _bitState(state) { }

			/// <summary>This constructor initializes bit with random state. It calls <see cref="Initialize" /> method.</summary>
			GaBit() { Initialize(); }

			/// <summary>This method is not thread-safe.</summary>
			/// <returns>Method returns state of bit.</returns>
			inline bool GACALL GetBitState() const { return _bitState; }

			/// <summary><c>SetBitState</c> method sets state of the bit. 
			///
			/// This method is not thread-safe.</summary>
			/// <param name="state">new state.</param>
			inline void GACALL SetBitState(bool state) { _bitState = state; }

		};// END CLASS DEFINITION GaBit

		/// <summary>This class is used by <see cref="GaBinaryChromosome" /> class as 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 GaBinaryChromosomeParams : public Chromosome::GaChromosomeParams
		{

		private:

			/// <summary>Probability in interval (0, 1) of bit抯 set state.</summary>
			float _probabilityOfSetState;

		public:

			/// <summary>This constructor initializes parameters with user-defined values.</summary>
			/// <param name="probabiltyOfSetState">probability of bit's set state (0,1).</param>
			/// <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>
			GaBinaryChromosomeParams(float probabiltyOfSetState,
				float mutationProbability,
				int mutationSize,
				bool improvingOnlyMutations,
				float crossoverProbability,
				int numberOfCrossoverPoints) : GaChromosomeParams(mutationProbability, mutationSize,
				improvingOnlyMutations, crossoverProbability, numberOfCrossoverPoints),
				_probabilityOfSetState(probabiltyOfSetState) { }

			/// <summary>This constructor initializes parameters with default values. Default probability of set state is 0.5,
			/// for othere values see specification of <see cref="GaChromosomeParams" /> class.</summary>
			GaBinaryChromosomeParams() : _probabilityOfSetState(0.5) { }

			/// <summary>This method is not thread-safe.</summary>
			/// <returns>Method returns probability in interval (0, 1) of bit's set state.</returns>
			inline float GetProbabilityOfSetState() const { return _probabilityOfSetState; }

			/// <summary><c>SetProbabilityOfSetState</c> method sets probability of bit's set state.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="probability">probability in interval (0, 1).</param>
			inline void SetProbabilityOfSetState(float probability) { _probabilityOfSetState = probability; }

		};// END CLASS DEFINITION GaBinaryChromosomeParams

		/// <summary>This class can be used for solutions of problems which can be encoded as array of bits.
		///
		/// 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 GaBinaryChromosome : public GaDynamicOperationChromosome,
			public GaSwapableCode,
			public GaSizableCode,
			public GaMutableCode,
			public GaMultiValueCode
		{

		protected:

			/// <summary>Array of booleans which represents binary code. </summary>
			bool* _bits;

			/// <summary>Number of bits in code.</summary>
			int _codeSize;

			/// <summary>Array of backed-up bits of chromosome's code.</summary>
			bool* _backupBits;

			/// <summary>Number of bits in backed-up code.</summary>
			int _backupSize;

		public:

			/// <summary>More details are given in specification of <see cref="GaSizableCode::Remove" /> method.
			///
			/// This method is not thread-safe.</summary>
			GAL_API
			virtual void GACALL Remove(int start,
				int size);

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

			/// <summary>More details are given in specification of <see cref="GaSwapableCode::Swap" /> method.
			///
			/// This method is not thread-safe.</summary>
			GAL_API
			virtual void GACALL Swap(int start1,
				int size1,
				int start2,
				int size2);

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

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

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

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

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

			/// <summary>This constructor initializes chromosome with CCB.</summary>
			/// <param name="configBlock">pointer to CCB.</param>
			GAL_API
			GaBinaryChromosome(GaChromosomeOperationsBlock* configBlock);

			/// <summary>This constructor initializes chromosome with CCB and makes random chromosome's code with defined size.</summary>
			/// <param name="size">size of chromosome's code.</param>
			/// <param name="configBlock">pointer to CCB.</param>
			GAL_API
			GaBinaryChromosome(int size,
				GaChromosomeOperationsBlock* configBlock);

			/// <summary>This constructor initializes chromosome with CCB and its code.</summary>
			/// <param name="code">array of bits which is copied to chromosome's code.</param>
			/// <param name="size">size of chromosome's code.</param>
			/// <param name="configBlock">pointer to CCB.</param>
			GAL_API
			GaBinaryChromosome(bool* code,
				int size,
				GaChromosomeOperationsBlock* configBlock);

			/// <summary>This is copy constructor.</summary>
			/// <param name="c">reference to chromosome which is copied.</param>
			/// <param name="setupOnly">if this parameter is <c>true</c>, only pointer to CCB is copied. If this parameter is <c>false</c>,

⌨️ 快捷键说明

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