Hello to all,
I posted again today. I have the code bellow where I am trying to get the same results in the two print outputs ( the fitness of chromosome SAME and its not true SAME ) for each old_chrome
the one is the result without the function and the other with the function
BUT MY OUTPUT IS AS BELOW
0 0 0.110000
0 1 3.100000
0 2 5.120000
1 0 2.530000
1 1 2.270000
1 2 4.130000
2 0 3.100000
2 1 1.240000
2 2 7.230000
3 0 6.780000
3 1 7.450000
3 2 3.390000
8.220000
the fitness of chromosome 0 8.220000
its not true 6.200000
the fitness of chromosome 1 6.400000
its not true 6.200000
the fitness of chromosome 2 8.470000
its not true 6.200000
the fitness of chromosome 3 10.840000
its not true 6.200000
The first lines is the reading of the parameters.
I would welcome some help about how it will work and some explanation if you also could
Many thanks
Chris
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
struct chromosome {
double fitness;
double genes[3];
};
struct chromosome old_chrome[4];
double fit(struct chromosome *p);
main()
{
/****DECLARE VARIABLES****/
FILE *fpl_0;
FILE *fpl_1;
FILE *fpl_2;
FILE *fpl_3;
double n1;
int i, j,n2;
int k=0;
int num_of_chromes;
int num_of_genes;
char line[100];
/*****************************/
/********READ INPUT DAT..........****/
fpl_0=fopen("data_fit", "r");
if (fpl_0==NULL)
{
printf("UNABLE TO OPEN INPUT FILE TO READ PROGRAM PARAMETERS\n");
exit(1);
}
while (fgets(line,100,fpl_0)!=NULL)
{
sscanf(line,"%d %d", &num_of_chromes, &num_of_genes);
}
fclose(fpl_0);
fpl_1=fopen("input_fit","r");
if (fpl_1==NULL)
{
printf("UNABLE TO OPEN INPUT FILE TO READ GENES DATA\n");
exit(2);
}
for (i=0;i<num_of_chromes;i++)
{
for (j=0;j<num_of_genes;j++)
{
fscanf(fpl_1,"%lf",&old_chrome[i].genes[j]);
}
}
fclose(fpl_1);
/********************CLOSE INPUT FILES*************/
/****************PRINT INPUT DAT..........***************/
printf("*****************INPUT*************************\n");
for(i=0;i<num_of_chromes;i++)
{
for (j=0;j<num_of_genes;j++)
{
printf("%d %d %lf\n",i,j,old_chrome[i].genes[j]);
}
}
printf("%lf\n",old_chrome[0].genes[1]+old_chrome[0].genes[2]);
/* UNTILL HERE READ DATA TO STRUCTURE FROM FILE AND CHECK IF IS OK***/
for (i=0;i<num_of_chromes;i++)
{
old_chrome[i].fitness=old_chrome[i].genes[1]+old_chrome[i].genes[2];
printf("the fitness of chromosome %d %lf \n", i,old_chrome[i].fitness);
n1=fit(old_chrome);
printf("its not true %lf\n",n1);
}
}
/*******fitness function******/
double fit(struct chromosome *p)
{
int i;
double a;
a = p->genes[1]+p->genes[1];
return a;
}