Lorenzo
Lorenzo

Reputation: 95

Clingo: logic OR in integrity constraint

For a lecture exercise, I have to represent in Answer Set Programming (we use Clingo as interpreter) a the following integrity constraint:

"You have to plan the calendar of a Masterclass. Normally, the lectures are on Fridays (8 hours) and Saturday(4 or 5 hours). And the 7th and 16th week are full, which means the lectures goes from Monday to Friday, with 8 hours per day, and on Saturday, with 4 or 5 hours of lecture."

The basic settings for the problem are the following:

#const n_weeks = 2. % for now we limit the problem size to 2 weeks
#const n_days = 6. % days in a fullweek

week(1..n_weeks).
day(1..n_days).
hour(1..8). % from the 1st to the 8th hour of the day

% the second week is a fullweek (lectures from 1st to 8th hour from Monday to Friday)
fullweek(2). 

% We number all the weekdays (mon-fri) (we need it for the saturday)
fullday(1..5). 

% some professors just for test
prof("prof1").
prof("prof2").
prof("prof3").
prof("prof4").

% subj, total hours, prof
subject("subj1", 8, "prof1").
subject("subj2", 14, "prof2").
subject("subj3", 24, "prof3").
subject("subj4", 11, "prof1").

% The main predicate, to print out at the end.
0 {calendar(W, D, H, W*100+D*10+H, lecture(S, P))} 1 :- week(W), day(D), hour(H), subject(S, _, P).

Now, as mentioned above (the final line in bold), we have some problems with the following constraint:

"In this masterclass the hours of a lecture on Saturday can be 4 or 5."

For now, me and my colleagues represented this constraint like this:

% The Saturday has 4 or 5 hours of lecture
:- #count{I : calendar(W, D, _, I, lecture(_, _))} > 5, week(W), day(D), not fullday(D).
:- #count{I : calendar(W, D, _, I, lecture(_, _))} < 4, week(W), day(D), not fullday(D).

Is it the right way to represent constraint like this? There is a better approach?

Upvotes: 0

Views: 734

Answers (1)

tphilipp
tphilipp

Reputation: 466

I do not believe there is the "right way" to represent the constraint as long as it is technically correct. I suggest to consider the following points:

  • The way on how to express Saturday is complicated, i.e. you can replace the variable D by 6 and eliminate the predicates day and fullday.

  • I do not understand why you use "lecture(_, _)" instead of the underscore.

  • I am not sure why you use the variable I for counting and think you like to count the hours instead.

  • Maybe it make sense to use disjunction explicitly, i.e. use a predicate like "hours_on_sunday(H)" and write a rule that H must be 4 or 5.

Upvotes: 1

Related Questions