I am writing a genetic algortihm and i have a problem in printing some struct variables. I know that the code reads them properly because i get a result but when i try to print them on screen or in a file a get zeros..
This is the code i am using can you please help me out...??
This is the code i am using can you please help me out...??
#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);
}