mardi 2 juillet 2019

My C code for a genetic algortihtm is not functioning, it dosent enter into the if condition

This is GA for a timetable problem. Im trying to create an initial population, but it isnt working as it isnt entering the if condition. can someone point out the error.

i tried inserting statements in each conditition, but everything checks out. Still i dont 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 initialised for this timetable problem but the code does not enter the if condtion in the do while loop.




Aucun commentaire:

Enregistrer un commentaire