# Copyright (c) 2026 AIRBUS and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from collections import defaultdict
from typing import Any, Generic
from discrete_optimization.generic_tasks_tools.base import Task
from discrete_optimization.generic_tasks_tools.entities import (
SchedulingEntity,
)
from discrete_optimization.generic_tasks_tools.enums import StartOrEnd
from discrete_optimization.generic_tasks_tools.generic_scheduling import (
CumulativeResource,
)
from discrete_optimization.generic_tasks_tools.resource_blocking import (
BlockingConstraintMetadata,
BlockingMode,
)
from discrete_optimization.generic_tasks_tools.solvers.cp.base.cumulative_resource import (
CumulativeResource,
CumulativeResourceSchedulingCpSolver,
OtherCalendarResource,
)
from discrete_optimization.generic_tasks_tools.solvers.cp.base.entity import (
EntityIntervalsMixin,
)
from discrete_optimization.generic_tasks_tools.solvers.cp.base.typing import (
CpVar,
IntervalVar,
)
[docs]
class ResourceBlockingCpSolver(
EntityIntervalsMixin[Task],
CumulativeResourceSchedulingCpSolver[
Task, CumulativeResource, OtherCalendarResource
],
Generic[Task, CumulativeResource, OtherCalendarResource],
):
"""CP mixin for handling resource blocking constraints.
This mixin adds support for:
- Flexible gap blocking: resources blocked between two scheduling entities
- Span blocking: resources blocked during the span of a set of tasks
The mixin handles overlaps between blocking intervals and:
- Calendar unavailability periods (fake tasks)
- Actual task execution intervals
- Other blocking intervals
It creates appropriate cumulative constraints per resource, properly accounting
for blocking intervals without double-counting consumption.
"""
_blocking_intervals: dict[
CumulativeResource,
list[tuple[IntervalVar, int, BlockingConstraintMetadata]],
]
_bounds_entity: dict[SchedulingEntity, CpVar]
_choices_blocking_constraint_vars: dict[str, dict[int, CpVar]]
_choices_demands_variable: dict[str, dict[CumulativeResource, CpVar]]
[docs]
def init_model(self, **kwargs: Any) -> None:
"""Initialize model and reset blocking interval storage."""
super().init_model(**kwargs)
self._blocking_intervals = {}
self.init_entity_intervals_storage()
self._choices_blocking_constraint_vars = {}
self._choices_demands_variable = {}
[docs]
def get_lb_ub_size(
self,
entity1: SchedulingEntity,
start_or_end1: StartOrEnd,
entity2: SchedulingEntity,
start_or_end2: StartOrEnd,
):
lbs1, ubs1, lbe1, ube1 = self.get_lb_ub_entity(entity1)
lbs2, ubs2, lbe2, ube2 = self.get_lb_ub_entity(entity2)
if start_or_end1 == StartOrEnd.START:
if start_or_end2 == StartOrEnd.START:
return max(0, lbs2 - ubs1), max(0, ubs2 - lbs1)
if start_or_end2 == StartOrEnd.END:
return max(0, lbe2 - ubs1), max(0, ube2 - lbs1)
if start_or_end1 == StartOrEnd.END:
if start_or_end2 == StartOrEnd.START:
return max(0, lbs2 - ube1), max(0, ubs2 - lbe1)
if start_or_end2 == StartOrEnd.END:
return max(0, lbe2 - ube1), max(0, ube2 - lbe1)
return None, None
[docs]
def create_entity_intervals(self) -> None:
"""Create interval variables for all entities in resource blocking constraints."""
all_entities = []
for constraint in self.problem.get_flexible_gap_blocking_constraints():
all_entities.append(constraint.left_entity)
all_entities.append(constraint.right_entity)
for span_constraint in self.problem.get_span_blocking_constraints():
all_entities.append(span_constraint.entity)
for entity in all_entities:
self.define_start_and_end_var_and_constraint(entity)
[docs]
def create_flexible_gap_blocking_intervals(self) -> None:
"""Create interval variables for flexible gap blocking constraints.
For each flexible gap constraint, creates an interval from the first entity's
reference point to the second entity's reference point, with the specified
resource demands.
"""
for i_constraint, constraint in enumerate(
self.problem.get_flexible_gap_blocking_constraints()
):
gap_interval = self._create_flexible_gap_blocking_interval(
constraint, i_constraint
)
metadata = constraint.metadata
default_resources = constraint.default_resource_blocked
# Store blocking intervals per resource with metadata and involved tasks
for resource, demand in default_resources.items():
if resource not in self._blocking_intervals:
self._blocking_intervals[resource] = []
# Store interval with its metadata and tasks for later processing
self._blocking_intervals[resource].append(
(gap_interval, demand, metadata)
)
if constraint.has_a_choice():
name_choice = metadata.name_choice
self._choices_blocking_constraint_vars[name_choice] = {}
self._choices_demands_variable[name_choice] = {}
resources = set()
possible_values_per_resource = defaultdict(set)
for value in constraint.choice_resource_blocked:
var = self.create_bool_var(f"{name_choice}_{value}")
self._choices_blocking_constraint_vars[name_choice][value] = var
resources.update(
set(constraint.choice_resource_blocked[value].keys())
)
for r in constraint.choice_resource_blocked[value]:
possible_values_per_resource[r].add(
constraint.choice_resource_blocked[value][r]
)
self.add_exactly_one(
self._choices_blocking_constraint_vars[name_choice].values()
)
for r in resources:
self._choices_demands_variable[name_choice][r] = (
self.create_int_var_from_possible_values(
possible_values=list(possible_values_per_resource[r]) + [0],
name=f"{name_choice}_{r}",
)
)
for value in constraint.choice_resource_blocked:
for r in constraint.choice_resource_blocked[value]:
self.add_implication(
self._choices_blocking_constraint_vars[name_choice][value],
self._choices_demands_variable[name_choice][r]
== constraint.choice_resource_blocked[value][r],
)
for r in resources:
if r not in self._blocking_intervals:
self._blocking_intervals[r] = []
self._blocking_intervals[r].append(
(
gap_interval,
self._choices_demands_variable[name_choice][r],
metadata,
)
)
def _create_flexible_gap_blocking_interval(
self, constraint, i_constraint
) -> IntervalVar:
entity1 = constraint.left_entity
ref1 = constraint.start_or_end_left_entity
entity2 = constraint.right_entity
ref2 = constraint.start_or_end_right_entity
# Get time variables for the gap boundaries
gap_start = (
self._starts_entity[entity1]
if ref1 == StartOrEnd.START
else self._ends_entity[entity1]
)
gap_end = (
self._starts_entity[entity2]
if ref2 == StartOrEnd.START
else self._ends_entity[entity2]
)
lb_size, ub_size = self.get_lb_ub_size(entity1, ref1, entity2, ref2)
# Create a variable for the gap size
# The gap may be 0 or positive (we'll enforce positive later)
gap_size = self.create_int_var(
lb=lb_size,
ub=ub_size,
name=f"gap_size_{i_constraint}",
)
# Create a boolean variable for gap validity (positive duration)
gap_is_present = self.create_bool_var(f"gap_valid_{i_constraint}")
# Constrain gap_size = gap_end - gap_start when gap is present
self.add_implication(gap_is_present, gap_size == gap_end - gap_start)
# Create interval variable for the gap
gap_interval = self.create_interval_var(
start=gap_start,
duration=gap_size,
end=gap_end,
presence=gap_is_present,
name=f"blocking_gap_{entity1.entity_id}_{ref1}_to_{entity2.entity_id}_{ref2}",
)
is_active_entity_list = [
is_active_entity
for entity in (entity1, entity2)
# discard always active entities
if not (
isinstance(
(is_active_entity := self._get_entity_is_active_var(entity)),
int,
)
and is_active_entity == 1
)
]
if len(is_active_entity_list) > 0:
# gap present <=> all entities are active
self.add_implication(is_active_entity_list, gap_is_present == 1)
for is_active_var in is_active_entity_list:
self.add_implication(~is_active_var, gap_is_present == 0)
else:
# both entities always active
self.add_bool_constraint(gap_is_present == 1)
return gap_interval
[docs]
def create_span_blocking_intervals(self) -> None:
"""Create interval variables for span blocking constraints.
For each span constraint, creates an interval from the minimum start time
to the maximum end time of the specified tasks, with the specified resource demands.
"""
for i_constraint, constraint in enumerate(
self.problem.get_span_blocking_constraints()
):
entity = constraint.entity
resources = constraint.default_resource_blocked
metadata = constraint.metadata
# Store blocking intervals per resource with metadata
for resource, demand in resources.items():
if resource not in self._blocking_intervals:
self._blocking_intervals[resource] = []
# Store interval with its metadata and task set for overlap handling
self._blocking_intervals[resource].append(
(self._intervals_entity[entity], demand, metadata)
)
if constraint.has_a_choice():
name_choice = metadata.name_choice
self._choices_blocking_constraint_vars[name_choice] = {}
self._choices_demands_variable[name_choice] = {}
resources = set()
possible_values_per_resource = defaultdict(set)
for value in constraint.choice_resource_blocked:
var = self.create_bool_var(f"{name_choice}_{value}")
self._choices_blocking_constraint_vars[name_choice][value] = var
resources.update(
set(constraint.choice_resource_blocked[value].keys())
)
for r in constraint.choice_resource_blocked[value]:
possible_values_per_resource[r].add(
constraint.choice_resource_blocked[value][r]
)
self.add_exactly_one(
self._choices_blocking_constraint_vars[name_choice].values()
)
for r in resources:
self._choices_demands_variable[name_choice][r] = (
self.create_int_var_from_possible_values(
possible_values=list(possible_values_per_resource[r]) + [0],
name=f"{name_choice}_{r}",
)
)
for value in constraint.choice_resource_blocked:
for r in constraint.choice_resource_blocked[value]:
self.add_implication(
self._choices_blocking_constraint_vars[name_choice][value],
self._choices_demands_variable[name_choice][r]
== constraint.choice_resource_blocked[value][r],
)
for r in resources:
if r not in self._blocking_intervals:
self._blocking_intervals[r] = []
self._blocking_intervals[r].append(
(
self._intervals_entity[entity],
self._choices_demands_variable[name_choice][r],
metadata,
)
)
[docs]
def get_blocking_intervals_and_demands(
self, resource: CumulativeResource
) -> tuple[list[tuple[IntervalVar, int]], list[tuple[IntervalVar, int]]]:
blocking_data = self._blocking_intervals.get(resource, [])
reservation_blocking = []
active_blocking = []
for blocking_entry in blocking_data:
interval, demand, metadata = blocking_entry
if metadata.mode == BlockingMode.RESERVATION:
reservation_blocking.append((interval, demand))
else: # ACTIVE
active_blocking.append((interval, demand))
return reservation_blocking, active_blocking
[docs]
def get_individual_task_consumption_resource_to_be_included(
self, resource: CumulativeResource
):
# return tasks that should be included in resource constraint
# I.e not covered by span constraints
task_conso = {}
if self.problem.is_cumulative_resource(resource):
if (
self.avoid_interval_optional_for_cumulative_resources
or self.problem.has_any_cumulative_consumption_dependent()
):
# no optional interval, use rather demand variables
task_conso = {
task: (self.get_task_interval(task=task), conso)
for task in self.problem.tasks_list
if not isinstance(
(
conso := self.get_cumulative_resource_demand_variable(
task=task, resource=resource
)
),
int,
)
or conso > 0
}
else:
task_conso = {
task: (self.get_task_mode_interval(task=task, mode=mode), conso)
for task in self.problem.tasks_list
for mode in self.problem.get_task_modes(task=task)
if (
conso := self.problem.get_cumulative_resource_consumption(
resource=resource, task=task, mode=mode
)
)
> 0
}
remove = set()
for sc in self.problem.get_span_blocking_constraints():
tasks = sc.entity.get_tasks()
if resource in sc.default_resource_blocked:
val = sc.default_resource_blocked[resource]
if val >= 1:
if sc.remove_individual_task_contrib:
for t in tasks:
if t in task_conso:
remove.add(t)
for t in remove:
task_conso.pop(t)
return list(task_conso.values()), remove
[docs]
def create_cumulative_constraint_including_blocking(
self, resource: CumulativeResource
) -> None:
"""Create cumulative constraints including blocking intervals.
Blocking is ALWAYS ADDITIVE: task consumption + blocking consumption.
Creates TWO cumulative constraints to properly handle BlockingMode:
Constraint 1 (WITHOUT calendar):
- All task intervals
- ALL blocking intervals (RESERVATION + ACTIVE)
- NO fake tasks (calendar gaps)
Purpose: Enforces RESERVATION blocking even during unavailable periods
Constraint 2 (WITH calendar):
- All task intervals
- ONLY ACTIVE blocking intervals
- Fake tasks (calendar gaps)
Purpose: Enforces ACTIVE blocking only during available periods
Args:
resource: The cumulative resource to constrain
"""
# Get task consumption intervals
# task_intervals = self.get_resource_consumption_intervals(resource)
task_intervals, _ = (
self.get_individual_task_consumption_resource_to_be_included(resource)
)
# Get fake tasks for calendar gaps
fake_tasks_intervals = [
(
self.create_interval_var(
start=start,
duration=end - start,
name=f"fake_task_{resource}_{i_task}",
),
value,
)
for i_task, (start, end, value) in enumerate(
self.problem.get_fake_tasks(resource=resource)
)
]
# Separate blocking intervals by mode
reservation_blocking, active_blocking = self.get_blocking_intervals_and_demands(
resource
)
# Get resource capacity
capacity = self.problem.get_resource_max_capacity(resource)
# CONSTRAINT 1: Tasks + ALL blocking (RESERVATION + ACTIVE) - NO calendar
# This enforces RESERVATION blocking even during unavailable periods
intervals_no_calendar = []
intervals_no_calendar.extend(task_intervals)
intervals_no_calendar.extend(reservation_blocking)
intervals_no_calendar.extend(active_blocking)
intervals_1 = [
interval
for interval, demand in intervals_no_calendar
if not isinstance(demand, int) or demand > 0
]
demands_1 = [
demand
for interval, demand in intervals_no_calendar
if not isinstance(demand, int) or demand > 0
]
if len(intervals_1) > 0:
if capacity == 1 and all(isinstance(v, int) and v == 1 for v in demands_1):
if self.use_no_overlap_for_capa_1 or not self.use_cumulative_for_capa_1:
self.add_no_overlap(intervals_1)
if self.use_cumulative_for_capa_1:
self.add_cumulative(
intervals=intervals_1, demands=demands_1, capacity=capacity
)
else:
self.add_cumulative(
intervals=intervals_1, demands=demands_1, capacity=capacity
)
# CONSTRAINT 2: Tasks + ACTIVE blocking + calendar - NO RESERVATION blocking
# This enforces ACTIVE blocking only during available periods (constrained by calendar)
if active_blocking or fake_tasks_intervals:
intervals_with_calendar = []
intervals_with_calendar.extend(task_intervals)
intervals_with_calendar.extend(active_blocking)
intervals_with_calendar.extend(fake_tasks_intervals)
intervals_2 = [
interval
for interval, demand in intervals_with_calendar
if not isinstance(demand, int) or demand > 0
]
demands_2 = [
demand
for interval, demand in intervals_with_calendar
if not isinstance(demand, int) or demand > 0
]
if len(intervals_2) > 0:
if capacity == 1 and all(
isinstance(v, int) and v == 1 for v in demands_2
):
if (
self.use_no_overlap_for_capa_1
or not self.use_cumulative_for_capa_1
):
self.add_no_overlap(intervals_2)
if self.use_cumulative_for_capa_1:
self.add_cumulative(
intervals=intervals_2, demands=demands_2, capacity=capacity
)
else:
self.add_cumulative(
intervals=intervals_2, demands=demands_2, capacity=capacity
)
[docs]
def create_resource_blocking_constraints(self) -> None:
"""Create all resource blocking interval variables.
This should be called during model initialization, before cumulative
resource constraints are created (so that create_calendar_resources_constraint
can check for blocking intervals).
"""
self.create_entity_intervals()
self.create_flexible_gap_blocking_intervals()
self.create_span_blocking_intervals()
[docs]
def create_calendar_resources_constraint(
self, resource: CumulativeResource
) -> None:
"""Create calendar resource constraint, using blocking-aware version if needed.
Overrides the parent method to automatically use blocking-aware cumulative
constraints when blocking intervals exist for this resource.
Args:
resource: The resource to constrain
"""
# Check if this resource has blocking constraints
has_blocking = (
resource in self._blocking_intervals
and len(self._blocking_intervals[resource]) > 0
)
if has_blocking:
# Use specialized method that includes blocking intervals
self.create_cumulative_constraint_including_blocking(resource=resource)
else:
# Use standard parent method
super().create_calendar_resources_constraint(resource=resource)