-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBILP-Q_benchmark.py
256 lines (197 loc) · 11.6 KB
/
BILP-Q_benchmark.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
from Utils_CSG import *
from Utils_Solvers import *
def running_dwave(linear, quadratic, exact_solution, colnames,
params={'distr':'', 'n':0}, n_runs=1000):
"""
Solve the experimental input instance using the dwave device
:params
linear: dictionary of linear coefficient terms in the QUBO formulation of the CSG problem.
quadratic: dictionary of quadratic coefficient terms in the QUBO formulation of the CSG problem.
exact_solution: This is the exact solution of the input problem instance for verifying the output_ from dwave system.
colnames: list of column headers for generating a report using a standard schema.
:return
row: pandas Series object consisting of outputs and input distribution name and agents to generate the final report.
sample_set_dwave: input parameters used for solving the problem instance using dwave system.
"""
print(f'running dwave - {params["distr"]} - {params["n"]}')
sample_set_dwave = dwave_solver(linear, quadratic, runs=n_runs)
solution, fval, prob, rank, time_run = results_from_dwave(sample_set_dwave)
flag = exact_solution == solution
row = pd.Series([params['distr'], params['n'], solution, None, fval, prob,
rank, time_run, 'dwave', flag, None, None], index=colnames)
return row, sample_set_dwave
def running_dwave_exact(linear, quadratic, exact_solution, colnames,
params={'distr':'', 'n':0}):
"""
Solve the experimental data input using the dwave device exactly
:params
linear: dictionary of linear coefficient terms in the QUBO formulation of the CSG problem.
quadratic: dictionary of quadratic coefficient terms in the QUBO formulation of the CSG problem.
exact_solution: This is the exact solution of the input problem instance for verifying the output_ from dwave system.
colnames: list of column headers for generating a report using a standard schema.
:return
row: pandas Series object consisting of outputs and input distribution name and agents to generate the final report.
sample_set_dwave: input parameters used for solving the problem instance using dwave system.
"""
print(f'exact dwave - {params["distr"]} - {params["n"]}')
start = time.time()
sample_set_exact = exact_solver(linear, quadratic)
end = time.time()
time_run = end - start
solution, fval, prob, rank, _ = results_from_dwave(sample_set_exact, exact=True)
flag = exact_solution == solution
row = pd.Series([params['distr'], params['n'], solution, None, fval, prob,
rank, time_run, 'exact dwave', flag, None, None], index=colnames)
return row, sample_set_exact
def running_QAOA(linear, quadratic, exact_solution, colnames,
params={'distr':'', 'n':0}, n_init=20, p_list=np.arange(1,20)):
"""
Solve the experimental data input using the QAOA
:params
linear: dictionary of linear coefficient terms in the QUBO formulation of the CSG problem.
quadratic: dictionary of quadratic coefficient terms in the QUBO formulation of the CSG problem.
exact_solution: This is the exact solution of the input problem instance for verifying the output_ from dwave system.
colnames: list of column headers for generating a report using a standard schema.
params: input parameterization for qiskit's QAOA
:return
row: pandas Series object consisting of outputs and input distribution name and agents to generate the final report.
sample_set_dwave: input parameters used for solving the problem instance using dwave system.
"""
print(f'running qaoa - {params["distr"]} - {params["n"]}')
qaoa_result, p, init, _ = QAOA_optimization(linear, quadratic, n_init=n_init, p_list=p_list)
solution, fval, prob, rank, time_run = results_from_QAOA(qaoa_result)
flag = sum(exact_solution == solution)==len(solution)
print(exact_solution, '\n', solution)
row = pd.Series([params['distr'], params['n'], solution, p, fval, prob,
rank+1, time_run, 'QAOA', flag, None, None], index = colnames)
return row, qaoa_result, p, init
def run_all(distributions, n_agents, root_folder, penalty=None, dwave_runs = 1000,
create_file = True, seed=12345,
QAOA=True, dwave=True, exact=True, classical_BILP=True, folder='__'):
"""
Solve the experimental data input using the dwave device
:params
distributions: list of function names of distributions in Utils_CSG.py .
n_agents: list of integers mentioning the number of agents considered for experiments.
root_folder: root folder for the outputs.
penalty: hyper parameter to reduce contraint problem to unconstraint problem, large value is recommended as n_agents increase.
dwave_runs: number of runs for each instance on the dwave system.
create_file: Boolean value, if True, a new directory is created for each Distribution.
seed: seed value for numpy to generate random numbers.
QAOA: Boolean value to specify to coonsideration of QAOA for running the experiments.
dwave: Boolean value to specify to coonsideration of dwave system for running the experiments.
exact: Boolean value to specify to coonsideration of dwave system exactly for running the experiments.
classical_BILP: Boolean value to specify to coonsideration of classically solving equivalent BILP problem for running the experiments.
folder: subfolder inside root_folder to save the results
:return
None
"""
root_folder = os.path.join(root_folder, f'{seed}', folder)
create_dir(root_folder)
colnames = ["distribution", "n_agents", "solution", "p", "fval", "prob",
"rank", "time", "device", "flag", "time_bilp", "penalty"]
path_all = os.path.join(root_folder, 'all_results.csv')
qaoa_file, dwave_file, exact_dwave = 'qaoa.csv', 'dwave.csv', 'exact_dwave.csv'
# Create file
if create_file:
df_complete = pd.DataFrame(columns=colnames)
df_complete.to_csv(path_all, index=False)
for distribution in distributions:
distr = distribution.__name__
# Create file
create_dir(os.path.join(root_folder, distr))
path_distr = os.path.join(root_folder, distr, 'distr_results.csv')
if create_file:
df_distribution = pd.DataFrame(columns=colnames)
df_distribution.to_csv(path_distr, index=False)
for n in n_agents:
if penalty is None:
penalty=10**(n+1)
path = os.path.join(root_folder, distr, 'n_' + str(n))
create_dir(path, log=True)
create_dir(os.path.join(path, 'metadata'))
np.random.seed(seed=seed)
coalition_values, linear, quadratic = get_linear_quads(distribution, n, penalty)
if classical_BILP:
start = time.time()
exact_solution = list(map(np.float, list(solve_BILP_classical(coalition_values))))
end = time.time()
bilp_time = (end - start)*1000
else:
exact_solution = (2 ** n) * [0.]
bilp_time = None
## Dwave
if dwave:
row, sample_set = running_dwave(linear, quadratic, exact_solution, colnames,
n_runs=dwave_runs, params={'distr':distr, 'n':n})
row['time_bilp'] = bilp_time
row['penalty'] = penalty
row = pd.DataFrame(row).transpose()
row.to_csv(os.path.join(path, dwave_file), mode='a', index=False)
row.to_csv(path_distr, mode='a', index=False, header=False)
row.to_csv(path_all, mode='a', index=False, header=False)
sample_set.to_pandas_dataframe().to_csv(os.path.join(path, 'metadata', 'dwave_distr.csv'), index=False)
## Exact Dwave
if exact:
row, sample_set = running_dwave_exact(linear, quadratic,
exact_solution, colnames, params={'distr':distr, 'n':n})
row['time_bilp'] = bilp_time
row['penalty'] = penalty
row = pd.DataFrame(row).transpose()
row.to_csv(os.path.join(path, exact_dwave), mode='a', index=False)
row.to_csv(path_distr, mode='a', index=False, header=False)
row.to_csv(path_all, mode='a', index=False, header=False)
sample_set.to_pandas_dataframe().to_csv(os.path.join(path, 'metadata', 'exact_dwave_distr.csv'), index=False)
# QAOA
if QAOA:
row, qaoa_result, p, init = running_QAOA(linear, quadratic,
exact_solution, colnames, params={'distr':distr, 'n':n})
row['time_bilp'] = bilp_time
row['penalty'] = penalty
row = pd.DataFrame(row).transpose()
row.to_csv(os.path.join(path, qaoa_file), mode='a', index=False)
row.to_csv(path_distr, mode='a', index=False, header=False)
row.to_csv(path_all, mode='a', index=False, header=False)
# ----------------------------------------------------------------------- #
## Save metadata QAOA
df, data_solution = ranking_results_QAOA(qaoa_result, exact_solution)
df.to_csv(os.path.join(path, 'metadata', 'qaoa_distr.csv'), index=False)
with open(os.path.join(path, 'metadata', 'qaoa_metadata.txt'), "w") as output:
output.write(f'coalition_values: {coalition_values} \n')
output.write(f'linear: {linear} \n')
output.write(f'quadratic: {quadratic} \n')
output.write(f'p: {p} \n')
output.write(f'init: {init} \n')
output.write(f'solution: {data_solution} \n')
if __name__=="__main__":
# import shutil
# shutil.rmtree(r'output_')
seed = 12
root = 'output'
create_dir(root)
# Running the experiments for all distributions, with 2 and 3 agents
distributions = [Agent_based_uniform, Agent_based_normal, Modified_uniform_distribution,
Normal_distribution, Weibull_distribution, Weighted_random_with_chisquare,
F_distribution, Laplace_or_double_exponential, Rayleigh_distribution,
SVA_BETA_distribution]
n_agents = [2,3]
penalty = 100
run_all(distributions, n_agents, root, penalty=penalty,
create_file=True, seed=seed, QAOA=True, dwave=True, exact=True,
classical_BILP=True, folder='QAOA_QA_23')
penalty = 1000
run_all(distributions, n_agents, root, penalty=penalty,
create_file=True, seed=seed, QAOA=True, dwave=True, exact=True,
classical_BILP=True, folder='QAOA_QA_23')
# ----------------------------------------------------------------------------- #
# Running the Quantum Annealing (Dwave) solution for all distributions, from 2 to 7 agents
distributions = [Agent_based_uniform, Agent_based_normal, Modified_uniform_distribution,
Normal_distribution, Weibull_distribution, Weighted_random_with_chisquare,
F_distribution, Laplace_or_double_exponential, Rayleigh_distribution,
SVA_BETA_distribution]
for i in range(3,8):
n_agents = [4, 5, 6, 7]
penalty = 10**i
run_all(distributions, n_agents, root, penalty=penalty, dwave_runs=1000,
create_file=True, seed=seed, QAOA=False, dwave=True, exact=False, classical_BILP=False,
folder=f'QA_hyper_params_47_{i}')