diff --git a/sim/main.py b/sim/main.py index e83acb7..c6566ac 100644 --- a/sim/main.py +++ b/sim/main.py @@ -3,6 +3,8 @@ import sys from pathlib import Path +from sim.planning.comparison import score_planning_rows + sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from sim.config import ( @@ -109,6 +111,7 @@ def fmt_elapsed(minute_of_day: float, start_time_hour: float) -> str: speeds_mph=SPEED_SWEEP_MPH, use_api_weather=True, ) + rows = score_planning_rows(rows) print_speed_sweep_summary(rows) diff --git a/sim/planning/comparison.py b/sim/planning/comparison.py index 0fbf9b9..4deb95a 100644 --- a/sim/planning/comparison.py +++ b/sim/planning/comparison.py @@ -42,9 +42,62 @@ def sort_planning_rows(rows: list[dict]) -> list[dict]: return sorted( rows, key=lambda row: ( - row["completed_full_window"], - row["laps"], - row["final_soc_pct"], + row["feasibility"], + row["score"] ), reverse=True, ) + +def score_planning_rows(rows: list[dict], config: dict = None): + import json + from pathlib import Path + + if config is None: + with (Path(__file__).parent / "config.json").open(encoding="utf-8") as config_file: + config = json.load(config_file) + + laps_weight = config["weights"]["laps"] + minimize_soc_margin_weight = config["weights"]["minimize_soc_margin"] + + realistic_speed = config["realistic_speed"] + realistic_speed_value = realistic_speed["value"] + realistic_speed_acceptable_range = realistic_speed["acceptable_range"] + realistic_speed_penalty = realistic_speed["penalty"] + + strategy_adjustments = { + strategy: adjustment + for strategy, adjustment in config["strategy_adjustments"].items() + if not strategy.startswith("_") + } + + if not rows: + return rows + + most_laps = max((row.get("laps", 0) for row in rows), default=0) + min_realistic_speed = realistic_speed_value - realistic_speed_acceptable_range + max_realistic_speed = realistic_speed_value + realistic_speed_acceptable_range + + for row in rows: + # general score calc + lap_score = row["laps"] / most_laps * 100 if most_laps else 0 + soc_score = 100 - row["soc_margin_pct"] + score = lap_score * laps_weight + soc_score * minimize_soc_margin_weight + + # penalty for speeds that aren't... that realistic + avg_speed = row.get("avg_speed_mph") + if avg_speed is not None and not min_realistic_speed <= avg_speed <= max_realistic_speed: + score -= realistic_speed_penalty + row['realistic_speed_penalty'] = True + else: + row['realistic_speed_penalty'] = False + + # attempt to get the strategy name, could be under strategy or strategy + strategy_key = row.get("strategy") + if strategy_key not in strategy_adjustments: + strategy_key = row.get("strategy") + + row['strategy_adjustment'] = strategy_adjustments.get(strategy_key, 0) + score += strategy_adjustments.get(strategy_key, 0) + row["score"] = score + return rows + diff --git a/sim/planning/config.json b/sim/planning/config.json new file mode 100644 index 0000000..d66ba41 --- /dev/null +++ b/sim/planning/config.json @@ -0,0 +1,21 @@ +{ + "weights": { + "_comment": "Weights should add up to 1", + "laps": 0.5, + "minimize_soc_margin": 0.5 + }, + + "realistic_speed": { + "_comment": "Sets typical speed - if speed is beyond higher or lower the acceptable range, subtract penalty from score", + "value": 25, + "acceptable_range": 10, + "penalty": 5 + }, + + "strategy_adjustments": { + "_comment": "Custom score adjustments for specific strategies w/o fixed speeds", + "pi": 0, + "stepped": 0, + "interval-hold": 0 + } +} \ No newline at end of file diff --git a/sim/planning/report.py b/sim/planning/report.py index a70c244..966de7d 100644 --- a/sim/planning/report.py +++ b/sim/planning/report.py @@ -1,5 +1,5 @@ from sim.config import RaceConfig, CarConfig -from sim.planning.comparison import sort_planning_rows +from sim.planning.comparison import sort_planning_rows, score_planning_rows def print_planning_report( @@ -11,6 +11,7 @@ def print_planning_report( car_config: CarConfig, ) -> None: combined_rows = fixed_speed_rows + strategy_rows + combined_rows = score_planning_rows(combined_rows) best_fixed_speed_row = ( sort_planning_rows(fixed_speed_rows)[0] if fixed_speed_rows @@ -73,9 +74,13 @@ def print_planning_report( print(" No planning rows provided.") else: print(f" Use {best_row['label']}.") + print() print( - f" Expected result: {best_row['laps']} laps, " - f"{best_row['final_soc_pct']:.1f}% final SoC." + f" Expected result: {best_row['laps']} laps\n" + f" {best_row['soc_margin_pct']:.1f} SoC margin (%)\n" + f" Penalized for unrealistic speed: {best_row['realistic_speed_penalty']}\n" + f" Strategy adjustment to score: {best_row['strategy_adjustment']}\n" + f" Overall score: {best_row['score']:.2f}\n" ) def export_planning_report_to_csv( diff --git a/tests/scoring_test.py b/tests/scoring_test.py new file mode 100644 index 0000000..8db8087 --- /dev/null +++ b/tests/scoring_test.py @@ -0,0 +1,64 @@ +import unittest + +from sim.planning.comparison import score_planning_rows, sort_planning_rows + + +class TestScoring(unittest.TestCase): + def test_feasible_over_infeasible(self): + rows = [ + { + "completed_full_window": True, + "final_soc_pct": 50, + "target_soc_pct": 40, + "feasibility": True, + "score": 50, + }, + { + "completed_full_window": False, + "final_soc_pct": 90, + "target_soc_pct": 40, + "feasibility": False, + "score": 100, + }, + ] + + sorted_rows = sort_planning_rows(rows) + self.assertTrue(sorted_rows[0]["feasibility"]) + + def test_calculations(self): + config = { + "weights": {"laps": 0.7, "minimize_soc_margin": 0.3}, + "realistic_speed": { + "value": 25, + "acceptable_range": 5, + "penalty": 15, + }, + "strategy_adjustments": {"stepped": 10, "pi": -5}, + } + rows = [ + { + "laps": 10, + "soc_margin_pct": 20, + "strategy": "stepped", + }, + { + "laps": 8, + "soc_margin_pct": 10, + "strategy": "pi", + }, + { + "laps": 5, + "soc_margin_pct": 30, + "avg_speed_mph": 10, + "strategy": "fixed" + } + ] + + score_planning_rows(rows, config) + + self.assertEqual(rows[0]["score"], 104) + self.assertEqual(rows[0]["realistic_speed_penalty"], False) + self.assertEqual(rows[1]["score"], 78) + self.assertEqual(rows[1]["realistic_speed_penalty"], False) + self.assertEqual(rows[2]["score"], 41) + self.assertEqual(rows[2]["realistic_speed_penalty"], True)