File size: 8,427 Bytes
a871dae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Task graders for the Autonomous Traffic Control OpenEnv environment.

Defines three tasks of increasing difficulty:
  1. basic_flow          – high-volume baseline throughput optimisation
  2. emergency_priority  – emergency vehicle management + throughput
  3. dynamic_scenarios   – surge-traffic + emergencies under hard constraints

Each grader returns a GradeResult(score, metrics, feedback) with 0-1 score.
"""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any, Dict, Optional


@dataclass
class GradeResult:
    """Standardised grading result."""
    score:    float             # 0.0 – 1.0
    metrics:  Dict[str, Any]   = field(default_factory=dict)
    feedback: str              = ""


# ---------------------------------------------------------------------------
# Public entry point
# ---------------------------------------------------------------------------

def grade(
    task_id: str,
    *,
    total_vehicles_passed: int   = 0,
    total_emergency_passed: int  = 0,
    total_waiting_time: float    = 0.0,
    total_collisions: int        = 0,
    total_emergency_delay: float = 0.0,
    total_phase_changes: int     = 0,
    step_count: int              = 1,
) -> GradeResult:
    """Route to the appropriate task grader."""
    graders = {
        "basic_flow":          _grade_basic_flow,
        "emergency_priority":  _grade_emergency_priority,
        "dynamic_scenarios":   _grade_dynamic_scenarios,
    }
    if task_id not in graders:
        return GradeResult(
            score=0.0,
            feedback=f"Unknown task_id '{task_id}'. "
                     f"Valid tasks: {list(graders.keys())}",
        )
    return graders[task_id](
        total_vehicles_passed=total_vehicles_passed,
        total_emergency_passed=total_emergency_passed,
        total_waiting_time=total_waiting_time,
        total_collisions=total_collisions,
        total_emergency_delay=total_emergency_delay,
        total_phase_changes=total_phase_changes,
        step_count=max(step_count, 1),
    )


# ---------------------------------------------------------------------------
# Task 1 – Basic Flow  (weight in overall hackathon score: 30%)
# ---------------------------------------------------------------------------

_BASIC_FLOW_TARGET_THROUGHPUT_PER_STEP = 1.8  # vehicles/step considered "perfect"

def _grade_basic_flow(
    *,
    total_vehicles_passed: int,
    total_waiting_time: float,
    total_collisions: int,
    step_count: int,
    **_ignored,
) -> GradeResult:
    throughput_per_step = total_vehicles_passed / step_count
    throughput_score    = min(throughput_per_step / _BASIC_FLOW_TARGET_THROUGHPUT_PER_STEP, 1.0)
    efficiency_score    = 1.0 / (1.0 + total_waiting_time / max(step_count, 1) * 0.1)
    collision_penalty   = 0.8 if total_collisions > 0 else 0.0

    raw   = throughput_score * 0.6 + efficiency_score * 0.4
    score = max(0.0, raw - collision_penalty)

    return GradeResult(
        score=round(score, 4),
        metrics={
            "throughput_per_step":  round(throughput_per_step, 3),
            "throughput_score":     round(throughput_score,    4),
            "efficiency_score":     round(efficiency_score,    4),
            "total_collisions":     total_collisions,
            "collision_penalty":    collision_penalty,
        },
        feedback=(
            f"Throughput {throughput_per_step:.2f} veh/step "
            f"(target {_BASIC_FLOW_TARGET_THROUGHPUT_PER_STEP}). "
            + ("⚠ Collision penalty applied!" if total_collisions else "No collisions βœ“.")
        ),
    )


# ---------------------------------------------------------------------------
# Task 2 – Emergency Priority  (weight: 40%)
# ---------------------------------------------------------------------------

_EMERG_TARGET_DELAY_PER_VEHICLE = 3.0  # steps/emergency vehicle

def _grade_emergency_priority(
    *,
    total_vehicles_passed: int,
    total_emergency_passed: int,
    total_waiting_time: float,
    total_collisions: int,
    total_emergency_delay: float,
    step_count: int,
    **_ignored,
) -> GradeResult:
    throughput_per_step = total_vehicles_passed / step_count
    throughput_score    = min(throughput_per_step / 1.5, 1.0)

    # Emergency throughput (score = 1 if β‰₯ 1 emergency vehicle cleared per 20 steps)
    em_rate       = total_emergency_passed / step_count
    em_rate_score = min(em_rate / (1.0 / 20.0), 1.0)

    # Emergency delay score
    if total_emergency_passed > 0:
        avg_delay     = total_emergency_delay / total_emergency_passed
        delay_score   = max(0.0, 1.0 - avg_delay / (_EMERG_TARGET_DELAY_PER_VEHICLE * 4))
    else:
        delay_score   = 0.5

    efficiency_score  = 1.0 / (1.0 + total_waiting_time / max(step_count, 1) * 0.05)
    collision_penalty = 0.85 if total_collisions > 0 else 0.0

    raw   = (throughput_score * 0.30 + em_rate_score * 0.35 +
             delay_score * 0.20    + efficiency_score * 0.15)
    score = max(0.0, raw - collision_penalty)

    avg_delay_str = (
        f"{total_emergency_delay / total_emergency_passed:.1f} steps"
        if total_emergency_passed else "N/A"
    )

    return GradeResult(
        score=round(score, 4),
        metrics={
            "throughput_per_step":       round(throughput_per_step, 3),
            "throughput_score":          round(throughput_score,    4),
            "emergency_rate_score":      round(em_rate_score,       4),
            "emergency_delay_score":     round(delay_score,         4),
            "efficiency_score":          round(efficiency_score,    4),
            "total_emergency_passed":    total_emergency_passed,
            "avg_emergency_delay_steps": avg_delay_str,
            "total_collisions":          total_collisions,
        },
        feedback=(
            f"Cleared {total_emergency_passed} emergency vehicles "
            f"(avg delay {avg_delay_str}). "
            f"Throughput {throughput_per_step:.2f} veh/step. "
            + ("⚠ Collision!" if total_collisions else "No collisions βœ“.")
        ),
    )


# ---------------------------------------------------------------------------
# Task 3 – Dynamic Scenarios  (weight: 30%)
# ---------------------------------------------------------------------------

def _grade_dynamic_scenarios(
    *,
    total_vehicles_passed: int,
    total_emergency_passed: int,
    total_waiting_time: float,
    total_collisions: int,
    total_emergency_delay: float,
    total_phase_changes: int,
    step_count: int,
    **_ignored,
) -> GradeResult:
    throughput_per_step = total_vehicles_passed / step_count
    throughput_score    = min(throughput_per_step / 2.0, 1.0)

    em_rate       = total_emergency_passed / step_count
    em_rate_score = min(em_rate / (1.0 / 15.0), 1.0)

    if total_emergency_passed > 0:
        avg_delay   = total_emergency_delay / total_emergency_passed
        delay_score = max(0.0, 1.0 - avg_delay / 5.0)
    else:
        delay_score = 0.0

    efficiency_score  = 1.0 / (1.0 + total_waiting_time / max(step_count, 1) * 0.08)
    adaptability_score = 1.0 / (1.0 + total_phase_changes / max(step_count, 1) * 0.5)
    collision_penalty  = 0.9 if total_collisions > 0 else 0.0

    raw   = (throughput_score    * 0.25 + em_rate_score  * 0.30 +
             delay_score         * 0.20 + efficiency_score * 0.15 +
             adaptability_score  * 0.10)
    score = max(0.0, raw - collision_penalty)

    return GradeResult(
        score=round(score, 4),
        metrics={
            "throughput_per_step":    round(throughput_per_step, 3),
            "throughput_score":       round(throughput_score,    4),
            "emergency_rate_score":   round(em_rate_score,       4),
            "emergency_delay_score":  round(delay_score,         4),
            "efficiency_score":       round(efficiency_score,    4),
            "adaptability_score":     round(adaptability_score,  4),
            "total_collisions":       total_collisions,
            "total_phase_changes":    total_phase_changes,
        },
        feedback=(
            f"Dynamic task: throughput {throughput_per_step:.2f} veh/step, "
            f"{total_emergency_passed} emergencies cleared, "
            f"{total_phase_changes} phase changes over {step_count} steps. "
            + ("⚠ Collision!" if total_collisions else "No collisions βœ“.")
        ),
    )