How to Configure Timefold to leave unassigned values in planning entity

I’m working on a scheduling problem where I need to assign technicians to service devices. However, in some cases, it is preferable to leave a device unassigned if no suitable technician (lack of demanded skills) is available (i.e., the device.pesel should be None). I’ve structured my domain model as follows:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@dataclass
class Technician:
id: Annotated[int, PlanningId]
pesel: str
name: str
rbh_per_week: int
rbh_per_year: float
rbh_week_plan: float
selected_rbh: float
free_rbh: float
iums: Set[str]
def has_ium(self, ium: str) -> bool:
return ium in this.iums
def __str__(self) -> str:
return (f"Technician(id={self.id}, name={self.name}, pesel={self.pesel}, "
f"rbh_per_week={self.rbh_per_week}, rbh_per_year={self.rbh_per_year}, "
f"rbh_week_plan={self.rbh_week_plan}, selected_rbh={self.selected_rbh}, "
f"free_rbh={self.free_rbh}, iums={self.iums})")
@planning_entity
@dataclass
class Device:
index: Annotated[int, PlanningId]
ind_rek: str
ium: str
nazwa: str
typ: str
nr_fab: str
norma_rbh: float
data_dostawy: str
uzytkownik: str
pesel: Annotated[Technician | None, PlanningVariable(value_range_provider_refs=['technicianRange'], nullable=True)] = field(default=None)
def __str__(self) -> str:
technician_str = str(self.pesel) if self.pesel else "None"
return (f"Device(index={self.index}, ind_rek={self.ind_rek}, ium={self.ium}, "
f"nazwa={self.nazwa}, typ={self.typ}, nr_fab={self.nr_fab}, "
f"norma_rbh={self.norma_rbh}, data_dostawy={self.data_dostawy}, "
f"uzytkownik={self.uzytkownik}, assigned_technician={technician_str})")
@planning_solution
@dataclass
class DeviceSchedule:
id: str
technician_list: Annotated[List[Technician], ProblemFactCollectionProperty, ValueRangeProvider]
device_list: Annotated[List[Device], PlanningEntityCollectionProperty]
score: Annotated[HardSoftScore, PlanningScore] = field(default=None)
def __str__(self):
return (
f"DeviceSchedule("
f"id={self.id},n"
f"technician_list={self.technician_list},n"
f"device_list={self.device_list},n"
f"score={self.score}"
f")"
)
</code>
<code>@dataclass class Technician: id: Annotated[int, PlanningId] pesel: str name: str rbh_per_week: int rbh_per_year: float rbh_week_plan: float selected_rbh: float free_rbh: float iums: Set[str] def has_ium(self, ium: str) -> bool: return ium in this.iums def __str__(self) -> str: return (f"Technician(id={self.id}, name={self.name}, pesel={self.pesel}, " f"rbh_per_week={self.rbh_per_week}, rbh_per_year={self.rbh_per_year}, " f"rbh_week_plan={self.rbh_week_plan}, selected_rbh={self.selected_rbh}, " f"free_rbh={self.free_rbh}, iums={self.iums})") @planning_entity @dataclass class Device: index: Annotated[int, PlanningId] ind_rek: str ium: str nazwa: str typ: str nr_fab: str norma_rbh: float data_dostawy: str uzytkownik: str pesel: Annotated[Technician | None, PlanningVariable(value_range_provider_refs=['technicianRange'], nullable=True)] = field(default=None) def __str__(self) -> str: technician_str = str(self.pesel) if self.pesel else "None" return (f"Device(index={self.index}, ind_rek={self.ind_rek}, ium={self.ium}, " f"nazwa={self.nazwa}, typ={self.typ}, nr_fab={self.nr_fab}, " f"norma_rbh={self.norma_rbh}, data_dostawy={self.data_dostawy}, " f"uzytkownik={self.uzytkownik}, assigned_technician={technician_str})") @planning_solution @dataclass class DeviceSchedule: id: str technician_list: Annotated[List[Technician], ProblemFactCollectionProperty, ValueRangeProvider] device_list: Annotated[List[Device], PlanningEntityCollectionProperty] score: Annotated[HardSoftScore, PlanningScore] = field(default=None) def __str__(self): return ( f"DeviceSchedule(" f"id={self.id},n" f"technician_list={self.technician_list},n" f"device_list={self.device_list},n" f"score={self.score}" f")" ) </code>
@dataclass
class Technician:
    id: Annotated[int, PlanningId]
    pesel: str
    name: str
    rbh_per_week: int
    rbh_per_year: float
    rbh_week_plan: float
    selected_rbh: float
    free_rbh: float
    iums: Set[str]

    def has_ium(self, ium: str) -> bool:
        return ium in this.iums

    def __str__(self) -> str:
        return (f"Technician(id={self.id}, name={self.name}, pesel={self.pesel}, "
                f"rbh_per_week={self.rbh_per_week}, rbh_per_year={self.rbh_per_year}, "
                f"rbh_week_plan={self.rbh_week_plan}, selected_rbh={self.selected_rbh}, "
                f"free_rbh={self.free_rbh}, iums={self.iums})")

@planning_entity
@dataclass
class Device:
    index: Annotated[int, PlanningId]
    ind_rek: str
    ium: str
    nazwa: str
    typ: str
    nr_fab: str
    norma_rbh: float
    data_dostawy: str
    uzytkownik: str
    pesel: Annotated[Technician | None, PlanningVariable(value_range_provider_refs=['technicianRange'], nullable=True)] = field(default=None)

    def __str__(self) -> str:
        technician_str = str(self.pesel) if self.pesel else "None"
        return (f"Device(index={self.index}, ind_rek={self.ind_rek}, ium={self.ium}, "
                f"nazwa={self.nazwa}, typ={self.typ}, nr_fab={self.nr_fab}, "
                f"norma_rbh={self.norma_rbh}, data_dostawy={self.data_dostawy}, "
                f"uzytkownik={self.uzytkownik}, assigned_technician={technician_str})")

@planning_solution
@dataclass
class DeviceSchedule:
    id: str
    technician_list: Annotated[List[Technician], ProblemFactCollectionProperty, ValueRangeProvider]
    device_list: Annotated[List[Device], PlanningEntityCollectionProperty]
    score: Annotated[HardSoftScore, PlanningScore] = field(default=None)

    def __str__(self):
        return (
            f"DeviceSchedule("
            f"id={self.id},n"
            f"technician_list={self.technician_list},n"
            f"device_list={self.device_list},n"
            f"score={self.score}"
            f")"
        )

My constraints are:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@constraint_provider
def define_constraints(constraint_factory: ConstraintFactory):
return [
# HARD constraint to avoid assigning a technician without the required skill
technician_skill_conflict(constraint_factory),
]
def technician_skill_conflict(constraint_factory: ConstraintFactory):
return (constraint_factory
.for_each(Device)
.filter(lambda device: device.pesel is not None and not device.pesel.has_ium(device.ium))
.penalize(HardSoftScore.ONE_HARD)
.as_constraint("Technician skill conflict"))
</code>
<code>@constraint_provider def define_constraints(constraint_factory: ConstraintFactory): return [ # HARD constraint to avoid assigning a technician without the required skill technician_skill_conflict(constraint_factory), ] def technician_skill_conflict(constraint_factory: ConstraintFactory): return (constraint_factory .for_each(Device) .filter(lambda device: device.pesel is not None and not device.pesel.has_ium(device.ium)) .penalize(HardSoftScore.ONE_HARD) .as_constraint("Technician skill conflict")) </code>
@constraint_provider
def define_constraints(constraint_factory: ConstraintFactory):
    return [
        # HARD constraint to avoid assigning a technician without the required skill
        technician_skill_conflict(constraint_factory),
    ]

def technician_skill_conflict(constraint_factory: ConstraintFactory):
    return (constraint_factory
            .for_each(Device)
            .filter(lambda device: device.pesel is not None and not device.pesel.has_ium(device.ium))
            .penalize(HardSoftScore.ONE_HARD)
            .as_constraint("Technician skill conflict"))

The Problem:
Even though I have implemented the nullable=True option in the PlanningVariable, and I have defined a hard constraint that penalizes assigning a technician without the required skill, the solver still tends to assign a technician to every device, even when it would be better to leave the device unassigned.

My Question:
How should I structure my classes and constraints to ensure that the solver leaves the device.pesel as None when no suitable technician is available, without incurring hard penalties for leaving the device unassigned?

I will be appreciated for any insights or suggestions how to handle this situation.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật