-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
143 lines (112 loc) · 4.2 KB
/
plot.py
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
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 2 23:26:46 2022
@author: yifan, zhaonan
"""
import os
import regex
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
TIME_START = 0
TIME_STOP = 1000
TIME_RESOLUTION = 200
CUTOFF_TIMES = np.linspace(TIME_START, TIME_STOP, TIME_RESOLUTION)
QUALITIES_START = 2200
QUALITIES_STOP = 2260
QUALITIES_RESOLUTION = 200
QUALITIES = np.linspace(QUALITIES_START, QUALITIES_STOP, QUALITIES_RESOLUTION)
CUTOFF_TIMES_LOOSE = [10, 40, 100, 1000]
QUALITIES_LOOSE = [2200, 2210, 2220, 2230, 2240]
OPT = {"power": 2203, "star2": 4542}
# %% functions
def load_trace_file(filename):
trace = pd.read_csv(filename, header=None)
trace.columns = ["time", "quality"]
return trace
def load_trace_files(dir, graph, algo):
pattern = f"{graph}_{algo}_[\S\s]*\.trace"
traces = []
for path, subdirs, files in os.walk(dir):
for file in files:
if regex.match(pattern, file):
traces.append(load_trace_file(path+"\\"+file))
return pd.concat(traces)
def calc_qrtd(graph, trace: pd.DataFrame, cutoff_times, qualities):
qrtd = pd.DataFrame(index=cutoff_times, columns=qualities)
for cutoff_time in cutoff_times:
for quality in qualities:
n_runs = len(trace[(((trace.quality-OPT[graph])/OPT[graph]) <= quality)].axes[0])
n_success = len(trace[(trace.time <= cutoff_time) & (
((trace.quality-OPT[graph])/OPT[graph]) <= quality)].axes[0])
qrtd.at[cutoff_time, quality] = n_success / \
n_runs if n_runs != 0 else np.nan
return qrtd
def calc_sqd(graph, trace, cutoff_times, qualities):
sqd = pd.DataFrame(index=qualities, columns=cutoff_times)
for cutoff_time in cutoff_times:
for quality in qualities:
n_runs = len(trace[(trace.time <= cutoff_time)].axes[0])
n_success = len(trace[(trace.time <= cutoff_time) & (((trace.quality-OPT[graph])/OPT[graph])<= quality)].axes[0])
sqd.at[quality, cutoff_time] = n_success / \
n_runs if n_runs != 0 else np.nan
return sqd
def plot_boxplot(trace):
fig, ax1 = plt.subplots()
trace
plt.boxplot(trace,)
plt.show()
def plot_qrtd(qrtd: pd.DataFrame):
fig, ax1 = plt.subplots()
for column in qrtd.columns:
qrtd.plot(y=column, ax=ax1, logx=True)
plt.title(f"QRTD plot for {algo} on {graph}.graph")
plt.xlabel("cutoff time/s")
plt.ylabel(r"$P_{solve}$")
plt.show()
def plot_sqd(sqd: pd.DataFrame):
fig, ax1 = plt.subplots()
for column in sqd.columns:
sqd.plot(y=column, ax=ax1)
plt.title(f"SQD plot for {algo} on {graph}.graph")
plt.xlabel("quality")
plt.ylabel(r"$P_{solve}$")
plt.show()
def get_cutoff_times(start, stop, resolution, mode):
if mode=="linear":
return np.round(np.linspace(start, stop, resolution),decimals=2)
elif mode=="log":
return np.round(np.geomspace(stop**(1/resolution), stop, num=resolution),decimals=2)
else:
raise ValueError("mode not supported")
def get_qualities(start, stop, resolution, mode):
if mode=="linear":
return np.round(np.linspace(start, stop, resolution),decimals=4)
elif mode=="log":
return np.round(np.geomspace(start, stop, num=resolution),decimals=4)
else:
raise ValueError("mode not supported")
# %% load trace
# import trace file from different runs
filename = r"E:\Users\wizar\OneDrive\Documents\github\CSE-6140-project\plotting\star\power_LS1_1000_20221203.trace"
dirname = "."
graph = "power"
algo = "LS1"
# trace=load_trace_file(filename)
trace = load_trace_files(dirname, graph, algo)
# print(trace.columns)
# %% calculation
# Qualified Runtime for various solution qualities
# max_q=(max(trace["quality"])-OPT[graph])/OPT[graph]
# print(max_q)
# min_q=(min(trace["quality"])-OPT[graph])/OPT[graph]
# print(min_q)
# qrtd = calc_qrtd(graph, trace, get_cutoff_times(0, 1000, 200, "log"), get_qualities(min_q,max_q,5,"linear"))
# print(qrtd)
# Solution Quality Distributions for various run-times
# sqd = calc_sqd(graph, trace, get_cutoff_times(0, 1000, 5, "log"), get_qualities(min_q,max_q,200,"linear"))
# print(sqd)
# %% plot
# plot_qrtd(qrtd)
# plot_sqd(sqd)
plot_boxplot(trace)