Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

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

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("
");
      for(j=0;j<9;j++)
        printf("    %d",bench.days[i][j]);  
    }
    for(i=0;i<6;i++)
    {   
        printf(" 
 %d",oldpop.subjects[i]);
    }
    cou=0;
    for(i=0;i<6;i++)
    { 
        cou=cou+ oldpop.subjects[i];
    }
    printf("
%d
",cou);
    do
    {   
        s=random_number_creator(5,0);
        printf("
subject number:%d
",s);
        printf("
loop 1 entery %d",cou);
        do
        {   
            printf("
loop 2 entry
");
            d=random_number_creator(5,0);h=random_number_creator(8,0);
            printf("
Day Number:%d   
Hour Number:%d
",d,h);
            if(bench.days[d][h]==0&&oldpop.subjects[s]!=0)
            {   
                 printf("
if condition reached
");
                 oldpop.days[d][h]=10+s;
                 bench.days[d][h]=11111;
                 stop=0;
                 cou--;
                 oldpop.subjects[s]--;
            }

            else  
            {
                printf("
If condition not satisified.
");
                break; 
            }
        }while(stop!=0);
    }while(cou!=0);

    for(i=0;i<5;i++)
    {
        printf("final entery 
");
        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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem in your program comes from the subject selection (after adding the missing }):

s=random_number_creator(5,0); 

Will return a random number between 0 and 4 included.

To correct this, just replace this line by

s=random_number_creator(7,0);

To pick a number between 0 and 6. So the cou variable will be able to reach 0


Your code can be improved:

Instead of this kind of block:

for(i=0;i<5;i++)
{
    printf("final entery 
");
    for(j=0;j<9;j++)
        printf("    %d",oldpop.days[i][j]);

}

Create a function, learn to use printf:

void print_table(struct pop pop, const char *label)
{
    int i, j;
    printf("
%s
", label);
    for (i=0;i<5;i++)
    { 
        for (j=0;j<9;j++)
        {
            printf(" %5d",pop.days[i][j]);
        }
    }
}

And use it this way

print_table(oldpop, "oldpop");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...