You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

625 lines
22 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #!/usr/bin/env python3
  2. import util.script as script
  3. import util.queries as queries
  4. import dimod
  5. import random
  6. import numpy as np
  7. from tqdm import tqdm
  8. def main():
  9. #instance_parameters()
  10. #wmis_results()
  11. #wmis_siman_results_alpha_num_of_assignments()
  12. wmis_qpu_results_alpha_num_of_assignments()
  13. #primitive_2_siman_results_alpha_num_of_assignments()
  14. #primitive_5_siman_results_alpha_num_of_assignments()
  15. #primitive_5_qpu_results_alpha_num_of_assignments()
  16. #primitive_8_siman_results_alpha_num_of_assignments()
  17. #wmis_2_qpu_results()
  18. #minisat_runs()
  19. #wmis_engergy()
  20. #wmis_2_engergy()
  21. def instance_parameters():
  22. edb = script.connect_to_experimetns_db()
  23. edb_cursor = edb.cursor()
  24. idb = script.connect_to_instance_pool()
  25. instances = queries.Instance_scope_query(idb)
  26. instances.query("c42_vLogistic_6")
  27. insert_row = ("INSERT INTO c42_vLogistic_6_instances "
  28. "(instance_id, "
  29. " number_of_clauses, "
  30. " number_of_variables) "
  31. "VALUES (%s, %s, %s)")
  32. for instance, instance_id in tqdm(instances):
  33. edb_cursor.execute(insert_row, (str(instance_id),
  34. int(instance.getNumberOfClauses()),
  35. int(instance.getNumberOfVariables())))
  36. edb.commit()
  37. edb_cursor.close()
  38. edb.close()
  39. def wmis_siman_results():
  40. db = script.connect_to_instance_pool()
  41. q = queries.WMIS_result_scope_query_raw(db)
  42. q.query("c45_v[5-45]_1", "wmis_siman_results")
  43. for i in range(2):
  44. result = q.__next__()
  45. sample_set = queries.read_raw_wmis_sample_set(result["data"])
  46. data = script.analyze_wmis_sample(sample_set.first)
  47. print(data)
  48. def wmis_siman_results_alpha_num_of_assignments():
  49. edb = script.connect_to_experimetns_db()
  50. edb_cursor = edb.cursor()
  51. idb = script.connect_to_instance_pool()
  52. q = queries.WMIS_result_scope_query_raw(idb)
  53. q.query("c42_vLogistic_6", "wmis_siman_results")
  54. insert_row = ("INSERT INTO c42_vLogistic_6_wmis_siman_results "
  55. "(result_id, "
  56. " run, "
  57. " instance_id, "
  58. " number_of_found_assignments, "
  59. " chain_break_fraction, "
  60. " num_occurrences, "
  61. " energy, "
  62. " satisfiable) "
  63. "VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ")
  64. run = int(input("run: "))
  65. for result in tqdm(q):
  66. if "run" in result and result["run"] == run:
  67. #if result["run"] == run:
  68. sample_set = queries.read_raw_wmis_sample_set(result["data"])
  69. data = script.analyze_wmis_sample(sample_set.first)
  70. sat = queries.get_instance_by_id(idb["instances"], result["instance"])
  71. model = script.majority_vote_sample(sample_set.first.sample)
  72. isSatisfiable = sat.checkAssignment(model)
  73. edb_cursor.execute(insert_row, (str(result["_id"]),
  74. int(result["run"]),
  75. str(result["instance"]),
  76. int(data["number_of_assignments"]),
  77. float(data["chain_break_fraction"]),
  78. int(data["num_occurrences"]),
  79. int(data["energy"]),
  80. isSatisfiable))
  81. edb.commit()
  82. edb_cursor.close()
  83. edb.close()
  84. def primitive_2_siman_results_alpha_num_of_assignments():
  85. edb = script.connect_to_experimetns_db()
  86. edb_cursor = edb.cursor()
  87. idb = script.connect_to_instance_pool()
  88. q = queries.WMIS_result_scope_query_raw(idb)
  89. q.query("c42_vLogistic_6", "primitive_isings_2_siman_results")
  90. insert_row = ("INSERT INTO c42_vLogistic_6_primitive_2_siman_results "
  91. "(result_id, "
  92. " run, "
  93. " instance_id, "
  94. " chain_break_fraction, "
  95. " num_occurrences, "
  96. " energy, "
  97. " satisfiable) "
  98. "VALUES (%s, %s, %s, %s, %s, %s, %s) ")
  99. for result in tqdm(q):
  100. sample_set = queries.read_raw_primitive_ising_sample_set(result["data"])
  101. data = script.analyze_wmis_sample(sample_set.first)
  102. sat = queries.get_instance_by_id(idb["instances"], result["instance"])
  103. model = queries.extract_primitive_ising_model(sample_set.first.sample)
  104. isSatisfiable = sat.checkAssignment(model)
  105. edb_cursor.execute(insert_row, (str(result["_id"]),
  106. int(result["run"]),
  107. str(result["instance"]),
  108. float(data["chain_break_fraction"]),
  109. int(data["num_occurrences"]),
  110. int(data["energy"]),
  111. isSatisfiable))
  112. edb.commit()
  113. edb_cursor.close()
  114. edb.close()
  115. def primitive_5_siman_results_alpha_num_of_assignments():
  116. edb = script.connect_to_experimetns_db()
  117. edb_cursor = edb.cursor()
  118. idb = script.connect_to_instance_pool()
  119. q = queries.WMIS_result_scope_query_raw(idb)
  120. q.query("c42_vLogistic_6", "primitive_isigns_5_siman")
  121. insert_row = ("INSERT INTO c42_vLogistic_6_primitive_5_siman_results "
  122. "(result_id, "
  123. " run, "
  124. " instance_id, "
  125. " chain_break_fraction, "
  126. " num_occurrences, "
  127. " energy, "
  128. " satisfiable, "
  129. " num_conflicts, "
  130. " monte_carlo_steps) "
  131. "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) ")
  132. for result in tqdm(q):
  133. sample_set = queries.read_raw_primitive_ising_sample_set(result["data"])
  134. data = script.analyze_wmis_sample(sample_set.first)
  135. sat = queries.get_instance_by_id(idb["instances"], result["instance"])
  136. post_process_results = __post_process_prim_5_sample(sat, sample_set.first.sample)
  137. #print(post_process_results)
  138. edb_cursor.execute(insert_row, (str(result["_id"]),
  139. int(result["run"]),
  140. str(result["instance"]),
  141. float(data["chain_break_fraction"]),
  142. int(data["num_occurrences"]),
  143. int(data["energy"]),
  144. bool(post_process_results["satisfiable"]),
  145. int(len(post_process_results["conflicts"])),
  146. int(post_process_results["monte_carlo_steps"])))
  147. edb.commit()
  148. edb_cursor.close()
  149. edb.close()
  150. def primitive_5_qpu_results_alpha_num_of_assignments():
  151. edb = script.connect_to_experimetns_db()
  152. edb_cursor = edb.cursor()
  153. idb = script.connect_to_instance_pool()
  154. q = queries.WMIS_result_scope_query_raw(idb)
  155. q.query("c42_vLogistic_6", "primitive_isings_5_qpu")
  156. insert_row = ("INSERT INTO c42_vLogistic_6_primitive_5_qpu_results "
  157. "(result_id, "
  158. " run, "
  159. " instance_id, "
  160. " chain_break_fraction, "
  161. " num_occurrences, "
  162. " energy, "
  163. " satisfiable, "
  164. " num_conflicts, "
  165. " monte_carlo_steps, "
  166. " anneal_time) "
  167. "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ")
  168. run = int(input("run: "))
  169. for result in tqdm(q):
  170. if True:
  171. #if result["run"] == run:
  172. sample_set = queries.read_raw_primitive_ising_sample_set(result["data"])
  173. data = script.analyze_wmis_sample(sample_set.first)
  174. sat = queries.get_instance_by_id(idb["instances"], result["instance"])
  175. post_process_results = __post_process_prim_5_sample(sat,
  176. sample_set.first.sample)
  177. anneal_time = result["data"]["info"]["timing"]["qpu_anneal_time_per_sample"]
  178. #print(post_process_results)
  179. edb_cursor.execute(insert_row, (str(result["_id"]),
  180. int(result["run"]),
  181. str(result["instance"]),
  182. float(data["chain_break_fraction"]),
  183. int(data["num_occurrences"]),
  184. int(data["energy"]),
  185. bool(post_process_results["satisfiable"]),
  186. int(len(post_process_results["conflicts"])),
  187. int(post_process_results["monte_carlo_steps"]),
  188. int(anneal_time)))
  189. edb.commit()
  190. edb_cursor.close()
  191. edb.close()
  192. def primitive_8_siman_results_alpha_num_of_assignments():
  193. edb = script.connect_to_experimetns_db()
  194. edb_cursor = edb.cursor()
  195. idb = script.connect_to_instance_pool()
  196. q = queries.WMIS_result_scope_query_raw(idb)
  197. q.query("c42_vLogistic_6", "wmis_4_qubos_siman")
  198. insert_row = ("INSERT INTO c42_vLogistic_6_wmis_4_siman_results "
  199. "(result_id, "
  200. " run, "
  201. " instance_id, "
  202. " chain_break_fraction, "
  203. " num_occurrences, "
  204. " energy, "
  205. " satisfiable) "
  206. "VALUES (%s, %s, %s, %s, %s, %s, %s) ")
  207. run = int(input("run: "))
  208. for result in tqdm(q):
  209. if result["run"] == run:
  210. sample_set = queries.read_raw_primitive_ising_sample_set(result["data"])
  211. data = script.analyze_wmis_sample(sample_set.first)
  212. sat = queries.get_instance_by_id(idb["instances"], result["instance"])
  213. post_process_results = __post_process_prim_5_sample(sat,
  214. sample_set.first.sample)
  215. #print(post_process_results)
  216. edb_cursor.execute(insert_row, (str(result["_id"]),
  217. int(result["run"]),
  218. str(result["instance"]),
  219. float(data["chain_break_fraction"]),
  220. int(data["num_occurrences"]),
  221. int(data["energy"]),
  222. bool(post_process_results["satisfiable"])))
  223. edb.commit()
  224. edb_cursor.close()
  225. edb.close()
  226. def wmis_2_qpu_results():
  227. edb = script.connect_to_experimetns_db()
  228. edb_cursor = edb.cursor()
  229. idb = script.connect_to_instance_pool()
  230. q = queries.WMIS_result_scope_query_raw(idb)
  231. q.query("c42_vLogistic_6", "wmis_2_qubos_2_qpu")
  232. insert_row = ("INSERT INTO c42_vLogistic_6_wmis_2_vertex_eq_edge_qpu_results "
  233. "(result_id, "
  234. " run, "
  235. " instance_id, "
  236. " chain_break_fraction, "
  237. " num_occurrences, "
  238. " energy, "
  239. " satisfiable, "
  240. " anneal_time, "
  241. " energy_reach, "
  242. " sample_size) "
  243. "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ")
  244. run = int(input("run: "))
  245. for result in tqdm(q):
  246. if True:
  247. #if result["run"] == run:
  248. sample_set = queries.read_raw_primitive_ising_sample_set(result["data"])
  249. data = script.analyze_wmis_sample(sample_set.first)
  250. sat = queries.get_instance_by_id(idb["instances"], result["instance"])
  251. post_process_results = __post_process_prim_5_sample(sat,
  252. sample_set.first.sample)
  253. anneal_time = result["data"]["info"]["timing"]["qpu_anneal_time_per_sample"]
  254. #print(post_process_results)
  255. energy_reach = abs(sample_set.record["energy"].max() -
  256. sample_set.record["energy"].min())
  257. sample_size = np.sum(sample_set.record["num_occurrences"])
  258. edb_cursor.execute(insert_row, (str(result["_id"]),
  259. int(result["run"]),
  260. str(result["instance"]),
  261. float(data["chain_break_fraction"]),
  262. int(data["num_occurrences"]),
  263. int(data["energy"]),
  264. bool(post_process_results["satisfiable"]),
  265. int(anneal_time),
  266. int(energy_reach),
  267. int(sample_size)))
  268. edb.commit()
  269. edb_cursor.close()
  270. edb.close()
  271. def __post_process_prim_5_sample(sat, sample):
  272. post_process_results = {}
  273. assignments = {}
  274. vars = set()
  275. for node, energy in sample.items():
  276. if node[0] == "x":
  277. lit = int(node[1:])
  278. vars.add(abs(lit))
  279. assignments[lit] = energy
  280. conflicts = set()
  281. for var in vars:
  282. if var in assignments and -var in assignments:
  283. if assignments[var] == assignments[-var]:
  284. conflicts.add(var)
  285. model = [True for i in range(len(vars))]
  286. for var in vars:
  287. if var in assignments:
  288. model[var - 1] = True if assignments[var] == 1 else False
  289. elif -var in assignments:
  290. model[var - 1] = True if assignments[-var] == 0 else False
  291. #var_list = list(conflicts)
  292. #
  293. #monte_carlo_steps = 0
  294. #if len(conflicts) > 0:
  295. # for i in range(1000):
  296. # rand_var = random.choice(var_list)
  297. #
  298. # if sat.checkAssignment(model):
  299. # monte_carlo_steps
  300. # break
  301. #
  302. # model[rand_var - 1] = not model[rand_var - 1]
  303. #
  304. # monte_carlo_steps += 1
  305. post_process_results["conflicts"] = conflicts
  306. post_process_results["satisfiable"] = sat.checkAssignment(model)
  307. #post_process_results["monte_carlo_steps"] = monte_carlo_steps
  308. return post_process_results
  309. def wmis_qpu_results_alpha_num_of_assignments():
  310. edb = script.connect_to_experimetns_db()
  311. edb_cursor = edb.cursor()
  312. idb = script.connect_to_instance_pool()
  313. q = queries.WMIS_result_scope_query_raw(idb)
  314. q.query("c42_vLogistic_6", "wmis_qpu_results")
  315. insert_row = ("INSERT INTO c42_vLogistic_6_wmis_qpu_results "
  316. "(result_id, "
  317. " run, "
  318. " instance_id, "
  319. " number_of_found_assignments, "
  320. " chain_break_fraction, "
  321. " num_occurrences, "
  322. " energy, "
  323. " satisfiable, "
  324. " anneal_time, "
  325. " energy_reach, "
  326. " sample_size) "
  327. "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ")
  328. run = int(input("run: "))
  329. for result in tqdm(q):
  330. #if result["run"] == run:
  331. if True:
  332. sample_set = queries.read_raw_wmis_sample_set(result["data"])
  333. data = script.analyze_wmis_sample(sample_set.first)
  334. sat = queries.get_instance_by_id(idb["instances"], result["instance"])
  335. model = script.majority_vote_sample(sample_set.first.sample)
  336. isSatisfiable = sat.checkAssignment(model)
  337. anneal_time = result["data"]["info"]["timing"]["qpu_anneal_time_per_sample"]
  338. energy_reach = abs(sample_set.record["energy"].max() -
  339. sample_set.record["energy"].min())
  340. sample_size = np.sum(sample_set.record["num_occurrences"])
  341. edb_cursor.execute(insert_row, (str(result["_id"]),
  342. int(result["run"]),
  343. str(result["instance"]),
  344. int(data["number_of_assignments"]),
  345. float(data["chain_break_fraction"]),
  346. int(data["num_occurrences"]),
  347. int(data["energy"]),
  348. isSatisfiable,
  349. int(anneal_time),
  350. int(energy_reach),
  351. int(sample_size)))
  352. edb.commit()
  353. edb_cursor.close()
  354. edb.close()
  355. def wmis_engergy():
  356. edb = script.connect_to_experimetns_db()
  357. edb_cursor = edb.cursor()
  358. idb = script.connect_to_instance_pool()
  359. q = queries.WMIS_result_scope_query_raw(idb)
  360. q.query("c42_vLogistic_6", "wmis_qpu_results")
  361. insert_row = ("INSERT INTO c42_vLogistic_6_wmis_qpu_energy "
  362. "(result_id, "
  363. " sample_index, "
  364. " run, "
  365. " instance_id, "
  366. " energy, "
  367. " anneal_time, "
  368. " sample_size) "
  369. "VALUES (%s, %s, %s, %s, %s, %s, %s) ")
  370. run = int(input("run: "))
  371. for result in tqdm(q):
  372. #if result["run"] == run:
  373. if True:
  374. sample_set = queries.read_raw_wmis_sample_set(result["data"])
  375. anneal_time = result["data"]["info"]["timing"]["qpu_anneal_time_per_sample"]
  376. sample_size = np.sum(sample_set.record["num_occurrences"])
  377. count = 1
  378. for energy, in sample_set.data(fields=["energy"], sorted_by=("energy")):
  379. edb_cursor.execute(insert_row, (str(result["_id"]),
  380. int(count),
  381. int(result["run"]),
  382. str(result["instance"]),
  383. int(energy),
  384. int(anneal_time),
  385. int(sample_size)))
  386. count += 1
  387. edb.commit()
  388. edb_cursor.close()
  389. edb.close()
  390. def wmis_2_engergy():
  391. edb = script.connect_to_experimetns_db()
  392. edb_cursor = edb.cursor()
  393. idb = script.connect_to_instance_pool()
  394. q = queries.WMIS_result_scope_query_raw(idb)
  395. q.query("c42_vLogistic_6", "wmis_2_qubos_2_qpu")
  396. insert_row = ("INSERT INTO c42_vLogistic_6_wmis_2_vertex_eq_edge_qpu_energy "
  397. "(result_id, "
  398. " sample_index, "
  399. " run, "
  400. " instance_id, "
  401. " energy, "
  402. " anneal_time, "
  403. " sample_size) "
  404. "VALUES (%s, %s, %s, %s, %s, %s, %s) ")
  405. run = int(input("run: "))
  406. for result in tqdm(q):
  407. if result["run"] == run:
  408. #if True:
  409. sample_set = queries.read_raw_wmis_sample_set(result["data"])
  410. anneal_time = result["data"]["info"]["timing"]["qpu_anneal_time_per_sample"]
  411. sample_size = np.sum(sample_set.record["num_occurrences"])
  412. count = 1
  413. for energy, in sample_set.data(fields=["energy"], sorted_by=("energy")):
  414. edb_cursor.execute(insert_row, (str(result["_id"]),
  415. int(count),
  416. int(result["run"]),
  417. str(result["instance"]),
  418. int(energy),
  419. int(anneal_time),
  420. int(sample_size)))
  421. count += 1
  422. edb.commit()
  423. edb_cursor.close()
  424. edb.close()
  425. def minisat_runs():
  426. edb = script.connect_to_experimetns_db()
  427. edb_cursor = edb.cursor();
  428. idb = script.connect_to_instance_pool()
  429. runs = queries.Minisat_run_scope_query_raw(idb)
  430. runs.query("c42_vLogistic_6", "minisat_runs")
  431. insert_row = ("INSERT INTO c42_vLogistic_6_minisat_runs "
  432. "(run_id, "
  433. " instance_id, "
  434. " satisfiable) "
  435. "VALUES (%s, %s, %s) ")
  436. for run in tqdm(runs):
  437. data = script.analyde_minisat_run(run)
  438. edb_cursor.execute(insert_row, (str(run["_id"]),
  439. str(run["instance"]),
  440. int(data["satisfiable"])))
  441. edb.commit()
  442. edb_cursor.close()
  443. edb.close()
  444. def wmis_results():
  445. edb = script.connect_to_experimetns_db()
  446. edb_cursor = edb.cursor();
  447. idb = script.connect_to_instance_pool()
  448. q = queries.WMIS_result_scope_query_raw(idb)
  449. q.query("c42_v[5-84]_1", "wmis_qbsolv_results")
  450. for i in range(501):
  451. q.__next__()
  452. res = q.__next__()
  453. sample_set = queries.read_raw_wmis_sample_set(res["data"])
  454. model = script.majority_vote_sample(sample_set.first.sample)
  455. sat = queries.get_instance_by_id(idb["instances"], res["instance"])
  456. print(model)
  457. print(sat.getNumberOfVariables())
  458. print(sat.checkAssignment(model))
  459. if __name__ == "__main__":
  460. main()