📄 prog.cpp
字号:
for (mem = 0; mem < POPSIZE; mem++)
{
population[mem].rfitness = population[mem].fitness/sum;
}
population[0].cfitness = population[0].rfitness;
/* calculate cumulative fitness */
for (mem = 1; mem < POPSIZE; mem++)
{
population[mem].cfitness = population[mem-1].cfitness +
population[mem].rfitness;
}
/* finally select survivors using cumulative fitness. */
for (i = 0; i < POPSIZE; i++)
{
p = rand()%1000/1000.0;
if (p < population[0].cfitness)
newpopulation[i] = population[0];
else
{
for (j = 0; j < POPSIZE;j++)
if (p >= population[j].cfitness &&
p<population[j+1].cfitness)
newpopulation[i] = population[j+1];
}
}
/* once a new population is created, copy it back */
//***************************************************************
/* finally select survivors using cumulative fitness. */
//
//struct genotype GMax;
//
//for (i = 0; i < POPSIZE; i++)
//{
// GMax.fitness = 0;
// srand((unsigned)time(NULL));
// for(int k=0;k<K;k++)
// {
// p = rand()%1000/1000.0;
// if (p < population[0].cfitness && GMax.fitness<population[0].fitness)
// GMax = population[0];
// else
// {
// for (j = 0; j < POPSIZE;j++)
// {
// if (p >= population[j].cfitness &&
// p<population[j+1].cfitness
// && GMax.fitness<population[j+1].fitness)
// GMax = population[j+1];
// }
// }
// }
// newpopulation[i]=GMax;
//}
//
//*************************************************************
//int q[K];
//int m, n, min, best;
//double max;
//
//for(i=0;i<POPSIZE;i++)
//{
// for(j=0;j<K;j++)
// q[j] = ((int)(rand()%POPSIZE));
// min=q[0];
// best=min;
// max=population[min].rfitness;
//
// //best=q[0];
// //max=population[best].rfitness;
//
// for(j=1;j<K;j++)
// {
// min=q[j];
// if(population[min].rfitness>max)
// {
// max=population[min].rfitness;
// best=min;
// }
// }
// newpopulation[i] = population[best];
//}
//********************************************************************
for (i = 0; i < POPSIZE; i++)
population[i] = newpopulation[i];
}
/***************************************************************/
/* Crossover selection: selects two parents that take part in */
/* the crossover. Implements a single point crossover */
/***************************************************************/
void crossover(void)
{
int i, mem, one;
int first = 0; /* count of the number of members chosen */
double x;
for (mem = 0; mem < POPSIZE; ++mem)
{
x = rand()%1000/1000.0;
if (x < PXOVER)
{
++first;
if (first % 2 == 0)
Xover(one, mem);
else
one = mem;
}
}
}
/**************************************************************/
/* Crossover: performs crossover of the two selected parents. */
/**************************************************************/
void Xover(int one, int two)
{
int i;
int point; /* crossover point */
/* select crossover point */
if(NVARS > 1)
{
if(NVARS == 2)
point = 1;
else
point = (rand() % (NVARS - 1)) + 1;
for (i = 0; i < point; i++)
swap(&population[one].gene[i], &population[two].gene[i]);
}
}
/*************************************************************/
/* Swap: A swap procedure that helps in swapping 2 variables */
/*************************************************************/
void swap(double *x, double *y)
{
double temp;
temp = *x;
*x = *y;
*y = temp;
}
/**************************************************************/
/* Mutation: Random uniform mutation. A variable selected for */
/* mutation is replaced by a random value between lower and */
/* upper bounds of this variable */
/**************************************************************/
void mutate(void)
{
int i, j;
double lbound, hbound;
double x;
for (i = 0; i < POPSIZE; i++)
for (j = 0; j < NVARS; j++)
{
x = rand()%1000/1000.0;
if (x < PMUTATION)
{
/* find the bounds on the variable to be mutated */
lbound = population[i].lower[j];
hbound = population[i].upper[j];
population[i].gene[j] = randval(lbound, hbound);
}
}
}
/***************************************************************/
/* Report function: Reports progress of the simulation. Data */
/* dumped into the output file are separated by commas */
/***************************************************************/
void report(void)
{
int i;
double best_val; /* best population fitness */
double avg; /* avg population fitness */
double stddev; /* std. deviation of population fitness */
double sum_square; /* sum of square for std. calc */
double square_sum; /* square of sum for std. calc */
double sum; /* total population fitness */
sum = 0.0;
sum_square = 0.0;
for (i = 0; i < POPSIZE; i++)
{
sum += population[i].fitness;
sum_square += population[i].fitness * population[i].fitness;
}
avg = sum/(double)POPSIZE;
square_sum = avg * avg * POPSIZE;
stddev = sqrt(fabs(sum_square - square_sum)/(POPSIZE - 1));
best_val = population[POPSIZE].fitness;
fprintf(galog, "\n%5d %6.9f %6.9f %6.9f ", generation,
best_val, avg, stddev);
}
/**************************************************************/
/* Main function: Each generation involves selecting the best */
/* members, performing crossover & mutation and then */
/* evaluating the resulting population, until the terminating */
/* condition is satisfied */
/**************************************************************/
void main(void)
{
int i;
if ((galog = fopen("galog.txt","w"))==NULL)
{
exit(1);
}
generation = 0;
fprintf(galog, "\n generation best average standard \n");
fprintf(galog, " number value fitness deviation \n");
initialize();
evaluate();
keep_the_best();
while(generation<MAXGENS)
{
generation++;
select();
crossover();
mutate();
report();
evaluate();
elitist();
}
fprintf(galog,"\n\n Simulation completed\n");
fprintf(galog,"\n Best member: \n");
FILE *fBest;
int gener = Best[0]+1;
fBest=fopen("Best.txt","w");
fprintf(fBest,"%-8d %-8.3f %-8.3f %-10d %-12.9f",POPSIZE,PXOVER,PMUTATION,gener,Best[1]);
fclose(fBest);
for (i = 0; i < NVARS; i++)
{
fprintf (galog,"\n var(%d) = %3.9f",i,population[POPSIZE].gene[i]);
}
fprintf(galog,"\n\n Best fitness = %3.9f",population[POPSIZE].fitness);
fclose(galog);
printf("Success\n");
}
/***************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -