from __future__ import annotations
from abc import abstractmethod
from discrete_optimization.generic_tasks_tools.base import (
Task,
TasksProblem,
TasksSolution,
)
from discrete_optimization.generic_tasks_tools.enums import AbsentValue, StartOrEnd
from discrete_optimization.generic_tasks_tools.utils import optional_override
from discrete_optimization.generic_tools.enums import SignEnum
[docs]
class SchedulingProblem(TasksProblem[Task]):
"""Base class for scheduling problems.
A scheduling problems is about finding start and end times to tasks.
"""
[docs]
@optional_override
def get_last_tasks(self) -> list[Task]:
"""Get a sublist of tasks that are candidate to be the last one scheduled.
Default to all tasks.
"""
return self.tasks_list
[docs]
@optional_override
def get_makespan_lower_bound(self) -> int:
"""Get a lower bound on global makespan.
Default to 0. But can be overriden for problems with more information.
"""
return 0
[docs]
@abstractmethod
def get_makespan_upper_bound(self) -> int:
"""Get an upper bound on global makespan."""
pass
[docs]
class SchedulingSolution(TasksSolution[Task]):
"""Base class for solution to scheduling problems."""
problem: SchedulingProblem[Task]
[docs]
@abstractmethod
def get_end_time(self, task: Task) -> int | AbsentValue:
"""Get end time of the task
Hypothesis:
The returned time can have AbsentValue.ABSENT only if `self.is_present(task) is False`.
Args:
task:
Returns:
"""
...
[docs]
@abstractmethod
def get_start_time(self, task: Task) -> int | AbsentValue:
"""Get start time of the task
Hypothesis:
The returned time can have AbsentValue.ABSENT only if `self.is_present(task) is False`.
Args:
task:
Returns:
"""
...
[docs]
def is_scheduled(self, task: Task) -> bool:
"""Tell whether the task been scheduled."""
return (
self.get_start_time(task) is not AbsentValue.ABSENT
and self.get_end_time(task) is not AbsentValue.ABSENT
)
[docs]
def is_present(self, task: Task) -> bool:
return self.is_scheduled(task)
[docs]
def get_start_or_end_time(
self, task: Task, start_or_end: StartOrEnd
) -> int | AbsentValue:
"""Get the start or end time for a given task.
The returned time can have AbsentValue.ABSENT only if `self.is_present(task) is False`.
"""
if start_or_end == StartOrEnd.START:
return self.get_start_time(task)
else:
return self.get_end_time(task)
[docs]
def get_duration(self, task: Task) -> int | AbsentValue:
if (start := self.get_start_time(task)) is not AbsentValue.ABSENT and (
end := self.get_end_time(task)
) is not AbsentValue.ABSENT:
return end - start
return AbsentValue.ABSENT
[docs]
def get_max_end_time(self) -> int:
"""Get makespan of the solution.
If no tasks are done, will return 0.
Returns:
"""
active_last_tasks = [
task for task in self.problem.get_last_tasks() if self.is_present(task)
]
if len(active_last_tasks) == 0:
return 0
else:
return max(self.get_end_time(task) for task in active_last_tasks)
[docs]
def constraint_on_task_satisfied(
self, task: Task, start_or_end: StartOrEnd, sign: SignEnum, time: int
) -> bool:
actual_time = self.get_start_or_end_time(task=task, start_or_end=start_or_end)
if sign == SignEnum.UEQ:
return actual_time >= time
elif sign == SignEnum.LEQ:
return actual_time <= time
elif sign == SignEnum.LESS:
return actual_time < time
elif sign == SignEnum.UP:
return actual_time > time
elif sign == SignEnum.EQUAL:
return actual_time == time
else:
raise NotImplementedError()
[docs]
def constraint_chaining_tasks_satisfied(self, task1: Task, task2: Task) -> bool:
return self.get_end_time(task1) == self.get_start_time(task2)
[docs]
def get_running_tasks(self, time: int) -> list[Task]:
"""Extract tasks running at given time.
Optional tasks not "present" (according to `is_present()`) are removed from the list.
"""
return [
task
for task in self.problem.tasks_list
if (
self.is_present(task)
and self.get_start_time(task) <= time < self.get_end_time(task)
)
]