我的遗传算法C代码不起作用,它进入了if条件 [英] My C code for a genetic algortihtm is not functioning, it dosent enter into the if condition

查看:153
本文介绍了我的遗传算法C代码不起作用,它进入了if条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是GA的时间表问题.我正在尝试创建初始填充,但由于未输入if条件,因此无法正常工作.有人可以指出错误吗?

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]);

    }
}

我希望针对此时间表问题初始化oldpop变量,但代码不会在do while循环中输入if条件.

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 }):

s=random_number_creator(5,0); 

将返回0到4之间的随机数.

Will return a random number between 0 and 4 included.

要纠正此问题,只需用

s=random_number_creator(7,0);

要选择一个介于0到6之间的数字,因此cou变量将能够到达0

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

您的代码可以改进:

代替这种阻止:

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

}

创建函数,学习使用printf:

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

并以这种方式使用

print_table(oldpop, "oldpop");

这篇关于我的遗传算法C代码不起作用,它进入了if条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆