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

📄 domainchromosome.h

📁 遗传算法的四个源程序和源代码
💻 H
字号:

/*! \file DomainChromosome.h
    \brief This file contains declaration and implementation of abstract class for chromosomes and CCB that have value sets.
*/

/*
 * 
 * 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_DOMAIN_CHROMOSOME_H__
#define __GA_DOMAIN_CHROMOSOME_H__

#include "Chromosome.h"
#include "Platform.h"
#include "ValueSets.h"

using namespace Chromosome;
using namespace Chromosome::Representation;

namespace Chromosome
{
	namespace Representation
	{

		template <typename T>
		class GaDomainChromosome;
		
		template <typename T>
		class GaSingleValueChromosome;

		template <typename T>
		class GaSVArithmeticChromosome;
		
		template <typename T>
		class GaMultiValueChromosome;

		template <typename T>
		class GaMVArithmeticChromosome;

		/// <summary>This class is CCB for chromosomes which have codes with values which must bi in defined domain. This CCB stores pointer to 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="T">type of data used by value set.</param>
		template <typename T>
		class GaChromosomeDomainBlock : public GaChromosomeOperationsBlock
		{
			friend class GaDomainChromosome<T>;
			friend class GaSingleValueChromosome<T>;
			friend class GaSVArithmeticChromosome<T>;
			friend class GaMultiValueChromosome<T>;
			friend class GaMVArithmeticChromosome<T>;

		protected:

			/// <summary>Pointer to values set.</summary>
			GaValueSet<T>* _domain;

		public:

			/// <summary>This constructor initializes CCB with pointer to chromosomes' parameters, extern genetic operations and value set.</summary>
			/// <param name="domain">pointer to chromosomes' value set.</param>
			/// <param name="crossover">pointer to extern crossover operation.</param>
			/// <param name="mutation">pointer to extern mutation operation.</param>
			/// <param name="fitnessOperation">pointer to extern fitness operation.</param>
			/// <param name="fitnessComparator">pointer to extern fitness comparator.</param>
			/// <param name="parameters">pointer to chromosomes' parameters.</param>
			GaChromosomeDomainBlock(GaValueSet<T>* domain,
				GaCrossoverOperation* crossover,
				GaMutationOperation* mutation,
				GaFitnessOperation* fitnessOperation,
				GaFitnessComparator* fitnessComparator,
				GaChromosomeParams* parameters) : GaChromosomeOperationsBlock(crossover, mutation, fitnessOperation, fitnessComparator, parameters),
					_domain(domain) { }

			/// <summary>This is copy constructor. The constructor doesn't create copy of parameters', operations' and value set's objects,
			///it only copies pointer to chromosomes' parameters, operations and value set.</summary>
			/// <param name="rhs">reference to CCB which is copied.</param>
			GaChromosomeDomainBlock(const GaChromosomeDomainBlock& rhs) : GaChromosomeOperationsBlock(rhs),
				_domain(rhs._domain) { }

			/// <summary>This constructor initializes empty CCB.</summary>
			GaChromosomeDomainBlock() : _domain(NULL) { }

			/// <summary>This method is not thread-safe.</summary>
			/// <returns>Method returns reference to value set.</returns>
			inline const GaValueSet<T>& GACALL GetValueSet() const { return *_domain; }

			/// <summary><c>SeValueSet</c> method sets pointer to value set in CCB.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="domain">pointer to value set.</param>
			inline void GACALL SetValueSet(GaValueSet<T>* domain) { _domain = domain; }

		};// END CLASS DEFINITION GaChromosomeDomainBlock

		/// <summary><c>GaDomainChromosome</c> class should be base for chromosomes which have codes with values which must bi in defined domain. 
		///
		/// 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="T">type of data used by value set.</param>
		template <typename T>
		class GaDomainChromosome : public GaDynamicOperationChromosome
		{

		public:

			/// <summary>This constructor initializes chromosome with CCB.</summary>
			/// <param name="configBlock">pointer to CCB.</param>
			GaDomainChromosome(GaChromosomeDomainBlock<T>* configBlock) : GaDynamicOperationChromosome(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>, chromosome's data and CCB is copied.</param>
			GaDomainChromosome(const GaDomainChromosome<T>& c,
				bool setupOnly) : GaDynamicOperationChromosome(c, setupOnly) { }

			/// <summary><c>GetClosestValue</c> method returns nearest value which can be found in value set to specified value.
			///
			/// This method is not thread-safe.</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>
			inline const T& GetClosestValue(const T& value) const
			{
				GaValueSet<T>* d = ( (GaChromosomeDomainBlock<T>*)_configBlock )->_domain;
				return d ? d->ClosestValue( value ) : value;
			}

		};// END CLASS DEFINITION GaDomainChromosome

	} // Representation
} // Chromosome

#endif // __GA_DOMAIN_CHROMOSOME_H__

⌨️ 快捷键说明

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