Skip to content

Instantly share code, notes, and snippets.

@dimbleby
Created January 4, 2016 20:04
Show Gist options
  • Save dimbleby/cc356023e3b021a1cd28 to your computer and use it in GitHub Desktop.
Save dimbleby/cc356023e3b021a1cd28 to your computer and use it in GitHub Desktop.
int: n = 4; % number of gangs
set of int: Gang = 1..n;
array[Gang] of int: m = [3, 2, 2, 3]; % size of each gang
int: t = sum(m);
set of int: Time = 1..t;
set of int: Student = 1..t;
%%%%%%%
array[Student] of var Time: interview; % at what time do we see a given student?
% Which gang is each student in?
array[Student] of Gang: gang = [ g | g in Gang, j in 1..m[g] ];
function array[int] of Student: members(Gang: g) = [ s | s in Student where gang[s] = g ];
% Who is the leader of each gang?
function Student: leader(Gang: g) = min(members(g));
% Confession points for each gang.
array[Gang] of var int: points;
% Does a leader crack? Yes if there's any other gang whose interviews are already complete.
function var bool: leader_cracks(Gang: g) =
exists (g2 in Gang) ((max (i in members(g2)) (interview[i])) > interview[leader(g)]);
% Give points for interviewing a leader if he cracks, else don't.
constraint forall (g in Gang)
(points[g] = if leader_cracks(g)
then
let { var int: sold_out = sum (i in members(g))
(bool2int(interview[i] > interview[leader(g)])) }
in sold_out
else 0 endif);
%%%%%%%
solve satisfy;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment