(こんにちは、この質問はあなたの元の質問よりもはるかに的を射ているので、簡単に答えられます。)
これは、決定変数の 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 の異なる解があり、最初の 2 つ (ソルバーとして 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 が時間 4 に部屋 1 で開始され、試験 2 が時間 1 に開始され、これも部屋 1 で開始されることを意味します。 )。
これがモデルをさらに進めるのに役立つことを願っています。
/ハカンク