Minizinc syntax errors? requesting help/advice

73 Views Asked by At

I have the following model, which is giving syntax errors. Any help greatly appreciated.

int: N = 8;  % Number of participants
int: M = 2;  % Team size
int: S = 4;  % Number of sessions
int: A = 4;  % Number of activities

% Define decision variables
array[1..N, 1..S] of var 1..A: team_assignment;

% Constraints
constraint
   % Each team can only participate in one activity
   forall(t in 1..N)
      (sum(s in 1..S)(team_assignment[t, s] == 1)) == 1;

   % No participant is assigned more than once in a session
   forall(s in 1..S, p in 1..N)
      (sum(t in 1..N)(team_assignment[p, s] == p)) == 1;

   % No participant is in the same activity more than once
   forall(p in 1..N, a in 1..A)
      (sum(s in 1..S)(team_assignment[p, s] == a)) <= 1;

solve satisfy;

% Output the solution
output [show(team_assignment)];

These syntax errors continue?

SessionActivity8:17.52-53:
MiniZinc: syntax error: syntax error, unexpected =, expecting ++ or ':'
SessionActivity8:21.52-53:
MiniZinc: syntax error: syntax error, unexpected <=, expecting ++ or ':'
Process finished with non-zero exit code 1.
2

There are 2 best solutions below

5
On

The syntax errors are because you are forgetting to repeat the constraint keyword before each constraint.

After fixing this issue there are various typing errors. Assuming you want to count the number of times the condition hold, the resulting model would look like:

    int: N = 8;  % Number of participants
    int: M = 2;  % Team size
    int: S = 4;  % Number of sessions
    int: A = 4;  % Number of activities
    
    % Define decision variables
    array[1..N, 1..S] of var 1..A: team_assignment;
    
    % Constraints
    constraint
       % Each team can only participate in one activity
       count(t in 1..N)
          (sum(s in 1..S)(team_assignment[t, s]) == 1) == 1;
    
    constraint
       % No participant is assigned more than once in a session
       count(s in 1..S, p in 1..N)
          (sum(t in 1..N)(team_assignment[p, s]) == p) == 1;
    
    constraint
       % No participant is in the same activity more than once
       count(p in 1..N, a in 1..A)
          (sum(s in 1..S)(team_assignment[p, s]) == a) <= 1;
    
    solve satisfy;
    
    % Output the solution
    output [show(team_assignment)];
0
On

I did find solutions to this puzzle and thought I should show the solution in hopes it may help someone else. I appreciate the help given to me by Jip Dekker.

%output code to show teams vs True/False from Jip Dekker

% THE PROBLEM/PUZZLE TO BE SOLVED/SATISFIED

% Teams are made up of 2 player unique combinations from 8 choose 2. (A,B,C,D,E,F,G,H)
% A team can only participate in 1 activity in 1 session,  then Is removed from further play.
% A Player, regardless of Team, can only play 1 activity 0 or 1 time.
% A Player can only play in 1 activity per session.
% Each Activity in each Session has exactly 1 Team assigned to it.
% The Activities are  CHECKERS, HORSESHOES, DARTS, TABLE_TENNIS
% The Sessions are 9 AM, 10 AM, 11 Am, 12 Noon.

%----------miniZinc instructions/code follows-------------- 

include "globals.mzn";  %include minizinc globals library

%set up main factors of the puzzle
enum ACTIVITY = {CHECKERS, HORSESHOES, DARTS, TABLE_TENNIS}; %4 activities
enum PLAYER = {A, B, C, D, E, F, G, H};                      %8 Players
enum SESSION = Session(9..12);                               %4 Sessions

% Define the set of pairs of players for teams  8 choose 2
array [int] of set of PLAYER: TEAMS = [{A, B}, {A, C}, {A, D}, {A, E}, {A, F}, {A, G},
 {A, H}, {B, C}, {B, D}, {B, E}, {B, F}, {B, G}, {B, H}, {C, D}, {C, E}, {C, F}, {C, G},
 {C, H}, {D, E}, {D, F}, {D, G}, {D, H}, {E, F}, {E, G}, {E, H}, {F, G}, {F, H}, {G, H}];

% Define the decision variable for the assignment of teams to activities in sessions
array[index_set(TEAMS), ACTIVITY, SESSION] of var bool: x; 
% 3 dimensional array of booleans
% Each element of the array x is a boolean variable that can be either true or false. 
% The value of  x[t,a,s] indicates whether team t performs activity a in session s or not.  

% Define the constraints of the problem

% A team can only play one activity one time in one session and then that combination/team is excluded from further play 

constraint forall(t in index_set(TEAMS), a in ACTIVITY) ( 
   sum(s in SESSION) (x[t, a, s]) <= 1 );
constraint forall(t in index_set(TEAMS),s in SESSION ) ( 
    sum(a in ACTIVITY) (x[t, a, s]) <= 1); 
constraint forall(t in index_set(TEAMS) ) (                      
   sum(a in ACTIVITY,s in SESSION) (x[t, a, s]) <= 1);

% A player can only participate once in a specific activity, regardless of the team and the session  
constraint forall(p in PLAYER, a in ACTIVITY) (
   sum(t in index_set(TEAMS) where p in TEAMS[t], s in SESSION) (x[t, a, s]) <= 1);

% A player can only participate once in a session, regardless of the team and the activity 
constraint forall(p in PLAYER,s in SESSION ) (
   sum(t in index_set(TEAMS) where p in TEAMS[t],a in ACTIVITY ) (x[t, a, s]) <= 1);

% Each activity in each session has exactly one team assigned to it  
constraint forall(a in ACTIVITY, s in SESSION) (
    sum(t in index_set(TEAMS)) (x[t, a, s]) == 1);

solve satisfy;
% JED I switched Session and Actvity in line below to get  Activity columns, Session rows
% Got assistance from Jip Dekker to convert 3D array of booleans to 2D array showing Teams
% Define a 2D array y showing session and activity with values of players making up Teams
array[SESSION,ACTIVITY] of set of PLAYER: y ::output_only = [
  (s,a): {p | i in index_set(TEAMS) where fix(x[i,a,s]), p in TEAMS[i]}
  | s in SESSION, a in ACTIVITY];
  
%Output the 2D array showing session and activity with values of players making up Teams
output [show2d(y) ++ "\n"];````