簡體   English   中英

Minizinc:這種約束可能嗎?

[英]Minizinc: is this constraint possible?

我想弄清楚如何寫這個約束:我有一份考試清單,每項考試都有時間; 最后的輸出是實際時間表的顯示,在各列中有可用時間,早上四點和下午四點,午餐中途有兩個小時不可用。所以,讓我這很清楚,如果我有兩次考試,並且每項考試都有指定的持續時間,我想在時間表中顯示與其持續時間相關的考試編號,因為我的變量是考試。

例如:我有兩次考試,第一場考試需要一個小時,第二場是三個小時

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])
);

提前致謝

(嗨,這個問題比您原來的問題更容易回答,因為它更重要了。)

這是一個使用兩個額外的決策變量數組的版本:“ 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)。 )。

希望這可以幫助您進一步發展模型。

/哈坎克語

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM