1

After running through the tutorials, thinking I got the concepts, I set about creating my own model and immediately ran into problems. Better to get stuck in asap to realise just how little you actually know, imo.

In this example I would simply like to get an output where classes consist of students that want them, i.e. true, as represented by my array (not every student is able to attend on every day).

There could be all sorts of badness in the way the model was written...

Needless to say, I get the error WARNING: model inconsistency detected

enum Students = { Mark, Bart, Mary, Anne, Sue };

enum Classes = { Mon, Tue, Wed};

array[Students, Classes] of bool: pref =  
              [| true, true, false,
              | false, true, true,
              | true, false, true,
              | true, false, false,
              | false, false, true |];

constraint forall (i in Students, j in Classes)(pref[i,j]=true);

solve satisfy;

output [ " \(pref[Students,Classes]);\n"  ]

Bonus would be limiting it so each Student only appears once...

Perfect outcome would be something like a list of all the combinations where students preferences are satisfied and they are in 1 class.

[Mark, Anne | Bart | Mary, Sue]

Thank you for helping me along my way!

MikeyB
  • 460
  • 1
  • 6
  • 22

1 Answers1

2

The vital part that you might not understand is that MiniZinc is a declarative language. You do not tell it how to solve a problem, but merely specify the problem and let the language do the rest.

A MiniZinc model is specified in terms of

  • variables: decisions that need to be made / things you want to know
  • parameters: things you already know
  • constraints: the rules that are enforced between any or all variables and parameters.

In your model you didn't specify any variables, merely parameters and constraints. It is said to be inconsistent because you give it an array pref that contains both true and false, but then your constraint enforces that every element in the array must be true.

Likely you meant to make pref your set of variables and meant that your constraint should force one preference for every student:

enum Students = { Mark, Bart, Mary, Anne, Sue };
enum Classes = { Mon, Tue, Wed};

array[Students, Classes] of var bool: pref;
constraint forall (i in Students)(
  count(j in Classes) (pref[i, j]) = 1
);

solve satisfy;
output [ "pref = ", show2d(pref), ";"  ];

If you only want each student to have at least one preference you can use exists instead:

enum Students = { Mark, Bart, Mary, Anne, Sue };
enum Classes = { Mon, Tue, Wed};

array[Students, Classes] of var bool: pref;
constraint forall (i in Students)(
  exists(j in Classes) (pref[i, j])
);

solve satisfy;
output [ "pref = ", show2d(pref), ";"  ];

This would work, but if you know, as is the case in the first solution, that the students only make one preference, then it is better to just enforce this as part of the variable type:

enum Students = { Mark, Bart, Mary, Anne, Sue };
enum Classes = { Mon, Tue, Wed};

array[Students] of var Classes: pref;

solve satisfy;
output [ "pref = ", show(pref), ";"  ];

Then no constraint is required to ensure a student has exactly one preference.

Dekker1
  • 5,565
  • 25
  • 33
  • Thanks Dekker for taking the time! Unfortunately I'm still unclear and am getting errors. I'm not sure what to include, exclude and change in my code. E.g. where do I actually specify the content of that array? I'm not using a data file. It's the syntax of it that's giving me problems, let alone clear understanding, as well as how to call the output to print say. I've edited my question to show ideal desired outcome. I'd really like a working model to then bridge my gap in understanding by seeing why it works and why it doesn't. – MikeyB Jun 29 '21 at 08:12
  • Ugh, every random combination of everything you've suggested and everything I had and nothing is working as I would hope. Talk to me like I'm even more of an idiot, please. – MikeyB Jun 29 '21 at 09:16
  • Hi @MikeyB, I've clarified my answer by replacing the constraint fragments by full models. You should be able to run them directly. My understanding was that the preferences would have to be determined by the solver, so they are not assigned at all. If this is not how you envisioned it, then maybe it would be good to clarify your question in the terms what are your variables, parameters, and constraint in natural language. If that becomes more clear I'm happy to adjust my answer. – Dekker1 Jun 30 '21 at 00:45
  • cool, thanks, nice to see 3 different ways to go about solving it. I hadn't learnt about show2d() yet and was still trying to incorporate my array, which is still something I'd like to do. Not every student can attend every class, eg Mark can do Mon, Tue but not Wed. I will try some ideas myself to further this but I expect to fail :) In the end I will be adding more more parameters/constraints but I'm just building up a foundation of understanding at the minute. – MikeyB Jun 30 '21 at 07:43
  • Do you know of any forums or discord groups or even online teachers that can teach me this stuff? I'm finding it hard to transition between reading the examples on the minzinc site, all which make sense as they are written, but some core gaps in my understanding of the way things work under the hood are making it hard for me to visualise my own models for the particular problems I'd like to solve. – MikeyB Jun 30 '21 at 07:48
  • Have a look at the MiniZinc website: https://www.minizinc.org. It has a section on learning MiniZinc. The Coursera courses are a quick (and free) way to get started. – Dekker1 Jun 30 '21 at 23:49
  • Thanks, thought at first you mean their documentation, which itself is very thorough but I was hoping for something more 'hands on'. Didn't realise they developed a whole video course on Coursera as well. Good on them! – MikeyB Jul 01 '21 at 10:20
  • Might you have any further suggestions on how to include the preferences the students have, and not just find all the combinations? E.g. Anne is only ok with Mon. `count(j in Classes) (pref[i, j]) = 1` and the last find ~150 solutions, while the second finds thousands as it is not limiting the students to just one day. – MikeyB Jul 01 '21 at 10:27