Markdown Converter
Agent skill for markdown-converter
Build me a Python simulation using Pygame that demonstrates a smart AI traffic light system.
Sign in to like and favorite skills
PROMPTS:
#1: Build me a Python simulation using Pygame that demonstrates a smart AI traffic light system.
#2: Build me a web-based simulation using HTML, CSS, and JavaScript (Canvas).
#3: TASK: Implement a scheduling layer for a traffic intersection simulation. This module will be used by the simulation to produce phase schedules (green cycles + durations). Implement the following:
Algorithms to implement: a) Round Robin (RR): cycles through directions/phases in fixed order. Duration per phase can be proportional to queue length or fixed. b) Shortest Job First (SJF): choose the next phase which will clear the smallest expected remaining 'job' (e.g., vehicles waiting + expected arrivals in short horizon) to reduce average waiting time. c) Priority Scheduling: assign priorities by vehicle type + waiting time. Emergency vehicles have highest priority and must preempt schedule. Public transport (if present) higher than private cars; else use waiting time to break ties. d) Meta-Scheduler: chooses among RR, SJF, Priority dynamically based on heuristics or rule set: - If emergency vehicles present -> always use Priority (immediate preemption). - If overall load low (avg queue < low_threshold) -> use RR for simplicity. - If high variance in queue lengths -> prefer SJF to reduce backlog. - Expose
policy_params to tune thresholds, minimum green, max green.
Interfaces & types (Python):
IntersectionState (dict or dataclass) containing:
queues: Dict[str,int] # counts per approach: "N","E","S","W"
waiting_times: Dict[str,List[float]] # per-vehicle waiting times (seconds)
arrival_rates: Dict[str,float] # estimated λ for short horizon (vehicles/sec)
emergency: List[EmergencyVehicle] # each has direction, time_to_intersection (ETA seconds), id
current_phase: str # e.g., "NS_green" or "EW_green" or "all_red"
sim_time: float # current sim time in secondsActionPlan -> List[Phase], where Phase = {"phase":str, "duration":float, "preemptable":bool}policy_params dict containing:
min_green: float (e.g., 7s)
max_green: float (e.g., 60s)
yellow_duration: float (default 3s)
rr_cycle_order: ["NS_green","EW_green"]
low_load_threshold: float
high_variance_threshold: float
sjf_horizon: float (seconds to forecast arrivals)
emergency_preempt_buffer: float (seconds to safely transition)Emergency handling:
emergency list not empty and any EV ETA <= emergency_preempt_buffer OR already in queue, produce a plan that:
a) Immediately schedules green for EV direction (with all-red + yellow transitions as required).
b) Limit preemption duration to clear EVs (configurable).
c) After EV clears, smoothly resume previously-in-progress scheduling algorithm (remember prior phase and remaining duration).Safety & constraints:
min_switch_interval (configurable).Unit tests (pytest):
Logging & metrics:
debug=True) that prints chosen policy, reasons (e.g., "high variance -> SJF"), and emergency actions.explain_decision(state, policy) that returns a short string explanation for the selection.Integration hooks:
apply_action_plan(sim, action_plan) stub that shows how the simulation should consume the plan (calls sim.set_phase(phase, duration)).simulate_one_step(state, action_plan) example to validate behavior inside unit tests.Code quality:
Output:
schedulers.py with implementations and docstringstest_schedulers.py with the 4 unit tests using pytest and a minimal deterministic simulation helper.Edge cases:
all_red pause.Notes for Copilot: produce readable, well-documented code with small helper functions (compute_queue_variance, estimate_jobs_in_horizon, schedule_transition_with_yellow). Use simple deterministic math; no ML required in this module.