File size: 8,796 Bytes
3a970a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""
Visualization Generator for Safe-State Investigation.
Generates publication-quality SVG plots for all 8 experimental figures.

Author: T Bharath (USN: 25BTRCL269)
"""

import os
import csv
from typing import Dict, List

def create_svg_line_chart(title: str, x_label: str, y_label: str, x_vals: List, 
                          series: Dict[str, List[float]], output_path: str, y_max: float = 1.0):
    width, height = 750, 450
    margin_l, margin_r, margin_t, margin_b = 90, 160, 60, 60
    plot_w = width - margin_l - margin_r
    plot_h = height - margin_t - margin_b
    
    colors = ["#2563EB", "#DC2626", "#059669", "#D97706"]
    
    svg = [
        f'<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="{height}" viewBox="0 0 {width} {height}" style="background-color: #ffffff; font-family: Segoe UI, Arial, sans-serif;">',
        f'<text x="{width/2}" y="32" font-size="16" font-weight="bold" text-anchor="middle" fill="#1e293b">{title}</text>',
        # Grid and axes
        f'<rect x="{margin_l}" y="{margin_t}" width="{plot_w}" height="{plot_h}" fill="#f8fafc" stroke="#cbd5e1" stroke-width="1.5"/>'
    ]
    
    # Y-axis ticks
    num_y_ticks = 5
    for i in range(num_y_ticks + 1):
        y_val = (y_max / num_y_ticks) * i
        y_pos = margin_t + plot_h - (i / num_y_ticks) * plot_h
        svg.append(f'<line x1="{margin_l}" y1="{y_pos}" x2="{margin_l + plot_w}" y2="{y_pos}" stroke="#e2e8f0" stroke-width="1"/>')
        svg.append(f'<text x="{margin_l - 12}" y="{y_pos + 4}" font-size="11" text-anchor="end" fill="#64748b">{y_val:.2f}</text>')
        
    # X-axis ticks
    num_x = len(x_vals)
    x_coords = []
    for i, x in enumerate(x_vals):
        x_pos = margin_l + (i / max(1, num_x - 1)) * plot_w if num_x > 1 else margin_l + plot_w / 2
        x_coords.append(x_pos)
        svg.append(f'<line x1="{x_pos}" y1="{margin_t}" x2="{x_pos}" y2="{margin_t + plot_h}" stroke="#e2e8f0" stroke-width="1"/>')
        svg.append(f'<text x="{x_pos}" y="{margin_t + plot_h + 20}" font-size="11" text-anchor="middle" fill="#64748b">{x}</text>')
        
    # Axis labels
    svg.append(f'<text x="{margin_l + plot_w/2}" y="{height - 15}" font-size="13" font-weight="600" text-anchor="middle" fill="#334155">{x_label}</text>')
    svg.append(f'<text x="25" y="{margin_t + plot_h/2}" font-size="13" font-weight="600" text-anchor="middle" fill="#334155" transform="rotate(-90 25 {margin_t + plot_h/2})">{y_label}</text>')
    
    # Series curves and legend
    leg_x = margin_l + plot_w + 20
    leg_y = margin_t + 20
    
    for s_idx, (name, y_data) in enumerate(series.items()):
        col = colors[s_idx % len(colors)]
        pts = []
        for x_pos, y_val in zip(x_coords, y_data):
            y_clamped = max(0.0, min(y_max, y_val))
            y_pos = margin_t + plot_h - (y_clamped / y_max) * plot_h
            pts.append((x_pos, y_pos))
            
        pts_str = " ".join(f"{x:.1f},{y:.1f}" for x, y in pts)
        svg.append(f'<polyline fill="none" stroke="{col}" stroke-width="2.5" points="{pts_str}"/>')
        for x_pos, y_pos in pts:
            svg.append(f'<circle cx="{x_pos:.1f}" cy="{y_pos:.1f}" r="4.5" fill="{col}" stroke="#ffffff" stroke-width="1.5"/>')
            
        # Legend item
        svg.append(f'<line x1="{leg_x}" y1="{leg_y + s_idx * 24}" x2="{leg_x + 20}" y2="{leg_y + s_idx * 24}" stroke="{col}" stroke-width="3"/>')
        svg.append(f'<circle cx="{leg_x + 10}" cy="{leg_y + s_idx * 24}" r="3.5" fill="{col}"/>')
        svg.append(f'<text x="{leg_x + 28}" y="{leg_y + s_idx * 24 + 4}" font-size="11" fill="#334155">{name}</text>')
        
    svg.append('</svg>')
    
    with open(output_path, "w", encoding="utf-8") as f:
        f.write("\n".join(svg))
    print(f"Generated plot: {output_path}")

def generate_all_plots():
    base_dir = os.path.dirname(__file__)
    
    # Fig 1: P(Unsafe) vs Allocation Pressure (rho)
    create_svg_line_chart(
        title="Fig 1: Probability of Reaching Unsafe State vs. Resource Pressure (rho)",
        x_label="Resource Pressure (rho)",
        y_label="Probability of Reaching Unsafe State P(Unsafe)",
        x_vals=["0.80", "1.00", "1.20", "1.40", "1.60", "2.00"],
        series={
            "Uniform (Low Var)": [0.000, 0.000, 0.000, 0.033, 0.633, 1.000],
            "Bimodal (Whale-Guppy)": [0.000, 0.000, 0.000, 0.600, 0.967, 1.000]
        },
        output_path=os.path.join(base_dir, "fig1_punsafe_pressure.svg"),
        y_max=1.05
    )

    # Fig 2: Mean Time to Unsafe (MTTU) vs Resource Pressure (rho)
    create_svg_line_chart(
        title="Fig 2: Mean Time-to-Unsafe State (MTTU) vs. Resource Pressure (rho)",
        x_label="Resource Pressure (rho)",
        y_label="Mean Time to Unsafe State (MTTU steps)",
        x_vals=["0.80", "1.00", "1.20", "1.40", "1.60", "2.00"],
        series={
            "Uniform (Low Var)": [1000.0, 1000.0, 1000.0, 996.0, 686.1, 164.6],
            "Bimodal (Whale-Guppy)": [1000.0, 1000.0, 1000.0, 671.0, 332.7, 195.1]
        },
        output_path=os.path.join(base_dir, "fig2_mttu_pressure.svg"),
        y_max=1050.0
    )

    # Fig 3: Banker's Denial Rate vs Resource Pressure (rho)
    create_svg_line_chart(
        title="Fig 3: Banker's Deadlock Avoidance Denial Rate vs. Resource Pressure",
        x_label="Resource Pressure (rho)",
        y_label="Request Denial Rate (Fraction Deferred)",
        x_vals=["0.80", "1.00", "1.20", "1.40", "1.60", "2.00"],
        series={
            "Uniform (Low Var)": [0.000, 0.000, 0.000, 0.000, 0.003, 0.120],
            "Bimodal (Whale-Guppy)": [0.000, 0.000, 0.000, 0.016, 0.078, 0.213]
        },
        output_path=os.path.join(base_dir, "fig3_denial_pressure.svg"),
        y_max=0.25
    )

    # Fig 4: System Utilization at First Unsafe State vs Resource Pressure
    create_svg_line_chart(
        title="Fig 4: Resource Utilization at First Unsafe Transition vs. Pressure",
        x_label="Resource Pressure (rho)",
        y_label="Capacity Utilization at First Unsafe State",
        x_vals=["0.80", "1.00", "1.20", "1.40", "1.60", "2.00"],
        series={
            "Uniform (Low Var)": [0.392, 0.491, 0.580, 0.693, 0.906, 0.962],
            "Bimodal (Whale-Guppy)": [0.414, 0.535, 0.621, 0.817, 0.885, 0.812]
        },
        output_path=os.path.join(base_dir, "fig4_util_pressure.svg"),
        y_max=1.05
    )

    # Fig 5: P(Unsafe) vs Concurrency Scale (n)
    create_svg_line_chart(
        title="Fig 5: Probability of Reaching Unsafe State vs. Concurrency Scale (n)",
        x_label="Process Count (n)",
        y_label="Probability of Reaching Unsafe State P(Unsafe)",
        x_vals=["5", "10", "15", "25", "50"],
        series={
            "Uniform (Low Var)": [0.967, 0.067, 0.000, 0.000, 0.000],
            "Bimodal (Whale-Guppy)": [0.967, 0.633, 0.133, 0.000, 0.000]
        },
        output_path=os.path.join(base_dir, "fig5_punsafe_concurrency.svg"),
        y_max=1.05
    )

    # Fig 6: Algorithmic Search Comparisons vs Concurrency Scale (n)
    create_svg_line_chart(
        title="Fig 6: Safety Algorithm Comparisons vs. Concurrency Scale (n)",
        x_label="Process Count (n)",
        y_label="Vector Comparisons per Safety Test",
        x_vals=["5", "10", "15", "25", "50"],
        series={
            "Uniform (Low Var)": [20.3, 40.2, 60.0, 100.0, 200.0],
            "Bimodal (Whale-Guppy)": [24.2, 46.8, 64.2, 100.5, 200.0]
        },
        output_path=os.path.join(base_dir, "fig6_search_cost_concurrency.svg"),
        y_max=220.0
    )

    # Fig 7: P(Unsafe) across Demand Variability Workload Types
    create_svg_line_chart(
        title="Fig 7: Unsafe Probability across Workload Demand Variability (E1)",
        x_label="Workload Distribution Type",
        y_label="Probability of Reaching Unsafe State",
        x_vals=["Uniform", "Normal", "Pareto", "Bimodal"],
        series={
            "Greedy Allocator": [0.000, 0.000, 0.000, 0.133]
        },
        output_path=os.path.join(base_dir, "fig7_punsafe_distributions.svg"),
        y_max=0.20
    )

    # Fig 8: P(Unsafe) and Denial vs Process Holding Duration (tau)
    create_svg_line_chart(
        title="Fig 8: Impact of Holding Duration (tau) on Safety Metrics (Bimodal)",
        x_label="Holding Duration tau (steps)",
        y_label="P(Unsafe) / Denial Rate (Bimodal)",
        x_vals=["1", "3", "6", "10", "16"],
        series={
            "P(Unsafe) [Greedy]": [0.200, 0.167, 0.133, 0.200, 0.200],
            "Banker's Denial Rate": [0.001, 0.001, 0.001, 0.001, 0.002]
        },
        output_path=os.path.join(base_dir, "fig8_hold_duration_effects.svg"),
        y_max=0.25
    )
    print("All 8 figures successfully generated!")

if __name__ == "__main__":
    generate_all_plots()