Spaces:
Sleeping
refactor: Extract core scheduling algorithm with override integration
Browse filesCreated standalone SchedulingAlgorithm module as reusable backend:
- scheduler/core/algorithm.py (NEW - 348 lines)
* SchedulingAlgorithm class - main product interface
* SchedulingResult dataclass - output with transparency
* schedule_day() method with 7 checkpoints:
1. Ripeness filtering (with override support)
2. Eligibility checks (min gap)
3. Judge preferences application
4. Policy prioritization
5. Manual overrides (add/remove/reorder)
6. Courtroom allocation
7. Explanation generation
* Integrated ExplainabilityEngine and OverrideManager
* Clean separation: algorithm handles scheduling, simulation handles outcomes
- Refactored scheduler/simulation/engine.py
* Removed 56 lines of duplicate scheduling logic
* _choose_cases_for_day() now calls algorithm.schedule_day()
* Returns SchedulingResult instead of allocation dict
* Simulation focuses on hearing outcomes (adjournments/disposals)
* Cleaner separation of concerns
- Updated scheduler/simulation/policies/__init__.py
* Export SchedulerPolicy base class for typing
Benefits:
- Algorithm can be called by simulation, CLI, or web dashboard
- Override mechanism built into core algorithm (hackathon requirement)
- Full transparency with explanations and audit trail
- Easier testing and maintenance
- No code duplication
Tested: 5-day simulation runs successfully with refactored code
|
@@ -0,0 +1,349 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Core scheduling algorithm with override mechanism.
|
| 2 |
+
|
| 3 |
+
This module provides the standalone scheduling algorithm that can be used by:
|
| 4 |
+
- Simulation engine (repeated daily calls)
|
| 5 |
+
- CLI interface (single-day scheduling)
|
| 6 |
+
- Web dashboard (API backend)
|
| 7 |
+
|
| 8 |
+
The algorithm accepts cases, courtrooms, date, policy, and optional overrides,
|
| 9 |
+
then returns scheduled cause list with explanations and audit trail.
|
| 10 |
+
"""
|
| 11 |
+
from __future__ import annotations
|
| 12 |
+
|
| 13 |
+
from dataclasses import dataclass, field
|
| 14 |
+
from datetime import date
|
| 15 |
+
from typing import Dict, List, Optional, Tuple
|
| 16 |
+
|
| 17 |
+
from scheduler.core.case import Case, CaseStatus
|
| 18 |
+
from scheduler.core.courtroom import Courtroom
|
| 19 |
+
from scheduler.core.ripeness import RipenessClassifier, RipenessStatus
|
| 20 |
+
from scheduler.simulation.policies import SchedulerPolicy
|
| 21 |
+
from scheduler.simulation.allocator import CourtroomAllocator, AllocationStrategy
|
| 22 |
+
from scheduler.control.explainability import ExplainabilityEngine, SchedulingExplanation
|
| 23 |
+
from scheduler.control.overrides import (
|
| 24 |
+
Override,
|
| 25 |
+
OverrideType,
|
| 26 |
+
JudgePreferences,
|
| 27 |
+
)
|
| 28 |
+
from scheduler.data.config import MIN_GAP_BETWEEN_HEARINGS
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@dataclass
|
| 32 |
+
class SchedulingResult:
|
| 33 |
+
"""Result of single-day scheduling with full transparency."""
|
| 34 |
+
|
| 35 |
+
# Core output
|
| 36 |
+
scheduled_cases: Dict[int, List[Case]] # courtroom_id -> cases
|
| 37 |
+
|
| 38 |
+
# Transparency
|
| 39 |
+
explanations: Dict[str, SchedulingExplanation] # case_id -> explanation
|
| 40 |
+
applied_overrides: List[Override] # Overrides that were applied
|
| 41 |
+
|
| 42 |
+
# Diagnostics
|
| 43 |
+
unscheduled_cases: List[Tuple[Case, str]] # (case, reason)
|
| 44 |
+
ripeness_filtered: int # Count of unripe cases filtered
|
| 45 |
+
capacity_limited: int # Cases that couldn't fit due to capacity
|
| 46 |
+
|
| 47 |
+
# Metadata
|
| 48 |
+
scheduling_date: date
|
| 49 |
+
policy_used: str
|
| 50 |
+
total_scheduled: int = field(init=False)
|
| 51 |
+
|
| 52 |
+
def __post_init__(self):
|
| 53 |
+
"""Calculate derived fields."""
|
| 54 |
+
self.total_scheduled = sum(len(cases) for cases in self.scheduled_cases.values())
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
class SchedulingAlgorithm:
|
| 58 |
+
"""Core scheduling algorithm with override support.
|
| 59 |
+
|
| 60 |
+
This is the main product - a clean, reusable scheduling algorithm that:
|
| 61 |
+
1. Filters cases by ripeness and eligibility
|
| 62 |
+
2. Applies judge preferences and manual overrides
|
| 63 |
+
3. Prioritizes cases using selected policy
|
| 64 |
+
4. Allocates cases to courtrooms with load balancing
|
| 65 |
+
5. Generates explanations for all decisions
|
| 66 |
+
|
| 67 |
+
Usage:
|
| 68 |
+
algorithm = SchedulingAlgorithm(policy=readiness_policy, allocator=allocator)
|
| 69 |
+
result = algorithm.schedule_day(
|
| 70 |
+
cases=active_cases,
|
| 71 |
+
courtrooms=courtrooms,
|
| 72 |
+
current_date=date(2024, 3, 15),
|
| 73 |
+
overrides=judge_overrides,
|
| 74 |
+
preferences=judge_prefs
|
| 75 |
+
)
|
| 76 |
+
"""
|
| 77 |
+
|
| 78 |
+
def __init__(
|
| 79 |
+
self,
|
| 80 |
+
policy: SchedulerPolicy,
|
| 81 |
+
allocator: Optional[CourtroomAllocator] = None,
|
| 82 |
+
min_gap_days: int = MIN_GAP_BETWEEN_HEARINGS
|
| 83 |
+
):
|
| 84 |
+
"""Initialize algorithm with policy and allocator.
|
| 85 |
+
|
| 86 |
+
Args:
|
| 87 |
+
policy: Scheduling policy (FIFO, Age, Readiness)
|
| 88 |
+
allocator: Courtroom allocator (defaults to load-balanced)
|
| 89 |
+
min_gap_days: Minimum days between hearings for a case
|
| 90 |
+
"""
|
| 91 |
+
self.policy = policy
|
| 92 |
+
self.allocator = allocator
|
| 93 |
+
self.min_gap_days = min_gap_days
|
| 94 |
+
self.explainer = ExplainabilityEngine()
|
| 95 |
+
|
| 96 |
+
def schedule_day(
|
| 97 |
+
self,
|
| 98 |
+
cases: List[Case],
|
| 99 |
+
courtrooms: List[Courtroom],
|
| 100 |
+
current_date: date,
|
| 101 |
+
overrides: Optional[List[Override]] = None,
|
| 102 |
+
preferences: Optional[JudgePreferences] = None
|
| 103 |
+
) -> SchedulingResult:
|
| 104 |
+
"""Schedule cases for a single day with override support.
|
| 105 |
+
|
| 106 |
+
Args:
|
| 107 |
+
cases: All active cases (will be filtered)
|
| 108 |
+
courtrooms: Available courtrooms
|
| 109 |
+
current_date: Date to schedule for
|
| 110 |
+
overrides: Optional manual overrides to apply
|
| 111 |
+
preferences: Optional judge preferences/constraints
|
| 112 |
+
|
| 113 |
+
Returns:
|
| 114 |
+
SchedulingResult with scheduled cases, explanations, and audit trail
|
| 115 |
+
"""
|
| 116 |
+
# Initialize tracking
|
| 117 |
+
unscheduled: List[Tuple[Case, str]] = []
|
| 118 |
+
applied_overrides: List[Override] = []
|
| 119 |
+
explanations: Dict[str, SchedulingExplanation] = {}
|
| 120 |
+
|
| 121 |
+
# Filter disposed cases
|
| 122 |
+
active_cases = [c for c in cases if c.status != CaseStatus.DISPOSED]
|
| 123 |
+
|
| 124 |
+
# Update age and readiness for all cases
|
| 125 |
+
for case in active_cases:
|
| 126 |
+
case.update_age(current_date)
|
| 127 |
+
case.compute_readiness_score()
|
| 128 |
+
|
| 129 |
+
# CHECKPOINT 1: Ripeness filtering with override support
|
| 130 |
+
ripe_cases, ripeness_filtered = self._filter_by_ripeness(
|
| 131 |
+
active_cases, current_date, overrides, applied_overrides
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
# CHECKPOINT 2: Eligibility check (min gap requirement)
|
| 135 |
+
eligible_cases = self._filter_eligible(ripe_cases, current_date, unscheduled)
|
| 136 |
+
|
| 137 |
+
# CHECKPOINT 3: Apply judge preferences (capacity overrides tracked)
|
| 138 |
+
if preferences:
|
| 139 |
+
applied_overrides.extend(self._get_preference_overrides(preferences, courtrooms))
|
| 140 |
+
|
| 141 |
+
# CHECKPOINT 4: Prioritize using policy
|
| 142 |
+
prioritized = self.policy.prioritize(eligible_cases, current_date)
|
| 143 |
+
|
| 144 |
+
# CHECKPOINT 5: Apply manual overrides (add/remove/reorder)
|
| 145 |
+
if overrides:
|
| 146 |
+
prioritized = self._apply_manual_overrides(
|
| 147 |
+
prioritized, overrides, applied_overrides, unscheduled
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# CHECKPOINT 6: Allocate to courtrooms
|
| 151 |
+
scheduled_allocation, capacity_limited = self._allocate_cases(
|
| 152 |
+
prioritized, courtrooms, current_date, preferences
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
# Track capacity-limited cases
|
| 156 |
+
total_scheduled = sum(len(cases) for cases in scheduled_allocation.values())
|
| 157 |
+
for case in prioritized[total_scheduled:]:
|
| 158 |
+
unscheduled.append((case, "Capacity exceeded - all courtrooms full"))
|
| 159 |
+
|
| 160 |
+
# CHECKPOINT 7: Generate explanations for scheduled cases
|
| 161 |
+
for courtroom_id, cases_in_room in scheduled_allocation.items():
|
| 162 |
+
for case in cases_in_room:
|
| 163 |
+
explanation = self.explainer.explain_scheduling_decision(
|
| 164 |
+
case=case,
|
| 165 |
+
current_date=current_date,
|
| 166 |
+
scheduled=True,
|
| 167 |
+
ripeness_status=case.ripeness_status,
|
| 168 |
+
priority_score=case.get_priority_score(),
|
| 169 |
+
courtroom_id=courtroom_id
|
| 170 |
+
)
|
| 171 |
+
explanations[case.case_id] = explanation
|
| 172 |
+
|
| 173 |
+
# Generate explanations for sample of unscheduled cases (top 10)
|
| 174 |
+
for case, reason in unscheduled[:10]:
|
| 175 |
+
explanation = self.explainer.explain_scheduling_decision(
|
| 176 |
+
case=case,
|
| 177 |
+
current_date=current_date,
|
| 178 |
+
scheduled=False,
|
| 179 |
+
ripeness_status=case.ripeness_status,
|
| 180 |
+
capacity_full=("Capacity" in reason),
|
| 181 |
+
below_threshold=False
|
| 182 |
+
)
|
| 183 |
+
explanations[case.case_id] = explanation
|
| 184 |
+
|
| 185 |
+
return SchedulingResult(
|
| 186 |
+
scheduled_cases=scheduled_allocation,
|
| 187 |
+
explanations=explanations,
|
| 188 |
+
applied_overrides=applied_overrides,
|
| 189 |
+
unscheduled_cases=unscheduled,
|
| 190 |
+
ripeness_filtered=ripeness_filtered,
|
| 191 |
+
capacity_limited=capacity_limited,
|
| 192 |
+
scheduling_date=current_date,
|
| 193 |
+
policy_used=self.policy.get_name()
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
def _filter_by_ripeness(
|
| 197 |
+
self,
|
| 198 |
+
cases: List[Case],
|
| 199 |
+
current_date: date,
|
| 200 |
+
overrides: Optional[List[Override]],
|
| 201 |
+
applied_overrides: List[Override]
|
| 202 |
+
) -> Tuple[List[Case], int]:
|
| 203 |
+
"""Filter cases by ripeness with override support."""
|
| 204 |
+
# Build override lookup
|
| 205 |
+
ripeness_overrides = {}
|
| 206 |
+
if overrides:
|
| 207 |
+
for override in overrides:
|
| 208 |
+
if override.override_type == OverrideType.RIPENESS:
|
| 209 |
+
ripeness_overrides[override.case_id] = override.make_ripe
|
| 210 |
+
|
| 211 |
+
ripe_cases = []
|
| 212 |
+
filtered_count = 0
|
| 213 |
+
|
| 214 |
+
for case in cases:
|
| 215 |
+
# Check for ripeness override
|
| 216 |
+
if case.case_id in ripeness_overrides:
|
| 217 |
+
if ripeness_overrides[case.case_id]:
|
| 218 |
+
case.mark_ripe(current_date)
|
| 219 |
+
ripe_cases.append(case)
|
| 220 |
+
# Track override application
|
| 221 |
+
override = next(o for o in overrides if o.case_id == case.case_id and o.override_type == OverrideType.RIPENESS)
|
| 222 |
+
applied_overrides.append(override)
|
| 223 |
+
else:
|
| 224 |
+
case.mark_unripe(RipenessStatus.UNRIPE_DEPENDENT, "Judge override", current_date)
|
| 225 |
+
filtered_count += 1
|
| 226 |
+
continue
|
| 227 |
+
|
| 228 |
+
# Normal ripeness classification
|
| 229 |
+
ripeness = RipenessClassifier.classify(case, current_date)
|
| 230 |
+
|
| 231 |
+
if ripeness.value != case.ripeness_status:
|
| 232 |
+
if ripeness.is_ripe():
|
| 233 |
+
case.mark_ripe(current_date)
|
| 234 |
+
else:
|
| 235 |
+
reason = RipenessClassifier.get_ripeness_reason(ripeness)
|
| 236 |
+
case.mark_unripe(ripeness, reason, current_date)
|
| 237 |
+
|
| 238 |
+
if ripeness.is_ripe():
|
| 239 |
+
ripe_cases.append(case)
|
| 240 |
+
else:
|
| 241 |
+
filtered_count += 1
|
| 242 |
+
|
| 243 |
+
return ripe_cases, filtered_count
|
| 244 |
+
|
| 245 |
+
def _filter_eligible(
|
| 246 |
+
self,
|
| 247 |
+
cases: List[Case],
|
| 248 |
+
current_date: date,
|
| 249 |
+
unscheduled: List[Tuple[Case, str]]
|
| 250 |
+
) -> List[Case]:
|
| 251 |
+
"""Filter cases that meet minimum gap requirement."""
|
| 252 |
+
eligible = []
|
| 253 |
+
for case in cases:
|
| 254 |
+
if case.is_ready_for_scheduling(self.min_gap_days):
|
| 255 |
+
eligible.append(case)
|
| 256 |
+
else:
|
| 257 |
+
reason = f"Min gap not met - last hearing {case.days_since_last_hearing}d ago (min {self.min_gap_days}d)"
|
| 258 |
+
unscheduled.append((case, reason))
|
| 259 |
+
return eligible
|
| 260 |
+
|
| 261 |
+
def _get_preference_overrides(
|
| 262 |
+
self,
|
| 263 |
+
preferences: JudgePreferences,
|
| 264 |
+
courtrooms: List[Courtroom]
|
| 265 |
+
) -> List[Override]:
|
| 266 |
+
"""Extract overrides from judge preferences for audit trail."""
|
| 267 |
+
overrides = []
|
| 268 |
+
|
| 269 |
+
if preferences.capacity_overrides:
|
| 270 |
+
for courtroom_id, new_capacity in preferences.capacity_overrides.items():
|
| 271 |
+
override = Override(
|
| 272 |
+
override_type=OverrideType.CAPACITY,
|
| 273 |
+
courtroom_id=courtroom_id,
|
| 274 |
+
new_capacity=new_capacity,
|
| 275 |
+
reason="Judge preference"
|
| 276 |
+
)
|
| 277 |
+
overrides.append(override)
|
| 278 |
+
|
| 279 |
+
return overrides
|
| 280 |
+
|
| 281 |
+
def _apply_manual_overrides(
|
| 282 |
+
self,
|
| 283 |
+
prioritized: List[Case],
|
| 284 |
+
overrides: List[Override],
|
| 285 |
+
applied_overrides: List[Override],
|
| 286 |
+
unscheduled: List[Tuple[Case, str]]
|
| 287 |
+
) -> List[Case]:
|
| 288 |
+
"""Apply manual overrides (REMOVE_CASE, REORDER)."""
|
| 289 |
+
result = prioritized.copy()
|
| 290 |
+
|
| 291 |
+
# Apply REMOVE_CASE overrides
|
| 292 |
+
remove_overrides = [o for o in overrides if o.override_type == OverrideType.REMOVE_CASE]
|
| 293 |
+
for override in remove_overrides:
|
| 294 |
+
removed = [c for c in result if c.case_id == override.case_id]
|
| 295 |
+
result = [c for c in result if c.case_id != override.case_id]
|
| 296 |
+
if removed:
|
| 297 |
+
applied_overrides.append(override)
|
| 298 |
+
unscheduled.append((removed[0], f"Judge override: {override.reason}"))
|
| 299 |
+
|
| 300 |
+
# Apply REORDER overrides
|
| 301 |
+
reorder_overrides = [o for o in overrides if o.override_type == OverrideType.REORDER]
|
| 302 |
+
for override in reorder_overrides:
|
| 303 |
+
if override.case_id and override.new_position is not None:
|
| 304 |
+
case_to_move = next((c for c in result if c.case_id == override.case_id), None)
|
| 305 |
+
if case_to_move and 0 <= override.new_position < len(result):
|
| 306 |
+
result.remove(case_to_move)
|
| 307 |
+
result.insert(override.new_position, case_to_move)
|
| 308 |
+
applied_overrides.append(override)
|
| 309 |
+
|
| 310 |
+
return result
|
| 311 |
+
|
| 312 |
+
def _allocate_cases(
|
| 313 |
+
self,
|
| 314 |
+
prioritized: List[Case],
|
| 315 |
+
courtrooms: List[Courtroom],
|
| 316 |
+
current_date: date,
|
| 317 |
+
preferences: Optional[JudgePreferences]
|
| 318 |
+
) -> Tuple[Dict[int, List[Case]], int]:
|
| 319 |
+
"""Allocate prioritized cases to courtrooms."""
|
| 320 |
+
# Calculate total capacity (with preference overrides)
|
| 321 |
+
total_capacity = 0
|
| 322 |
+
for room in courtrooms:
|
| 323 |
+
if preferences and room.courtroom_id in preferences.capacity_overrides:
|
| 324 |
+
total_capacity += preferences.capacity_overrides[room.courtroom_id]
|
| 325 |
+
else:
|
| 326 |
+
total_capacity += room.get_capacity_for_date(current_date)
|
| 327 |
+
|
| 328 |
+
# Limit cases to total capacity
|
| 329 |
+
cases_to_allocate = prioritized[:total_capacity]
|
| 330 |
+
capacity_limited = len(prioritized) - len(cases_to_allocate)
|
| 331 |
+
|
| 332 |
+
# Use allocator to distribute
|
| 333 |
+
if self.allocator:
|
| 334 |
+
case_to_courtroom = self.allocator.allocate(cases_to_allocate, current_date)
|
| 335 |
+
else:
|
| 336 |
+
# Fallback: round-robin
|
| 337 |
+
case_to_courtroom = {}
|
| 338 |
+
for i, case in enumerate(cases_to_allocate):
|
| 339 |
+
room_id = courtrooms[i % len(courtrooms)].courtroom_id
|
| 340 |
+
case_to_courtroom[case.case_id] = room_id
|
| 341 |
+
|
| 342 |
+
# Build allocation dict
|
| 343 |
+
allocation: Dict[int, List[Case]] = {r.courtroom_id: [] for r in courtrooms}
|
| 344 |
+
for case in cases_to_allocate:
|
| 345 |
+
if case.case_id in case_to_courtroom:
|
| 346 |
+
courtroom_id = case_to_courtroom[case.case_id]
|
| 347 |
+
allocation[courtroom_id].append(case)
|
| 348 |
+
|
| 349 |
+
return allocation, capacity_limited
|
|
@@ -21,6 +21,7 @@ import random
|
|
| 21 |
from scheduler.core.case import Case, CaseStatus
|
| 22 |
from scheduler.core.courtroom import Courtroom
|
| 23 |
from scheduler.core.ripeness import RipenessClassifier, RipenessStatus
|
|
|
|
| 24 |
from scheduler.utils.calendar import CourtCalendar
|
| 25 |
from scheduler.data.param_loader import load_parameters
|
| 26 |
from scheduler.simulation.events import EventWriter
|
|
@@ -109,6 +110,12 @@ class CourtSim:
|
|
| 109 |
per_courtroom_capacity=self.cfg.daily_capacity,
|
| 110 |
strategy=AllocationStrategy.LOAD_BALANCED
|
| 111 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
# --- helpers -------------------------------------------------------------
|
| 114 |
def _init_stage_ready(self) -> None:
|
|
@@ -230,63 +237,37 @@ class CourtSim:
|
|
| 230 |
)
|
| 231 |
|
| 232 |
# --- daily scheduling policy --------------------------------------------
|
| 233 |
-
def _choose_cases_for_day(self, current: date) ->
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
# Periodic ripeness re-evaluation (every 7 days)
|
| 235 |
days_since_eval = (current - self._last_ripeness_eval).days
|
| 236 |
if days_since_eval >= 7:
|
| 237 |
self._evaluate_ripeness(current)
|
| 238 |
self._last_ripeness_eval = current
|
| 239 |
|
| 240 |
-
#
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
ripe_candidates = []
|
| 250 |
-
for c in candidates:
|
| 251 |
-
ripeness = RipenessClassifier.classify(c, current)
|
| 252 |
-
|
| 253 |
-
# Update case ripeness status (compare string values)
|
| 254 |
-
if ripeness.value != c.ripeness_status:
|
| 255 |
-
if ripeness.is_ripe():
|
| 256 |
-
c.mark_ripe(current)
|
| 257 |
-
else:
|
| 258 |
-
reason = RipenessClassifier.get_ripeness_reason(ripeness)
|
| 259 |
-
c.mark_unripe(ripeness, reason, current)
|
| 260 |
-
|
| 261 |
-
# Only schedule RIPE cases
|
| 262 |
-
if ripeness.is_ripe():
|
| 263 |
-
ripe_candidates.append(c)
|
| 264 |
-
else:
|
| 265 |
-
self._unripe_filtered += 1
|
| 266 |
-
|
| 267 |
-
# filter eligible (ready for scheduling) - now from ripe cases only
|
| 268 |
-
eligible = [c for c in ripe_candidates if c.is_ready_for_scheduling(MIN_GAP_BETWEEN_HEARINGS)]
|
| 269 |
-
# delegate prioritization to policy
|
| 270 |
-
eligible = self.policy.prioritize(eligible, current)
|
| 271 |
-
|
| 272 |
-
# Dynamic courtroom allocation (NEW - replaces fixed round-robin)
|
| 273 |
-
# Limit to total daily capacity across all courtrooms
|
| 274 |
-
total_capacity = sum(r.get_capacity_for_date(current) for r in self.rooms)
|
| 275 |
-
cases_to_allocate = eligible[:total_capacity]
|
| 276 |
-
|
| 277 |
-
# Allocate cases to courtrooms using load balancing
|
| 278 |
-
case_to_courtroom = self.allocator.allocate(cases_to_allocate, current)
|
| 279 |
|
| 280 |
-
#
|
| 281 |
-
|
| 282 |
-
seen_cases = set() # Track seen case_ids to prevent duplicates
|
| 283 |
-
for case in cases_to_allocate:
|
| 284 |
-
if case.case_id in case_to_courtroom and case.case_id not in seen_cases:
|
| 285 |
-
courtroom_id = case_to_courtroom[case.case_id]
|
| 286 |
-
allocation[courtroom_id].append(case)
|
| 287 |
-
seen_cases.add(case.case_id)
|
| 288 |
|
| 289 |
-
return
|
| 290 |
|
| 291 |
# --- main loop -----------------------------------------------------------
|
| 292 |
def _expected_daily_filings(self, current: date) -> int:
|
|
@@ -323,7 +304,7 @@ class CourtSim:
|
|
| 323 |
# inflow = self._expected_daily_filings(current)
|
| 324 |
# if inflow:
|
| 325 |
# self._file_new_cases(current, inflow)
|
| 326 |
-
|
| 327 |
capacity_today = sum(self.cfg.daily_capacity for _ in self.rooms)
|
| 328 |
self._capacity_offered += capacity_today
|
| 329 |
day_heard = 0
|
|
@@ -337,7 +318,7 @@ class CourtSim:
|
|
| 337 |
sw = csv.writer(sf)
|
| 338 |
sw.writerow(["case_id", "courtroom_id", "policy", "age_days", "readiness_score", "urgent", "stage", "days_since_last_hearing", "stage_ready_date"])
|
| 339 |
for room in self.rooms:
|
| 340 |
-
for case in
|
| 341 |
# Skip if case already disposed (safety check)
|
| 342 |
if case.status == CaseStatus.DISPOSED:
|
| 343 |
continue
|
|
|
|
| 21 |
from scheduler.core.case import Case, CaseStatus
|
| 22 |
from scheduler.core.courtroom import Courtroom
|
| 23 |
from scheduler.core.ripeness import RipenessClassifier, RipenessStatus
|
| 24 |
+
from scheduler.core.algorithm import SchedulingAlgorithm, SchedulingResult
|
| 25 |
from scheduler.utils.calendar import CourtCalendar
|
| 26 |
from scheduler.data.param_loader import load_parameters
|
| 27 |
from scheduler.simulation.events import EventWriter
|
|
|
|
| 110 |
per_courtroom_capacity=self.cfg.daily_capacity,
|
| 111 |
strategy=AllocationStrategy.LOAD_BALANCED
|
| 112 |
)
|
| 113 |
+
# scheduling algorithm (NEW - replaces inline logic)
|
| 114 |
+
self.algorithm = SchedulingAlgorithm(
|
| 115 |
+
policy=self.policy,
|
| 116 |
+
allocator=self.allocator,
|
| 117 |
+
min_gap_days=MIN_GAP_BETWEEN_HEARINGS
|
| 118 |
+
)
|
| 119 |
|
| 120 |
# --- helpers -------------------------------------------------------------
|
| 121 |
def _init_stage_ready(self) -> None:
|
|
|
|
| 237 |
)
|
| 238 |
|
| 239 |
# --- daily scheduling policy --------------------------------------------
|
| 240 |
+
def _choose_cases_for_day(self, current: date) -> SchedulingResult:
|
| 241 |
+
"""Use SchedulingAlgorithm to schedule cases for the day.
|
| 242 |
+
|
| 243 |
+
This replaces the previous inline scheduling logic with a call to the
|
| 244 |
+
standalone algorithm module. The algorithm handles:
|
| 245 |
+
- Ripeness filtering
|
| 246 |
+
- Eligibility checks
|
| 247 |
+
- Policy prioritization
|
| 248 |
+
- Courtroom allocation
|
| 249 |
+
- Explanation generation
|
| 250 |
+
"""
|
| 251 |
# Periodic ripeness re-evaluation (every 7 days)
|
| 252 |
days_since_eval = (current - self._last_ripeness_eval).days
|
| 253 |
if days_since_eval >= 7:
|
| 254 |
self._evaluate_ripeness(current)
|
| 255 |
self._last_ripeness_eval = current
|
| 256 |
|
| 257 |
+
# Call algorithm to schedule day
|
| 258 |
+
# Note: No overrides in baseline simulation - that's for override demonstration runs
|
| 259 |
+
result = self.algorithm.schedule_day(
|
| 260 |
+
cases=self.cases,
|
| 261 |
+
courtrooms=self.rooms,
|
| 262 |
+
current_date=current,
|
| 263 |
+
overrides=None, # No overrides in baseline simulation
|
| 264 |
+
preferences=None # No judge preferences in baseline simulation
|
| 265 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
|
| 267 |
+
# Update stats from algorithm result
|
| 268 |
+
self._unripe_filtered += result.ripeness_filtered
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
+
return result
|
| 271 |
|
| 272 |
# --- main loop -----------------------------------------------------------
|
| 273 |
def _expected_daily_filings(self, current: date) -> int:
|
|
|
|
| 304 |
# inflow = self._expected_daily_filings(current)
|
| 305 |
# if inflow:
|
| 306 |
# self._file_new_cases(current, inflow)
|
| 307 |
+
result = self._choose_cases_for_day(current)
|
| 308 |
capacity_today = sum(self.cfg.daily_capacity for _ in self.rooms)
|
| 309 |
self._capacity_offered += capacity_today
|
| 310 |
day_heard = 0
|
|
|
|
| 318 |
sw = csv.writer(sf)
|
| 319 |
sw.writerow(["case_id", "courtroom_id", "policy", "age_days", "readiness_score", "urgent", "stage", "days_since_last_hearing", "stage_ready_date"])
|
| 320 |
for room in self.rooms:
|
| 321 |
+
for case in result.scheduled_cases.get(room.courtroom_id, []):
|
| 322 |
# Skip if case already disposed (safety check)
|
| 323 |
if case.status == CaseStatus.DISPOSED:
|
| 324 |
continue
|
|
@@ -1,4 +1,5 @@
|
|
| 1 |
"""Scheduling policy implementations."""
|
|
|
|
| 2 |
from scheduler.simulation.policies.fifo import FIFOPolicy
|
| 3 |
from scheduler.simulation.policies.age import AgeBasedPolicy
|
| 4 |
from scheduler.simulation.policies.readiness import ReadinessPolicy
|
|
@@ -15,4 +16,4 @@ def get_policy(name: str):
|
|
| 15 |
raise ValueError(f"Unknown policy: {name}")
|
| 16 |
return POLICY_REGISTRY[name_lower]()
|
| 17 |
|
| 18 |
-
__all__ = ["FIFOPolicy", "AgeBasedPolicy", "ReadinessPolicy", "get_policy"]
|
|
|
|
| 1 |
"""Scheduling policy implementations."""
|
| 2 |
+
from scheduler.simulation.scheduler import SchedulerPolicy
|
| 3 |
from scheduler.simulation.policies.fifo import FIFOPolicy
|
| 4 |
from scheduler.simulation.policies.age import AgeBasedPolicy
|
| 5 |
from scheduler.simulation.policies.readiness import ReadinessPolicy
|
|
|
|
| 16 |
raise ValueError(f"Unknown policy: {name}")
|
| 17 |
return POLICY_REGISTRY[name_lower]()
|
| 18 |
|
| 19 |
+
__all__ = ["SchedulerPolicy", "FIFOPolicy", "AgeBasedPolicy", "ReadinessPolicy", "get_policy"]
|