Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Genetic Algorithm Library

, 7 Apr 2012
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 CrossoverOperations.cpp
    \brief This file contains implementation of crossover operation 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.
 *
 */

#include "GlobalRandomGenerator.h"
#include "RepresentationInterfaces.h"
#include "CrossoverOperations.h"

using namespace Chromosome;
using namespace Chromosome::Representation;
using namespace Chromosome::CrossoverOperations;

namespace Chromosome
{
	namespace CrossoverOperations
	{

		// Performs crossover operation and make new child from two given parents.
		GaChromosomePtr GaMultiValueCrossover::operator ()(const GaChromosome* parent1,
			const GaChromosome* parent2) const
		{
			GaChromosomePtr newChromosome = parent1->MakeCopy( true );
			GaMultiValueCode* code = dynamic_cast<GaMultiValueCode*>( &( *newChromosome ) );

			const GaMultiValueCode* pc1 = dynamic_cast<const GaMultiValueCode*>( parent1 );
			const GaMultiValueCode* pc2 = dynamic_cast<const GaMultiValueCode*>( parent2 );

			int maxPos = parent1->GetCodeSize() <= parent2->GetCodeSize()
				 ? parent1->GetCodeSize() 
				 : parent2->GetCodeSize();

			// create temp buffer for making new code
			GaCodeValuesBuffer* buffer = code->MakeBuffer( parent1->GetCodeSize() >= parent2->GetCodeSize()
				 ? parent1->GetCodeSize() 
				 : parent2->GetCodeSize() );

			// array of crossover points
			int* pos = new int[ parent1->GetParameters().GetNumberOfCrossoverPoints() + 1 ];
			pos[ 0 ] = 0;
			int c = 1;

			// make random crossover points
			for( int i = parent1->GetParameters().GetNumberOfCrossoverPoints() - 1; i >= 0; i-- )
			{
				bool found = false;
				int p;
				do
				{
					// generate radnom point
					p = GaGlobalRandomIntegerGenerator->Generate( maxPos - 1 );

					// check if point already exists
					for( int j = c - 1; j >= 0; j-- )
					{
						// found?
						if( p == pos[ j ] )
						{
							found = true;
							break;
						}
					}

					found = false;
				}
				while( found );

				int j = c;
				// fin position of crossover point in array
				for( ; j > 0 && p < pos[ j - 1 ]; j-- )
					pos[ j ] = pos[ j - 1 ];

				// inser crossover point
				pos[ j ] = p;
				c++;
			}

			bool k = GaGlobalRandomBoolGenerator->Generate();
			for( int i = 0; i < c; i++ )
			{
				int p = pos[ i ];

				// copy code form parent1
				if( k )
				{
					// size of part which will be copied into new chromosome
					int s = i != ( c - 1 ) ? pos[ i + 1 ] - pos[ i ] : parent1->GetCodeSize() - pos[ i ];

					// copy part of parent into buffer for new chromosome
					pc1->FillBuffer( p, s, *buffer );
				}
				// copy code form parent2
				else
				{
					// size of part which will be copied into new chromosome
					int s = i != ( c - 1 ) ? pos[ i + 1 ] - pos[ i ] : parent2->GetCodeSize() - pos[ i ];

					// copy part of parent into buffer for new chromosome
					pc2->FillBuffer( p, s, *buffer );
				}

				k = !k;
			}

			// set new code for chromosome
			code->FromBuffer( *buffer );

			// delete temp buffers
			delete[] pos;
			delete buffer;

			return newChromosome;
		}

		// Performs crossover operation and make new child from two given parents.
		GaChromosomePtr GaAddCrossover::operator ()(const GaChromosome* parent1,
			const GaChromosome* parent2) const
		{
			return *dynamic_cast<const GaArithmeticalCode*>( parent1 ) + *dynamic_cast<const GaArithmeticalCode*>( parent2 );
		}

		// Performs crossover operation and make new child from two given parents.
		GaChromosomePtr GaSubCrossover::operator ()(const GaChromosome* parent1,
			const GaChromosome* parent2) const
		{
			return *dynamic_cast<const GaArithmeticalCode*>( parent1 ) - *dynamic_cast<const GaArithmeticalCode*>( parent2 );
		}

		// Performs crossover operation and make new child from two given parents.
		GaChromosomePtr GaMidpointCrossover::operator ()(const GaChromosome* parent1,
			const GaChromosome* parent2) const
		{
			return dynamic_cast<const GaArithmeticalCode*>( parent1 )->Midpoint( *dynamic_cast<const GaArithmeticalCode*>( parent2 ) );
		}

	} // Crossover
} // Chromosome

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 7 Apr 2012
Article Copyright 2008 by Mladen Janković
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid