📄 ga_enhangced1225.c
字号:
/************************************************************
* Function: Find the best individual so far *
* current generation. *
* Variables: None. *
************************************************************/
void FindBestAndWorstIndividual(void)
{
int i;
double sum=0.0;
bestIndividual=population[0];
worstIndividual=population[0];
/*find out the best and worst of this generation*/
for(i=1;i<POPSIZE;i++)
{
if(population[i].fitness>bestIndividual.fitness)
{
bestIndividual=population[i];
best_index=i;
}
else if(population[i].fitness<worstIndividual.fitness)
{
worstIndividual=population[i];
worst_index=i;
}
sum+=population[i].fitness;
}
/*find out the best individual so far*/
if(generation==0)
currentBest=bestIndividual;
else
{
if(bestIndividual.fitness>currentBest.fitness)
currentBest=bestIndividual;
}
}
/************************************************************
* Function: Perform evolution operation based on *
* elitise model.Elitist model is to re- *
* place the worst of this generation by *
* the current best one. *
* Variables: None. *
************************************************************/
void PerformEvolution(void)
{
if(bestIndividual.fitness>currentBest.fitness)
currentBest=population[best_index];
else
population[worst_index]=currentBest;
}
/************************************************************
* Function: Perform Selection Operation. *
* Variables: None. *
************************************************************/
void SelectionOperator(void)
{
int i,index;
double p,sum=0.0;
double cfitness[POPSIZE];/*cumulative fitness value*/
struct individual newpopulation[POPSIZE];
/*calculate relative fitness*/
for(i=0;i<POPSIZE;i++)
sum+=population[i].fitness;
for(i=0;i<POPSIZE;i++)
cfitness[i]=population[i].fitness/sum;
/*calculate cumulative fitness*/
for(i=1;i<POPSIZE;i++)
cfitness[i]=cfitness[i-1]+cfitness[i];
/*selection operation*/
for(i=0;i<POPSIZE;i++)
{
p=rand()%1000/1000.0;
index=0;
while(p>cfitness[index])
index++;
newpopulation[i]=population[index];
}
for(i=0;i<POPSIZE;i++)
population[i]=newpopulation[i];
}
/************************************************************
* Function: Perform Crossover Operation. *
* Variables: None. *
************************************************************/
void CrossoverOperator(void)
{
int i,j;
int index[POPSIZE];
int point,temp;
double p;
char ch,chrom_i_grad[CHROMLENGTH],chrom_j_grad[CHROMLENGTH];
/*make a pair of individual randomly*/
for(i=0;i<POPSIZE;i++)
index[i]=i;
for(i=0;i<POPSIZE;i++)
{
point=random(POPSIZE-i);
temp=index[i];
index[i]=index[point+i];
index[point+i]=temp;
}
for(i=0;i<POPSIZE-1;i+=2)
{
p=rand()%1000/1000.0;
if(p<Pc)
{
point=random(CHROMLENGTH-1)+1;
for(j=point;j<CHROMLENGTH;j++)
{
ch=population[index[i]].chrom[j];
population[index[i]].chrom[j]=population[index[i+1]].chrom[j];
population[index[i+1]].chrom[j]=ch;
}
}
}
}
/************************************************************
* Function: Perform Mutation Operation. *
* Variables: None. *
************************************************************/
void MutationOperator(void)
{
int i,j;
double p;
char chrom[CHROMLENGTH];
for(i=0;i<POPSIZE;i++)
for(j=0;j<CHROMLENGTH;j++)
{
p=rand()%1000/1000.0;
if(p<Pm)
{
population[i].chrom[j]=population[i].chrom[j]^1;
}
}
}
/************************************************************
* Function: Record and Output the results of *
* currnet population. *
* Variables: None. *
************************************************************/
void OutputTextReport(void)
{
int i;
int temp[NUMBER];
double sum;
double average;
sum=0.0;
for(i=0;i<POPSIZE;i++)
sum+=population[i].value;
average=sum/POPSIZE;
printf(" gen=%d, avg=%f, best=%f,\n",generation,average,currentBest.value);
printf(" chromosome=");
for(i=0;i<CHROMLENGTH;i++)
printf("%c",currentBest.chrom[i]);
printf("\n");
/*calculate the optimum Xs and its object value and record them in recordLog*/
temp[0]=DecodeChromosome(currentBest.chrom,0,Length[0]);
X_optimal[0]=(x_max[0]-x_min[0])*temp[0]/((pow(2,Length[0])-1.0))-(x_max[0]-x_min[0])/2.0;
for(i=1;i<NUMBER;i++)
{
temp[i]=DecodeChromosome(currentBest.chrom,Length[i-1],Length[i]);
X_optimal[i]=(x_max[i]-x_min[i])*temp[i]/((pow(2,Length[i])-1.0))-(x_max[i]-x_min[i])/2.0;
}
recordLog=(struct log *)malloc(sizeof(struct log));
if(recordLog==NULL)
{
printf("\nLack of Memory!\n");
exit(0);
}
recordLog->next=NULL;
end->generation=generation;
for(i=0;i<NUMBER;i++)
end->info[i]=X_optimal[i];
end->value=currentBest.value;
end->next=recordLog;
end=recordLog;
log_length++;
}
/************************************************************
* Function: Write results to documents E:\log.txt *
* . *
* Variables: None. *
************************************************************/
void writeToFile(void)
{
int i,j;
int temp1,temp2;
double X[NUMBER];
FILE *logFile;
recordLog=head;
printf("\n\nConvergence Process is Iterative and Recorded in E:\\log.txt.\n");
if((logFile=fopen("E:\\log.txt","w+"))==NULL)
{
printf("\nCannot open file!\n");
exit(1);
}
fprintf(logFile,"\t\t\t");
for(i=0;i<NUMBER;i++)
fprintf(logFile," X%d\t\t",i+1);
fprintf(logFile," f(X[...])\n");
while(recordLog->next!=NULL)
{
fprintf(logFile,"Generation %4d:\t",recordLog->generation);
for(i=0;i<NUMBER;i++)
fprintf(logFile,"%.8f\t",recordLog->info[i]);
fprintf(logFile,"%.10f\n",recordLog->value);
recordLog=recordLog->next;
fprintf(logFile,"\n");
}
fclose(logFile);
/*for(i=0;i<log_length;i++)
{
recordLog=head;
head=head->next;
free(recordLog);
}*/
}
/************************************************************
* Function: Draw the convergence process on screen *
* . *
* Variables: None. *
************************************************************/
void drawLine(void)
{
int i=0;
float value[200];
initgr(); /* BGI初始化 */
recordLog=head;
while(recordLog->next!=NULL)
{
value[i]=recordLog->value;
recordLog=recordLog->next;
i++;
}
printf("\n\n\n\n\n\n\n\n\nTHE CONVERGENCE PROCESS IS DRAWN BELOW!");
for(i=1;i<MaxGeneration;i++)
{
line((i-1)*3,0.1*value[i-1],i*3,0.1*value[i]);
}
getch(); /* 暂停一下,看看前面绘图代码的运行结果 */
closegr(); /* 恢复TEXT屏幕模式 */
/*释放链表所占空间*/
for(i=0;i<log_length;i++)
{
recordLog=head;
head=head->next;
free(recordLog);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -