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

Genetic Algorithm Library

, 7 Apr 2012
A framework for genetic algorithms
Prize winner in Competition "Best C++/MFC article of May 2008"
galdemoapps.zip
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
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 Statistics.cpp
    \brief This file contains implementation of classes needed for tracking statistical information about populations and genetic algorithms.
*/

/*
 * 
 * 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 "Statistics.h"
#include <algorithm>

using namespace std;

namespace Common
{

	// Clears all values
	void GaStatistics::Clear()
	{
		_currentGeneration = 0;

		for( int i = 0; i < GaNumberOfStatValueTypes; i++ )
		{
			_values[ i ].SetCurrent( 0 );
			_values[ i ].SetPrevious( 0 );
		}
	}

	// The generation switch, updates statistical information for new and previous generation.
	void GaStatistics::NextGeneration()
	{
		// move curent values to previous
		for( int i = 0; i < GaNumberOfStatValueTypes; i++ )
			_values[ i ].SetPrevious( _values[ i ].GetCurrent() );

		_currentGeneration++;
	}

	// Copies statistic values
	void GaStatistics::CopyFrom(const GaStatistics& stats,
		bool previous,
		bool current,
		bool currentGeneration)
	{
		// copy stats from previous generation
		if( previous )
		{
			for( int i = 0; i < GaNumberOfStatValueTypes; i++ )
				_values[ i ].SetPrevious( stats._values[ i ].GetPrevious() );
		}

		// copy stats from current generation
		if( current )
		{
			for( int i = 0; i < GaNumberOfStatValueTypes; i++ )
				_values[ i ].SetCurrent( stats._values[ i ].GetCurrent() );
		}

		// copy current generation
		if( currentGeneration )
			_currentGeneration = stats._currentGeneration;
	}

	// Get progress of a value between this and previous generation
	float GaStatistics::GetValueProgress(GaStatValueType value,
									 bool percent) const
	{
		// calculate progress
		const GaFloatStatValue* v = _values + value;
		float ret = v->GetCurrent() - v->GetPrevious();

		// convert to percents if needed
		if( percent )
			ret = v->GetPrevious() ? ret * 100 / v->GetPrevious() : 100;

		return ret;
	}

	// Change value
	void GaStatistics::ChangeValue(GaStatValueType type,
							   float value,
							   bool relative)
	{
		// save current value
		float temp = _values[ type ].GetCurrent();

		// update value
		if( relative )
			temp += value;
		else
			temp = value;
		_values[ type ].SetCurrent( temp );

		// update avarge values if needed
		if( type == GSV_TOTAL_FITNESS )
			_values[ GSV_AVG_FITNESS ].SetCurrent( temp / _values[ GSV_POPULATION_SIZE ].GetCurrent() );
		else if( type == GSV_TOTAL_FITNESS_SCALED )
			_values[ GSV_AVG_FITNESS_SCALED ].SetCurrent( temp / _values[ GSV_POPULATION_SIZE ].GetCurrent() );
		else if( type == GSV_POPULATION_SIZE )
		{
			_values[ GSV_AVG_FITNESS ].SetCurrent( temp / _values[ GSV_POPULATION_SIZE ].GetCurrent() );
			_values[ GSV_AVG_FITNESS_SCALED ].SetCurrent( temp / _values[ GSV_POPULATION_SIZE ].GetCurrent() );
		}
	}

	// Combines information
	GaStatistics& GaStatistics::operator +=(const GaStatistics& rhs)
	{
		// fitness of the best chromosome
		float a = _values[ GSV_BEST_FITNESS ].GetCurrent();
		float b = rhs._values[ GSV_BEST_FITNESS ].GetCurrent();

		_values[ GSV_BEST_FITNESS ].SetCurrent( ( *_fitnessComparator )( a, b ) > 0 ? a : b );
		_values[ GSV_BEST_FITNESS_SCALED ].SetCurrent( 0 );

		// fitness of the worst chromosome
		a = _values[ GSV_WORST_FITNESS ].GetCurrent();
		b = rhs._values[ GSV_WORST_FITNESS ].GetCurrent();

		_values[ GSV_WORST_FITNESS ].SetCurrent( ( *_fitnessComparator )( a, b ) < 0 ? a : b );
		_values[ GSV_WORST_FITNESS_SCALED ].SetCurrent( 0 );

		// sum of all fitness values
		_values[ GSV_TOTAL_FITNESS ].SetCurrent( _values[ GSV_TOTAL_FITNESS ].GetCurrent() + rhs._values[ GSV_TOTAL_FITNESS ].GetCurrent() );
		_values[ GSV_TOTAL_FITNESS_SCALED ].SetCurrent( 0 );

		// sum of two population sizes
		_values[ GSV_POPULATION_SIZE ].SetCurrent( _values[ GSV_POPULATION_SIZE ].GetCurrent() + rhs._values[ GSV_TOTAL_FITNESS ].GetCurrent() );

		// avarge fitness value
		_values[ GSV_AVG_FITNESS ].SetCurrent( _values[ GSV_TOTAL_FITNESS ].GetCurrent() / _values[ GSV_POPULATION_SIZE ].GetCurrent() );
		_values[ GSV_AVG_FITNESS_SCALED ].SetCurrent( 0 );

		return *this;
	}

} // Common

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)

About the Author

Mladen Janković
Software Developer
Serbia Serbia
No Biography provided

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