Simple example

Open In Colab

If you’re using this notebook in Google Colab, be sure to install PyJobShop first by executing pip install pyjobshop in a cell.

This notebook provides a brief overview of PyJobShop’s modeling features for solving scheduling problems, specifically:

  • How to model a simple scheduling problem using PyJobShop’s Model interface,

  • How to inspect the resulting solution, and

  • How to plot the found solution.

[1]:
from pyjobshop import Model

model = Model()

Let’s add some data to the model. We add four jobs, each consisting of two tasks.

[2]:
jobs = [model.add_job() for _ in range(4)]
tasks = [[model.add_task(job=job) for _ in range(2)] for job in jobs]

Passing the job argument to m.add_task(job=job) ensures that the created task belongs to the right job. Next, we have to create the machines and also specify processing times for each task. The duration of a task is equal to the job index plus one (1, 2, 3 or 4).

[3]:
machines = [model.add_machine() for _ in range(2)]

for job in range(len(jobs)):
    for task in tasks[job]:
        for machine in machines:
            duration = job + 1
            model.add_mode(task, machine, duration)

Now that we have defined all important elements, we can double-check that our model is correct by printing its summary:

[4]:
print(model.summary())
4 jobs
2 resources
└─ 2 machines
8 tasks
16 modes
0 constraints
objective
└─ weight_makespan=1

That checks out! Let’s now solve this model. By default, the model aims to minimize the makespan, which is the maximum completion time of all jobs.

[5]:
result = model.solve(display=False)
print(result)
Solution results
================
  objective: 10.00
lower bound: 10.00
     status: Optimal
    runtime: 0.01 seconds

We found the optimal solution!

Solution

The result variable stores a Result object that contains information about the solving progress and the best found solution. A solution consists of TaskDatas, which stores, for each task, the index of the selected processing mode, the index of the assigned resources, the start time, and end time.

[6]:
for task in result.best.tasks:
    print(task)
TaskData(mode=1, resources=[1], start=6, end=7, idle=0, breaks=0, present=True)
TaskData(mode=2, resources=[0], start=5, end=6, idle=0, breaks=0, present=True)
TaskData(mode=4, resources=[0], start=0, end=2, idle=0, breaks=0, present=True)
TaskData(mode=7, resources=[1], start=0, end=2, idle=0, breaks=0, present=True)
TaskData(mode=8, resources=[0], start=2, end=5, idle=0, breaks=0, present=True)
TaskData(mode=11, resources=[1], start=7, end=10, idle=0, breaks=0, present=True)
TaskData(mode=12, resources=[0], start=6, end=10, idle=0, breaks=0, present=True)
TaskData(mode=15, resources=[1], start=2, end=6, idle=0, breaks=0, present=True)

Plotting

Each scheduled task can now be nicely plotted in a Gantt chart.

[7]:
from pyjobshop.plot import plot_machine_gantt

plot_machine_gantt(result.best, model.data())
../_images/examples_simple_example_17_0.png

The plot shows a Gantt chart of our solution. Each row represents a machine and each horizontal bar represents a scheduled task. The colors of the tasks depict the individual jobs they are associated with, with each job having a unique color.

Conclusion

This concludes this example. We showed how to use PyJobShop’s Model interface to setup a simple example, and we showed the representation of a solution and how it can be plotted. For more details about Model, see the API documentation. In the next notebooks, we show you how to model classical scheduling models.