(嗨,这个问题比你原来的问题更容易回答,因为它更重要。)
这是一个使用两个额外的决策变量数组的版本:“ExamsRoom”用于处理房间分配给考试,“ExamsStart”用于考试的开始时间。也许这些并不是真正必要的,但它们可以更容易地说明考试持续时间的限制;房间和时间的分配也显示得更清楚。它们也可能有助于添加更多约束。
我还添加了参数“Rooms = 2”,因为您的示例中缺少它。
int: Exams;
array[1..Exams] of int: Exams_duration;
int: Slotstime; % number of slots
int: Rooms; % number of rooms
array[1..Slotstime,1..Rooms] of var 0..Exams: Timetable_exams;
array[1..Exams] of var 1..Rooms: ExamsRoom; % new
array[1..Exams] of var 1..Slotstime: ExamsStart; % new
solve satisfy;
% solve :: int_search(x, first_fail, indomain_min, complete) satisfy;
constraint
% hakank's version
% for each exam
forall(e in 1..Exams) (
% find a room
exists(r in 1..Rooms) (
% assign the room to the exam
ExamsRoom[e] = r /\
% assign the exam to the slot times and room in the timetable
forall(t in 0..Exams_duration[e]-1) (
Timetable_exams[t+ExamsStart[e],r] = e
)
)
)
/\ % ensure that we have the correct number of exam slots
sum(Exams_duration) = sum([bool2int(Timetable_exams[t,r]>0) | t in 1..Slotstime, r in 1..Rooms])
;
output [
if r = 1 then "\n" else " " endif ++
show(Timetable_exams[t,r])
| t in 1..Slotstime, r in 1..Rooms
]
++
[
"\nExamsRoom: ", show(ExamsRoom), "\n",
"ExamsStart: ", show(ExamsStart), "\n",
]
;
%
% Data
%
Exams=2;
Exams_duration=[1,3];
Slotstime=4;
% was not defined
Rooms = 2;
这个模型有 20 个不同的解,前两个(使用 Gecode 作为解算器)是
2 0
2 0
2 0
1 0
ExamsRoom: [1, 1]
ExamsStart: [4, 1]
----------
2 1
2 0
2 0
0 0
ExamsRoom: [2, 1]
ExamsStart: [1, 1]
----------
第一个解决方案意味着考试 1 在房间 1 的时间 4 开始,考试 2 在时间 1 开始,也在房间 1。第二个解决方案对考试 2 有相同的分配,但将考试 1 设置为房间 2(在时间 1 )。
希望这可以帮助您进一步了解模型。
/hakank