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

📄 multivaluechromosome.h

📁 遗传算法的四个源程序和源代码
💻 H
📖 第 1 页 / 共 2 页
字号:
					for( int i = 0; i < size; i++ )
						_values.push_back( GetClosestValue( values[ i ] ) );
				}
			}

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

				for( int i = 0; i < size; i++ )
					_values.push_back( ( (GaChromosomeDomainBlock<TYPE>*)this->_configBlock )->_domain->GenerateRandom() );
			}

			/// <summary>This constructor initializes chromosome with CCB.</summary>
			/// <param name="configBlock">pointer to CCB.</param>
			GaMultiValueChromosome(GaChromosomeDomainBlock<TYPE>* configBlock) : GaDomainChromosome<TYPE>(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>
			GaMultiValueChromosome(const GaMultiValueChromosome<TYPE>& c,
				bool setupOnly) : GaDomainChromosome<TYPE>(c, setupOnly)
			{
				// copy code to new chromosome
				if( !setupOnly )
					_values = c._values;
			}

			/// <summary>More details are given in specification of <see cref="GaChromosome::MakeCopy" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual GaChromosomePtr GACALL MakeCopy(bool setupOnly) const { return new GaMultiValueChromosome( *this, setupOnly ); }

			/// <summary>This method uses <see cref="MakeCopy" /> method to create chromosome's object, so if additional steps are not needed
			/// for creating chromosome from prototype, classes which inherits this class don't have to override this method.
			/// 
			/// More details are given in specification of <see cref="GaChromosome::MakeNewFromPrototype" /> method.</summary>
			virtual GaChromosomePtr GACALL MakeNewFromPrototype() const
			{
				// make chromosome with exact setup
				GaChromosomePtr newPtr = MakeCopy( true );
				GaMultiValueChromosome* newChromosome = dynamic_cast<GaMultiValueChromosome*>( &( *newPtr ) );

				// generate random chromosome code
				if( _values.size() )
				{
					vector<TYPE>& collection = newChromosome->_values;
					collection.reserve( _values.size() );

					for( int i = 0; i < (int)_values.size(); i++ )
						collection.push_back( ( (GaChromosomeDomainBlock<TYPE>*)this->_configBlock )->_domain->GenerateRandom() );
				}

				return newPtr;
			}

			/// <summary>This method is not thread-safe.</summary>
			/// <returns>Method always returns number of values in chromosome's code.</returns>
			virtual int GACALL GetCodeSize() const { return (int)_values.size(); }

			/// <summary><c>GetAt</c> method returns value of chromosome's at specified position.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="pos">position of the value.</param>
			/// <returns>Method returns value of chromosome's code at specified position.</returns>
			inline TYPE GACALL GetAt(int pos) const { return _values[ pos ]; }

			/// <summary><c>SetAt</c> method sets value of chromosome's code at given position.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="value">new value.</param>
			/// <param name="pos">position of value.</param>
			inline void GACALL SetAt(TYPE value,
				int pos) { _values[ pos ] = GetClosestValue( value ); }

			/// <summary>This method is not thread-safe.</summary>
			/// <returns>Method returns reference to vector of values which represent chromosome's code.</returns>
			inline const vector<TYPE>& GACALL GetCode() const { return _values; }

			/// <summary>More details are given in specification of <see cref="GaChromosome::operator =" />.
			///
			/// This method is not thread-safe.</summary>
			virtual GaChromosome& GACALL operator =(const GaChromosome& rhs)
			{
				const vector<TYPE>& c = dynamic_cast<const GaMultiValueChromosome&>( rhs )._values;
				_values = c;

				return GaDefaultChromosome::operator =( rhs );
			}

			/// <summary>More details are given in specification of <see cref="GaChromosome::operator ==" />.
			///
			/// This method is not thread-safe.</summary>
			virtual float GACALL operator ==(const GaChromosome& c) const
			{
				const vector<TYPE>& b = dynamic_cast<const GaMultiValueChromosome&>( c )._values;
				int s = (int)b.size();

				int sim = 0, t = s + (int)_values.size();

				// get shorter chromosome
				if( s > (int)_values.size() )
					s = (int)_values.size();

				// compare codes
				for( int i = 0; i < s; i++ )
				{
					if( b[ i ] == _values[ i ] )
						sim += 2;
				}

				return ( (float)sim / t ) * 100;
			}

		protected:

			/// <summary><c>PreapareForMutation</c> method copies values form chromosome's code to backup vector.</summary>
			virtual void GACALL PreapareForMutation() { _backup.insert( _backup.begin(), _values.begin(), _values.end() ); }

			/// <summary>This method only clears backup vector.</summary>
			virtual void GACALL AcceptMutation() { _backup.clear(); }

			/// <summary><c>RejectMutation</c> method restores chromosome's code from backup vector. It also clears backup vector.</summary>
			virtual void GACALL RejectMutation()
			{
				_values.clear();
				_values = _backup;
				_backup.clear();
			}

		};// END CLASS DEFINITION GaMultiValueChromosome

		/// <summary>This class can be used for chromosomes which code has multiple values that support arithmetic operations. 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>
		/// </summary>
		/// <param name="TYPE">type of value in chromosome's code. This type must support binary operators
		/// <c>+</c>, <c>-</c>, <c>*</c>, <c>/</c> and <c>/</c> with right hand operand of <c>int</c> type.</param>
		template <typename TYPE>
		class GaMVArithmeticChromosome : public GaMultiValueChromosome<TYPE>,
			public GaArithmeticalCode
		{

		public:

			/// <summary>More details are given in specification of <see cref="GaArithmeticalCode::operator +" />.
			///
			/// This method is not thread-safe.</summary>
			GaChromosomePtr GACALL operator +(const GaArithmeticalCode& rhs) const
			{
				GaChromosomePtr newPtr = MakeCopy( true );
				GaMVArithmeticChromosome* newChromosome = 
					dynamic_cast<GaMVArithmeticChromosome*>( &( *newPtr ) );
				vector<TYPE> b = dynamic_cast<const GaMVArithmeticChromosome&>( rhs )._values;

				// size of new chromosome
				int s = (int)( b.size() <= this->_values.size() ? b.size() : this->_values.size() );
				vector<TYPE>& res = newChromosome->_values;
				res.reserve( s );

				// make new code
				for( int i = 0; i < s; i++ )
					res.push_back( GetClosestValue( this->_values[ i ] + b[ i ] ) );

				return newPtr;
			}

			/// <summary>More details are given in specification of <see cref="GaArithmeticalCode::operator -" />.
			///
			/// This method is not thread-safe.</summary>
			GaChromosomePtr GACALL operator -(const GaArithmeticalCode& rhs) const
			{
				GaChromosomePtr newPtr = MakeCopy( true );
				GaMVArithmeticChromosome* newChromosome =  
					dynamic_cast<GaMVArithmeticChromosome*>( &( *newPtr ) );
				vector<TYPE> b = dynamic_cast<const GaMVArithmeticChromosome&>( rhs )._values;

				// size of new chromosome
				int s = (int)( b.size() <= this->_values.size() ? b.size() : this->_values.size() );
				vector<TYPE>& res = newChromosome->_values;
				res.reserve( s );

				// make new code
				for( int i = 0; i < s; i++ )
					res.push_back( GetClosestValue( this->_values[ i ] - b[ i ] ) );

				return newPtr;
			}

			/// <summary>More details are given in specification of <see cref="GaArithmeticalCode::operator *" />.
			///
			/// This method is not thread-safe.</summary>
			GaChromosomePtr GACALL operator *(const GaArithmeticalCode& rhs) const
			{
				GaChromosomePtr newPtr = MakeCopy( true );
				GaMVArithmeticChromosome* newChromosome = 
					dynamic_cast<GaMVArithmeticChromosome*>( &( *newPtr ) );
				vector<TYPE> b = dynamic_cast<const GaMVArithmeticChromosome&>( rhs )._values;

				// size of new chromosome
				int s = (int)( b.size() <= this->_values.size() ? b.size() : this->_values.size() );
				vector<TYPE>& res = newChromosome->_values;
				res.reserve( s );

				// make new code
				for( int i = 0; i < s; i++ )
					res.push_back( GetClosestValue( this->_values[ i ] * b[ i ] ) );

				return newPtr;
			}

			/// <summary>More details are given in specification of <see cref="GaArithmeticalCode::operator /" />.
			///
			/// This method is not thread-safe.</summary>
			GaChromosomePtr GACALL operator /(const GaArithmeticalCode& rhs) const
			{
				GaChromosomePtr newPtr = MakeCopy( true );
				GaMVArithmeticChromosome* newChromosome = 
					dynamic_cast<GaMVArithmeticChromosome*>( &( *newPtr ) );
				vector<TYPE> b = dynamic_cast<const GaMVArithmeticChromosome&>( rhs )._values;

				// size of new chromosome
				int s = (int)( b.size() <= this->_values.size() ? b.size() : this->_values.size() );
				vector<TYPE>& res = newChromosome->_values;
				res.reserve( s );

				// make new code
				for( int i = 0; i < s; i++ )
					res.push_back( GetClosestValue( this->_values[ i ] / b[ i ] ) );

				return newPtr;
			}

			/// <summary>More details are given in specification of <see cref="GaArithmeticalCode::Midpoint" /> method.
			///
			/// This method is not thread-safe.</summary>
			GaChromosomePtr GACALL Midpoint(const GaArithmeticalCode& c) const
			{
				GaChromosomePtr newPtr = MakeCopy( true );
				GaMVArithmeticChromosome* newChromosome = 
					dynamic_cast<GaMVArithmeticChromosome*>( &( *newPtr ) );
				vector<TYPE> b = dynamic_cast<const GaMVArithmeticChromosome&>( c )._values;

				// size of new chromosome
				int s = (int)( b.size() <= this->_values.size() ? b.size() : this->_values.size() );
				vector<TYPE>& res = newChromosome->_values;
				res.reserve( s );

				// make new code
				for( int i = 0; i < s; i++ )
					res.push_back( GetClosestValue( ( this->_values[ i ] + b[ i ] ) / 2 ) );

				return newPtr;
			}

			/// <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>
			GaMVArithmeticChromosome(TYPE* values,
				int size,
				GaChromosomeDomainBlock<TYPE>* configBlock) :
				GaMultiValueChromosome<TYPE>(values, size, configBlock) { }

			/// <summary>This constructor initializes chromosome with CCB and random code with defined size.</summary>
			/// <param name="size">size of chromosome's code.</param>
			/// <param name="configBlock">pointer to CCB.</param>
			GaMVArithmeticChromosome(int size,
				GaChromosomeDomainBlock<TYPE>* configBlock) : GaMultiValueChromosome<TYPE>(size, configBlock) { }

			/// <summary>This constructor initializes chromosome with CCB.</summary>
			/// <param name="configBlock">pointer to CCB.</param>
			GaMVArithmeticChromosome(GaChromosomeDomainBlock<TYPE>* configBlock) : 
				GaMultiValueChromosome<TYPE>(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>
			GaMVArithmeticChromosome(const GaMVArithmeticChromosome<TYPE>& c,
				bool setupOnly) : GaMultiValueChromosome<TYPE>(c, setupOnly) { }

			/// <summary>More details are given in specification of <see cref="GaChromosome::MakeCopy" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual GaChromosomePtr GACALL MakeCopy(bool setupOnly) const { return new GaMVArithmeticChromosome( *this, setupOnly  ); }

		};// END CLASS DEFINITION GaMVArithmeticChromosome

	} // Representation
} // Chromosome

#endif // __GA_MULTI_VALUE_CHROMOSOME_H__

⌨️ 快捷键说明

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