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

📄 stdga.h

📁 free library of genetic algorithms
💻 H
字号:
/*    cpplibga, free library of genetic algorithms.    Copyright (C) 2003  Burger Y.A. (aka Jo Kruger)    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USAContacts:   address: planet Terra   e-mail:  jo_kruger@mail.ru            kruger@selena.net.ua*/#ifndef __CPPLIBGA_STDGA_H__#define __CPPLIBGA_STDGA_H__#include <vector>#include <algorithm>#include <cpplibga/Types.h>#include <cpplibga/Predicates.h>#include <cpplibga/Solution.h>#include <cpplibga/Allocator.h>#include <cpplibga/accepting/Any.h>#include <cpplibga/accepting/OnlyNewGenotype.h>#include <cpplibga/accepting/OnlyNewPhenotype.h>#include <cpplibga/genotype/Binary.h>#include <cpplibga/grouping/AllWithAll.h>#include <cpplibga/grouping/BestWithAll.h>#include <cpplibga/mutation/InvertOneDot.h>#include <cpplibga/mutation/DensityInvert.h>#include <cpplibga/mutation/Incest.h>#include <cpplibga/placing/InsteadOfTheWorst.h>#include <cpplibga/reproduction/OneDotCrossover.h>#include <cpplibga/reproduction/TwoDotCrossover.h>#include <cpplibga/reproduction/UnifiedCrossover.h>#include <cpplibga/selection/Random.h>#include <cpplibga/selection/Elitar.h>namespace cpplibga{    template<	     typename Selection,	     typename Grouping,	     typename Reproduction,	     typename Mutation,	     typename Accepting,	     typename Placing,	     typename Genotype,	     typename Fitness	    > class StdGA : public Selection,	    		    public Grouping,			    public Reproduction,			    public Mutation,			    public Accepting,			    public Placing    {	    typedef Solution<Genotype, Fitness>				SolutionType;	    typedef std::pair<const SolutionType*, const SolutionType*>	Pair;	    typedef std::vector<SolutionType*>				Population;	    typedef std::vector<SolutionType*>				SelectedSolutions;	    typedef std::vector<Pair>					Pairs;	    typedef std::vector<SolutionType*>				Childs;	    typedef Allocator<SolutionType, Fitness>			SolutionAllocator;	    	    SolutionAllocator 	allocator;	    Population		population;	    SelectedSolutions	selectedSolutions;	    Pairs		pairs;	    Childs		childs;                        unsigned long       step;    	public:	    	    StdGA(population_size_type populationSize,		  Fitness* fitness) : allocator(fitness), step(0)	    {		population.reserve(populationSize);		createPopulation(populationSize);	    }	    	    ~StdGA()	    {		for(typename Population::iterator i=population.begin(); i!=population.end(); ++i)		{		    allocator.Delete(*i);		}	    }            unsigned long getStep() const {return step;}	    SolutionType getBestSolution() const 	    {		return **std::max_element(population.begin(), population.end(), XLessThanY());	    }	    	    Genotype getBestGenotype() const	    {		return getBestSolution().getGenotype();	    }	    		    void evolutionStep()	    {		select(population, &selectedSolutions);		group(selectedSolutions, &pairs);		selectedSolutions.clear();		for(typename Pairs::iterator i=pairs.begin(); i!=pairs.end(); ++i)		{		    reproduct(*i, &childs, &allocator);		    for(typename Childs::iterator j=childs.begin(); j!=childs.end(); ++j)		    {			mutate(*j, *i);			if(accept(**j, population))			{			    place(*j, &population, &allocator);			}			else			{			    allocator.Delete(*j);			}		    }		    childs.clear();		}				pairs.clear();                                ++step;	    }	    	private:		    void createPopulation(population_size_type size)	    {		while(size>0)		{		    --size;		    SolutionType* tmp=allocator.New();		    tmp->fillRand();		    population.push_back(tmp);		}	    }    };}#endif

⌨️ 快捷键说明

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