2013-03-01 123 views
0

我正在写一个遗传算法,我在打印结构变量时遇到了问题。我知道代码正确地读取它们,因为我得到了一个结果,但是当我尝试在屏幕上或文件中打印它们时,得到零..结构变量打印

这是我使用的代码,请你帮我解决.. ??

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 

#define POPSIZE 50 
#define MAXGENS 1000 
#define NVARS 3 
#define PXOVER 0.8 
#define PMUTATION 0.15 
#define B 2 
#define TOURNAMENT_SIZE 2 
#define TRUE 1 
#define FALSE 0 

int generation ; 
int cur_best; 
FILE *galog_tournament; 

struct genotype 
{ 
    double gene[NVARS]; 
    double fitness; 
    double upper[NVARS]; 
    double lower[NVARS]; 
    double cfitness; 
    double rfitness; 
}; 

struct genotype population[POPSIZE+1]; 
struct genotype newpopulation[POPSIZE+1]; 


void initialize(void); 
double randval(double,double); 
void evaluate(void); 
void keep_the_best(void); 
void elitist(void); 
void tournament_selection(void); 
void crossover(void); 
void random_Xover(int,int); 
void swap(double *,double *); 
void non_uniform_mutation(void); 
void report(void); 
double delta2(int,int); 


void initialize(void) 
{ 
    FILE *infile; 
    FILE *bounds; 
    FILE *popu; 
    int i,j; 
    double lbound,ubound; 

    bounds = fopen("bounds.txt","w"); 
    popu = fopen("population.txt","w"); 


    if((infile = fopen("gadata.txt","r"))== NULL) 
    { 
     fprintf(galog_tournament,"\n Cannot open input file\n"); 
     exit(1); 
    } 


    for(i = 0; i < NVARS; i++) 
    { 
     fscanf(infile, "%lf",&lbound); 
     fscanf(infile, "%lf",&ubound); 

     printf("%lf %lf\n",lbound,ubound); 

     for(j = 0; j < NVARS; j++) 
      { 
       population[j].fitness = 0; 
       population[j].rfitness = 0; 
       population[j].cfitness = 0; 
       population[j].lower[i] = lbound; 
       population[j].upper[i] = ubound; 

       fprintf(bounds,"%lf %lf\n",&population[j].lower[i],&population[j].upper[i]); 

       population[j].gene[i] = randval(population[j].lower[i],population[j].upper[i]); 

       fprintf(popu,"%lf\n",population[j].gene[i]); 
      } 

    } 
    fclose(infile); 
    fclose(bounds); 
    fclose(popu); 
} 

家伙非常感谢你为你居然帮我tryied调试代码,并改变它了一点,所以这是打印屏幕上的值..可惜的是我仍然得到零...!这是代码,并与下我的输入文件和上限

void initialize(void) 
{ 
    FILE *infile; 
    FILE *test; 
    int i,j; 
    double lbound,ubound; 

    test = fopen("test.txt","w"); 



    if((infile = fopen("gadata.txt","r"))== NULL) 
    { 
     fprintf(galog_tournament,"\n Cannot open input file\n"); 
     exit(1); 
    } 


    for(i = 0; i < NVARS; i++) 
    { 
     fscanf(infile, "%lf",&lbound); 
     fscanf(infile, "%lf",&ubound); 

     printf("%lf %lf\n",lbound,ubound); 


    for (j=0; j < POPSIZE; j++) { 
     population[j].fitness = 0; 
     population[j].rfitness = 0; 
     population[j].cfitness = 0; 
     population[j].lower[i] = lbound; 
     population[j].upper[i] = ubound; 
     population[j].gene[i] = randval(population[j].lower[i], 
             population[j].upper[i]); 






    } 


    } 

    fclose(infile); 
    fclose(test); 

} 






    0.000000 0.000000 
    0.000514 0.010514 
    0.011307 0.021307 
    0.021876 0.031876 
    0.033994 0.043994 
    0.043272 0.053272 
    0.050229 0.060229 
    0.053976 0.063976 
    0.053803 0.063803 
    0.046640 0.056640 
    0.029907 0.039907 
    0.017619 0.027619 
    0.002316 0.
    -0.005428 0.004572 
    -0.00683 0.00317 
    -0.009743 0.000257 
    -0.10582 -0.09582 
    -0.026304 -0.016304 
    -0.027064 -0.017064 
    -0.025243 -0.015243 
    -0.022386 -0.
    -0.019108 -0.009108 
    -0.015788 -0.005788 
    -0.012185 -0.002185 
    -0.009452 0.000548 
    -0.052133 0.002867 
    -0.006128 0.003872 
    0.000000 0.000000 
+0

尝试调试你的代码,并隔离问题。 – aaaaaa123456789 2013-03-01 10:48:34

回答

4

fprintf(bounds,"%lf %lf\n",&population[j].lower[i],&population[j].upper[i]); 

取出&这么变成:

fprintf(bounds,"%lf %lf\n",population[j].lower[i],population[j].upper[i]); 

要打印的值你的变量不是该变量的地址。

设置您的编译器显示警告,在这种情况下,它会告诉您有关问题。

+0

可能更容易记住:如果%s,则传递指针,否则传递值。 – Triang3l 2013-03-01 11:03:33

0

你可能不想你的代码运行,如果FSCANF不读任何东西,返回0。你为什么不改变它的财产以后像

while(0 != fscanf(infile, "%lf",&ubound)) 
{ 
    if(0 == fscanf(infile, "%lf",&lbound) || i >= NVARS)  
     { 
     return 0; 
     } 
    else 
     { 
     printf("%lf %lf\n",lbound,ubound); 
     i++ 
     } 
} 

然后如果它dosnt打印任何东西,你知道你不是在任何扫描与fscanf。我猜你不想继续用0填充你的数组,如果你的scanf没有读取任何东西,它可能会指向你在错误的方向