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.

456 lines
16 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
  1. #!/usr/bin/env python3
  2. import util.script as script
  3. import util.queries as queries
  4. import dimod
  5. import random
  6. from tqdm import tqdm
  7. def main():
  8. #instance_parameters()
  9. #wmis_results()
  10. wmis_siman_results_alpha_num_of_assignments()
  11. #wmis_qpu_results_alpha_num_of_assignments()
  12. #primitive_2_siman_results_alpha_num_of_assignments()
  13. #primitive_5_siman_results_alpha_num_of_assignments()
  14. #primitive_5_qpu_results_alpha_num_of_assignments()
  15. #primitive_8_siman_results_alpha_num_of_assignments()
  16. #minisat_runs()
  17. def instance_parameters():
  18. edb = script.connect_to_experimetns_db()
  19. edb_cursor = edb.cursor()
  20. idb = script.connect_to_instance_pool()
  21. instances = queries.Instance_scope_query(idb)
  22. instances.query("c42_vLogistic_6")
  23. insert_row = ("INSERT INTO c42_vLogistic_6_instances "
  24. "(instance_id, "
  25. " number_of_clauses, "
  26. " number_of_variables) "
  27. "VALUES (%s, %s, %s)")
  28. for instance, instance_id in tqdm(instances):
  29. edb_cursor.execute(insert_row, (str(instance_id),
  30. int(instance.getNumberOfClauses()),
  31. int(instance.getNumberOfVariables())))
  32. edb.commit()
  33. edb_cursor.close()
  34. edb.close()
  35. def wmis_siman_results():
  36. db = script.connect_to_instance_pool()
  37. q = queries.WMIS_result_scope_query_raw(db)
  38. q.query("c45_v[5-45]_1", "wmis_siman_results")
  39. for i in range(2):
  40. result = q.__next__()
  41. sample_set = queries.read_raw_wmis_sample_set(result["data"])
  42. data = script.analyze_wmis_sample(sample_set.first)
  43. print(data)
  44. def wmis_siman_results_alpha_num_of_assignments():
  45. edb = script.connect_to_experimetns_db()
  46. edb_cursor = edb.cursor()
  47. idb = script.connect_to_instance_pool()
  48. q = queries.WMIS_result_scope_query_raw(idb)
  49. q.query("c42_vLogistic_6", "wmis_qubos_2_siman")
  50. insert_row = ("INSERT INTO c42_vLogistic_6_wmis_1_2_siman_results "
  51. "(result_id, "
  52. " run, "
  53. " instance_id, "
  54. " number_of_found_assignments, "
  55. " chain_break_fraction, "
  56. " num_occurrences, "
  57. " energy, "
  58. " satisfiable) "
  59. "VALUES (%s, %s, %s, %s,wa %s, %s, %s, %s) ")
  60. for result in tqdm(q):
  61. sample_set = queries.read_raw_wmis_sample_set(result["data"])
  62. data = script.analyze_wmis_sample(sample_set.first)
  63. sat = queries.get_instance_by_id(idb["instances"], result["instance"])
  64. model = script.majority_vote_sample(sample_set.first.sample)
  65. isSatisfiable = sat.checkAssignment(model)
  66. edb_cursor.execute(insert_row, (str(result["_id"]),
  67. int(result["run"]),
  68. str(result["instance"]),
  69. int(data["number_of_assignments"]),
  70. float(data["chain_break_fraction"]),
  71. int(data["num_occurrences"]),
  72. int(data["energy"]),
  73. isSatisfiable))
  74. edb.commit()
  75. edb_cursor.close()
  76. edb.close()
  77. def primitive_2_siman_results_alpha_num_of_assignments():
  78. edb = script.connect_to_experimetns_db()
  79. edb_cursor = edb.cursor()
  80. idb = script.connect_to_instance_pool()
  81. q = queries.WMIS_result_scope_query_raw(idb)
  82. q.query("c42_vLogistic_6", "primitive_isings_2_siman_results")
  83. insert_row = ("INSERT INTO c42_vLogistic_6_primitive_2_siman_results "
  84. "(result_id, "
  85. " run, "
  86. " instance_id, "
  87. " chain_break_fraction, "
  88. " num_occurrences, "
  89. " energy, "
  90. " satisfiable) "
  91. "VALUES (%s, %s, %s, %s, %s, %s, %s) ")
  92. for result in tqdm(q):
  93. sample_set = queries.read_raw_primitive_ising_sample_set(result["data"])
  94. data = script.analyze_wmis_sample(sample_set.first)
  95. sat = queries.get_instance_by_id(idb["instances"], result["instance"])
  96. model = queries.extract_primitive_ising_model(sample_set.first.sample)
  97. isSatisfiable = sat.checkAssignment(model)
  98. edb_cursor.execute(insert_row, (str(result["_id"]),
  99. int(result["run"]),
  100. str(result["instance"]),
  101. float(data["chain_break_fraction"]),
  102. int(data["num_occurrences"]),
  103. int(data["energy"]),
  104. isSatisfiable))
  105. edb.commit()
  106. edb_cursor.close()
  107. edb.close()
  108. def primitive_5_siman_results_alpha_num_of_assignments():
  109. edb = script.connect_to_experimetns_db()
  110. edb_cursor = edb.cursor()
  111. idb = script.connect_to_instance_pool()
  112. q = queries.WMIS_result_scope_query_raw(idb)
  113. q.query("c42_vLogistic_6", "primitive_isigns_5_siman")
  114. insert_row = ("INSERT INTO c42_vLogistic_6_primitive_5_siman_results "
  115. "(result_id, "
  116. " run, "
  117. " instance_id, "
  118. " chain_break_fraction, "
  119. " num_occurrences, "
  120. " energy, "
  121. " satisfiable, "
  122. " num_conflicts, "
  123. " monte_carlo_steps) "
  124. "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) ")
  125. for result in tqdm(q):
  126. sample_set = queries.read_raw_primitive_ising_sample_set(result["data"])
  127. data = script.analyze_wmis_sample(sample_set.first)
  128. sat = queries.get_instance_by_id(idb["instances"], result["instance"])
  129. post_process_results = __post_process_prim_5_sample(sat, sample_set.first.sample)
  130. #print(post_process_results)
  131. edb_cursor.execute(insert_row, (str(result["_id"]),
  132. int(result["run"]),
  133. str(result["instance"]),
  134. float(data["chain_break_fraction"]),
  135. int(data["num_occurrences"]),
  136. int(data["energy"]),
  137. bool(post_process_results["satisfiable"]),
  138. int(len(post_process_results["conflicts"])),
  139. int(post_process_results["monte_carlo_steps"])))
  140. edb.commit()
  141. edb_cursor.close()
  142. edb.close()
  143. def primitive_5_qpu_results_alpha_num_of_assignments():
  144. edb = script.connect_to_experimetns_db()
  145. edb_cursor = edb.cursor()
  146. idb = script.connect_to_instance_pool()
  147. q = queries.WMIS_result_scope_query_raw(idb)
  148. q.query("c42_vLogistic_6", "primitive_isings_5_qpu")
  149. insert_row = ("INSERT INTO c42_vLogistic_6_primitive_5_qpu_results "
  150. "(result_id, "
  151. " run, "
  152. " instance_id, "
  153. " chain_break_fraction, "
  154. " num_occurrences, "
  155. " energy, "
  156. " satisfiable, "
  157. " num_conflicts, "
  158. " monte_carlo_steps, "
  159. " anneal_time) "
  160. "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ")
  161. run = int(input("run: "))
  162. for result in tqdm(q):
  163. if True:
  164. #if result["run"] == run:
  165. sample_set = queries.read_raw_primitive_ising_sample_set(result["data"])
  166. data = script.analyze_wmis_sample(sample_set.first)
  167. sat = queries.get_instance_by_id(idb["instances"], result["instance"])
  168. post_process_results = __post_process_prim_5_sample(sat,
  169. sample_set.first.sample)
  170. anneal_time = result["data"]["info"]["timing"]["qpu_anneal_time_per_sample"]
  171. #print(post_process_results)
  172. edb_cursor.execute(insert_row, (str(result["_id"]),
  173. int(result["run"]),
  174. str(result["instance"]),
  175. float(data["chain_break_fraction"]),
  176. int(data["num_occurrences"]),
  177. int(data["energy"]),
  178. bool(post_process_results["satisfiable"]),
  179. int(len(post_process_results["conflicts"])),
  180. int(post_process_results["monte_carlo_steps"]),
  181. int(anneal_time)))
  182. edb.commit()
  183. edb_cursor.close()
  184. edb.close()
  185. def primitive_8_siman_results_alpha_num_of_assignments():
  186. edb = script.connect_to_experimetns_db()
  187. edb_cursor = edb.cursor()
  188. idb = script.connect_to_instance_pool()
  189. q = queries.WMIS_result_scope_query_raw(idb)
  190. q.query("c42_vLogistic_6", "wmis_2_qubos_siman")
  191. insert_row = ("INSERT INTO c42_vLogistic_6_wmis_2_2_siman_results "
  192. "(result_id, "
  193. " run, "
  194. " instance_id, "
  195. " chain_break_fraction, "
  196. " num_occurrences, "
  197. " energy, "
  198. " satisfiable) "
  199. "VALUES (%s, %s, %s, %s, %s, %s, %s) ")
  200. run = int(input("run: "))
  201. for result in tqdm(q):
  202. if result["run"] == run:
  203. sample_set = queries.read_raw_primitive_ising_sample_set(result["data"])
  204. data = script.analyze_wmis_sample(sample_set.first)
  205. sat = queries.get_instance_by_id(idb["instances"], result["instance"])
  206. post_process_results = __post_process_prim_5_sample(sat,
  207. sample_set.first.sample)
  208. #print(post_process_results)
  209. edb_cursor.execute(insert_row, (str(result["_id"]),
  210. int(result["run"]),
  211. str(result["instance"]),
  212. float(data["chain_break_fraction"]),
  213. int(data["num_occurrences"]),
  214. int(data["energy"]),
  215. bool(post_process_results["satisfiable"])))
  216. edb.commit()
  217. edb_cursor.close()
  218. edb.close()
  219. def __post_process_prim_5_sample(sat, sample):
  220. post_process_results = {}
  221. assignments = {}
  222. vars = set()
  223. for node, energy in sample.items():
  224. if node[0] == "x":
  225. lit = int(node[1:])
  226. vars.add(abs(lit))
  227. assignments[lit] = energy
  228. conflicts = set()
  229. for var in vars:
  230. if var in assignments and -var in assignments:
  231. if assignments[var] == assignments[-var]:
  232. conflicts.add(var)
  233. model = [True for i in range(len(vars))]
  234. for var in vars:
  235. if var in assignments:
  236. model[var - 1] = True if assignments[var] == 1 else False
  237. elif -var in assignments:
  238. model[var - 1] = True if assignments[-var] == 0 else False
  239. var_list = list(conflicts)
  240. monte_carlo_steps = 0
  241. if len(conflicts) > 0:
  242. for i in range(1000):
  243. rand_var = random.choice(var_list)
  244. if sat.checkAssignment(model):
  245. monte_carlo_steps
  246. break
  247. model[rand_var - 1] = not model[rand_var - 1]
  248. monte_carlo_steps += 1
  249. post_process_results["conflicts"] = conflicts
  250. post_process_results["satisfiable"] = sat.checkAssignment(model)
  251. post_process_results["monte_carlo_steps"] = monte_carlo_steps
  252. return post_process_results
  253. def wmis_qpu_results_alpha_num_of_assignments():
  254. edb = script.connect_to_experimetns_db()
  255. edb_cursor = edb.cursor()
  256. idb = script.connect_to_instance_pool()
  257. q = queries.WMIS_result_scope_query_raw(idb)
  258. q.query("c42_vLogistic_6", "wmis_qpu_results")
  259. insert_row = ("INSERT INTO c42_vLogistic_6_wmis_qpu_results "
  260. "(result_id, "
  261. " run, "
  262. " instance_id, "
  263. " number_of_found_assignments, "
  264. " chain_break_fraction, "
  265. " num_occurrences, "
  266. " energy, "
  267. " satisfiable, "
  268. " anneal_time) "
  269. "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) ")
  270. run = int(input("run: "))
  271. for result in tqdm(q):
  272. if result["run"] == run:
  273. sample_set = queries.read_raw_wmis_sample_set(result["data"])
  274. data = script.analyze_wmis_sample(sample_set.first)
  275. sat = queries.get_instance_by_id(idb["instances"], result["instance"])
  276. model = script.majority_vote_sample(sample_set.first.sample)
  277. isSatisfiable = sat.checkAssignment(model)
  278. anneal_time = result["data"]["info"]["timing"]["qpu_anneal_time_per_sample"]
  279. edb_cursor.execute(insert_row, (str(result["_id"]),
  280. int(result["run"]),
  281. str(result["instance"]),
  282. int(data["number_of_assignments"]),
  283. float(data["chain_break_fraction"]),
  284. int(data["num_occurrences"]),
  285. int(data["energy"]),
  286. isSatisfiable,
  287. int(anneal_time)))
  288. edb.commit()
  289. edb_cursor.close()
  290. edb.close()
  291. def minisat_runs():
  292. edb = script.connect_to_experimetns_db()
  293. edb_cursor = edb.cursor();
  294. idb = script.connect_to_instance_pool()
  295. runs = queries.Minisat_run_scope_query_raw(idb)
  296. runs.query("c42_vLogistic_6", "minisat_runs")
  297. insert_row = ("INSERT INTO c42_vLogistic_6_minisat_runs "
  298. "(run_id, "
  299. " instance_id, "
  300. " satisfiable) "
  301. "VALUES (%s, %s, %s) ")
  302. for run in tqdm(runs):
  303. data = script.analyde_minisat_run(run)
  304. edb_cursor.execute(insert_row, (str(run["_id"]),
  305. str(run["instance"]),
  306. int(data["satisfiable"])))
  307. edb.commit()
  308. edb_cursor.close()
  309. edb.close()
  310. def wmis_results():
  311. edb = script.connect_to_experimetns_db()
  312. edb_cursor = edb.cursor();
  313. idb = script.connect_to_instance_pool()
  314. q = queries.WMIS_result_scope_query_raw(idb)
  315. q.query("c42_v[5-84]_1", "wmis_qbsolv_results")
  316. for i in range(501):
  317. q.__next__()
  318. res = q.__next__()
  319. sample_set = queries.read_raw_wmis_sample_set(res["data"])
  320. model = script.majority_vote_sample(sample_set.first.sample)
  321. sat = queries.get_instance_by_id(idb["instances"], res["instance"])
  322. print(model)
  323. print(sat.getNumberOfVariables())
  324. print(sat.checkAssignment(model))
  325. if __name__ == "__main__":
  326. main()