Add negative shifts in flex map logic and make it dynamic

This commit is contained in:
2026-03-14 20:52:59 +02:00
parent d7fd717c95
commit 770b0e84c9
4 changed files with 24 additions and 3 deletions

View File

@@ -138,10 +138,13 @@ impl UserConfig {
supply
}
pub fn flexibility_map(&self) -> HashMap<ResidentId, u8> {
pub fn flexibility_map(&self, from: Day) -> HashMap<ResidentId, u16> {
let mut map = HashMap::new();
for r in &self.residents {
map.insert(r.id, r.allowed_types.len() as u8);
let count = (from.0..=self.total_days)
.map(|d| r.available_slots_on(Day(d)))
.sum();
map.insert(r.id, count);
}
map
}

View File

@@ -64,6 +64,16 @@ impl Resident {
self
}
pub fn available_slots_on(&self, day: Day) -> u16 {
if self.negative_shifts.contains(&day) {
return 0;
}
day.shift_types()
.iter()
.filter(|t| self.allowed_types.contains(t))
.count() as u16
}
pub fn with_negative_shifts(mut self, negative_shifts: Vec<Day>) -> Self {
self.negative_shifts = negative_shifts;
self

View File

@@ -200,7 +200,7 @@ impl Scheduler {
tracker: &WorkloadTracker,
slot: Slot,
) {
let flex_map = self.config.flexibility_map();
let flex_map = self.config.flexibility_map(slot.day);
let mut rng = SmallRng::from_rng(&mut rand::rng());
resident_ids.shuffle(&mut rng);
resident_ids.sort_by_key(|r_id| {

View File

@@ -136,6 +136,14 @@ impl Day {
!self.0.is_multiple_of(2)
}
pub fn shift_types(&self) -> &[ShiftType] {
if self.is_open_shift() {
&[ShiftType::OpenFirst, ShiftType::OpenSecond]
} else {
&[ShiftType::Closed]
}
}
pub fn next(&self) -> Self {
Self(self.0 + 1)
}