Solver tips

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 contains tips for working with the CP solvers supported by PyJobShop.

[1]:
from pyjobshop import Model

Solver-specific parameters

The solve method accepts additional keyword arguments that are passed directly to the underlying solver. This lets you fine-tune solver behavior beyond what PyJobShop exposes directly. For a list of available parameters, see the OR-Tools CP-SAT parameters and the CP Optimizer parameters.

Finding feasible solutions

In some situations, it may be only needed to find a feasible solution. You can achieve this by passing an additional parameter to the solve function, depending on the solver used:

  • OR-Tools: stop_after_first_solution=True.

  • CP Optimizer: SolutionLimit=1.

Below we demonstrate it with OR-Tools (used by default).

[2]:
model = Model()
tasks = [model.add_task(name=idx) for idx in range(6)]
machines = [model.add_machine(name=idx) for idx in range(2)]

for task in tasks:
    model.add_mode(task, machines[0], duration=1)
    model.add_mode(task, machines[1], duration=3)
[3]:
result = model.solve(display=False, stop_after_first_solution=True)
print(result)
Solution results
================
  objective: 5.00
lower bound: 5.00
     status: Optimal
    runtime: 0.01 seconds

Let’s double-check that the optimal solution is better:

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

Potpourri

  • Zero-duration tasks can be difficult to handle for CP solvers. A zero-duration task has no time span, so it can be placed anywhere without consuming resources, which may lead to unexpected behavior. If you encounter strange results, consider using a small duration (e.g., duration=1) instead.