?? example_1.cpp
字號(hào):
#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;}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -