课表问题及其算法

surlogics 2009-07-23 05:23:53
我们老师让我们做一个排课表程序,要求见http://mcm.saicai.com/bbs/viewthread.php?tid=1799
用MATLAB去做,我准备建立25个二维矩阵去储存25个教师的课表,利用数学规划,最小化冲突。为了分解难度,我打算按照课程类别一类一类的排,但是预测到一个难题,就是那个“教师属性”中有的老师胜任两类课,但是他们的周最大课时数是一定的,如何处理?每位教师的周最大课时数不可能全部满足(因为它们小于课程要求的数量)。另外“尽可能不同天的老师”又如何办?

谢谢。
...全文
502 26 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
enzo_zhou 2010-07-19
  • 打赏
  • 举报
回复
有学问,正好遇到问题在求解,希望有所启发。
hsblaoyao 2010-05-01
  • 打赏
  • 举报
回复
谢谢啦,我会认真研究的
surlogics 2009-07-30
  • 打赏
  • 举报
回复
ooo目前我看不懂LINGO,我还以为是matlab语言呐。
我先恶补一下吧!

谢谢啦,我会认真研究的
iwantnon 2009-07-29
  • 打赏
  • 举报
回复
今天还能上不?我下午写一下。
surlogics 2009-07-29
  • 打赏
  • 举报
回复
我八月就不方便上网了,呵呵。
iwantnon2 2009-07-29
  • 打赏
  • 举报
回复
刚才在原文后面加了个 续,现在已经差不多将所有约束都用上了。
iwantnon 2009-07-29
  • 打赏
  • 举报
回复
刚才又发了一回,这下不出乱码了,
楼上的连接作废,看这个新的吧:
http://blog.csdn.net/iwantnon/archive/2009/07/29/4391368.aspx
iwantnon 2009-07-29
  • 打赏
  • 举报
回复
过程在这个连接:
http://blog.csdn.net/iwantnon/archive/2009/07/29/4391123.aspx
文章开头有一大段乱码,不知道是怎么出来的,跳过去就行了。
三点钟就写完了,发上去后又有乱码、两行之间还总自动加一个空行。整了两个多小时也没弄好,而且每次提交就得五分钟!!
不过对内容没有影响,只是难看点儿,你先凑合着看吧,我再弄弄...
surlogics 2009-07-28
  • 打赏
  • 举报
回复
这算是我在11楼的问题一个中文版吧


我们老师让我们做一个课表编排问题,题目见mcm.saicai.com/bbs/viewthread.php?tid=1799
我试图用基于MATLAB的一个软件TOMLAB做,因为他有一个例子:见
tomsym.com/examples/tomsym_collegetimetable.html

由于我对MATLAB、TOMLAB应用不熟练,我试图先写一个程序尽可能和例子相似。

但是发现tomArrayIdx与subsindex不兼容,并且tomArray无法比较大小的问题。有谁告诉我怎么改程序。谢谢


我将问题简化,先安排第一类课程,有三个老师,5门课。并且我不考虑教室问题。由于每堂课是以两个课时为一个单位,五门课每周分别上2 2 322堂课,每个老师教任意的课,他们的每周最大课时数分别是2 2 3,每天可以上4节课(晚上不排课)(以上的“一节课”均指两小节课)
优化目标:
1:最好在每天的第2、3节安排课程,第一节、第四节尽可能不安排课
2:尽可能满足老师们的最大课时数,使他们加班尽可能少。

程序(TOMLAB实现)
teacher=[1 2 3];
lesson=[1 2 3 4 5];
lesson_times=[2 2 3 2 2];
slots=4*5;
t=tomArrayIdx('t',1:3);
l=tomArrayIdx('l',1:length(lesson));
s=tomArrayIdx('s',1:20);
teach=tomArray('teach',[3,5,20]); %create a array of 3*5*20 (teacher*lesson*slots)

bnds1={0 <=teach <=1}; % All variables are binary

bnds2={sum(sum(teach(t,l,s),s),t)==lesson_times};
%所有的课程必须全部安排进课表

bnds3={sum(sum(teach(t,l,s),t),l) <=1};
% Teacher constraint, one teacher per slot

bnds={bnds1,bnds2,bnds3};

not_so_good_slots=tomArrayIdx('l',[1,4,5,8,9,12,13,16,17,20]);
objective1=sum(vec(teach(l,t,not_so_good_slots)));
%the goal is to minimize teaching courses in these no so good slots

max_work=[2 2 3];
objective2=0;
for i=1:3
overwork=sum(sum(teach(i,l,s),s),l)-max_work(i)
if overwork>0
objective2=objective2+10*abs(overwork);
end
end


但是,当我输入objective1=sum(vec(teach(l,t,not_so_good_slots)));后,提示:
Error in ==> tomArray.subsref at 78
checkIndexes(o);


我输入
for i=1:3
overwork=sum(sum(teach(i,l,s),s),l)-max_work(i)
if overwork>0
objective2=objective2+10*abs(overwork);
end
end
后提示
??? Function 'gt' is not defined for values of class 'tomArray'.

Error in ==> gt at 18
[varargout{1:nargout}] = builtin('gt', varargin{:});

我检查了好几遍,但是还是不知道哪里出的错,又能帮我的吗?
或者用MATLAB的语句如何实现
surlogics 2009-07-28
  • 打赏
  • 举报
回复
我认为不一定非要满足全部条件,我在一开始没思路的情况下手动排第一类课程,但是发现不可能完成,我们可以通过设置一个
罚函数,优化这个问题。使得与所给条件冲突的达到最小.

首先的难题就是那个T01,T08,T15等老师兼任两门学科。我不会处理。

我试图用基于MATLAB的TOMLAB实现,但是遇到tomArray和普通的subsindex不兼容的问题。为此我在论坛上提问,但没有得到回答。

如有知道什么规划软件能够更好的处理这种问题能否说一下,谢谢。

以下是我的提问(因为实在英文网站上发,所以用英文。如有需要我可以将它翻译成中文)
我这个问题中的源程序中是指考虑第一类课程及能胜任第一类课程的老师t01,t02,t03,且不考虑上午下午问题,只考虑教室匹配。
I am studying timetabling by the examples given in tomsym, but I seem to have misunderstanding in the definition of tomArray.
Question1:
If a 1*1 tomarray 'overwork' :
which is:overwork = tomArray() with tomSym data:
sum(vec(sum(teach([... 20 by 5 array ...]),1)),1)-4
How can I write my program to compare each value of 'overwork' with the value zero,
I tried to use 'overwork>0' or 'overwork(i)>0' but it can't work. How can I chage it into a matrix or something like that.
Question2:
Is it possible to use a variable which class is of 'tomArrayIdx' to indicate the subsindex of a matrix in matlab?


My problem in detail:
I have studied your case in tomSym,which is to establish a college timetable. (http://tomsym.com/examples/tomsym_collegetimetable.html).They use tomArrayIdx to create a tomarray of multidimensions. I tried to apply it into a similiar case I have encountered.
The timetable in my program: there are 3 teachers, 5 courses(each lesson has 2,2,3,2,2 lessons per week respectively),these teachers can teach any of these 5 courses.there are 5 workdays a week and each day has 4 slots.
Objective1:The desirable time for students to have courses are the 2nd and 3rd slots each day. so we have to minimize the courses' appearing on the 1st and 4th slots each day.
Objective2:The desirable schedule in the part of the 3 teachers are to teach only 2,2,3 courses per week respectively.This can't be satisfied,due to the prescribed courses per week is 2+2+3+2+2,and it's larger than 2+2+3.but I tried to minimize 'overwork' time.


My program using tomlab:
teacher=[4 2;4 1;6 1]; %three teachers,first column 4 4 6 are their desirable schedule perweek, second column is the kind of classrooms they wanted to teach in(1 is for multimedia classroom,2 is for auditorium)
course=[2 50 2;2 30 1;3 40 1;2 25 2;2 60 1]; %5 courses and their properties,first column:lessons per week,second column:number of students attending the class,third column is the kind of classroom the courses to be taught in(1 is for multimedia classroom,2 is for auditorium)
slots=4*5; %numbers of slots:4 classes per day, 5 workdays a week
room=[100 2;100 1;100 1;50 2;50 1;50 1;50 1;... %18 rooms and their properties,first column:maximum number of seats in the classroom.second column:the property of the classrooms(1 is for multimedia classroom,2 is for auditorium)
60 1;60 1;60 2;60 1;60 3;40 3;...
40 3;40 3;40 2;50 1;40 1];
t=tomArrayIdx('t',1:size(teacher,1));
l=tomArrayIdx('l',1:size(course,1));
s=tomArrayIdx('s',1:slots);
r=tomArrayIdx('r',1:size(room,1));
teach=tom('teach',3*5*20*18,1,'int');
teach=tomArray(teach,[3,5,20,18]);
bnds1={0<=teach<=1}; %variables are binary
bnds2={sum(sum(sum(teach(t,l,s,r),s),t),r)==course(:,1)}; %all courses must be taught
bnds3={sum(sum(teach(t,l,s,r),t),l)<=1}; %only one lesson in the classroom at one time
bnds4={sum(sum(teach(t,l,s,r),r),l)<=1}; %one teacher can only teach one lesson at a time
bnds5={room(r,1)>=course(r,3)}; %###REPORTS ERROR###seats in the classroom must be greater than the number of students attending the course
bnds={bnds1,bnds2,bnds3,bnds4,bnds5};
not_so_good_slots=tomArrayIdx('s',[1,4,5,8,9,12,13,16,17,20]); %this is the same with your example in tomsym:establishing a college timetable
objective1=sum(vec(teach(t,l,not_so_good_slots,r)));
objective2=0;
for i=1:3
overwork=sum(sum(sum(teach(t,l,s,r),s),l),r)-teacher(i,1); %###REPORTS ERROR###minimize the overtime of the teachers
if overwork>0
objective2=objective2+10*abs(overwork);
end
end
objective={objective1,objective2};
options=struct;
options.solver='cplex';
options.name='Timetable';
sol=ezsolve(objective,bnds,[],options);

When i enter
for i=1:3
overwork=sum(sum(sum(teach(t,l,s,r),s),l),r)-teacher(i,1);
if overwork>0
objective2=objective2+10*abs(overwork);
end
end
it says:
??? Function 'gt' is not defined for values of class 'tomArray'.

Error in ==> gt at 18
[varargout{1:nargout}] = builtin('gt', varargin{:});

When i entered:
bnds5={room(r,1)>=course(r,3)};
it says
??? Error using ==> subsindex
Function 'subsindex' is not defined for values of class 'tomArrayIdx'.

Could you give me some suggestions on how to implement these on tomlab?
Thanks for your attention.Any suggestions are appreciated.
surlogics 2009-07-28
  • 打赏
  • 举报
回复
如果可以的话能先贴出来吗?我在编排的时候也是出来一个高阶矩阵后使用reshape凑合着看结果是否合理,最后才美观一下输出。

我现在关键就是没思路,因为我不会用matlab的程序或命令去解MIP规划。

如果可以请贴出一部分代码,咱们一起研究。好吗?
iwantnon 2009-07-28
  • 打赏
  • 举报
回复
我靠,我晕了,楼主的数据是自己编造的么?太多内在矛盾呀,例如:
C13这门课 是类别3 要在 多媒体教室 上,时间在 下午。
但是你去教师表中找找看,教 3类课的老师中哪有 “对教室的要求”="多媒体教室" 同时上课时间又是在 下午 的?
根本没有,所以C13这门课根本就没人讲吗。
--
模型我已经列出来了,就是没有合理的数据,无法进一步往下走了。
我现正在自己编数据。
而如果楼主有正确完整的数据的话,希望能提供出来。
iwantnon 2009-07-28
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 surlogics 的回复:]
请问如何用MATLAB的最优化工具箱解决这个问题?因为没有关于时间表的例子,我不会用MATLAB语言描述。
请问iwantnon是如何将1、2、3、4四类课安排进去,因为不同类的老师教的课不一样。
[/Quote]
我用matlab一般都是自己写程序,没用过工具箱。
iwantnon 2009-07-28
  • 打赏
  • 举报
回复
我将才把全部数据导入运行了一下,已经得出结果了,不过现在结果是不易读的形式,还需要导入matlab作些处理。。。
等下,就这一两天我会把这个问题搞完,到时候把完整过程帖上来。
surlogics 2009-07-28
  • 打赏
  • 举报
回复
请问如何用MATLAB的最优化工具箱解决这个问题?因为没有关于时间表的例子,我不会用MATLAB语言描述。
请问iwantnon是如何将1、2、3、4四类课安排进去,因为不同类的老师教的课不一样。
iwantnon 2009-07-28
  • 打赏
  • 举报
回复
刚才试了一下,用数学规划也是可以描述软约束的,刚才弄了一个 1~4类课的运行了一下,可以得到解。
等我再将数据输全一些看看。
iwantnon 2009-07-28
  • 打赏
  • 举报
回复
问题确实是无解的,由下面的简单分析就可以看出:

表1和表2的数据存在如下矛盾:
比如只考虑第1,2,3类课程:
由表1可知C01~C15属于1,2,3类课程,其总课时数为:4+4+6+4+3+4+4+2+4+3+6+4+6+2+3=59.
再看表2,只有T01~T10及T23这11名教师能胜任1,2,3类课程,其总共能提供的课时数为:4+4+6+4+4+6+4+3+4+6+4=49.
也就是说,1,2,3类课程需要59课时,但是教师方面只能提供49课时,根本满足不了需求!!
因此问题当然是无解的。
你看一下是不是这样。
表1:
---------------------------------------------------
课程编号 课程类别 周课时数 对教室座位最大要求数 对教室类别要求 时间要求
C01 1 4 50 多媒体教室 上午
C02 1 4 30 普通教室 下午
C03 1 6 40 普通教室 下午
C04 1 4 25 多媒体教室 上午
C05 1 3 60 普通教室 下午
C06 2 4 100 普通教室 下午
C07 2 4 50 多媒体教室 上午
C08 2 2 30 普通教室 上午
C09 2 4 40 普通教室 下午
C10 2 3 25 多媒体教室 上午
C11 3 6 60 普通教室 上午
C12 3 4 80 普通教室 上午
C13 3 6 50 多媒体教室 下午
C14 3 2 30 普通教室 下午
C15 3 3 40 普通教室 下午
...
----------------------------------------------------
表2:
---------------------------------------------------------------
教师编号 能胜任课程类别 周最大课时数 对教室类别要求 上课时间要求 尽可能不同天上课的教师
T01 1,8 4 多媒体教室或机房 上午 T4
T02 1 4 普通教室 下午
T03 1 6 普通教室 上午
T04 2 4 多媒体教室 上午 T1
T05 2 4 普通教室 下午
T06 2 6 普通教室 无
T07 3 4 普通教室 上午
T08 3,8 3 普通教室或机房 下午
T09 3 4 普通教室 上午 T11,T18
T10 3 6 多媒体教室 上午
...
T23 3,8 4 普通教室或机房 无 T15
...
---------------------------------------------------------------
iwantnon 2009-07-28
  • 打赏
  • 举报
回复
哦,今天又想了想,5楼那个也可以不理,直接写约束条件就可以了。
我已经写出了一个模型,但是没找到可行解,我再看看问题出在哪儿...是不是课时这个概念理解错了呢?
我是按“上午两节大课,下午两节大课,一节大课为一课时”来算的,这样的话没有可行解。
那到是一节大课是不是一课时呢??
iwantnon 2009-07-28
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 surlogics 的回复:]
我认为不一定非要满足全部条件,我在一开始没思路的情况下手动排第一类课程,但是发现不可能完成,我们可以通过设置一个
罚函数,优化这个问题。使得与所给条件冲突的达到最小.


首先的难题就是那个T01,T08,T15等老师兼任两门学科。我不会处理。

我试图用基于MATLAB的TOMLAB实现,但是遇到tomArray和普通的subsindex不兼容的问题。为此我在论坛上提问,但没有得到回答。

如有知道什么规划软件能够更好的处理这种问题能…
[/Quote]
那你出题的时候应该说明约束都是软的。
我全当成硬约束来做了,也正是由于这个原因我才放弃了用matlab,而改用了lingo。
我本以为楼主解这道题的难处在于如何追求精确,原来不是这样,从楼主的思路来看,你对结果精确程度要求不是很高,既然允许有冲突,那么用matlab来编程做就不是很难了。
不过这两天一直在研究这个题的lingo模型,有点疲惫了。
我歇一阵再来。
希望有更多人参与!
surlogics 2009-07-27
  • 打赏
  • 举报
回复
5楼所说的问题我没有太理解,我认为没有单独教第8类课程的老师,所以必须要有老师兼职,但是如何写算法却是个问题
加载更多回复(6)

33,027

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧