Spaces:
Sleeping
Sleeping
File size: 14,590 Bytes
7bdbe90 | 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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | """Random scenario generator for the meeting-scheduling RL environment.
Produces solvable scenarios with controlled difficulty so agents cannot
memorise fixed answers. Difficulty is governed by a parameter dict that
controls attendee count, calendar density, and preference strictness.
Usage:
from server.scenario_generator import generate_scenario
scenario = generate_scenario("random_medium")
scenario = generate_scenario("random_easy", seed=42) # reproducible
"""
from __future__ import annotations
import random
from datetime import date, datetime, timedelta, timezone
from typing import Any, Dict, List, Optional, Tuple
from .scheduling_logic import (
find_conflicts,
is_slot_free,
within_collective_hours,
calculate_collective_hours,
)
# ββ Difficulty presets ββββββββββββββββββββββββββββββββββββββββββββββββ
DIFFICULTY_PARAMS: Dict[str, Dict[str, Any]] = {
"easy": {
"num_attendees": (2, 2), # (min, max)
"meetings_per_user": (1, 3),
"meeting_duration": (30, 30), # requested meeting duration in min
"request_priority": (3, 4), # lower number = higher priority
"existing_priority_range": (2, 5),
"pref_start_range": (8, 10),
"pref_end_range": (16, 18),
"max_meetings_day_range": (5, 8),
"avoid_btb_prob": 0.1,
"buffer_range": (0, 10),
"calendar_slot_min": 30,
"calendar_slot_max": 60,
"guarantee_free_slot": True, # easy always has a free slot
},
"medium": {
"num_attendees": (3, 4),
"meetings_per_user": (3, 5),
"meeting_duration": (30, 60),
"request_priority": (2, 3),
"existing_priority_range": (2, 5),
"pref_start_range": (9, 10),
"pref_end_range": (15, 17),
"max_meetings_day_range": (4, 6),
"avoid_btb_prob": 0.5,
"buffer_range": (10, 20),
"calendar_slot_min": 30,
"calendar_slot_max": 90,
"guarantee_free_slot": False,
},
"hard": {
"num_attendees": (4, 6),
"meetings_per_user": (4, 7),
"meeting_duration": (30, 60),
"request_priority": (1, 2),
"existing_priority_range": (2, 5),
"pref_start_range": (9, 11),
"pref_end_range": (15, 16),
"max_meetings_day_range": (3, 5),
"avoid_btb_prob": 0.7,
"buffer_range": (10, 25),
"calendar_slot_min": 30,
"calendar_slot_max": 90,
"guarantee_free_slot": False,
},
}
MEETING_SUMMARIES = [
"Standup", "Sprint planning", "Design review", "Code review",
"Client call", "1-on-1", "Team sync", "Project checkpoint",
"Lunch meeting", "Strategy session", "Architecture review",
"Product demo", "Budget meeting", "Office hours", "Workshop",
"Training session", "Coffee chat", "Retrospective",
"Performance review", "Brainstorming", "Board meeting",
"Vendor call", "Onboarding session", "Knowledge sharing",
]
# ββ Helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _rand_int(lo: int, hi: int, rng: random.Random) -> int:
return rng.randint(lo, hi)
def _rand_range(r: Tuple[int, int], rng: random.Random) -> int:
return rng.randint(r[0], r[1])
def _random_weekday(rng: random.Random) -> date:
"""Pick a random weekday within the next 30 days."""
base = date(2025, 4, 7) # fixed base so TZ stays consistent
offset = rng.randint(0, 29)
d = base + timedelta(days=offset)
# shift to nearest weekday
while d.weekday() >= 5:
d += timedelta(days=1)
return d
def _generate_calendar(
user_id: str,
target_date: date,
num_meetings: int,
params: Dict[str, Any],
rng: random.Random,
reserved_slots: Optional[List[Tuple[datetime, datetime]]] = None,
) -> List[List]:
"""Generate random non-overlapping calendar entries for one user."""
tz = timezone.utc
day_start_hour = _rand_range(params["pref_start_range"], rng)
day_end_hour = _rand_range(params["pref_end_range"], rng)
if day_end_hour <= day_start_hour + 2:
day_end_hour = day_start_hour + 6
entries: List[List] = []
occupied: List[Tuple[datetime, datetime]] = []
if reserved_slots:
occupied.extend(reserved_slots)
attempts = 0
while len(entries) < num_meetings and attempts < 80:
attempts += 1
dur = _rand_range(
(params["calendar_slot_min"], params["calendar_slot_max"]),
rng,
)
# round to 15-min
dur = max(15, (dur // 15) * 15)
hour = rng.randint(day_start_hour, max(day_start_hour, day_end_hour - 1))
minute = rng.choice([0, 15, 30, 45])
start = datetime(target_date.year, target_date.month, target_date.day,
hour, minute, 0, tzinfo=tz)
end = start + timedelta(minutes=dur)
boundary = datetime(target_date.year, target_date.month, target_date.day,
day_end_hour, 0, 0, tzinfo=tz)
if end > boundary:
continue
# check overlap with already placed meetings
overlap = False
for occ_s, occ_e in occupied:
if start < occ_e and occ_s < end:
overlap = True
break
if overlap:
continue
priority = _rand_range(params["existing_priority_range"], rng)
summary = rng.choice(MEETING_SUMMARIES)
entries.append([start.isoformat(), end.isoformat(), priority, summary])
occupied.append((start, end))
# sort by start time
entries.sort(key=lambda e: e[0])
return entries
def _generate_preferences(
user_id: str,
params: Dict[str, Any],
rng: random.Random,
) -> Dict[str, Any]:
"""Generate random but realistic preferences for one user."""
start_h = _rand_range(params["pref_start_range"], rng)
end_h = _rand_range(params["pref_end_range"], rng)
if end_h <= start_h + 4:
end_h = start_h + 6
if end_h > 18:
end_h = 18
avoid_btb = rng.random() < params["avoid_btb_prob"]
buffer = _rand_range(params["buffer_range"], rng) if avoid_btb else 0
# round buffer to 5
buffer = (buffer // 5) * 5
return {
"preferred_hours": {"start": start_h, "end": end_h},
"max_meetings_per_day": _rand_range(params["max_meetings_day_range"], rng),
"avoid_back_to_back": avoid_btb,
"buffer_minutes": buffer,
}
# ββ Solvability check ββββββββββββββββββββββββββββββββββββββββββββββββ
def _find_solvable_slot(
calendars: Dict[str, List[List]],
attendees: List[str],
duration: int,
request_priority: int,
collective_hours: Dict[str, int],
target_date: date,
allow_rescheduling: bool = True,
) -> Optional[str]:
"""Check if at least one slot exists (possibly after rescheduling).
Returns the ISO start time of a viable slot, or None.
"""
tz = timezone.utc
min_h = collective_hours["min_start_hour"]
max_h = collective_hours["max_end_hour"]
candidate = datetime(target_date.year, target_date.month, target_date.day,
min_h, 0, 0, tzinfo=tz)
end_boundary = datetime(target_date.year, target_date.month, target_date.day,
max_h, 0, 0, tzinfo=tz)
step = timedelta(minutes=15)
while candidate + timedelta(minutes=duration) <= end_boundary:
c_start = candidate.isoformat()
c_end = (candidate + timedelta(minutes=duration)).isoformat()
conflicts = find_conflicts(calendars, c_start, c_end, attendees)
if len(conflicts) == 0:
return c_start
if allow_rescheduling:
# solvable if ALL conflicts have strictly lower priority (higher number)
all_reschedulable = all(c["priority"] > request_priority for c in conflicts)
if all_reschedulable:
return c_start
candidate += step
return None
# ββ Plant a guaranteed free slot (easy mode) ββββββββββββββββββββββββββ
def _plant_free_slot(
calendars: Dict[str, List[List]],
attendees: List[str],
duration: int,
collective_hours: Dict[str, int],
target_date: date,
rng: random.Random,
) -> Optional[str]:
"""Remove conflicts from a random viable slot to guarantee a free one.
Returns the ISO start of the planted slot.
"""
tz = timezone.utc
min_h = collective_hours["min_start_hour"]
max_h = collective_hours["max_end_hour"]
# collect all possible starts
candidates = []
t = datetime(target_date.year, target_date.month, target_date.day,
min_h, 0, 0, tzinfo=tz)
end_boundary = datetime(target_date.year, target_date.month, target_date.day,
max_h, 0, 0, tzinfo=tz)
step = timedelta(minutes=15)
while t + timedelta(minutes=duration) <= end_boundary:
candidates.append(t)
t += step
rng.shuffle(candidates)
for candidate in candidates:
c_start = candidate.isoformat()
c_end = (candidate + timedelta(minutes=duration)).isoformat()
# remove any overlapping entries for all attendees
for att in attendees:
calendars[att] = [
e for e in calendars[att]
if not (candidate < datetime.fromisoformat(e[1])
and datetime.fromisoformat(e[0]) < candidate + timedelta(minutes=duration))
]
# verify it's now free
conflicts = find_conflicts(calendars, c_start, c_end, attendees)
if len(conflicts) == 0:
return c_start
return None
# ββ Main generator ββββββββββββββββββββββββββββββββββββββββββββββββββββ
def generate_scenario(
difficulty: str = "medium",
seed: Optional[int] = None,
) -> Dict[str, Any]:
"""Generate a random solvable scheduling scenario.
Args:
difficulty: One of "easy", "medium", "hard".
seed: Optional RNG seed for reproducibility.
Returns:
A scenario dict with the same structure as the static JSON files.
"""
if difficulty not in DIFFICULTY_PARAMS:
raise ValueError(f"Unknown difficulty: {difficulty}. Use easy/medium/hard.")
params = DIFFICULTY_PARAMS[difficulty]
rng = random.Random(seed)
target_date = _random_weekday(rng)
num_attendees = _rand_range(params["num_attendees"], rng)
attendees = [f"user{i+1}" for i in range(num_attendees)]
duration = _rand_range(params["meeting_duration"], rng)
# round to 15
duration = max(15, (duration // 15) * 15)
request_priority = _rand_range(params["request_priority"], rng)
# generate preferences first (needed for collective hours)
preferences: Dict[str, Dict] = {}
for att in attendees:
preferences[att] = _generate_preferences(att, params, rng)
collective_hours = calculate_collective_hours(preferences)
# safety: ensure window is wide enough for the meeting
window = collective_hours["max_end_hour"] - collective_hours["min_start_hour"]
if window * 60 < duration:
collective_hours["max_end_hour"] = collective_hours["min_start_hour"] + (duration // 60) + 2
# also widen preferences
for att in attendees:
preferences[att]["preferred_hours"]["end"] = max(
preferences[att]["preferred_hours"]["end"],
collective_hours["max_end_hour"],
)
# generate calendars
calendars: Dict[str, List[List]] = {}
for att in attendees:
n_meetings = _rand_range(params["meetings_per_user"], rng)
calendars[att] = _generate_calendar(att, target_date, n_meetings, params, rng)
# ensure solvability
if params["guarantee_free_slot"]:
_plant_free_slot(calendars, attendees, duration, collective_hours, target_date, rng)
# verify at least one solution exists (with rescheduling allowed)
max_retries = 10
for attempt in range(max_retries):
viable = _find_solvable_slot(
calendars, attendees, duration, request_priority,
collective_hours, target_date, allow_rescheduling=True,
)
if viable is not None:
break
# regenerate calendars with fewer meetings to open up space
for att in attendees:
reduced = max(1, _rand_range(params["meetings_per_user"], rng) - 1)
calendars[att] = _generate_calendar(att, target_date, reduced, params, rng)
else:
# last resort: plant a free slot
_plant_free_slot(calendars, attendees, duration, collective_hours, target_date, rng)
# find solution info for metadata
free_slot = _find_solvable_slot(
calendars, attendees, duration, request_priority,
collective_hours, target_date, allow_rescheduling=False,
)
needs_rescheduling = free_slot is None
best_slot = free_slot or _find_solvable_slot(
calendars, attendees, duration, request_priority,
collective_hours, target_date, allow_rescheduling=True,
)
task_id = f"random_{difficulty}"
description_map = {
"easy": f"Schedule a {duration}-min meeting with {num_attendees} attendees (random easy)",
"medium": f"Schedule a {duration}-min meeting with {num_attendees} attendees (random medium)",
"hard": f"Schedule a {duration}-min meeting with {num_attendees} attendees (random hard)",
}
return {
"task_id": task_id,
"description": description_map[difficulty],
"difficulty": difficulty,
"meeting_request": {
"duration": duration,
"priority": request_priority,
"attendees": attendees,
"summary": rng.choice([
"Team Sync", "Planning Session", "Design Review",
"Sprint Review", "Cross-Team Standup", "Strategy Meeting",
]),
},
"calendars": calendars,
"preferences": preferences,
"expected_solution": {
"optimal_slot": best_slot,
"requires_rescheduling": needs_rescheduling,
"generated": True,
},
}
|