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

📄 myga.cpp

📁 基本遗传算法在求一个函数表达式的最大值应用!
💻 CPP
字号:
#include "stdafx.h"
#include "myga.h"
#include "population.h"
#include "myRandom.h"
#include "objfunction.h"
#include "parameter.h"
#include <iostream>
#include <string>
#include <fstream>


extern MyParameter myparameter;
extern MyRandom myRandom;

void MyGA::report(std::ofstream &outfile, Population *pop, int gen) {
	double x1,x2;
	x1=x2=0.0;
	x1=decode_to_X(-2.048,2.048,chro_decode(pop->chromosomes [pop->get_best ()],myparameter.get_lchrom ()/2,0));
	x2=decode_to_X(-2.048,2.048,chro_decode(pop->chromosomes [pop->get_best ()],myparameter.get_lchrom ()/2,myparameter.get_lchrom ()/2));
	//
	this->nodes [gen - 1].set_data (gen,pop->get_best (),pop->get_maxfit (),x1,x2);
	//控制台显示
	std::cout<<"Generation = "<<gen<<"  "<<pop->chromosomes [pop->get_best ()].genes_to_string ()
		     <<"    x1 = "<< x1
			 <<"    x2 = "<< x2<<std::endl ;
	//输出文件内容
	outfile<<"**************************************** Generation = "<<gen
		   <<" ****************************************"<<std::endl ;
	for(int i = 0;i< pop->get_popsize ();i++)
		outfile<<i<<":  "<<pop->chromosomes [i].genes_to_string ()
		       <<"   "<<pop->chromosomes [i].get_fitness ()
			   <<"    x1 = "<< decode_to_X(-2.048,2.048,chro_decode(pop->chromosomes[i],myparameter.get_lchrom ()/2,0))
			   <<"    x2 = "<< decode_to_X(-2.048,2.048,chro_decode(pop->chromosomes[i],myparameter.get_lchrom ()/2,myparameter.get_lchrom ()/2))<<std::endl ;
	outfile<<"***********************************************************************************************"<<std::endl ;
	outfile<<"Best = " <<pop->get_best ()<<"   MaxFitness = "<<pop->get_maxfit ()
		<<"   MinFitness = "<<pop->get_minfit ()<<"   AveFitness = "<<pop->get_avgfit ()<<std::endl;
	outfile<<"MatingPool :";
	for(int i=0;i<pop->get_popsize ();i++)
		outfile<<" "<<pop->MatingPool [i];
	outfile<<std::endl <<std::endl ;

}

MyGA::MyGA(int gen ){
	this->stop_generation = gen;
	this->nodes = new DataNode [gen];
} 

MyGA::~MyGA (){
	delete [] nodes;
}

void MyGA::run(std::ofstream &outfile,double pcro) {
		int generation ;
		int way = myparameter.get_select_way () ;

		//first population with random individuals
		Population *pop = new Population();
		pop->evaluate ();
		//selection
		if(0 == way)
			pop->selection ();
		else
			pop->tournamentSeclection ();
		//report
		report(outfile,pop,1);
		//
		for(generation = 1;generation<stop_generation;generation++){
			Population * temp_pop = new Population();
			//copy genes
			for(int i=0;i<myparameter.get_popsize();i++)
				temp_pop->chromosomes[i].copyGene(pop->chromosomes [pop->MatingPool [i]].myGenes ,myparameter.get_lchrom (),0);
			
			//delete unnecessary population
			delete pop;     
			pop = temp_pop;
			//cross
			for(int i=0;i<myparameter.get_popsize();){
				double my_rand = myRandom.double_random ();
				if( my_rand <= pcro){
					pop->chromosomes [i].tocross (pop->chromosomes[i+1] ,myRandom.lim_int_random (1,myparameter.get_lchrom () ));
				}
				i=i+2;
			}
			//mutate
			for(int i=0;i<myparameter.get_popsize();i++)
				pop->chromosomes [i].mutate ();

			pop->evaluate ();
            //select
			if(0 == way)
				pop->selection ();
			else
				pop->tournamentSeclection ();
			//report
			report(outfile,pop,generation+1);
		}
		delete pop;
		std::cout<<"~~~~~~~OVER~~~~~~~~"<<std::endl ;
}

⌨️ 快捷键说明

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