建立具有大量变量的时间表问题
我有一个经典的时间表问题,由变量类(~100)、房间(20)、学期(8)和工作日(5)组成。
为了简化问题,以下是减少的约束。
一天是9个小时。
有些课程是学生的必修课,第 1、3、5、7 学期(以及 2、4、6、8)的必修课不得相互重叠。
课程的时长不同,有的为 2 小时,有的为 3 小时。
有些课程应在特定房间进行。
同一堂课不能同时进行两场讲座。
我将使用 logilabs python 约束模块。我知道明智地选择变量和域将减少解决问题的时间。问题是我以前从未做过约束编程,并且很难构建问题以找出从哪里开始以及从什么开始。例如:
我可以设置一个约束,例如“同一天、同一房间的两个班级不能重叠彼此的时间段”。或者从“同一天预订房间的时间不得超过 9 小时”开始,然后继续缩小解决方案域。我估计(没有尝试)第一个约束将比另一个约束花费更长的时间来解决。但它还需要(我猜)更改变量和解决方案域或重建较小的问题。我已经对变量、域、间隔、实现等有点迷失了。
长话短说,我需要一些指导来构建问题、解决方案域、明智地选择变量等。
谢谢
更新
使用 logilab -constraint 包我已经制作了一个基本应用程序并将其上传到 github
I have a classic timetabling problem consisting of the variables classes(~100), rooms(20),terms(8) and weekdays(5).
To just simplfy the problem, following are the reduced constraints.
A day is 9 hours.
Some classes are mandatory for students, and mandatory classes of the terms 1,3,5,7 (and for 2,4,6,8) must not overlap eachother.
Classes have different weights in terms of hours, some are 2 hours, some are 3.
Some classes should be at specific rooms.
There can not be two lectures at the same class at the same time.
I'm going with logilabs python constraint module. And i'm aware that chosing the variables and domains wisely will result in lesser time for solving the problem. Problem is I've never done constraint programming before and having a hard time building up the problem to find out where and what to begin with. For example:
I can set a constraint such as "no two class with same room, same day can overlap eachother's time slot". Or start with " no room can have more than 9 hours reserved on the same day" then continue with reduced solution domain. I estimate(did not try) that the first constraint will take much longer to solve than the other one. But it also requires (i guess) changing of variables and solution domains or a rebuilding of a smaller problem. I'm already a bit lost in variables, domains, intervals, implementations etc.
Long story short, i need some pointers for building up the problem, solution domains, chosing variables wisely etc.
Thanks
UPDATE
Using logilab-constraint package i've made a basic application and uploaded it to github
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看 curriculum课程示例代码的Drools 规划器。基本上是同一件事,但术语略有不同:每门课程(=班级)都有许多讲座,需要安排在一个房间(=房间)和时段(=每个工作日的每个学期都是一个时段)。
诀窍是保持一个干净的域模型并将其与约束规则分开。
由于您的课程在小时数方面具有不同的权重,因此我建议仅为
Lecture
分配一个startingPeriod
,因此将 Lecture 移动到另一组的代码并不需要太多。期间(只需重新分配第一个期间)。Take a look at the curriculum course example code of Drools Planner. It's basically the same thing, but the terminology is slightly different: each course (= class) has a number of lectures which need to be scheduled into a room (=room) and period (= each term on each weekday is a period).
The trick is to keep a clean domain model and keep that separate from your constraint rules.
Because your classes have different weights in terms of hours, I suggest that a
Lecture
is only assigned astartingPeriod
so it's not a lot of code to move a Lecture to another set of Period (just reassign the first Period).我最终得到了一个将“明智地选择变量”和“减少选择域”捆绑在一起的解决方案。以下是我正在做的基于 django 的时间表应用程序的片段:
我已将基本约束嵌入到类中,
例如“这门课程可能在星期一或星期二,第 1 或 2 或 3 节
在房间 1 或 2 或 3 或 4 或 5",这允许我直接应用这些
选课之初的基本限制
数据库。
诸如“同一天的相同时间之间不能在同一房间内开设两门课程”之类的一般约束适用于来自数据库中完成的选择的减少的变量域。
看来现在还在工作。
I've ended up with a solution that bundles "chosing the variables wisely" and "reducing the domains at selection". Below are the snippets from the django based timetabling application i'm doing:
I've embedded the basic constraints into the classes,
such as "this course may be on monday or tuesday, in terms 1 or 2 or 3
in rooms 1 or 2 or 3 or 4or 5",which allows me to directly apply these
basic constraints at the beginning when selecting the courses from
database.
General constraints such as "no two courses can be at the same room inbetween the same hours on the same day" are applied to reduced variable domains coming from the selection done in database.
Seems to be working for now.