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.

215 lines
7.4 KiB

5 years ago
  1. from . import script
  2. from . import queries
  3. import dimod
  4. import random
  5. import numpy as np
  6. from tqdm import tqdm
  7. def extract_wmis_qpu_results():
  8. edb = script.connect_to_experimetns_db()
  9. edb_cursor = edb.cursor()
  10. idb = script.connect_to_instance_pool()
  11. scope = input("scope: ")
  12. result_collection = input("result collection: ")
  13. table_name = input("table name: ")
  14. q = queries.WMIS_result_scope_query_raw(idb)
  15. q.query(scope, result_collection)
  16. insert_row = ("INSERT INTO {} "
  17. "(result_id, "
  18. " run, "
  19. " instance_id, "
  20. " number_of_found_assignments, "
  21. " chain_break_fraction, "
  22. " num_occurrences, "
  23. " energy, "
  24. " satisfiable, "
  25. " anneal_time, "
  26. " energy_reach, "
  27. " sample_size) "
  28. "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ").format(table_name)
  29. run = int(input("run: "))
  30. for result in tqdm(q):
  31. if result["run"] == run:
  32. sample_set = queries.read_raw_wmis_sample_set(result["data"])
  33. data = script.analyze_wmis_sample(sample_set.first)
  34. sat = queries.get_instance_by_id(idb["instances"], result["instance"])
  35. model = script.majority_vote_sample(sample_set.first.sample)
  36. isSatisfiable = sat.checkAssignment(model)
  37. anneal_time = result["data"]["info"]["timing"]["qpu_anneal_time_per_sample"]
  38. energy_reach = abs(sample_set.record["energy"].max() -
  39. sample_set.record["energy"].min())
  40. sample_size = np.sum(sample_set.record["num_occurrences"])
  41. edb_cursor.execute(insert_row, (str(result["_id"]),
  42. int(result["run"]),
  43. str(result["instance"]),
  44. int(data["number_of_assignments"]),
  45. float(data["chain_break_fraction"]),
  46. int(data["num_occurrences"]),
  47. int(data["energy"]),
  48. isSatisfiable,
  49. int(anneal_time),
  50. int(energy_reach),
  51. int(sample_size)))
  52. edb.commit()
  53. edb_cursor.close()
  54. edb.close()
  55. def extract_wmis_2_qpu_results():
  56. edb = script.connect_to_experimetns_db()
  57. edb_cursor = edb.cursor()
  58. idb = script.connect_to_instance_pool()
  59. scope = input("scope: ")
  60. result_collection = input("result collection: ")
  61. table_name = input("table name: ")
  62. q = queries.WMIS_result_scope_query_raw(idb)
  63. q.query(scope, result_collection)
  64. insert_row = ("INSERT INTO {} "
  65. "(result_id, "
  66. " run, "
  67. " instance_id, "
  68. " chain_break_fraction, "
  69. " num_occurrences, "
  70. " energy, "
  71. " satisfiable, "
  72. " anneal_time, "
  73. " energy_reach, "
  74. " sample_size) "
  75. "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ").format(table_name)
  76. run = int(input("run: "))
  77. for result in tqdm(q):
  78. if result["run"] == run:
  79. sample_set = queries.read_raw_primitive_ising_sample_set(result["data"])
  80. data = script.analyze_wmis_sample(sample_set.first)
  81. sat = queries.get_instance_by_id(idb["instances"], result["instance"])
  82. post_process_results = __evaluate_wmis_2_sample(sat,
  83. sample_set.first.sample)
  84. anneal_time = result["data"]["info"]["timing"]["qpu_anneal_time_per_sample"]
  85. energy_reach = abs(sample_set.record["energy"].max() -
  86. sample_set.record["energy"].min())
  87. sample_size = np.sum(sample_set.record["num_occurrences"])
  88. edb_cursor.execute(insert_row, (str(result["_id"]),
  89. int(result["run"]),
  90. str(result["instance"]),
  91. float(data["chain_break_fraction"]),
  92. int(data["num_occurrences"]),
  93. int(data["energy"]),
  94. bool(post_process_results["satisfiable"]),
  95. int(anneal_time),
  96. int(energy_reach),
  97. int(sample_size)))
  98. edb.commit()
  99. edb_cursor.close()
  100. edb.close()
  101. def __evaluate_wmis_2_sample(sat, sample):
  102. post_process_results = {}
  103. assignments = {}
  104. vars = set()
  105. for node, energy in sample.items():
  106. if node[0] == "x":
  107. lit = int(node[1:])
  108. vars.add(abs(lit))
  109. assignments[lit] = energy
  110. model = [True for i in range(len(vars))]
  111. for var in vars:
  112. if var in assignments:
  113. model[var - 1] = True if assignments[var] == 1 else False
  114. elif -var in assignments:
  115. model[var - 1] = True if assignments[-var] == 0 else False
  116. post_process_results["satisfiable"] = sat.checkAssignment(model)
  117. return post_process_results
  118. def extract_minisat_results():
  119. edb = script.connect_to_experimetns_db()
  120. edb_cursor = edb.cursor();
  121. idb = script.connect_to_instance_pool()
  122. scope = input("scope: ")
  123. table_name = input("table name: ")
  124. runs = queries.Minisat_run_scope_query_raw(idb)
  125. runs.query(scope, "minisat_runs")
  126. insert_row = ("INSERT INTO {} "
  127. "(run_id, "
  128. " instance_id, "
  129. " satisfiable) "
  130. "VALUES (%s, %s, %s) ").format(table_name)
  131. for run in tqdm(runs):
  132. data = script.analyde_minisat_run(run)
  133. edb_cursor.execute(insert_row, (str(run["_id"]),
  134. str(run["instance"]),
  135. int(data["satisfiable"])))
  136. edb.commit()
  137. edb_cursor.close()
  138. edb.close()
  139. def extract_instance_parameters():
  140. edb = script.connect_to_experimetns_db()
  141. edb_cursor = edb.cursor()
  142. idb = script.connect_to_instance_pool()
  143. scope = input("scope: ")
  144. table_name = input("table name: ")
  145. instances = queries.Instance_scope_query(idb)
  146. instances.query(scope)
  147. insert_row = ("INSERT INTO {} "
  148. "(instance_id, "
  149. " number_of_clauses, "
  150. " number_of_variables) "
  151. "VALUES (%s, %s, %s)").format(table_name)
  152. for instance, instance_id in tqdm(instances):
  153. edb_cursor.execute(insert_row, (str(instance_id),
  154. int(instance.getNumberOfClauses()),
  155. int(instance.getNumberOfVariables())))
  156. edb.commit()
  157. edb_cursor.close()
  158. edb.close()