From 319988c66420fabe54e603b674a67b49ded2c1d0 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sun, 11 Oct 2026 15:47:23 -0400 Subject: [PATCH 01/10] mostly planning in my head --- sim/planning/comparison.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sim/planning/comparison.py b/sim/planning/comparison.py index 0fbf9b9..0c169ac 100644 --- a/sim/planning/comparison.py +++ b/sim/planning/comparison.py @@ -48,3 +48,11 @@ def sort_planning_rows(rows: list[dict]) -> list[dict]: ), reverse=True, ) + +# returns a tuple (feasible, score) +# feasible: true or false +# score = unitless numeric score +def score_planning_row(row: dict): + feasible = check_feasibility(row) # race window + + From e7fb7145c009090742e6d39ff19aff68450c692d Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 17 Oct 2026 02:05:26 -0400 Subject: [PATCH 02/10] Add JSON file to configure scoring --- sim/planning/config.json | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 sim/planning/config.json diff --git a/sim/planning/config.json b/sim/planning/config.json new file mode 100644 index 0000000..4ef044a --- /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": 5, + "penalty": 5 + }, + + "study_handicaps": { + "_comment": "Custom score adjustments for specific strategies w/o fixed speeds", + "pi": 0, + "stepped": 0, + "interval-hold": 0 + } +} \ No newline at end of file From 5d914f2b87d0349fc4f2913574da0f8eaaa038b5 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 17 Oct 2026 02:45:10 -0400 Subject: [PATCH 03/10] Read from JSON and calculate score --- sim/planning/comparison.py | 52 ++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/sim/planning/comparison.py b/sim/planning/comparison.py index 0c169ac..efd44e1 100644 --- a/sim/planning/comparison.py +++ b/sim/planning/comparison.py @@ -49,10 +49,52 @@ def sort_planning_rows(rows: list[dict]) -> list[dict]: reverse=True, ) -# returns a tuple (feasible, score) -# feasible: true or false -# score = unitless numeric score -def score_planning_row(row: dict): - feasible = check_feasibility(row) # race window +def score_planning_rows(rows: list[dict]): + import json + from pathlib import Path + 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"] + + study_handicaps = { + study: handicap + for study, handicap in config["study_handicaps"].items() + if not study.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 + + # attempt to get the study name, could be under strategy or study + handicap_key = row.get("strategy") + if handicap_key not in study_handicaps: + handicap_key = row.get("study") + + score -= study_handicaps.get(handicap_key, 0) + row["score"] = score + + return rows From be6a062d5378ad3fc1fe1e9e2d1ead188c2db894 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 17 Oct 2026 03:00:40 -0400 Subject: [PATCH 04/10] Adjust config --- sim/planning/config.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sim/planning/config.json b/sim/planning/config.json index 4ef044a..d66ba41 100644 --- a/sim/planning/config.json +++ b/sim/planning/config.json @@ -8,11 +8,11 @@ "realistic_speed": { "_comment": "Sets typical speed - if speed is beyond higher or lower the acceptable range, subtract penalty from score", "value": 25, - "acceptable_range": 5, + "acceptable_range": 10, "penalty": 5 }, - "study_handicaps": { + "strategy_adjustments": { "_comment": "Custom score adjustments for specific strategies w/o fixed speeds", "pi": 0, "stepped": 0, From 8e3a1fd520297f5c4abbf5cca6266ca987f0c38b Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 17 Oct 2026 03:01:03 -0400 Subject: [PATCH 05/10] Rename study handicap to strategy adjustment --- sim/planning/comparison.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/sim/planning/comparison.py b/sim/planning/comparison.py index efd44e1..a1f1406 100644 --- a/sim/planning/comparison.py +++ b/sim/planning/comparison.py @@ -43,8 +43,7 @@ def sort_planning_rows(rows: list[dict]) -> list[dict]: rows, key=lambda row: ( row["completed_full_window"], - row["laps"], - row["final_soc_pct"], + row["score"] ), reverse=True, ) @@ -64,10 +63,10 @@ def score_planning_rows(rows: list[dict]): realistic_speed_acceptable_range = realistic_speed["acceptable_range"] realistic_speed_penalty = realistic_speed["penalty"] - study_handicaps = { - study: handicap - for study, handicap in config["study_handicaps"].items() - if not study.startswith("_") + strategy_adjustments = { + strategy: adjustment + for strategy, adjustment in config["strategy_adjustments"].items() + if not strategy.startswith("_") } if not rows: @@ -87,14 +86,17 @@ def score_planning_rows(rows: list[dict]): 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 study name, could be under strategy or study - handicap_key = row.get("strategy") - if handicap_key not in study_handicaps: - handicap_key = row.get("study") + # 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") - score -= study_handicaps.get(handicap_key, 0) + row['strategy_adjustment'] = strategy_adjustments.get(strategy_key, 0) + score += strategy_adjustments.get(strategy_key, 0) row["score"] = score - return rows From 36584db9fe4df7497d7e365cac2c9bdc6e89f3f9 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 17 Oct 2026 03:08:00 -0400 Subject: [PATCH 06/10] Improve recommendation reporting --- sim/planning/report.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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( From b04a7736d59250feb45476ea50b18ce51a7cbea4 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 17 Oct 2026 03:11:39 -0400 Subject: [PATCH 07/10] Make this a little easier to test --- sim/planning/comparison.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sim/planning/comparison.py b/sim/planning/comparison.py index a1f1406..1c4fbc4 100644 --- a/sim/planning/comparison.py +++ b/sim/planning/comparison.py @@ -48,12 +48,13 @@ def sort_planning_rows(rows: list[dict]) -> list[dict]: reverse=True, ) -def score_planning_rows(rows: list[dict]): +def score_planning_rows(rows: list[dict], config: dict = None): import json from pathlib import Path - with (Path(__file__).parent / "config.json").open(encoding="utf-8") as config_file: - config = json.load(config_file) + 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"] From f5b9ea0eb6360596d12091c21ae9c09561d779cf Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 17 Oct 2026 03:14:31 -0400 Subject: [PATCH 08/10] Score speed sweep on its own as well --- sim/main.py | 3 +++ 1 file changed, 3 insertions(+) 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) From 75ac586c9b4eaaa8684a3c6a8f361bfc07c2b09e Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 17 Oct 2026 03:26:36 -0400 Subject: [PATCH 09/10] write simple tests yknow having copilot write it and then adjusting it isn't all that bad. writing tests is lame anyways. unless it's for DSA3 --- tests/scoring_test.py | 64 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/scoring_test.py 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) From ccca5b503a9474dc490c6a0c253de14182baa3b7 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 17 Oct 2026 03:50:21 -0400 Subject: [PATCH 10/10] This should be feasibility --- sim/planning/comparison.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sim/planning/comparison.py b/sim/planning/comparison.py index 1c4fbc4..4deb95a 100644 --- a/sim/planning/comparison.py +++ b/sim/planning/comparison.py @@ -42,7 +42,7 @@ def sort_planning_rows(rows: list[dict]) -> list[dict]: return sorted( rows, key=lambda row: ( - row["completed_full_window"], + row["feasibility"], row["score"] ), reverse=True,