C语言多元函数的遗传算法 遗传算法多元函数最优解原理( 四 )


void evaluate(void)
{
int mem;
int i;
double x[NVARS+1];
for (mem = 0; memPOPSIZE; mem++)
{
for (i = 0; iNVARS; i++)
x[i+1] = population[mem].gene[i];
population[mem].fitness = (x[1]*x[1]) - (x[1]*x[2]) + x[3];
}
}
/***************************************************************/
/* Keep_the_best function: This function keeps track of the*/
/* best member of the population. Note that the last entry in*/
/* the array Population holds a copy of the best individual*/
/***************************************************************/
void keep_the_best()
{
int mem;
int i;
cur_best = 0; /* stores the index of the best individual */
for (mem = 0; memPOPSIZE; mem++)
{
if (population[mem].fitnesspopulation[POPSIZE].fitness)
{
cur_best = mem;
population[POPSIZE].fitness = population[mem].fitness;
}
}
/* once the best member in the population is found, copy the genes */
for (i = 0; iNVARS; i++)
population[POPSIZE].gene[i] = population[cur_best].gene[i];
}
/****************************************************************/
/* Elitist function: The best member of the previous generation */
/* is stored as the last in the array. If the best member of*/
/* the current generation is worse then the best member of the*/
/* previous generation, the latter one would replace the worst*/
/* member of the current population*/
/****************************************************************/
void elitist()
{
int i;
double best, worst;/* best and worst fitness values */
int best_mem, worst_mem; /* indexes of the best and worst member */
best = population[0].fitness;
worst = population[0].fitness;
for (i = 0; iPOPSIZE - 1; ++i)
{
if(population[i].fitnesspopulation[i+1].fitness)
{
if (population[i].fitness = best)
{
best = population[i].fitness;
best_mem = i;
}
if (population[i+1].fitness = worst)
{
worst = population[i+1].fitness;
worst_mem = i + 1;
}
}
else
{
if (population[i].fitness = worst)
{
worst = population[i].fitness;
worst_mem = i;
}
if (population[i+1].fitness = best)
{
best = population[i+1].fitness;
best_mem = i + 1;
}
}
}
/* if best individual from the new population is better than */
/* the best individual from the previous population, then*/
/* copy the best from the new population; else replace the*/
/* worst individual from the current population with the*/
/* best one from the previous generation*/
if (best = population[POPSIZE].fitness)
{
for (i = 0; iNVARS; i++)
population[POPSIZE].gene[i] = population[best_mem].gene[i];
population[POPSIZE].fitness = population[best_mem].fitness;
}
else
{
for (i = 0; iNVARS; i++)
population[worst_mem].gene[i] = population[POPSIZE].gene[i];
population[worst_mem].fitness = population[POPSIZE].fitness;
}
}
/**************************************************************/
/* Selection function: Standard proportional selection for*/
/* maximization problems incorporating elitist model - makes*/
/* sure that the best member survives*/
/**************************************************************/
void select(void)
{
int mem, i, j, k;
double sum = 0;
double p;
/* find total fitness of the population */
for (mem = 0; memPOPSIZE; mem++)
{
sum += population[mem].fitness;
}
/* calculate relative fitness */
for (mem = 0; memPOPSIZE; mem++)
{
population[mem].rfitness =population[mem].fitness/sum;
}
population[0].cfitness = population[0].rfitness;

推荐阅读