Spaces:
Sleeping
Sleeping
File size: 4,472 Bytes
6257207 85ea5e3 6257207 21129f1 6257207 85ea5e3 6257207 21129f1 6257207 21129f1 6257207 85ea5e3 6257207 85ea5e3 6257207 21129f1 31993c7 85ea5e3 6257207 21129f1 85ea5e3 6257207 21129f1 85ea5e3 6257207 85ea5e3 21129f1 85ea5e3 6257207 21129f1 6257207 85ea5e3 21129f1 6257207 21129f1 85ea5e3 6257207 | 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 | """
Rule-based baseline agent for the Healthcare Scheduling environment.
Strategy:
1. Sort waiting patients by priority (1 = highest urgency).
2. Match patient's required_specialty to a compatible doctor.
3. Within matching doctors, try preferred doctor first.
4. Match time preference (morning slots 0-3, afternoon slots 4-7).
5. If no matching slots, try any compatible doctor.
6. If all else fails, waitlist the patient.
"""
from typing import Any, Dict, List
class BaselineAgent:
"""
Rule-based agent for appointment scheduling.
Handles specialty matching, priority ordering, and time preferences.
Works with both the typed HealthcareEnvironment and dict-based
observation format.
"""
def __init__(self, env=None):
self.env = env
def select_action(self, obs: Dict[str, Any]) -> Dict[str, Any]:
"""
Select the best scheduling action given the current observation.
Args:
obs: Dict with doctor_slots, doctor_specialties, patients,
waiting_queue.
Returns:
Action dict with type, patient_id, doctor_id, slot_id.
"""
waiting_queue = obs.get("waiting_queue", [])
if not waiting_queue:
return {"type": "waitlist", "patient_id": -1}
patients = obs.get("patients", {})
doctor_slots = obs.get("doctor_slots", {})
doctor_specialties = obs.get("doctor_specialties", {})
# Sort waiting patients by priority (1 is highest)
waiting_patients = [
patients[pid] for pid in waiting_queue if pid in patients
]
waiting_patients.sort(key=lambda x: x.get("priority", 3))
for patient in waiting_patients:
pid = patient["id"]
preferred_doctor = patient.get("preferred_doctor", 0)
required_specialty = patient.get("required_specialty", "General Medicine")
time_preference = patient.get("time_preference", "morning")
# ββ Find doctors with compatible specialty ββ
matching_docs = []
for did, spec in doctor_specialties.items():
if (spec == required_specialty
or spec == "General Medicine"
or required_specialty == "General Medicine"):
matching_docs.append(did)
# Fallback: all doctors
if not matching_docs:
matching_docs = list(doctor_slots.keys())
# Sort: preferred doctor first
pref_key = str(preferred_doctor)
matching_docs.sort(key=lambda d: (0 if str(d) == pref_key else 1))
# ββ Determine slot preference ββ
num_slots = (
max(len(slots) for slots in doctor_slots.values())
if doctor_slots else 8
)
half = num_slots // 2
if time_preference == "morning":
pref_slots = list(range(0, half))
other_slots = list(range(half, num_slots))
else:
pref_slots = list(range(half, num_slots))
other_slots = list(range(0, half))
for did in matching_docs:
did_key = str(did) if str(did) in doctor_slots else did
if did_key not in doctor_slots:
continue
slots = doctor_slots[did_key]
did_int = int(did)
# Try preferred time slots first
for sid in pref_slots:
if sid < len(slots) and slots[sid]:
return {
"type": "book",
"patient_id": pid,
"doctor_id": did_int,
"slot_id": sid,
}
# Then try other time slots
for sid in other_slots:
if sid < len(slots) and slots[sid]:
return {
"type": "book",
"patient_id": pid,
"doctor_id": did_int,
"slot_id": sid,
}
# No slots available anywhere, waitlist the first patient
first_pid = waiting_queue[0]
pid_int = int(first_pid) if isinstance(first_pid, str) else first_pid
return {"type": "waitlist", "patient_id": pid_int}
|