0

我想弄清楚如何写这个约束:我有一个考试清单,每场考试都有一个持续时间;最终输出是显示一个实时时间表,列中有可用时间,早上四点和下午四点,午餐中间有两个小时不可用。所以,让我做这很清楚,如果我有两门考试并且每门考试都有指定的持续时间,我想在时间表中显示与考试持续时间相关的考试编号,因为我的变量是考试。

例如:我有两次考试,第一次需要一个小时,第二次需要三个小时

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;

%Data

Exams=2;
Exam_duration=[1,3];
Slotstime=4;            

我想要这个输出: [1,2,2,2] 而不是 [0,0,0,4] (垂直模式) 可以在 Minizinc 中做吗?第二个输出的代码是:

constraint forall (p in 1..Rooms)                 
( 
  sum (s in 1..Slotstime) (Timetable_exams[s,p]) 
  = sum (f in 1..Exams)(Exams_duration[f])
);

提前致谢

4

1 回答 1

2

(嗨,这个问题比你原来的问题更容易回答,因为它更重要。)

这是一个使用两个额外的决策变量数组的版本:“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

于 2014-11-20T18:09:18.983 回答