#include <stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define LENGTH1 10
#define LENGTH2 10
#define CHROMLENGTH LENGTH1+LENGTH2
#define POPSIZE 300
int MaxGeneration =200;
struct individual
{
char chrom[CHROMLENGTH+1];
double value;//适应度
double affective;//亲和力
double concentration;//浓度
double activity;//激励度
};
int generation;
int best_index;
struct individual population[POPSIZE];
struct individual nextpopulation[POPSIZE];
struct individual array[POPSIZE];
struct individual A;
struct individual B;
struct individual bestindividual;
struct individual currentbest;
int PopSize =80;//全局变量
double umu =0.08;//全局变量
double r =0.001;//全局变量
double rad =0.3;
int clone_total =0;//全局变量
//*****************************************************************************
void GenerateInitialPopulation();
long DecodeChromosome(char *string,int point,int length);
void CalculateObjectValue(struct individual array[],int n);
void Calculateaffective(struct individual array[],int n);
void EvaluatePopulation();
void affectivesort(struct individual array[],int n);
void clonenum();
void MutationOperator(void);
void GenerateNextPopulation(void);
double CalculateSimilarity(struct individual A,struct individual B);
void Inhibition(void);
void chongzu();
void CalculateConcentrationValue(struct individual population[],int n);
void CalculateActivityValue(struct individual population[],int n);
void activeslect();
void sortnewmember();
void PerformEvolution();
void FindBestIndividual();
void OutputTextReport();
//******************************************************************************
void main()
{
generation=0;
GenerateInitialPopulation();
EvaluatePopulation();
while(generation<MaxGeneration)
{
generation++;
GenerateNextPopulation();
EvaluatePopulation();
PerformEvolution();
OutputTextReport();
}
}
//***********************************************************************************
void GenerateInitialPopulation()
{
int i,j;
for(i=0;i<PopSize ;i++)
{
for(j=0;j<CHROMLENGTH;j++)
{
population[i].chrom[j]=(rand()%10<5)?'0':'1';
}
population[i].chrom[CHROMLENGTH]='\0';
}
}
//*******************************************************************************
void GenerateNextPopulation(void)
{
affectivesort(population,PopSize);//排序选择亲和力高的进行克隆
clonenum();
MutationOperator();
Inhibition();
chongzu();
activeslect();
sortnewmember();
}
//********************************************************************************
void EvaluatePopulation(void)
{
CalculateObjectValue ( population,PopSize);
Calculateaffective ( population,PopSize);
FindBestIndividual();
}
//********************************************************************************
long DecodeChromosome(char *string,int point,int length)
{
int i;
long decimal=0L;
char *pointer;
for(i=0,pointer=string+point;i<length;i++,pointer++)
{
decimal+=(*pointer-'0')<<(length-1-i);
}
return(decimal);
}
//********************************************************************************
void CalculateObjectValue(struct individual array[],int n)
{
int i;
long temp1,temp2;
double x1,x2;
for (i=0;i<n;i++)
{
temp1=DecodeChromosome(array[i].chrom,0,LENGTH1);
temp2=DecodeChromosome(array[i].chrom,LENGTH1,LENGTH2);
x1=4.096*temp1/1023.0-2.048;
x2=4.096*temp2/1023.0-2.048;
array[i].value=100*(x1*x1-x2)*(x1*x1-x2)+(1-x1)*(1-x1);
}
}
//**********************************************************************************
void Calculateaffective(struct individual array[],int n)
{
int i;
for(i=0;i<n;i++)
{
array[i].affective=1.0/(exp(-(r*array[i].value))+1.0);
}
}
//**********************************************************************************
void affectivesort(struct individual array[],int n)
{
int i,j;
struct individual a;
for(j=0;j<n-1;j++)
{
for(i=0;i<n-1-j;i++)
{
if(array[i].affective<array[i+1].affective)
{
a=array[i+1];
array[i+1]=array[i];
array[i]=a;
}
}
}
}
//******************************************************************
void FindBestIndividual()
{
int i;
bestindividual=population[0];
for(i=0;i<PopSize;i++)
{
if(population[i].affective>bestindividual.affective)
{
bestindividual=population[i];
best_index=i;
}
if(generation==0)
{
currentbest=bestindividual;
}
else
{
if(bestindividual.affective>currentbest.affective)
{
currentbest=bestindividual;
}
}
}
}
void PerformEvolution()
{
if(bestindividual.affective>currentbest.affective)
{
currentbest=population[best_index];
}
}
//**********************************************************************************
void clonenum()
{
int i,j;
int M =100;
int m[POPSIZE];
int L=0;
double sum1=0;
double sg =0.8;
for(i=0;i<(int)(sg*M);i++)
{
sum1+=array[i].affective;
m[i]=(int)(array[i].affective*M/sum1);
clone_total+=m[i];
}
for(i=0;i<(int)(sg*M);i++)
{
for(j=0;j<m[i];j++)
nextpopulation[L++]=array[i];
}
}
//********************************************************************************
void MutationOperator(void)
{
int i,j;
double p,po;
for(i=0;i<clone_total;i++)
{
for(j=0;j<CHROMLENGTH;j++)
{
po=rand()%1000/1000.0;
p=exp((-1)*nextpopulation[i].affective);
if(po<p)
{
nextpopulation[i].chrom[j]=(nextpopulation[i].chrom[j]=='0')?'1':'0';
}
}
}
}
//********************************************************************************
double CalculateSimilarity (struct individual A,struct individual B)
{
int j=0;
double sum=0.0;
for(j=0;j<CHROMLENGTH;j++)
{
sum+=(A.chrom[j]=B.chrom[j])?0:1;
}
sum=sum*(log(2.0))/CHROMLENGTH;
return sum;
}
//************************************************************************************************
void Inhibition(void)
{
int i,j;
int L=0;
int numinh=0;
//double rad =0.3;
CalculateObjectValue(nextpopulation,clone_total);
Calculateaffective(nextpopulation,clone_total);
affectivesort(nextpopulation,clone_total);//排序进行抑制
for(i=0;i<clone_total-1;i++)
{
for(j=i+1;j<clone_total;j++)
{
if(CalculateSimilarity(nextpopulation[i],nextpopulation[j])>rad)
{
nextpopulation[++L]=nextpopulation[j];
}
}
clone_total=L+1;
L=i+1;
}
clone_total=L+1;
}
//*****************************************************************************************************
void chongzu()
{
int i;
for(i=0;i<clone_total;i++)
{
population[i+PopSize]=nextpopulation[i];
}
affectivesort(population,clone_total+PopSize);//很重要,去掉200代找不到最优解。
}
//*******************************************************************************************************
void CalculateConcentrationValue(struct individual population[],int n)
{
int i,j,m=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
m+=(CalculateSimilarity(population[i],population[j])<rad)?1:0;
}
}
population[i].concentration=m*(1.0)/(n);
}
//***********************************************************************************************************
void CalculateActivityValue(struct individual population[],int n)
{
int i;
double h=1.5;
for(i=0;i<n;i++)
{
population[i].activity=population[i].affective*exp(-population[i].concentration/h);
}
}
//*************************************************************************************************************
void activeslect(
基于人工免疫优化算法
需积分: 20 122 浏览量
2013-11-04
12:02:19
上传
评论
收藏 10KB RAR 举报
忆昔追梦
- 粉丝: 0
- 资源: 6
最新资源
- 微软常用运行库 游戏运行库 VC++各个版本
- 微信小程序开发教程.pptx
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- 锐捷网络认证中心网络管理.pdf
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- SD8233LF是一款用于单按键触摸及接近感应开关,其用途是替代传统的机械型开关芯片IC
- 基于YOLOv5的烟雾火焰检测算法研究
- 基于STM32的联合调试侦听设备解决方案原理图PCB源文件调试工具视频(大赛作品)
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈