File size: 12,318 Bytes
195a426 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
//! Demo data generators for Employee Scheduling.
use chrono::{Datelike, Duration, NaiveDate, NaiveDateTime, NaiveTime, Weekday};
use rand::prelude::*;
use rand::rngs::StdRng;
use rand::SeedableRng;
use crate::domain::{Employee, EmployeeSchedule, Shift};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DemoData {
Small,
Large,
}
impl std::str::FromStr for DemoData {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_uppercase().as_str() {
"SMALL" => Ok(DemoData::Small),
"LARGE" => Ok(DemoData::Large),
_ => Err(()),
}
}
}
impl DemoData {
pub fn as_str(&self) -> &'static str {
match self {
DemoData::Small => "SMALL",
DemoData::Large => "LARGE",
}
}
fn parameters(&self) -> DemoDataParameters {
match self {
DemoData::Small => DemoDataParameters {
locations: vec![
"Ambulatory care".to_string(),
"Critical care".to_string(),
"Pediatric care".to_string(),
],
required_skills: vec!["Doctor".to_string(), "Nurse".to_string()],
optional_skills: vec!["Anaesthetics".to_string(), "Cardiology".to_string()],
days_in_schedule: 14,
employee_count: 15,
optional_skill_distribution: vec![(1, 3.0), (2, 1.0)],
shift_count_distribution: vec![(1, 0.9), (2, 0.1)],
availability_count_distribution: vec![(1, 4.0), (2, 3.0), (3, 2.0), (4, 1.0)],
},
DemoData::Large => DemoDataParameters {
locations: vec![
"Ambulatory care".to_string(),
"Neurology".to_string(),
"Critical care".to_string(),
"Pediatric care".to_string(),
"Surgery".to_string(),
"Radiology".to_string(),
"Outpatient".to_string(),
],
required_skills: vec!["Doctor".to_string(), "Nurse".to_string()],
optional_skills: vec![
"Anaesthetics".to_string(),
"Cardiology".to_string(),
"Radiology".to_string(),
],
days_in_schedule: 28,
employee_count: 50,
optional_skill_distribution: vec![(1, 3.0), (2, 1.0)],
shift_count_distribution: vec![(1, 0.5), (2, 0.3), (3, 0.2)],
availability_count_distribution: vec![(5, 4.0), (10, 3.0), (15, 2.0), (20, 1.0)],
},
}
}
}
struct DemoDataParameters {
locations: Vec<String>,
required_skills: Vec<String>,
optional_skills: Vec<String>,
days_in_schedule: i64,
employee_count: usize,
optional_skill_distribution: Vec<(usize, f64)>,
shift_count_distribution: Vec<(usize, f64)>,
availability_count_distribution: Vec<(usize, f64)>,
}
/// List of available demo data sets.
pub fn list_demo_data() -> Vec<&'static str> {
vec!["SMALL", "LARGE"]
}
/// Generates a demo schedule for the given size.
pub fn generate(demo: DemoData) -> EmployeeSchedule {
let params = demo.parameters();
let mut rng = StdRng::seed_from_u64(0);
// First Monday from a reference date
let start_date = find_next_monday(NaiveDate::from_ymd_opt(2024, 1, 1).unwrap());
// Build location -> shift start times map (cycling through templates)
let shift_start_times_combos: Vec<Vec<NaiveTime>> = vec![
vec![time(6, 0), time(14, 0)],
vec![time(6, 0), time(14, 0), time(22, 0)],
vec![time(6, 0), time(9, 0), time(14, 0), time(22, 0)],
];
let location_to_shift_times: Vec<(&String, &Vec<NaiveTime>)> = params
.locations
.iter()
.enumerate()
.map(|(i, loc)| {
(
loc,
&shift_start_times_combos[i % shift_start_times_combos.len()],
)
})
.collect();
// Generate employee names (FIRST × LAST)
let name_permutations = generate_name_permutations(&mut rng);
// Generate employees
let mut employees = Vec::new();
for i in 0..params.employee_count {
let name = name_permutations[i % name_permutations.len()].clone();
// Pick optional skills based on distribution
let optional_count = pick_count(&mut rng, ¶ms.optional_skill_distribution);
let mut skills: Vec<String> = params
.optional_skills
.choose_multiple(&mut rng, optional_count.min(params.optional_skills.len()))
.cloned()
.collect();
// Add one required skill
if let Some(required) = params.required_skills.choose(&mut rng) {
skills.push(required.clone());
}
employees.push(Employee::new(i, &name).with_skills(skills));
}
// Generate shifts and assign availabilities
let mut shifts = Vec::new();
let mut shift_id = 0usize;
for day in 0..params.days_in_schedule {
let date = start_date + Duration::days(day);
// Pick employees to have availability entries on this day
let availability_count = pick_count(&mut rng, ¶ms.availability_count_distribution);
let employees_with_availability: Vec<usize> = (0..params.employee_count)
.collect::<Vec<_>>()
.choose_multiple(&mut rng, availability_count.min(params.employee_count))
.copied()
.collect();
for emp_idx in employees_with_availability {
match rng.gen_range(0..3) {
0 => {
employees[emp_idx].unavailable_dates.insert(date);
}
1 => {
employees[emp_idx].undesired_dates.insert(date);
}
2 => {
employees[emp_idx].desired_dates.insert(date);
}
_ => {}
}
}
// Generate shifts for each location/timeslot
for (location, shift_times) in &location_to_shift_times {
for &shift_start in *shift_times {
let start = NaiveDateTime::new(date, shift_start);
let end = start + Duration::hours(8);
// How many shifts at this timeslot?
let shift_count = pick_count(&mut rng, ¶ms.shift_count_distribution);
for _ in 0..shift_count {
// Pick required skill (50% required, 50% optional)
let required_skill = if rng.gen_bool(0.5) {
params.required_skills.choose(&mut rng)
} else {
params.optional_skills.choose(&mut rng)
}
.cloned()
.unwrap_or_else(|| "Doctor".to_string());
shifts.push(Shift::new(
shift_id.to_string(),
start,
end,
(*location).clone(),
required_skill,
));
shift_id += 1;
}
}
}
}
// Finalize employees to populate derived Vec fields
for emp in &mut employees {
emp.finalize();
}
EmployeeSchedule::new(employees, shifts)
}
fn time(hour: u32, minute: u32) -> NaiveTime {
NaiveTime::from_hms_opt(hour, minute, 0).unwrap()
}
fn find_next_monday(date: NaiveDate) -> NaiveDate {
let days_until_monday = match date.weekday() {
Weekday::Mon => 0,
Weekday::Tue => 6,
Weekday::Wed => 5,
Weekday::Thu => 4,
Weekday::Fri => 3,
Weekday::Sat => 2,
Weekday::Sun => 1,
};
date + Duration::days(days_until_monday)
}
/// Pick a count based on weighted distribution.
fn pick_count(rng: &mut StdRng, distribution: &[(usize, f64)]) -> usize {
let total_weight: f64 = distribution.iter().map(|(_, w)| w).sum();
let mut choice = rng.gen::<f64>() * total_weight;
for (count, weight) in distribution {
if choice < *weight {
return *count;
}
choice -= weight;
}
distribution.last().map(|(c, _)| *c).unwrap_or(1)
}
const FIRST_NAMES: &[&str] = &[
"Amy", "Beth", "Carl", "Dan", "Elsa", "Flo", "Gus", "Hugo", "Ivy", "Jay",
];
const LAST_NAMES: &[&str] = &[
"Cole", "Fox", "Green", "Jones", "King", "Li", "Poe", "Rye", "Smith", "Watt",
];
fn generate_name_permutations(rng: &mut StdRng) -> Vec<String> {
let mut names = Vec::with_capacity(FIRST_NAMES.len() * LAST_NAMES.len());
for first in FIRST_NAMES {
for last in LAST_NAMES {
names.push(format!("{} {}", first, last));
}
}
names.shuffle(rng);
names
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_generate_small() {
let schedule = generate(DemoData::Small);
assert_eq!(schedule.employees.len(), 15);
// 14 days × 3 locations × varying timeslots × varying shifts per timeslot
// Should be roughly 14 * 3 * avg(2,3,4) * avg(1,2) ≈ 14 * 3 * 3 * 1.1 ≈ 139
assert!(
schedule.shifts.len() >= 100,
"Expected >= 100 shifts, got {}",
schedule.shifts.len()
);
// All shifts should be unassigned initially
assert!(schedule.shifts.iter().all(|s| s.employee_idx.is_none()));
}
#[test]
fn test_generate_large() {
let schedule = generate(DemoData::Large);
assert_eq!(schedule.employees.len(), 50);
// 28 days × 7 locations × varying timeslots × varying shifts per timeslot
assert!(
schedule.shifts.len() >= 500,
"Expected >= 500 shifts, got {}",
schedule.shifts.len()
);
}
#[test]
fn test_employees_have_skills() {
let schedule = generate(DemoData::Small);
for employee in &schedule.employees {
assert!(
!employee.skills.is_empty(),
"Employee {} has no skills",
employee.name
);
}
}
#[test]
fn test_demo_data_from_str() {
assert_eq!("SMALL".parse::<DemoData>(), Ok(DemoData::Small));
assert_eq!("small".parse::<DemoData>(), Ok(DemoData::Small));
assert_eq!("LARGE".parse::<DemoData>(), Ok(DemoData::Large));
assert!("invalid".parse::<DemoData>().is_err());
}
#[test]
fn test_medical_domain() {
let schedule = generate(DemoData::Small);
// Check for medical skills
let all_skills: std::collections::HashSet<_> = schedule
.employees
.iter()
.flat_map(|e| e.skills.iter())
.collect();
assert!(
all_skills.iter().any(|s| *s == "Doctor" || *s == "Nurse"),
"Should have Doctor or Nurse skills"
);
// Check for medical locations
let locations: std::collections::HashSet<_> = schedule
.shifts
.iter()
.map(|s| s.location.as_str())
.collect();
assert!(
locations.contains("Ambulatory care") || locations.contains("Critical care"),
"Should have medical locations"
);
}
#[test]
fn test_empty_schedule_has_score() {
use crate::domain::EmployeeSchedule;
use solverforge::Solvable;
use tokio::sync::mpsc::unbounded_channel;
// Empty schedule with no shifts and no employees
let schedule = EmployeeSchedule::new(vec![], vec![]);
let (sender, mut receiver) = unbounded_channel();
schedule.solve(None, sender);
// Try to receive solution - with 0 entities, solver may close channel without sending
if let Some((result, _score)) = receiver.blocking_recv() {
assert!(
result.score.is_some(),
"Empty schedule should have a score after solving, got None"
);
} else {
// If no solution was sent (channel closed), that's acceptable for 0 entities
// The solver may optimize this case by not running at all
}
}
}
|