c - 遗传算法,种群初始化
问题描述
我正在尝试找到解决初始化时间表的问题的方法,必须打印days[][]
每个时间表才能成功生成新的时间表。oldpop
天数中的每个位置必须由一个数字组成,其中10 位显示房间号,一个位显示主题号。房间号数组由房间可用或空闲的每个小时组成,任何小时和天的组合都会告诉我们房间是否空闲,即 = 0或占用= 1。同样,主题数组告诉我们每个主题每周必须教多少次。
这段代码中有很多未使用的变量,请忽略它们,因为我稍后会实现它们。我也想为极其错误的缩进道歉。时间限制了我从现在开始纠正这一点。
#include<stdio.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include <time.h>
#define MAXP 10 //population size (best if chosen between 10-15)
struct pop{
int days[5][9];
int faculty;
int students;
int subjects[6];
int labs[2];
int rooms[9][5][9];
int labaratory[9][2];
int obj;
} old_pop[MAXP],benchmark;
int random_number_creator(int upper, int lower)
{
int n;
n = rand() % (upper-lower)+ lower;
return n;
}
void initialize_days(struct pop b)
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<9;j++)
if(j!=5)
b.days[i][j]=0;
else
b.days[i][j]=11111;
}
}
void initialize_benchmark(struct pop b)
{
int i,j,k;
for(i=0;i<5;i++)
{
for(j=0;j<9;j++)
if(j!=5)
b.days[i][j]=0;
else
b.days[i][j]=11111;
}
b.faculty=9;
b.students=100;
for(i=0;i<6;i++)
{
if(i<4)
b.subjects[i]=3;
else
b.subjects[i]=2;
}
for(i=0;i<2;i++)
{
b.labs[i]=3;
}
for(i=0;i<9;i++)
{
for(j=0;j<5;j++)
for(k=0;k<9;k++)
if(i!=5)
b.rooms[i][j][k]=0;
}
for(i=6;i<9;i++)
{
for(j=0;j<8;j++)
b.labaratory[i][j]=0;
}
}
int check_avail_days(struct pop p, int b)
{
int j,count=0;
for(j=0;j<9;j++)
{
if(p.days[b][j]==0)
count+=1;
}
return (count);
}
int check_avail_hours(struct pop b,int d)
{
int i,count=0;
for(i=0;i<9;i++)
{
if(b.days[d][i]==0)
count+=1;
}
return count;
}
int hours_alloter(struct pop b,int d)
{
int i,j=0;
int arr[9]={0,0,0,0,0,0,0,0,0};
for(i=0;i<9;i++)
{
if(b.days[d][i]==0)
{
arr[j]=i;
j++;
}
}
int n;
n=random_number_creator(j,0);
return arr[n];
}
int availble_room(struct pop b,int s, int r, int q)
{
if(b.rooms[s][r][q]==0)
return 1;
else
return 0;
}
void initpop()
{
int i,j,count=0, a=0;
for(i=0;i<MAXP;i++)
old_pop[i].obj=0;
for(i=0;i<MAXP;i++)
{
initialize_benchmark(benchmark);
initialize_days(old_pop[i]);
do{
printf("\nloop 1 entery\n");
int subject1,ss,fa=benchmark.faculty,h1;
for(subject1=random_number_creator(6,0);benchmark.subjects[subject1]>=0;benchmark.subjects[subject1]--)
{
printf("\nloop 2 entery\n");
int cou=1,r;
ss=subject1*10;
while(cou!=0&&benchmark.subjects[subject1]!=0&&fa!=0)
{
printf("\nloop 3 entery\n");
int d=random_number_creator(6,0),hi;
cou=check_avail_hours(benchmark,d);
hi=hours_alloter(benchmark,d);
old_pop[i].days[d][h1]=ss;
do
{
printf("\nloop 4 entery\n");
r=random_number_creator(9,0);
if(availble_room(benchmark,r,d,hi)==1)
{
benchmark.rooms[r][d][hi]=1;
old_pop[i].days[d][hi]+=r;
benchmark.faculty-=1;
fa++;
a++;
}
else
continue;
}while(a==0);
}
old_pop[i].faculty=fa;
}
for(i=0;i<6;i++)
{
count+=benchmark.subjects[i];
}
}while(count!=0);
}
}
int main()
{
initpop();
int i,j,k;
for(i=0;i<MAXP;i++)
{
printf("\nPop %d\n",i);
for(j=0;j<5;j++)
{ printf("\n");
for(k=0;k<9;k++)
printf(" %d",old_pop[i].days[j][k]);
}
}
return 0;
}
目前的问题
代码陷入无限循环,使我无法理解逻辑错误。
预期结果
随机生成10个时间表,每个位置在10的位置指定房间号,在1的位置指定主题编号。即,52 表示第 5 个房间和第 2 个主题
解决方案
推荐阅读
- haskell - 在haskell中如何将值解析为这个插入函数
- python-3.x - .destroy() 之后是否有可能“恢复”按钮?
- php - Liskov 替换原则和 PHP 接口
- bootstrap-4 - 媒体查询在引导移动模式下不起作用
- react-native - 在 flatlist 中加载 200 多个数据时,React 本机应用程序崩溃
- ios - 使用条件排序数组快速减少和求和
- asp.net-mvc - 如何从右到左制作网站标签,它不起作用
- python - 尝试使用 python 自动完成 Web 表单滑块
- webpack - 阻止 Nativescript 使用 webpack
- swift - 扩展中的重载函数会产生错误