-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_statistics.py
223 lines (124 loc) · 4.53 KB
/
make_statistics.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import pickle
import numpy as np
import os
import csv
from pathlib import Path
import dimod
from AGV_quantum import get_results
def read_single_file(size, k, solver):
file = f"annealing_results/{size}_AGV/new_{solver}_{k}.pkl"
with open (file, "rb") as f:
d = pickle.load(f)
return d
def feasibility_perc_cqm(examples, size, k):
cwd = os.getcwd()
lp_folder = Path(f"lp_files/lp_{examples[size]}.pkl")
with open(os.path.join(cwd, lp_folder), "rb") as f:
lp = pickle.load(f)
sampleset = read_smapleset(size, k, "cqm")
solutions = get_results(sampleset, lp)
return sum(sol["feasible"] for sol in solutions)/len(solutions)
def objective_cqm(examples, size, k):
cwd = os.getcwd()
lp_folder = Path(f"lp_files/lp_{examples[size]}.pkl")
with open(os.path.join(cwd, lp_folder), "rb") as f:
lp = pickle.load(f)
sampleset = read_smapleset(size, k, "cqm")
solutions = get_results(sampleset, lp)
solutions = sorted(solutions, key=lambda d: d["feasible"], reverse=True)
return solutions[0]["objective"]
def obj_cqm_array(examples, size):
return np.array([objective_cqm(examples, size, k) for k in range(1,11)])
def feas_cqm_array(examples, size):
return np.array([feasibility_perc_cqm(examples, size, k) for k in range(1,11)])
def read_series(size, solver, key):
return np.array([read_smapleset(size, k, solver).info[key] for k in range(1,11)])
def read_smapleset(size, k, solver):
sampleset = read_single_file(size, k, solver)
sampleset = dimod.SampleSet.from_serializable(sampleset)
return sampleset
def print_info(key, sizes):
print(key)
no_vars = []
means = []
stds = []
for size in sizes:
vars = get_no_vars(size)
series = read_series(size, "cqm", key)
if key == "qpu_access_time":
k = 1000
elif key == "run_time":
k=1000000
else:
k = 1
m = np.mean(series/k)
s = np.std(series/k)
no_vars.append(vars)
means.append(m)
stds.append(s)
print(vars, m, m-s, m+s)
return no_vars, means, stds
def print_obj(sizes, examples, optimum):
no_vars = []
means = []
stds = []
print("objective")
for size in sizes:
obj = obj_cqm_array(examples, size)
obj = obj/optimum[size]
vars = get_no_vars(size)
m = np.mean(obj)
s = np.std(obj)
print(vars, m,m-s, m+s)
no_vars.append(vars)
means.append(m)
stds.append(s)
print(obj)
print(np.mean(obj), np.std(obj))
return no_vars, means, stds
def print_feas(sizes, examples):
no_vars = []
means = []
stds = []
print("feasibility perc")
for size in sizes:
vars = get_no_vars(size)
perc = feas_cqm_array(examples, size)
m = np.mean(perc)
s = np.std(perc)
print(vars, m,m-s, m+s)
no_vars.append(vars)
means.append(m)
stds.append(s)
return no_vars, means, stds
def csv_write(file_name, no_vars, means, stds):
with open(file_name, 'w', newline='', encoding="utf-8") as csvfile:
fieldnames = ["no_vars", "mean", "mean-std", "mean+std"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
for i,v in enumerate(no_vars):
m = means[i]
s = stds[i]
writer.writerow({"no_vars": v, "mean": np.round(m, 4), "mean-std": np.round(m-s, 4), "mean+std": np.round(m+s, 4)})
def get_no_vars(size):
vars = read_smapleset(size, 1, "cqm").variables
return (len(vars))
if __name__ == "__main__":
#sizes = [2,4,6,7,12,15,21]
optimum = {2:4, 4: 8.2, 6: 3.22, 7: 4.25, 12: 9.175, 15: 10.975, 21: 17.625}
examples = {2:"smallest", 4:"small", 6:"medium_small", 7:"medium", 12:"large", 15:"largest", 21:"largest_ever"}
sizes = examples.keys()
key = 'qpu_access_time'
no_vars, means, stds = print_info(key, sizes)
file = "article_plots/CQM_QPU_time.csv"
csv_write(file, no_vars, means, stds)
key = 'run_time'
no_vars, means, stds = print_info(key, sizes)
file = "article_plots/time_CQM.csv"
csv_write(file, no_vars, means, stds)
no_vars, means, stds = print_feas(sizes, examples)
file = "article_plots/feasibility_CQM.csv"
csv_write(file, no_vars, means, stds)
sizes = examples.keys()
no_vars, means, stds = print_obj(sizes, examples, optimum)
file = "article_plots/obj_CQM.csv"
csv_write(file, no_vars, means, stds)