Minizinc: output for five days,there is a better flexible way?

2k views Asked by At

I have to extend the output and the solution of my project (make an exams scheduling):

-Extend the structure to five days (I have always worked on one day): I thought about moltiply the number of days for slotstimes (5*10) and then I tune the output! Is there a better way?

Now the whole code:

include "globals.mzn";include "alldifferent.mzn";

%------------------------------Scalar_data----------------------
int: Students;          % number of students
int: Exams;             % number of exams
int: Rooms;             % number of rooms
int: Slotstime;         % number of slots
int: Days;              % a period i.e. five days
int: Exam_max_duration; % the maximum length of any exam (in slots)

%------------------------------Vectors--------------------------
array[1..Rooms] of int  : Rooms_capacity;
array[1..Exams] of int  : Exams_duration; % the duration of written test
array[1..Slotstime, 1..Rooms] of 0..1: Unavailability;
array[1..Students,1..Exams]   of 0..1: Enrollments;

Enrollments keeps track of the registrations for every student; from this I obtain the number of students which will be at the exam, in order to choose the right room according to the capacity

%---------------------------Decision_variables------------------
array[1..Slotstime,1..Rooms] of var 0..Exams: Timetable_exams;
array[1..Exams] of var 1..Rooms: ExamsRoom;
array[1..Exams] of var 1..Slotstime: ExamsStart;

%---------------------------Constraints--------------------------

% Calculate the number of subscribers and assign classroom 
% according to time and capacity 

constraint forall (e in 1..Exams,r in 1..Rooms,s in 1..Slotstime)               
(if Rooms_capacity[r] <= sum([bool2int(Enrollments[st,e]>0)| st in 1..Students])
  then Timetable_exams[s,r] != e  
  else true
  endif
);

% Unavailability OK
constraint forall(c in 1..Slotstime, p in 1..Rooms)
(if Unavailability[c,p] == 1
  then Timetable_exams[c,p] = 0
  else true
  endif
);

% Assignment exams according with rooms and slotstimes   (Thanks Hakan)     
constraint forall(e in 1..Exams)                 % for each exam
(exists(r in 1..Rooms)                           % find a room
  ( ExamsRoom[e] = r         /\                  % assign the room to the exam
    forall(t in 0..Exams_duration[e]-1)               
% assign the exam to the slotstimes and room in the timetable
    (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]);

%---------------------------Solver--------------------------

solve satisfy;

%   solve::int_search([Timetable_exams[s, a] | s in 1..Slotstime, a in    
%   1..Rooms],first_fail,indomain_min,complete) satisfy;

And now the output, extremely heavy and full of strings.

%---------------------------Output--------------------------

output ["\n" ++ "MiniZinc paper: Exams schedule " ++ "\n" ]
++["\nDay I \n"]++      
[
if r=1 then "\n" else " " endif ++
show(Timetable_exams[t,r])
| t in 1..Slotstime div Days, r in 1..Rooms
]
++["\n\nDay II \n"]++ 
[
if r=1 then "\n" else " " endif ++
show(Timetable_exams[t,r])
| t in 11..((Slotstime div Days)*2), r in 1..Rooms
]
++["\n\nDay III \n"]++ 
[
if r=1 then "\n" else " " endif ++
show(Timetable_exams[t,r])
| t in 21..((Slotstime div Days)*3), r in 1..Rooms
]
++["\n\nDay IV \n"]++ 
[
if r=1 then "\n" else " " endif ++
show(Timetable_exams[t,r])
| t in 31..((Slotstime div Days)*4), r in 1..Rooms
]
++["\n\nDay V \n"]++ 
[
if r=1 then "\n" else " " endif ++
show(Timetable_exams[t,r])
| t in 41..Slotstime, r in 1..Rooms
]
++[ "\n"]++
[
"\nExams_Room:   ", show(ExamsRoom), "\n",
"Exams_Start:  ", show(ExamsStart), "\n",
]
++["Participants: "]++
[
if e=Exams then " " else " " endif ++
show (sum([bool2int(Enrollments[st,e]>0)| st in 1..Students]))
|e in 1..Exams
];

I finish with data:

%Data
Slotstime=10*Days;
Students=50;
Days=5;

% Exams
Exams = 5;
Exam_max_duration=4;
Exams_duration = [4,1,2,3,2]; 

% Rooms
Rooms = 4;
Rooms_capacity   = [20,30,40,50];

Unavailability = [|0,0,0,0           % Rooms rows % Slotstime columns
|0,0,0,0
|0,0,0,0
|0,0,0,0
|1,1,1,1
|1,1,1,1
|0,0,0,0
|0,0,0,0
|0,0,0,0
|0,0,0,0
                                      % End first day
|0,0,0,0
|0,0,0,0
|0,0,0,0
|0,0,0,0
|1,1,1,1
|1,1,1,1
|0,0,0,0
|0,0,0,0
|0,0,0,0
|0,0,0,0
                                      % End secon day
|0,0,0,0
|0,0,0,0
|0,0,0,0
|0,0,0,0
|1,1,1,1
|1,1,1,1
|0,0,0,0
|0,0,0,0
|0,0,0,0
|0,0,0,0
                                      % End third day
|0,0,0,0
|0,0,0,0
|0,0,0,0
|0,0,0,0
|1,1,1,1
|1,1,1,1
|0,0,0,0
|0,0,0,0
|0,0,0,0
|0,0,0,0
                                     %  End fourth day
|0,0,0,0
|0,0,0,0
|0,0,0,0
|0,0,0,0
|1,1,1,1
|1,1,1,1
|0,0,0,0
|0,0,0,0
|0,0,0,0
|0,0,0,0
                                       %End fifth day
|];

Enrollments= [|1,0,1,0,1        % Exams rows %Students columns              
|1,0,1,0,1
|0,1,0,0,0
|1,0,0,1,0
|0,1,0,0,0
|0,0,1,1,0
|1,0,0,1,0
|0,0,0,0,1
|1,0,0,0,1
|0,0,0,0,1
|0,1,0,0,0
|0,0,0,0,0
|0,1,0,0,1
|0,0,1,0,1
|1,0,1,0,1
|1,0,1,0,1
|0,1,0,0,0
|1,0,0,1,0
|0,1,0,0,0
|0,0,1,1,0
|1,0,0,1,0
|0,0,0,0,1
|1,0,0,0,1
|0,0,0,0,1
|0,1,0,0,0
|0,0,0,0,0
|0,1,0,0,1
|0,0,1,0,1
|1,0,1,0,1
|1,0,1,0,1
|0,1,0,0,0
|1,0,0,1,0
|0,1,0,0,0
|0,0,1,1,0
|1,0,0,1,0
|0,0,0,0,1
|1,0,0,0,1
|0,0,0,0,1
|0,1,0,0,0
|0,0,0,0,0
|0,1,0,0,1
|0,0,1,0,1
|1,0,1,0,1
|1,0,1,0,1
|0,1,0,0,0
|1,0,0,1,0
|0,1,0,0,0
|0,0,1,1,0
|1,0,0,1,0
|0,0,0,0,1
|];

Thanks in advance

1

There are 1 answers

5
hakank On BEST ANSWER

For the output section, the following code should work. I only changed the Day schedule, the rest is unchanged.

output ["\n" ++ "MiniZinc paper: Exams schedule " ++ "\n" ]
++
[
   if t mod 10 = 1 /\ r = 1 then
      "\n\nDay " ++ show(d) ++ " \n"
   else  "" endif ++
   if r=1 then "\n" else " " endif ++
      show(Timetable_exams[t,r])
| d in 1..Days, t in 1+(d-1)*10..(Slotstime div Days)*d, r in 1..Rooms,
]
++[ "\n"]++
[
"\nExams_Room:   ", show(ExamsRoom), "\n",
"Exams_Start:  ", show(ExamsStart), "\n",
]
++["Participants: "]++
[
if e=Exams then " " else " " endif ++
   show (sum([bool2int(Enrollments[st,e]>0)| st in 1..Students]))
|e in 1..Exams
];

If it's a requirement that the days should be numbered with "I","II", etc then you can define a string array with the day names, e.g.

 array[1..Days] of string: DaysStr = ["I","II","III","IV","V"];

and then use it in the output loop:

 % ....  
   if t mod 10 = 1 /\ r = 1 then
      "\n\nDay " ++ DaysStr[d] ++ " \n"  % <---
   else  "" endif ++
 % .... 

Later update:

One other thing to make the model a little more general (and smaller) is to replace the huge Unavailability matrix (and the constraint using it) with this:

 set of int: UnavailabilitySlots = {5,6};

 % ....
 constraint 
     forall(c in 1..Slotstime, p in 1..Rooms) (
       if c mod 10 in UnavailabilitySlots then 
          Timetable_exams[c,p] = 0
       else 
          true
       endif
  );

Yet another comment:

The original model has a flaw in that it allow exams that will be over two days, e.g. the 2 last hours of day I and the first 2 hours of day II. I think the following extra (and not so pretty) constraint will fix that. Again, the magic "10" is used.

constraint
   % do not pass over a day limit
   forall(e in 1..Exams) (
        not(exists(t in 1..Exams_duration[e]-1) (
           (ExamsStart[e]+t-1) mod 10 > (ExamsStart[e]+t) mod 10
        ))
   )
 ;