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

📄 ga_work.cpp

📁 基本遗传算法 基本遗传算法
💻 CPP
字号:
#include "iostream.h"
#include "math.h"
#include "stdio.h"
#include "GA_work.h"
#include "stdlib.h"
#include "time.h"

int C_G=0;
class individual Current_individual[POP];
class individual Next_individual[POP];
int i,j;
void (*Fit_fun)(individual &a);

void main()
{

	Pop_Init();
	Fit_fun=Fit_fun_1;
//	while(C_G<GENERATION){
		for(i=0;i<POP;i++){
//			Next_individual[i]=Current_individual[i];
			Fit_fun_1(Current_individual[i]);
			cout<<"x["<<i+1<<"]="<<Current_individual[i].x
				<<"  y["<<i+1<<"]="<<Current_individual[i].y
				<<"  fitness["<<i+1<<"]="<<Current_individual[i].fitness<<endl;
		}
//	}

  }
/////////////////////////////////////////////////////////////////////////
////////////////1.definition of some variables///////////////////////////
/////////////////////////////////////////////////////////////////////////
//1.Fitness Function:
void Fit_fun_1(individual &a){
//	double c;
//	cout<<a.x<<" "<<a.y<<endl;
//	cout<<sin(a.x)<<endl;
	a.fitness=sin(a.x)*sin(a.y)/(a.x*a.y);
}
void Fit_fun_2(double x,double y){

}
//2.
/////////////////////////////////////////////////////////////////////////
////////////////2.functions of the GA procedures/////////////////////////
/////////////////////////////////////////////////////////////////////////
//1.population initial
//随机产生第一代,并计算fitness value
void Pop_Init(){
	double M_x,M_y;
	cout<<"GEN No.= 1"<<endl;
	srand(time(0));
	for(i=0;i<POP;i++){
		//get random x between -10 and 10
			M_x=rand()%(PARA_MAX_MUL-PARA_MIN_MUL+1)+PARA_MIN_MUL;
			Current_individual[i].x=M_x/COV_PRECISE;
//			cout<<Current_individual[i].x<<endl;
//			cout<<sin(Current_individual[i].x)<<endl;
		
	    //get random y between -10 and 10
			M_y=rand()%(PARA_MAX_MUL-PARA_MIN_MUL+1)+PARA_MIN_MUL;
			Current_individual[i].y=M_y/COV_PRECISE;

		//get genotype
/*			if(Current_individual[i].x<0){ 
				Current_individual[i].gene[0]=1;
				Current_individual[i].x=(Current_individual[i].x)*(-1);
				Current_individual[i].x--;
				Current_individual[i].x=
			}
			else Current_individual[i].gene[0]=0;
*/

		}
	}

//2.fitness value computation
void Fitness_Comp(individual &a){
//	for(i=0;i<POP;i++){
//		a.fitness=Fit_fun_1(a);
//	}
}
//3.selection and replication

//4.crossover

//5.mutation

/////////////////////////////////////////////////////////////////////////
////////////////3.assistant functions  ///////////////////////////
/////////////////////////////////////////////////////////////////////////
void Bin2Dec(double x) 
{ 
	int result = 0; 
	
	for ( int i = b_count; i >= 0; i-- ) 
		if ( b[i] == '1' ) 
			result = result + pow( 2.0, b_count - i ); 
		
		printf("你输入的二进制数对应的十进制数为:%d\n",result); 
} 

void Dec2Bin(double &a,double &b) 
{ 
	int d_count = 0; 
	char p[20]; 
	
	while ( d > 0 ) 
	{ 
		p[d_count++] = d % 2 + 48; 
		d = ( d - d % 2 ) / 2; 
	} 
	
	for ( int i = d_count - 1; i >= 0; i-- ) 
		printf("%c",p[i]); 
	printf("\n"); 
} 




















⌨️ 快捷键说明

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