Click here to Skip to main content
11,584,040 members (44,226 online)
Click here to Skip to main content
Add your own
alternative version

Genetic Algorithm Library

, 7 Apr 2012 GPL3 271.3K 24.9K 518
A framework for genetic algorithms
galdemoapps.zip
TSP.exe
GeneticLibrary.dll
TestApp1.exe
TestApp2.exe
GaSchedule.exe
GaSchedule.cfg
GalDemoSource.zip
TestApp1
TestApp1.vcxproj.filters
TestApp2
TestApp2.vcxproj.filters
TSP
res
.svn
entries
prop-base
Toolbar.bmp.svn-base
TSP.ico.svn-base
props
text-base
Toolbar.bmp.svn-base
TSP.ico.svn-base
TSP.rc2.svn-base
tmp
prop-base
props
text-base
Toolbar.bmp
TSP.ico
TSP.vcxproj.filters
GaSchedule
Algorithm
GaSchedule.cfg
GaSchedule.vcxproj.filters
res
GaSchedule.ico
galdocumentation.zip
doxygen.png
Graphic
ab_cp.png
alg_st.png
a_cr.png
c_cp.png
i_cp.png
mp_cr.png
mv_cr.png
r_cp.png
s_cp.png
s_cr.png
galsource.zip
GeneticLibrary
build
release
gcc_bsd
gcc_linux
gcc_macos
gcc_solaris
icc_linux
icc_macos
icc_win
mingw
msvc
scc_solaris
makefiles
gcc_bsd_debug
gcc_bsd_release
gcc_linux_debug
gcc_linux_release
gcc_macos_debug
gcc_macos_release
gcc_solaris_debug
gcc_solaris_release
icc_linux_debug
icc_linux_release
icc_macos_debug
icc_macos_release
scc_solaris_debug
scc_solaris_release
source
vs
/*! \file ReplacementOperations.h
    \brief This file contains declaration of class of replacement operation that replaces the worst chromosomes.
*/

/*
 * 
 * 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_REPLACEMENT_OPERATIONS_H__
#define __GA_REPLACEMENT_OPERATIONS_H__

#include "PopulationOperations.h"

using namespace Population;

namespace Population
{
	/// <summary>Contains implementation of some basic replacement operations.</summary>
	namespace ReplacementOperations
	{

		/// <summary><c>GaReplaceWorst</c> replaces chromosomes with worst fitness values in population. If population is sorted this operation can
		/// replace any number of chromosomes, if it is not sorted this operation can only replace chromosomes which are stored in the worst chromosome 
		/// sorted group of population. This replacement operation use <see cref="GaReplacementParams" /> class for 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.
		/// Because this genetic operation is stateless all public method are thread-safe.</summary>
		class GaReplaceWorst : public GaReplacementOperation
		{

		public:

			/// <summary>More details are given in specification of <see cref="GaReplacementOperation::operator ()" /> method.
			///
			/// This method is thread-safe.</summary>
			GAL_API
			virtual void GACALL operator ()(GaPopulation& population,
				const GaReplacementParams& parameters,
				const GaCouplingResultSet& newChromosomes) const;

			/// <summary>More details are given in specification of <see cref="GaOperation::MakeParameters" /> method.
			///
			/// This method is thread-safe.</summary>
			/// <returns>Method returns new instance of <see cref="GaReplacementParams" /> class.</returns>
			virtual GaParameters* GACALL MakeParameters() const { return new GaReplacementParams(); }

			/// <summary>Valid parameters must have number of chromosomes which should be replaced greater then 0. 
			///
			/// More details are given in specification of <see cref="GaOperation::CheckParameters" /> method.
			///
			/// This method is thread-safe.</summary>
			virtual bool GACALL CheckParameters(const GaParameters& parameters) const
			{
				return ( (const GaReplacementParams&) parameters ).GetReplacementSize() > 0;
			}

		};// END CLASS DEFINITION GaReplaceWorst

		/// <summary>This class should be used (directly or by inheritance) by replacement operations which can unintentionally remove the best chromosomes
		/// from population. 
		///
		/// 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 GaReplaceElitismParams : public GaReplacementParams
		{

		private:

			/// <summary>Number of best chromosomes that will not be removed from population by replacement operation.</summary>
			int _elitism;

		public:

			/// <summary>This constructor initializes parameters with user defined values.</summary>
			/// <param name="replacementSize">number of chromosomes which replaced by the operation.</param>
			/// <param name="elitism">number of the best chromosomes that will not be removed from population by replacement operation.</param>
			GaReplaceElitismParams(int replacementSize,
				int elitism) : GaReplacementParams(replacementSize),
				_elitism(elitism) { }

			/// <summary>This constructor initializes parameters with default values. Default number of replaced chromosomes is 2,
			/// and default elitism size is 0.</summary>
			GaReplaceElitismParams() : _elitism(0) { }

			/// <summary>More details are given in specification of <see cref="GaParameters::Clone" /> method.
			///
			/// This method is not thread-safe.</summary>
			virtual GaParameters* GACALL Clone() const { return new GaReplaceElitismParams( *this ); }

			// Returns number of the best chromosome which will certainly surivive to the next generation
			/// <summary>This method is not thread-safe.</summary>
			/// <returns>Method returns number of the best chromosomes that will not be removed from population by replacement operation.</returns>
			inline int GACALL GetElitism() const { return _elitism; }

			// Sets number of the best chromosome which will certainly surivive to the next generation
			/// <summary><c>SetElitism</c> method sets number of the best chromosomes that will not be removed from population by replacement operation. 
			///
			/// This method is not thread-safe.</summary>
			/// <param name="elitism">number of the best chromosomes that will not be removed.</param>
			inline void GACALL SetElitism(int elitism) { _elitism = elitism; }

		};// END CLASS DEFINITION GaReplaceElitismParams

		/// <summary><c>GaReplaceRandom</c> randomly chooses chromosomes which are going to be replaced. This operation saves best chromosomes from replacement
		/// if specified in parameters. This replacement operation use <see cref="GaReplaceElitismParams" /> class for 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.
		/// Because this genetic operation is stateless all public method are thread-safe.</summary>
		class GaReplaceRandom : public GaReplacementOperation
		{

		public:

			/// <summary>More details are given in specification of <see cref="GaReplacementOperation::operator ()" /> method.
			///
			/// This method is thread-safe.</summary>
			GAL_API
			virtual void GACALL operator ()(GaPopulation& population,
				const GaReplacementParams& parameters,
				const GaCouplingResultSet& newChromosomes) const;

			/// <summary>More details are given in specification of <see cref="GaOperation::MakeParameters" /> method.
			///
			/// This method is thread-safe.</summary>
			/// <returns>Method returns new instance of <see cref="GaReplaceElitismParams" /> class.</returns>
			virtual GaParameters* GACALL MakeParameters() const { return new GaReplaceElitismParams(); }

			/// <summary>Valid parameters must have number of chromosomes which should be replaced greater then 0. 
			///
			/// More details are given in specification of <see cref="GaOperation::CheckParameters" /> method.
			///
			/// This method is thread-safe.</summary>
			virtual bool GACALL CheckParameters(const GaParameters& parameters) const
			{
				return ( (const GaReplacementParams&) parameters ).GetReplacementSize() > 0;
			}

		};// END CLASS DEFINITION GaReplaceRandom

		/// <summary><c>GaReplaceParents</c> replaces chromosomes which are marked as parents by coupling operation to offspring chromosomes.
		/// This operation saves the best chromosomes from replacement if specified in parameters. This replacement operation use
		/// <see cref="GaReplaceElitismParams" /> class for 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.
		/// Because this genetic operation is stateless all public method are thread-safe.</summary>
		class GaReplaceParents : public GaReplacementOperation
		{

		public:

			/// <summary>More details are given in specification of <see cref="GaReplacementOperation::operator ()" /> method.
			///
			/// This method is thread-safe.</summary>
			GAL_API
			virtual void GACALL operator ()(GaPopulation& population,
				const GaReplacementParams& parameters,
				const GaCouplingResultSet& newChromosomes) const;

			/// <summary>More details are given in specification of <see cref="GaOperation::MakeParameters" /> method.
			///
			/// This method is thread-safe.</summary>
			/// <returns>Method returns new instance of <see cref="GaReplaceElitismParams" /> class.</returns>
			virtual GaParameters* GACALL MakeParameters() const { return new GaReplaceElitismParams(); }

			/// <summary>Valid parameters must have number of chromosomes which should be replaced greater then 0. 
			///
			/// More details are given in specification of <see cref="GaOperation::CheckParameters" /> method.
			///
			/// This method is thread-safe.</summary>
			virtual bool GACALL CheckParameters(const GaParameters& parameters) const
			{
				return ( (const GaReplacementParams&) parameters ).GetReplacementSize() > 0;
			}

		};// END CLASS DEFINITION GaReplaceParents

		/// <summary><c>GaReplaceBest</c> replaces chromosomes with best fitness values in population. If population is sorted this operation can replace any
		/// number of chromosomes, if it is not sorted this operation can only replace chromosomes which are stored in the best chromosome sorted group
		/// of population. This replacement operation use <see cref="GaReplacementParams" /> class for 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.
		/// Because this genetic operation is stateless all public method are thread-safe.</summary>
		class GaReplaceBest : public GaReplacementOperation
		{

		public:

			/// <summary>More details are given in specification of <see cref="GaReplacementOperation::operator ()" /> method.
			///
			/// This method is thread-safe.</summary>
			GAL_API
			virtual void GACALL operator ()(GaPopulation& population,
				const GaReplacementParams& parameters,
				const GaCouplingResultSet& newChromosomes) const;

			/// <summary>More details are given in specification of <see cref="GaOperation::MakeParameters" /> method.
			///
			/// This method is thread-safe.</summary>
			/// <returns>Method returns new instance of <see cref="GaReplacementParams" /> class.</returns>
			virtual GaParameters* GACALL MakeParameters() const { return new GaReplacementParams(); }

			/// <summary>Valid parameters must have number of chromosomes which should be replaced greater then 0.
			///
			/// More details are given in specification of <see cref="GaOperation::CheckParameters" /> method.
			///
			/// This method is thread-safe.</summary>
			virtual bool GACALL CheckParameters(const GaParameters& parameters) const
			{
				return ( (const GaReplacementParams&) parameters ).GetReplacementSize() > 0;
			}

		};// END CLASS DEFINITION GaReplaceBest

	} // ReplacementOperations
} // Population

#endif // __GA_REPLACEMENT_OPERATIONS_H__

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Mladen Janković
Software Developer
Serbia Serbia
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 7 Apr 2012
Article Copyright 2008 by Mladen Janković
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid