任何人都可以帮我这个代码吗?我不明白为什么在两者之间停下来! [英] Can anyone help me with this code? I cant understand why is stopping in between!

查看:56
本文介绍了任何人都可以帮我这个代码吗?我不明白为什么在两者之间停下来!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/* ****************************************** * Simple Genetic Algorithm * ****************************************** */
#include <stdlib.h>
#include <stdio.h>
#include 
#include <math.h>
#include <conio.h>

#define RaND_MAX 0x7FFFFFFF
#define random(num) (rand()%(num))
#define randomize() (srand((unsigned)time(NULL)))
#define POPULATION_SIZE 4
#define CHROM_LENGTH 4
#define PCROSS 0.6
#define PMUT 0.050
#define MAX_GEN 3
struct population
{
int value;
double gain;
unsigned char string[CHROM_LENGTH];
unsigned int fitness;
};
struct population pool[POPULATION_SIZE];
struct population new_pool[POPULATION_SIZE];
int selected[POPULATION_SIZE];
int generations;
/******************************************** encode ** Code a integer into binary string ********************************************/
void encode(int index, int value)
{
int i;
for (i=0; i < CHROM_LENGTH; i++)
pool[index].string[CHROM_LENGTH-1-i] = (value >> i) & 0x01;    /* bit wise and */

}

/******************************************** initialise_population ** Creates and initialize a population ********************************************/
void initialise_population()
{
int i;
void randomize();
for (i=0; i < POPULATION_SIZE; i++)
{
encode(i, random(2^CHROM_LENGTH));
printf("the value of i is %d \n",i);
}

}
/******************************************** select ** Selects strings for reproduction ********************************************/
int select(double sum_fitness)
{
int i;
int j;
double r, parsum;
parsum = 0;
j= rand ();
printf("the value of j is %d \n",j);
r = (double)(j % (int)sum_fitness); /* spin the roulette */
for (i=0; i < POPULATION_SIZE, parsum <= r; i++)
parsum += pool[i].fitness;
return (--i); /* returns a selected string */
}
/******************************************** flip ** Toss a biased coin ********************************************/
int flip(double prob)
{
double i;
i=((double)rand())/RaND_MAX;
if ((prob == 1.0) || (i < prob))
return (1);
else
return (0);
}

/******************************************** crossover ** Swaps 2 sub-strings ********************************************/
void crossover(int parent1, int parent2, int child1, int child2)
{
int i, site;
if (flip(PCROSS))
site = random(CHROM_LENGTH);
else
site = CHROM_LENGTH-1;
for ( int i=0;  i < CHROM_LENGTH; i++)
{
if ((i <= site) || (site==0))
{
new_pool[child1].string[i] = pool[parent1].string[i];
new_pool[child2].string[i] = pool[parent2].string[i];
}else
{
new_pool[child1].string[i] = pool[parent2].string[i];
new_pool[child2].string[i] = pool[parent1].string[i];
}
}
}
/******************************************** mutation ** Changes the values of string position ********************************************/
void mutation ()
{
int i, j;
for (i=0; i < POPULATION_SIZE; i++)
{
for (j=0; j < CHROM_LENGTH; j++)
 if (flip(PMUT))
pool[i].string[j] = ~new_pool[i].string[j] & 0x01;
else
pool[i].string[j] = new_pool[i].string[j] & 0x01;
}
}
/******************************************** decode ** Decode a binary string into an integer ********************************************/
int decode(int index)
{
int i, value;
value = 0;
for (i=0; i < CHROM_LENGTH; i++)
value += (int)pow(2.0,(double)i) * pool[index].string[CHROM_LENGTH-1-i];
return(value);
}
/******************************************** evaluate ** Objective function f(x)=x^2 ********************************************/
int evaluate(int value)
{
	double gain,gm2,gm6,i5;
	gain=(2*gm2*gm6)/(i5*0.0000004311);
return(gain);
}
/******************************************** statistics ** Print entermediary results ********************************************/
void statistics()
{
int i, j;
double gain;
printf("\n;Generation: %d\n;Selected Strings\n;", generations);
for (i=0; i< POPULATION_SIZE; i++)
printf(" %d", selected[i]);
 printf("\n");
printf("\n;X\tf(x)\t New_String\tX' \t gain");
for (i=0; i< POPULATION_SIZE; i++)
{
printf("\n %d\t%u\t;", pool[i].value, pool[i].fitness);
for (j=0; j<chrom_length;>printf(" %d",pool[i].string[j]);
printf("\t%d", decode(i));
printf("\t%g",gain);
}
}

main()
{
int i;
double sum_fitness, avg_fitness, old_avg_fitness;
generations = 1;
avg_fitness = 1;
initialise_population();
do
{
old_avg_fitness = avg_fitness;
sum_fitness = 0;        /* fitness evaluation */
double gm2,gm6,i5;
printf("enter the values of gm2 \n gm6 \n i5");
scanf("%g %g %g", &gm2,&gm6,&i5);
for (i=0; i<population_size;>{
pool[i].gain = decode(i);
pool[i].value=pool[i].gain;
pool[i].fitness = evaluate(pool[i].gain);
sum_fitness += pool[i].fitness;
}
avg_fitness = sum_fitness / POPULATION_SIZE;
printf("the sum of fitness is %g \n",sum_fitness);
for (i=0; i<population_size;>selected[i] = select(sum_fitness);
for (i=0; i<population_size;>crossover(selected[i],selected[i+1],i,i+1);
mutation();
statistics();
printf ("\nImprovment: %f\n", avg_fitness/old_avg_fitness);
}
while ((++generations < MAX_GEN) &&
((avg_fitness/old_avg_fitness) > 1.005) ||                 /* logical or */
((avg_fitness/old_avg_fitness) < 1.0));
}





[edit]已添加代码块 - OriginalGriff [/ edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

for (i=0; i< POPULATION_SIZE; i++)
{
    printf("\n %d\t%u\t;", pool[i].value, pool[i].fitness);
    for (j=0; j<chrom_length;>printf(" %d",pool[i].string[j]);
    printf("\t%d", decode(i));
    printf("\t%g",gain);
}





第四行有错误。你想做什么?



there is an error on the fourth line. what are you trying to do?


这篇关于任何人都可以帮我这个代码吗?我不明白为什么在两者之间停下来!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆