modelingminizinc

MiniZinc -- can someone please fix my code?


I try to create a work management system. Overall, I try to develop a daily movement strategy, during which the car would end up in the last place at the beginning of the day and collect the people in this row at the end of the day.

Types of work:

Restrictions:

Aim of optimization:

My code:

int: num_jobs = 10;
array[1..num_jobs] of int: jobs = 1..num_jobs;
array[1..7] of int: days = 1..7;
int: full_day_workers = 2;
int: half_day_workers = 1;
int: daily_labor_limit = 8;
int: fuel_rate = 10; % Fuel rate in units per distance

% Define the time between jobs as a two-dimensional array
array[jobs, jobs] of int: time = 
[
    [0, 4, 6, 4, 7, 0, 8, 2, 3, 5],
    [4, 0, 3, 7, 2, 0, 6, 8, 1, 5],
    [6, 3, 0, 8, 0, 0, 3, 5, 9, 2],
    [4, 7, 8, 0, 4, 0, 5, 3, 6, 7],
    [7, 2, 0, 4, 0, 0, 2, 5, 3, 9],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [8, 6, 3, 5, 2, 0, 0, 6, 4, 8],
    [2, 8, 5, 3, 5, 0, 6, 0, 9, 1],
    [3, 1, 9, 6, 3, 0, 4, 9, 0, 2],
    [5, 5, 2, 7, 9, 0, 8, 1, 2, 0]
];

% Define the flattened time and distances arrays
array[jobs * jobs] of int: flat_time = [time[i,j] | i,j in jobs];
array[jobs * jobs] of int: distances = [flat_time[(i-1)*num_jobs + (j-1)] | i,j in jobs];

% Variables
array[jobs, days] of var 0..1: job_schedule;
array[jobs] of var days: end_day;
array[days] of var jobs: starting_job;

% Define job types
set of int: job_types = {1, 2};

% Full-day and half-day jobs
array[jobs] of bool: full_day_job = [if j = 1 \/ j = 3 \/ j = 5 \/ j = 7 \/ j = 9 \/ j = 10 \/ j = 8 then true else false endif | j in jobs];
array[jobs] of bool: half_day_job = [if j = 2 \/ j = 4 \/ j = 6 then true else false endif | j in jobs];

% Constraints
% All work should be done
constraint forall(j in jobs) (
    sum(d in days) (job_schedule[j,d]) = 1
);

% Not exceeding the daily labor limit
constraint forall(d in days) (
    sum(j in jobs) (
        job_schedule[j,d] * (if full_day_job[j] then full_day_workers elseif half_day_job[j] then half_day_workers else 0 endif)
    ) <= daily_labor_limit
);

% A specified person is required for job types
constraint forall(d in days) (
    sum(j in jobs) (
        job_schedule[j,d] * (if j = 1 then full_day_workers elseif j = 2 then half_day_workers else 0 endif)
    ) >= full_day_workers
);

% The car should end up in the last place at the beginning of the day
constraint forall(d in days) (
    starting_job[d] = if d = 1 then 1 else end_day[d-1] + 1 endif
);

% Each job should end within the day it started
constraint forall(j in jobs, d in days) (
    end_day[d] >= starting_job[d] /\
    end_day[d] <= starting_job[d] + sum(d2 in days where d2 > d) (sum(j2 in jobs) (job_schedule[j2, d2] * flat_time[(j-1)*num_jobs + (j2-1)] + fuel_rate * distances[(j-1)*num_jobs + (j2-1)]))
);

% Optimize total distance traveled
var int: total_distance = sum(j in jobs, d in days where d < 7) (fuel_rate * distances[(j-1)*num_jobs + (starting_job[d]-1)]);

solve minimize total_distance;

output [
    "Starting job: ", show(starting_job), "\n",
    "End day: ", show(end_day), "\n",
    "Job schedule: ", show(job_schedule), "\n",
    "Total distance: ", show(total_distance)
];

Solution

  • Your code have several syntax issues so the model cannot be run. Here are some of the issues:

    array[jobs, jobs] of int: time = array2d(jobs,jobs,
    [
        0, 4, 6, 4, 7, 0, 8, 2, 3, 5,
        4, 0, 3, 7, 2, 0, 6, 8, 1, 5,
        % ....
        5, 5, 2, 7, 9, 0, 8, 1, 2, 0
     ]);