Skip to main content
Yanked

This release has been yanked by its maintainers, and will be ignored by installers, except when explicitly specified.
Reason given by maintainers: Use the timefold package instead

Timefold Logo

Timefold Solver for Python

PyPI

Reliability Rating Security Rating Maintainability Rating Coverage

Timefold Solver is an AI constraint solver for Python to optimize the Vehicle Routing Problem, Employee Rostering, Maintenance Scheduling, Task Assignment, School Timetabling, Cloud Optimization, Conference Scheduling, Job Shop Scheduling, Bin Packing and many more planning problems.

Using Timefold Solver in Python is significantly slower than using Timefold Solver in Java or Kotlin.

Requirements

Source code overview

Domain

In Timefold Solver, the domain has three parts:

  • Problem Facts, which do not change.
  • Planning Entities, which have one or more planning variables.
  • Planning Solution, which define the facts and entities of the problem.

Problem Facts

Problem facts can be any Python class, which are used to describe unchanging facts in your problem:

from dataclasses import dataclass
from datetime import time

@dataclass
class Timeslot:
    id: int
    day_of_week: str
    start_time: time
    end_time: time

Planning Entities

To declare Planning Entities, use the @planning_entity decorator along with annotations:

from dataclasses import dataclass, field
from typing import Annotated
from timefold.solver.domain import planning_entity, PlanningId, PlanningVariable

@planning_entity
@dataclass
class Lesson:
    id: Annotated[int, PlanningId]
    subject: str
    teacher: str
    student_group: str
    timeslot: Annotated[Timeslot, PlanningVariable] = field(default=None)
    room: Annotated[Room, PlanningVariable] = field(default=None)
  • The PlanningVariable annotation is used to mark what fields the solver is allowed to change.

  • The PlanningId annotation is used to uniquely identify an entity object of a particular class. The same Planning Id can be used on entities of different classes, but the ids of all entities in the same class must be different.

Planning Solution

To declare the Planning Solution, use the @planning_solution decorator:

from dataclasses import dataclass, field
from typing import Annotated
from timefold.solver.domain import (planning_solution, ProblemFactCollectionProperty, ValueRangeProvider,
                                    PlanningEntityCollectionProperty, PlanningScore)
from timefold.solver.score import HardSoftScore

@planning_solution
@dataclass
class TimeTable:
    timeslots: Annotated[list[Timeslot], ProblemFactCollectionProperty, ValueRangeProvider]
    rooms: Annotated[list[Room], ProblemFactCollectionProperty, ValueRangeProvider]
    lessons: Annotated[list[Lesson], PlanningEntityCollectionProperty]
    score: Annotated[HardSoftScore, PlanningScore] = field(default=None)
  • The ValueRangeProvider annotation is used to denote a field that contains possible planning values for a PlanningVariable.

  • TheProblemFactCollection annotation is used to denote a field that contains problem facts. This allows these facts to be queried in your constraints.

  • The PlanningEntityCollection annotation is used to denote a field that contains planning entities. The planning variables of these entities will be modified during solving.

  • The PlanningScore annotation is used to denote the field that holds the score of the current solution. The solver will set this field during solving.

Constraints

You define your constraints by using the ConstraintFactory:

from domain import Lesson
from timefold.solver.score import Joiners, HardSoftScore, constraint_provider

@constraint_provider
def define_constraints(constraint_factory):
    return [
        # Hard constraints
        room_conflict(constraint_factory),
        # Other constraints here...
    ]

def room_conflict(constraint_factory):
    # A room can accommodate at most one lesson at the same time.
    return constraint_factory.for_each_unique_pair(Lesson,
                # ... in the same timeslot ...
                Joiners.equal(lambda lesson: lesson.timeslot),
                # ... in the same room ...
                Joiners.equal(lambda lesson: lesson.room)) \
        .penalize("Room conflict", HardSoftScore.ONE_HARD)

for more details on Constraint Streams, see https://timefold.ai/docs/timefold-solver/latest/constraints-and-score/score-calculation.

Solve

from timefold.solver import SolverFactory
from timefold.solver.config import SolverConfig, TerminationConfig, ScoreDirectorFactoryConfig, Duration
from constraints import define_constraints
from domain import TimeTable, Lesson, generate_problem

solver_config = SolverConfig(
    solution_class=TimeTable,
    entity_class_list=[Lesson],
    score_director_factory_config=ScoreDirectorFactoryConfig(
        constraint_provider_function=define_constraints
    ),
    termination_config=TerminationConfig(
        spent_limit=Duration(seconds=30)
    )
)

solver = SolverFactory.create(solver_config).build_solver()
solution = solver.solve(generate_problem())

solution will be a TimeTable instance with planning variables set to the final best solution found.

More information

For a full API spec, visit the Timefold Documentation.

Metadata

Release files for timefold-solver 1.10.0a0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for timefold-solver 1.10.0a0
File Size Uploaded
timefold_solver-1.10.0a0.tar.gz 374.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for timefold-solver 1.10.0a0
File Interpreter ABI Platform
timefold_solver-1.10.0a0-py3-none-any.whl Python 3 none any Details

Total release size: 17.1 MB

Release files / timefold_solver-1.10.0a0.tar.gz

Download URL timefold_solver-1.10.0a0.tar.gz
Size 374.0 kB
Tags Source
SHA-256 checksum
How to use checksums
c06285bd3d98e28272bc0f0e077e45db6b61a8b63ee15231ddd2c6f8c39b5f6f
BLAKE2b-256 checksum
How to use checksums
39a007ac59be29d4e8debc29fc82246a568db06c71cdade2e1e05381bf61464f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / timefold_solver-1.10.0a0-py3-none-any.whl

Download URL timefold_solver-1.10.0a0-py3-none-any.whl
Size 16.7 MB
Tags Python 3
SHA-256 checksum
How to use checksums
8d7f267953e04e1d7ae3272d7fd2d486c2cb969d0711f26ab003912a734b8f5f
BLAKE2b-256 checksum
How to use checksums
68e95f03431f01fa91f49165217a7c0087f180e27623e3c561f63247dd24cc7d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

1.10.0a0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page