#!/usr/bin/env python3 from __future__ import annotations from collections import Counter from dataclasses import asdict from typing import TYPE_CHECKING from inire.tests.trace_recording import NO_WARM_START_SCENARIOS, record_trace if TYPE_CHECKING: from inire.results import RoutingRunResult def _scenario_payload(result: RoutingRunResult) -> dict[str, object]: return {"frontier_trace": [asdict(entry) for entry in result.frontier_trace]} def _render_markdown(payload: dict[str, object]) -> str: lines = [ "# Frontier Trace", "", f"Generated at {payload['generated_at']} by `{payload['generator']}`.", "", ] for scenario in payload["scenarios"]: lines.extend( [ f"## {scenario['name']}", "", f"Results: {scenario['summary']['valid_results']} valid / " f"{scenario['summary']['reached_targets']} reached / " f"{scenario['summary']['total_results']} total.", "", "| Net | Hotspots | Closed-Set | Hard Collision | Self Collision | Cost | Samples |", "| :-- | --: | --: | --: | --: | --: | --: |", ] ) reason_counts: Counter[str] = Counter() hotspot_counts: Counter[tuple[str, int]] = Counter() for net_trace in scenario["frontier_trace"]: sample_count = len(net_trace["samples"]) lines.append( "| " f"{net_trace['net_id']} | " f"{len(net_trace['hotspot_bounds'])} | " f"{net_trace['pruned_closed_set']} | " f"{net_trace['pruned_hard_collision']} | " f"{net_trace['pruned_self_collision']} | " f"{net_trace['pruned_cost']} | " f"{sample_count} |" ) reason_counts["closed_set"] += net_trace["pruned_closed_set"] reason_counts["hard_collision"] += net_trace["pruned_hard_collision"] reason_counts["self_collision"] += net_trace["pruned_self_collision"] reason_counts["cost"] += net_trace["pruned_cost"] for sample in net_trace["samples"]: hotspot_counts[(net_trace["net_id"], sample["hotspot_index"])] += 1 lines.extend(["", "Prune totals by reason:", ""]) if reason_counts: for reason, count in reason_counts.most_common(): lines.append(f"- `{reason}`: {count}") else: lines.append("- None") lines.extend(["", "Top traced hotspots by sample count:", ""]) if hotspot_counts: for (net_id, hotspot_index), count in hotspot_counts.most_common(10): lines.append(f"- `{net_id}` hotspot `{hotspot_index}`: {count}") else: lines.append("- None") lines.extend(["", "Per-net sampled reason/move breakdown:", ""]) if scenario["frontier_trace"]: for net_trace in scenario["frontier_trace"]: reason_move_counts: Counter[tuple[str, str]] = Counter() hotspot_sample_counts: Counter[int] = Counter() for sample in net_trace["samples"]: reason_move_counts[(sample["reason"], sample["move_type"])] += 1 hotspot_sample_counts[sample["hotspot_index"]] += 1 lines.append(f"- `{net_trace['net_id']}`") if reason_move_counts: top_pairs = ", ".join( f"`{reason}` x `{move}` = {count}" for (reason, move), count in reason_move_counts.most_common(3) ) lines.append(f" sampled reasons: {top_pairs}") else: lines.append(" sampled reasons: none") if hotspot_sample_counts: top_hotspots = ", ".join( f"`{hotspot}` = {count}" for hotspot, count in hotspot_sample_counts.most_common(3) ) lines.append(f" hotspot samples: {top_hotspots}") else: lines.append(" hotspot samples: none") else: lines.append("- None") lines.append("") return "\n".join(lines) def main() -> None: record_trace( __file__, "frontier_trace", scenario_label="trace", default_scenarios=NO_WARM_START_SCENARIOS[:1], scenario_payload=_scenario_payload, render_markdown=_render_markdown, ) if __name__ == "__main__": main()