#!/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 {"iteration_trace": [asdict(entry) for entry in result.iteration_trace]} def _render_markdown(payload: dict[str, object]) -> str: lines = [ "# Iteration 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.", "", "| Iteration | Penalty | Routed Nets | Completed | Conflict Edges | Dynamic Collisions | " "Nodes | Congestion Checks | Candidate Ids | Exact Pairs |", "| --: | --: | --: | --: | --: | --: | --: | --: | --: | --: |", ] ) net_node_counts: Counter[str] = Counter() net_check_counts: Counter[str] = Counter() for entry in scenario["iteration_trace"]: lines.append( "| " f"{entry['iteration']} | " f"{entry['congestion_penalty']:.1f} | " f"{len(entry['routed_net_ids'])} | " f"{entry['completed_nets']} | " f"{entry['conflict_edges']} | " f"{entry['total_dynamic_collisions']} | " f"{entry['nodes_expanded']} | " f"{entry['congestion_check_calls']} | " f"{entry['congestion_candidate_ids']} | " f"{entry['congestion_exact_pair_checks']} |" ) for attempt in entry["net_attempts"]: net_node_counts[attempt["net_id"]] += attempt["nodes_expanded"] net_check_counts[attempt["net_id"]] += attempt["congestion_check_calls"] lines.extend(["", "Top nets by iteration-attributed nodes expanded:", ""]) if net_node_counts: for net_id, count in net_node_counts.most_common(10): lines.append(f"- `{net_id}`: {count}") else: lines.append("- None") lines.extend(["", "Top nets by iteration-attributed congestion checks:", ""]) if net_check_counts: for net_id, count in net_check_counts.most_common(10): lines.append(f"- `{net_id}`: {count}") else: lines.append("- None") lines.append("") return "\n".join(lines) def main() -> None: record_trace( __file__, "iteration_trace", scenario_label="iteration-trace", default_scenarios=NO_WARM_START_SCENARIOS, scenario_payload=_scenario_payload, render_markdown=_render_markdown, ) if __name__ == "__main__": main()