File size: 5,051 Bytes
ab849c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""Plotly visualization helpers."""

from __future__ import annotations

from typing import Any

import plotly.graph_objects as go


def build_solver_comparison_chart(rows: list[dict[str, Any]], metric: str = "objective_value") -> go.Figure:
    if not rows:
        return go.Figure()
    methods = [r.get("method_label", r.get("method_id", "")) for r in rows]
    values = [r.get(metric, 0) for r in rows]
    colors = ["#4f46e5" if r.get("winner") else "#94a3b8" for r in rows]
    fig = go.Figure(go.Bar(x=methods, y=values, marker_color=colors))
    fig.update_layout(
        title=f"Method Comparison — {metric.replace('_', ' ').title()}",
        xaxis_title="Method",
        yaxis_title=metric.replace("_", " ").title(),
        template="plotly_white",
        height=400,
    )
    return fig


def build_gap_timeline(rows: list[dict[str, Any]]) -> go.Figure:
    if not rows:
        return go.Figure()
    fig = go.Figure()
    for r in rows:
        fig.add_trace(go.Scatter(
            x=[r.get("elapsed_time_sec", 0)],
            y=[r.get("optimality_gap", 0)],
            mode="markers+text",
            name=r.get("method_label", ""),
            text=[r.get("method_label", "")],
            textposition="top center",
        ))
    fig.update_layout(
        title="Optimality Gap vs Solve Time",
        xaxis_title="Elapsed Time (s)",
        yaxis_title="Gap (%)",
        template="plotly_white",
        height=400,
    )
    return fig


def build_scalability_chart(rows: list[dict[str, Any]]) -> go.Figure:
    if not rows:
        return go.Figure()
    sizes = sorted(set(r.get("size", "") for r in rows))
    methods = sorted(set(r.get("method_id", "") for r in rows))
    fig = go.Figure()
    for mid in methods:
        subset = [r for r in rows if r.get("method_id") == mid]
        by_size = {r["size"]: r.get("elapsed_time_sec", 0) for r in subset}
        fig.add_trace(go.Scatter(
            x=sizes,
            y=[by_size.get(s, 0) for s in sizes],
            mode="lines+markers",
            name=mid,
        ))
    fig.update_layout(
        title="Scalability — Solve Time by Instance Size",
        xaxis_title="Size",
        yaxis_title="Time (s)",
        template="plotly_white",
        height=400,
    )
    return fig


def build_category_radar(rows: list[dict[str, Any]]) -> go.Figure:
    if not rows:
        return go.Figure()
    cats = sorted(set(r.get("method_category", "") for r in rows))
    metrics = ["objective_value", "optimality_gap", "elapsed_time_sec"]
    fig = go.Figure()
    for cat in cats:
        subset = [r for r in rows if r.get("method_category") == cat]
        if not subset:
            continue
        vals = []
        for m in metrics:
            avg = sum(r.get(m, 0) for r in subset) / len(subset)
            vals.append(avg)
        fig.add_trace(go.Scatterpolar(
            r=vals,
            theta=[m.replace("_", " ").title() for m in metrics],
            name=cat,
            fill="toself",
        ))
    fig.update_layout(
        polar=dict(radialaxis=dict(visible=True)),
        title="Method Category Profile",
        template="plotly_white",
        height=450,
    )
    return fig


def build_heatmap(comparisons: dict[str, Any], metric: str = "objective_value") -> go.Figure:
    if not comparisons:
        return go.Figure()
    problems = list(comparisons.keys())
    methods_set: set[str] = set()
    for pt_data in comparisons.values():
        for inst_data in pt_data.values():
            methods_set.update(inst_data.get("results", {}).keys())
    methods = sorted(methods_set)
    z = []
    for pt in problems:
        row = []
        for mid in methods:
            vals = []
            for inst_data in comparisons[pt].values():
                mdata = inst_data.get("results", {}).get(mid, {})
                if mdata:
                    vals.append(mdata.get(metric, 0))
            row.append(sum(vals) / len(vals) if vals else 0)
        z.append(row)
    fig = go.Figure(go.Heatmap(z=z, x=methods, y=problems, colorscale="Viridis"))
    fig.update_layout(
        title=f"Benchmark Heatmap — {metric.replace('_', ' ').title()}",
        template="plotly_white",
        height=500,
    )
    return fig


def build_progress_chart(result_metrics: dict[str, Any]) -> go.Figure:
    fig = go.Figure()
    labels = ["Objective", "Best Bound", "Gap %", "Time (s)", "Iterations"]
    values = [
        result_metrics.get("objective_value", 0),
        result_metrics.get("best_bound", 0),
        result_metrics.get("optimality_gap", 0),
        result_metrics.get("elapsed_time_sec", 0),
        result_metrics.get("iterations", 0),
    ]
    fig.add_trace(go.Bar(x=labels, y=values, marker_color=["#4f46e5", "#7c3aed", "#f59e0b", "#10b981", "#6366f1"]))
    fig.update_layout(title="Solve Progress Snapshot", template="plotly_white", height=350)
    return fig