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

📄 example_1.cpp

📁 free library of genetic algorithms
💻 CPP
字号:
#include <iostream>#include <math.h>#include <time.h>#include <cpplibga/StdGA.h>#define GeneSize 32#define GenotypeSize GeneSize*10typedef cpplibga::genotype::Binary<GenotypeSize> 	Genotype;typedef cpplibga::selection::Elitar			Selection;typedef cpplibga::grouping::AllWithAll			Grouping;typedef cpplibga::reproduction::UnifiedCrossover	Reproduction;typedef cpplibga::mutation::Incest      		Mutation;typedef cpplibga::accepting::OnlyNewPhenotype		Accepting;typedef cpplibga::placing::InsteadOfTheWorst		Placing;struct MyFitness{    double calc(const Genotype& genotype)    {	double PI=3.14159265358979323846;	double y=-100;	double x;	for(unsigned long t=0;t<10;t++)	{	    x=genotype.unpackToDouble(t*GeneSize,GeneSize);	    x=x*(5.12+5.12)/(double)(0xFFFFFFFF);	    x-=5.12;	    y+=10*cos(2*PI*x)-x*x;	}	return y;    }};int main(void){    MyFitness fitness;    cpplibga::StdGA<Selection,Grouping,Reproduction,Mutation,Accepting,Placing,		    Genotype,		    MyFitness> ga(256,&fitness);    ga.setSelectionGroupSize(16);    ga.setMutationProbability(0.01);    ga.setMutationDensity(0.1);//    ga.setEpsilum(0.0000000001);    double start=clock();    while(true)    {        ga.evolutionStep();        if(clock()-start > CLOCKS_PER_SEC)        {            std::cout <<"step: "      <<ga.getStep()                      <<" accepted: " <<ga.getNumberOfAcceptedSolutions()                      <<" rejected: " <<ga.getNumberOfRejectedSolutions()                      <<" replaced: " <<ga.getNumberOfReplacedSolutions()                         <<" V: "        <<(double)ga.getNumberOfReplacedSolutions()/((double)ga.getNumberOfAcceptedSolutions()+(double)ga.getNumberOfRejectedSolutions()+1)                      <<" F: "        <<ga.getBestSolution().calcFitness()                      << std::endl<<std::endl;            start=clock();        }    }       return 0;}

⌨️ 快捷键说明

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