CoolFace
Apppublic

arrow072/open_env_meta

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
tasks.py162 linesDownload Raw Back to root
1"""2tasks.py — Difficulty Configurations for TrafficEnv3=====================================================4 5Three pre-defined task configurations:6 7  EASY_CONFIG   – Stable, balanced traffic; good for initial training.8  MEDIUM_CONFIG – Random bursts, moderate congestion; standard benchmark.9  HARD_CONFIG   – High intensity, frequent emergencies, strict fairness.10 11Each config is a plain dict consumed by TrafficEnv.__init__().12"""13 14from __future__ import annotations15from typing import Any, Dict16 17 18# ---------------------------------------------------------------------------19# Easy20# ---------------------------------------------------------------------------21 22EASY_CONFIG: Dict[str, Any] = {23    # Traffic flow24    "arrival_rate":       (0, 1),    # 0–1 cars per lane per step25    "discharge_rate":     (4, 5),    # 4–5 cars discharged per green lane per step26    "max_queue":          15,        # queue cap per lane27    "max_steps":          50,28 29    # Emergencies — rare30    "emergency_prob":     0.01,31 32    # Bursts — none33    "burst_prob":         0.0,34    "burst_multiplier":   1.0,35 36    # Reward knobs37    "switch_penalty":         0.10,38    "starvation_threshold":   20,39    "r_efficiency_scale":     0.20,40    "p_congestion_scale":     0.30,41    "p_max_q_scale":          0.10,42    "p_starvation_scale":     0.10,43    "r_fairness_bonus":       0.05,44    "r_improvement_bonus":    0.15,45    "p_emergency_scale":      0.30,46    "r_ev_bonus_scale":       0.20,47    48    # Logic thresholds49    "ev_golden_window":       8,     # Easy: very generous window50    "ev_max_delay":           20,51}52 53# ---------------------------------------------------------------------------54# Medium55# ---------------------------------------------------------------------------56 57MEDIUM_CONFIG: Dict[str, Any] = {58    # Traffic flow59    "arrival_rate":       (1, 3),    # moderate, variable arrivals60    "discharge_rate":     (3, 5),    # standard discharge61    "max_queue":          25,62    "max_steps":          100,63 64    # Emergencies — occasional65    "emergency_prob":     0.05,66 67    # Random bursts — 10% chance, 1.5× arrivals68    "burst_prob":         0.10,69    "burst_multiplier":   1.5,70 71    # Reward knobs72    "switch_penalty":         0.20,73    "starvation_threshold":   15,74    "r_efficiency_scale":     0.20,75    "p_congestion_scale":     0.40,76    "p_max_q_scale":          0.15,77    "p_starvation_scale":     0.15,78    "r_fairness_bonus":       0.10,79    "r_improvement_bonus":    0.20,80    "p_emergency_scale":      0.40,81    "r_ev_bonus_scale":       0.25,82 83    # Logic thresholds84    "ev_golden_window":       5,     # Medium: standard window85    "ev_max_delay":           15,86}87 88# ---------------------------------------------------------------------------89# Hard90# ---------------------------------------------------------------------------91 92HARD_CONFIG: Dict[str, Any] = {93    # Traffic flow — high intensity94    "arrival_rate":       (2, 5),    # heavy, bursty arrivals95    "discharge_rate":     (2, 4),    # reduced discharge (lane friction)96    "max_queue":          40,97    "max_steps":          200,98 99    # Emergencies — frequent100    "emergency_prob":     0.15,101 102    # Frequent aggressive bursts103    "burst_prob":         0.20,104    "burst_multiplier":   2.0,105 106    # Reward knobs — stricter penalties107    "switch_penalty":         0.30,108    "starvation_threshold":   10,    # stricter fairness109    "r_efficiency_scale":     0.25,110    "p_congestion_scale":     0.50,111    "p_max_q_scale":          0.20,112    "p_starvation_scale":     0.20,113    "r_fairness_bonus":       0.15,114    "r_improvement_bonus":    0.25,115    "p_emergency_scale":      0.60,  # amplified emergency penalty116    "r_ev_bonus_scale":       0.30,117 118    # Logic thresholds119    "ev_golden_window":       3,     # Hard: must clear immediately120    "ev_max_delay":           10,121}122 123 124# ---------------------------------------------------------------------------125# Accessor126# ---------------------------------------------------------------------------127 128_CONFIGS = {129    "easy":   EASY_CONFIG,130    "medium": MEDIUM_CONFIG,131    "hard":   HARD_CONFIG,132}133 134 135def get_config(mode: str) -> Dict[str, Any]:136    """137    Return the config dict for the requested difficulty mode.138 139    Parameters140    ----------141    mode : str142        One of "easy", "medium", "hard" (case-insensitive).143 144    Returns145    -------146    dict147        Configuration dictionary suitable for ``TrafficEnv(config)``.148 149    Raises150    ------151    ValueError152        If an unknown mode is requested.153    """154    key = mode.strip().lower()155    if key not in _CONFIGS:156        raise ValueError(157            f"Unknown difficulty mode '{mode}'. "158            f"Choose one of: {list(_CONFIGS)}"159        )160    # Return a copy so callers can mutate without side-effects161    return dict(_CONFIGS[key])162