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

📄 gamain.c

📁 一个简单的遗传算法 采用实数编码、比例分配轮盘赌选择、杂交采用离散与算术双重杂交、一般性变异。
💻 C
字号:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>

#define POPSIZE 100
#define MAXGENS 1000
#define NVARS  3
#define UPPERBOUND 1.023
#define LOWERBOUND 2.750
#define PXOVER 0.8
#define PMUTATION 0.15

typedef struct  
{
	double gen[NVARS];
	double fitness;
	double rfitness;
	double critness;
	double upper[NVARS];
	double lower[NVARS];
}individual;

individual population[POPSIZE+1];
individual newpopulation[POPSIZE+1];

void   initialize();
double randval(double low,double high);
void   evaluate();  
void   keep_best();
void   elitist();
void   select();
void   crossover();
void   xover1(int one,int two);
void   xover2(int first,int second);
void   swap(double x,double y);
void   mutate();

void initialize()
{
	int i,j;
	for(i=0;i<=POPSIZE;i++)
	{
		population[i].fitness=0;
		population[i].rfitness=0;
		population[i].critness=0;
		for(j=0;j<NVARS;j++)
		{
			population[i].lower[j]=LOWERBOUND;
			population[i].upper[j]=UPPERBOUND;
			population[i].gen[j]=randval(population[i].lower[j],population[i].upper[j]);
		}
	}
}

double randval(double low,double high)
{
	double val;
	val=low+(high-low)*(double)rand()/RAND_MAX;
	return(val);
}

void evaluate()
{
	int i,j;
	double x[NVARS+1];
	for(i=0;i<POPSIZE;i++)
	{
		for(j=0;j<NVARS;j++)
		{
			x[j+1]=population[i].gen[j];
		}
		population[i].fitness=x[1]*x[1]-x[1]*x[2]+x[3];
	}
}

void keep_best()
{
	int cur_best;
	int i;
	for(i=0;i<POPSIZE;i++)
	{
		if(population[i].fitness>population[POPSIZE].fitness)
		{
			population[POPSIZE].fitness=population[i].fitness;
			cur_best=i;
		}
	}
	for(i=0;i<NVARS;i++)
	{
		population[POPSIZE].gen[i]=population[cur_best].gen[i];
	}
}

void elitist()
{
	int i;
	int best_num=0,worst_num=0;
	double best,worst;
	best=population[0].fitness;
	worst=population[0].fitness;

	for(i=1;i<POPSIZE;i++)
	{
		if(population[i].fitness>best)
		{
			best=population[i].fitness;
			best_num=i;
		}	
		if(population[i].fitness<worst)
		{
			worst=population[i].fitness;
			worst=i;
		}
	}
	if(best>population[POPSIZE].fitness)
	{
		for(i=0;i<NVARS;i++)
		{
			population[POPSIZE].gen[i]=	population[best_num].gen[i];
		}
		population[POPSIZE].fitness=population[best_num].fitness;
	}
	else
	{
		for(i=0;i<NVARS;i++)
		{
			population[worst_num].gen[i]=population[POPSIZE].gen[i];
		}
		population[worst_num].fitness=population[POPSIZE].fitness;
	}
}

void select()
{
	int i,j;
	double sum=0;
	for(i=0;i<POPSIZE;i++)
	{
		sum=sum+population[i].fitness;
	}
	for(i=0;i<POPSIZE;i++)
	{
		population[i].rfitness=population[i].fitness/sum;
	}
	population[0].critness=population[0].rfitness;
	for(i=0;i<POPSIZE;i++)
	{
		population[i+1].critness=population[i].critness+population[i+1].rfitness;
	}
	for(i=0;i<POPSIZE;i++)
	{
		double p=rand()/RAND_MAX;
		if(p<=population[i].critness)
		{
			newpopulation[i]=population[0];
		}
		else
		{
			for(j=0;j<POPSIZE;j++)
			{
				if(p>population[j].critness&&p<=population[j+1].critness)
				{
					newpopulation[i]=population[j+1];
				}					
			}
		}
	}
	for(i=0;i<POPSIZE;i++)
	{
		population[i]=newpopulation[i];
	}
}

void crossover()
{
	int i,one,first=0;
	double p;
	for(i=0;i<POPSIZE;i++)
	{
		p=rand()/RAND_MAX;
		if(p<PXOVER)
		{
			first++;
			if(first%2==0)
			{
				xover1(one,i);
				xover2(one,i);
			}
			else
			{
				one=i;
			}
		}
	}
}

void xover1(int one,int two)//交叉采用离散算子
{
	int i,point;
	if(NVARS>1)
	{
		if(NVARS==2)
		{
			point=1;
		}
		else
		{
			point=rand()%(NVARS-1)+1;
		}
	}
	for(i=0;i<point;i++)
	{
		swap(population[one].gen[i],population[two].gen[i]);
	}
}

void xover2(int first,int second)//交叉采用算术算子
{
	int i;
	double ta[NVARS],tb[NVARS];
	double ax,bx;
	for(i=0;i<NVARS;i++)
	{
		ax=rand()%RAND_MAX;
		ta[i]=(double)ax*population[first].gen[i]+(1-ax)*population[second].gen[i];
		bx=rand()%RAND_MAX;
		tb[i]=(double)bx*population[first].gen[i]+(1-bx)*population[second].gen[i];
	}
	for(i=0;i<NVARS;i++)
	{
		population[first].gen[i]=ta[i];
		population[second].gen[i]=tb[i];
	}
}

void swap(double x,double y)
{
	double temp;
	temp=x;
	x=y;
	y=temp;
}

void mutate()
{
	int i,j;
	double p;
	for(i=0;i<POPSIZE;i++)
	{
		for(j=0;j<NVARS;j++)
		{
			p=rand()%RAND_MAX;
			if(p<PMUTATION)
			{
				population[i].gen[j]=randval(LOWERBOUND,UPPERBOUND);
			}
		}
	}
}

main()
{
	int i,ngen=0;
	FILE *galog;
	galog=fopen("galog.txt","wt");
	if(galog==NULL)
	{
		printf("Can't open galog.txt");
	}
	srand(time(NULL));
	initialize();
	evaluate();
	keep_best();
	while(ngen<=MAXGENS)
	{
		ngen++;
		select();
		crossover();
		mutate();
		evaluate();
		elitist();
		for(i=0;i<NVARS;i++)
		{
			fprintf(galog," var(%d)=%3.3f",i+1,population[POPSIZE].gen[i]);
		}
		fprintf(galog,"  best fitness=%3.3f\n",population[POPSIZE].fitness);
	}
	fclose(galog);
}











⌨️ 快捷键说明

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