This is GA for a timetable problem. I'm trying to create an initial population, but it isn't working as it isn't entering the if condition. can someone point out the error?
I tried inserting statements in each condition, but everything checks out. Still, I don't seem to find a solution.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
int random_number_creator(int upper, int lower)
{
int n;
n = rand() % (upper-lower)+ lower;
return n;
}
struct pop{
int subjects[6];
int days[5][9];
}oldpop, bench;
main()
{
int i,j,s=1,d,h,stop=1,cou=0;
for(i=0;i<5;i++)
{
for(j=0;j<9;j++)
if(j!=6)
bench.days[i][j]=0;
else
bench.days[i][j]=11111;
}
for(i=0;i<6;i++)
{
if(i<4)
oldpop.subjects[i]=3;
else
oldpop.subjects[i]=2;
}
for(i=0;i<5;i++)
{
printf("\n");
for(j=0;j<9;j++)
printf(" %d",bench.days[i][j]);
}
for(i=0;i<6;i++)
{
printf(" \n %d",oldpop.subjects[i]);
}
cou=0;
for(i=0;i<6;i++)
{
cou=cou+ oldpop.subjects[i];
}
printf("\n%d\n",cou);
do
{
s=random_number_creator(5,0);
printf("\nsubject number:%d\n",s);
printf("\nloop 1 entery %d",cou);
do
{
printf("\nloop 2 entry\n");
d=random_number_creator(5,0);h=random_number_creator(8,0);
printf("\nDay Number:%d \nHour Number:%d\n",d,h);
if(bench.days[d][h]==0&&oldpop.subjects[s]!=0)
{
printf("\nif condition reached\n");
oldpop.days[d][h]=10+s;
bench.days[d][h]=11111;
stop=0;
cou--;
oldpop.subjects[s]--;
}
else
{
printf("\nIf condition not satisified.\n");
break;
}
}while(stop!=0);
}while(cou!=0);
for(i=0;i<5;i++)
{
printf("final entery \n");
for(j=0;j<9;j++)
printf(" %d",oldpop.days[i][j]);
}
}
I want the oldpop variable to be initialized for this timetable problem but the code does not enter the if condition in the do while loop.
The problem in your program comes from the subject selection (after adding the missing
}
):Will return a random number between 0 and 4 included.
To correct this, just replace this line by
To pick a number between 0 and 6. So the
cou
variable will be able to reach0
Your code can be improved:
Instead of this kind of block:
Create a function, learn to use
printf
:And use it this way