Anyone with MiniZinc expertise (1 Viewer)

jdraw

Super Moderator
Staff member
Local time
Today, 09:14
Joined
Jan 23, 2006
Messages
15,338
I have an interesting scheduling requirement that includes constraints. This is more an academic issue, but I do have a colleague who has a similar real life assignment.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 14:14
Joined
Jul 9, 2003
Messages
16,187
ChatGPT Said:-
For tackling a scheduling problem with constraints using MiniZinc, the approach would involve several steps:
  1. Define Variables: Identify the key variables of your scheduling problem (e.g., tasks, resources, time slots).
  2. Set Constraints: Define the constraints that apply to these variables. This could include resource availability, time restrictions, or specific conditions that must be met.
  3. Optimization Goals: Determine what you are trying to optimize (e.g., minimize total time, balance resource use).
  4. Model in MiniZinc: Write a MiniZinc model that incorporates your variables, constraints, and optimization goals. MiniZinc offers a high-level, declarative way to represent these elements.
  5. Solve the Model: Use a MiniZinc solver to find solutions that meet your constraints and optimization criteria. MiniZinc can interface with various solvers, each with different strengths.
  6. Analyze Results: Evaluate the solutions provided by the solver to see if they meet the real-world requirements of your scheduling problem.
For specific MiniZinc code or more detailed guidance, the context of the scheduling problem (e.g., nature of tasks, types of constraints) is needed. MiniZinc is quite flexible and can model a wide range of scheduling scenarios.
Would you like me to expand on this? Y or N.

Uncle Gizmo Said:-
>>>Write a MiniZinc model<<< Could you provide an example please


ChatGPT
Sure, I'll provide a basic example of a MiniZinc model for a simple scheduling problem. Let's assume we're scheduling tasks over a number of days, with each task requiring a certain number of days to complete and some tasks having dependencies on others.
Here's a simplified MiniZinc model to illustrate this:

Code:
% Number of tasks
int: numTasks = 5;

% Number of days available for scheduling
int: numDays = 10;

% Duration of each task (in days)
array[1..numTasks] of int: duration = [2, 3, 1, 4, 2];

% Dependencies between tasks (task 2 depends on task 1, etc.)
array[1..numTasks, 1..numTasks] of bool: dependency = array2d(1..numTasks, 1..numTasks, 
    [ 0, 1, 0, 0, 0,
      0, 0, 0, 0, 0,
      0, 1, 0, 0, 0,
      0, 0, 1, 0, 0,
      0, 0, 0, 1, 0 ]);

% Decision variables: start day for each task
array[1..numTasks] of var 1..numDays: startDay;

% Constraint: task must finish within the schedule
constraint forall(i in 1..numTasks) (
    startDay[i] + duration[i] - 1 <= numDays
);

% Constraint: dependencies between tasks
constraint forall(i in 1..numTasks, j in 1..numTasks) (
    if dependency[i,j] then
        startDay[j] >= startDay[i] + duration[i]
    else
        true
    endif
);

% Optimization goal: minimize the last finishing day
var int: lastFinish = max(i in 1..numTasks) (startDay[i] + duration[i] - 1);
solve minimize lastFinish;

% Output
output ["Task " ++ show(i) ++ " starts on day " ++ show(startDay[i]) | i in 1..numTasks];


This model schedules tasks over a set of days, ensuring that all tasks are completed within the available time frame and that any dependencies between tasks are respected. The goal is to minimize the latest finishing day of all tasks.
Remember, this is a simplified model. Real-world scheduling problems might involve more complex constraints and objectives.
 

jdraw

Super Moderator
Staff member
Local time
Today, 09:14
Joined
Jan 23, 2006
Messages
15,338
Hi Tony, I went around in circles with chatgpt. But I am new to MiniZinc.
Here is the issue my colleague is addressing.

The goal:
construct a program to do the following:

Assign a number of Teams to a number of
Activities over a number of Sessions.

Example: to overview the goal with a specific sample.
4 Sessions
4 Activities
8 Participants
2 Participants per Team
there are 28 unique combinations/pairs of 2 participants ( n Choose 2)
To illustrate:
The sessions are 9am, 10am, 11am, 12pm.
The activities are chess, snooker , bowling, darts
The participants are A, B, C, D, E, F, G, H
Each Team is a unique combination/pair of Participants
A Team can only do one activity and then is removed from further action.

The specifications, a participant can't be assigned to more
than one activity in the same session, and a participant can't be
assigned to the same activity more than once, and each Team is composed of
2 participants and each session activity involves a unique Team.



Ultimately, a template to allow for M activities in N Sessions by X participants where Teams can be of different sizes for different activities.
 

jdraw

Super Moderator
Staff member
Local time
Today, 09:14
Joined
Jan 23, 2006
Messages
15,338
Tony,
I copied your minizinc code to TonySched.mzn. This was the result

Running TonySched.mzn
81msec​

TonySched:12-55.17-22:
MiniZinc: type error: cannot determine coercion from type array[int,int] of int to type array[int,int] of bool
Process finished with non-zero exit code 1.
Finished in 81msec.

Same sort of results I got when running minizinc suggestions from chatgpt.
jack
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 14:14
Joined
Jul 9, 2003
Messages
16,187
I'm not sure yet.

It has led me up the garden path on numerous occasions.

However the more experience I have with it, I realise it's more about the way you ask your questions.

My advice to anyone starting out with chat GPT is to initially ask it programming questions in a language you are familiar with. This way you will be able to study its responses and come to understand how easily it can mislead you.

Chat GPT is like a very clever and very enthusiastic student who is doing their best to help you. But this student does not have the experience of a seasoned programmer. Not a problem if you are asking questions and setting it tasks in your programming language, you can steer it in the right direction.

However if both of you are inexperienced, then what is produced can lack something that an experienced programmer in say Python or in Jacks case in this thread, "MiniZinc" would spot and be able to correct and advise on straight away.

If you don't have any experience with the language and I know nothing about "MiniZinc" then you're approach to getting an answer might be to break it down into simple parts and solve each one individually and then bring it all together at the end.

Another idea - present the problem to chat GPT and ask it to suggest a suitable language to solve the problem. It might surprise you and find an entirely different language which is better suited to the task.
 

jdraw

Super Moderator
Staff member
Local time
Today, 09:14
Joined
Jan 23, 2006
Messages
15,338
It's too early to tell David. I have had some clear, quick responses to some questions/subjects, but much less than stellar in others. I asked chatGPT some questions about furnace symptoms and also about car tires/noises and got direct, useful responses. However, I have had some confusing, circuitous responses when dealing with Access/vba/vbe. My experience says that the software (Ver 3.5 that I have used) often makes things up (hallucinations). Several times, different sessions, I get a code snippet response from chatGPT that include s properties that don't exist. I also tried Bing chat with a question I had posed to chatGPT-- similar responses and seemed to be going in a circle. Bing chat seems just another interface to chatGPT, but I haven't pursued it enough to comment one way or the other.

Using the miniZinc as reference, I don't know the syntax nor capabilities and am not familiar with the software. I watched some videos and will get to practising with some examples. I tried the scheduling problem with Access/vba and shortly found I was "in over my head". I sent a PM to one of our "math-oriented" posters who assured me this was not a simple problem and one which he felt got complex quickly. This was scheduling with constraints/ optimization/linear programming/complex search.... He advised posting on a different site. The reaction I got there was less than helpful---"we're not here to do your homework".... "you have all the info you need to solve the problem". So that's when I went to chatGPT --since the human interaction was dead in the water. I asked chatGPT what kind of problem/family I was dealing with and how to solve. A few long chat sessions suggested some vba, then python and the miniZinc (which had not heard of), then back to Python.... So, off to youtube -miniZinc... and that's where I am at the moment. But I did wander to Optaplanner, and a series of Constraint Optimization Problem/Program videos. It's more of a diversion than a problem. It is interesting in that I/you can see how these types of problems were fundamental to the creation of the various models underlying chatGPT, AI etc.

My gut feel is that AI is a journey, not an endpoint. There will be some twists and turns, and some commercialization as things improve. A lot of the models are built on digitized info (internet sites/forums/articles/opinions/social media) and we all have thoughts/experiences on the facts and verification on that "knowledgebase".

So, it's too early to tell how good/bad/relative chatGPT is. Depends on context, like most things. For a quick answer to a general problem/question, it is an excellent start. More focused than general search or youtube. But, for me, it loses its helpfulness when it suggests A, then B, then returns to A without a recognized change in parameters. It certainly has established a phenomenal following and acceptance in a relatively short period of time. Much like the growth and acceptance of Starlink and LEO satellites.
 

jdraw

Super Moderator
Staff member
Local time
Today, 09:14
Joined
Jan 23, 2006
Messages
15,338
For anyone happening upon this thread.

I solved the puzzle with miniZinc IDE 2.8.1

The solution code;
Code:
%Dec -11-23   ----this is my working testing version % WORKING  
%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"];
[\code]

----------------------------a few solutions ------------------------------

[|              CHECKERS: HORSESHOES:  DARTS: TABLE_TENNIS: 
 |  Session(9):   {B, F},     {A, H}, {D, E},       {C, G}
 | Session(10):   {A, G},     {B, E}, {C, H},       {D, F}
 | Session(11):   {C, E},     {D, G}, {A, F},       {B, H}
 | Session(12):   {D, H},     {C, F}, {B, G},       {A, E}
 |]
----------
[|              CHECKERS: HORSESHOES:  DARTS: TABLE_TENNIS: 
 |  Session(9):   {D, F},     {A, H}, {C, E},       {B, G}
 | Session(10):   {A, G},     {D, E}, {B, H},       {C, F}
 | Session(11):   {B, E},     {C, G}, {A, F},       {D, H}
 | Session(12):   {C, H},     {B, F}, {D, G},       {A, E}
 |]
----------
[|              CHECKERS: HORSESHOES:  DARTS: TABLE_TENNIS: 
 |  Session(9):   {C, F},     {A, H}, {D, E},       {B, G}
 | Session(10):   {A, G},     {C, E}, {B, H},       {D, F}
 | Session(11):   {B, E},     {D, G}, {A, F},       {C, H}
 | Session(12):   {D, H},     {B, F}, {C, G},       {A, E}
 |]
----------
[|              CHECKERS: HORSESHOES:  DARTS: TABLE_TENNIS: 
 |  Session(9):   {B, E},     {A, H}, {D, G},       {C, F}
 | Session(10):   {A, G},     {B, F}, {C, E},       {D, H}
 | Session(11):   {C, H},     {D, E}, {A, F},       {B, G}
 | Session(12):   {D, F},     {C, G}, {B, H},       {A, E}
 |]
----------
[|              CHECKERS: HORSESHOES:  DARTS: TABLE_TENNIS: 
 |  Session(9):   {B, E},     {A, H}, {C, G},       {D, F}
 | Session(10):   {A, G},     {B, F}, {D, E},       {C, H}
 | Session(11):   {D, H},     {C, E}, {A, F},       {B, G}
 | Session(12):   {C, F},     {D, G}, {B, H},       {A, E}
 |]
----------
 

Users who are viewing this thread

Top Bottom