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.

264 lines
6.9 KiB

5 years ago
  1. from .kSAT import kSAT
  2. import bson
  3. class Instance_scope_query:
  4. def __init__(self, database):
  5. self.__database = database
  6. self.__query = None
  7. def query(self, scope):
  8. self.__query = self.__database["experiment_scopes"].aggregate([
  9. {
  10. "$match": {"_id": scope}
  11. },
  12. {
  13. "$unwind": "$instances"
  14. },
  15. {
  16. "$lookup":
  17. {
  18. "from": "instances",
  19. "localField": "instances",
  20. "foreignField": "_id",
  21. "as": "instance"
  22. }
  23. },
  24. {
  25. "$unwind": "$instance"
  26. },
  27. {
  28. "$replaceRoot": {"newRoot": "$instance"}
  29. }
  30. ])
  31. def __iter__(self):
  32. return self
  33. def __next__(self):
  34. document = self.__query.next()
  35. return self.__document_to_sat(document)
  36. def __document_to_sat(self, document):
  37. sat = kSAT()
  38. for clause in document["clauses"]:
  39. sat.addClause(clause);
  40. return sat, document["_id"]
  41. class WMIS_scope_query_raw:
  42. def __init__(self, database):
  43. self.__database = database
  44. self.__query = None
  45. def query(self, scope):
  46. self.__query = self.__database["experiment_scopes"].aggregate([
  47. {
  48. "$match": {"_id": scope}
  49. },
  50. {
  51. "$unwind": "$instances"
  52. },
  53. {
  54. "$lookup":
  55. {
  56. "from": "wmis_qubos",
  57. "localField": "instances",
  58. "foreignField": "instance",
  59. "as": "qubo"
  60. }
  61. },
  62. {
  63. "$unwind": "$qubo"
  64. },
  65. {
  66. "$replaceRoot": {"newRoot": "$qubo"}
  67. }
  68. ])
  69. def __iter__(self):
  70. return self
  71. def __next__(self):
  72. return self.__query.next()
  73. class WMIS_scope_query (WMIS_scope_query_raw):
  74. def __next__(self):
  75. doc = super(WMIS_scope_query, self).__next__()
  76. return read_raw_qubo(doc["qubo"]), doc["_id"]
  77. class WMIS_solver_input_scope_query_raw:
  78. def __init__(self, database):
  79. self.__database = database
  80. self.__query = None
  81. def query(self, scope, solver_graph_id):
  82. self.__query = self.__database["experiment_scopes"].aggregate([
  83. {
  84. "$match": {"_id": scope}
  85. },
  86. {
  87. "$unwind": "$instances"
  88. },
  89. {
  90. "$project": {"instance_id": "$instances"}
  91. },
  92. {
  93. "$lookup":
  94. {
  95. "from": "wmis_qubos",
  96. "localField": "instance_id",
  97. "foreignField": "instance",
  98. "as": "wmis_qubo"
  99. }
  100. },
  101. {
  102. "$unwind": "$wmis_qubo"
  103. },
  104. {
  105. "$lookup":
  106. {
  107. "from": "embeddings",
  108. "let":
  109. {
  110. "qubo_id": "$wmis_qubo._id",
  111. "solver_graph_id": bson.ObjectId(solver_graph_id)
  112. },
  113. "pipeline":
  114. [
  115. {
  116. "$match":
  117. {
  118. "$expr":
  119. {
  120. "$and":
  121. [
  122. {"$eq": ["$qubo", "$$qubo_id"]},
  123. {"$eq": ["$solver_graph", "$$solver_graph_id"]}
  124. ]
  125. }
  126. }
  127. },
  128. {
  129. "$project": { "list": "$embeddings" }
  130. }
  131. ],
  132. "as": "embeddings"
  133. }
  134. },
  135. {
  136. "$match": {"embeddings": {"$exists": True, "$not": {"$size": 0}}}
  137. },
  138. {
  139. "$unwind": "$embeddings"
  140. },
  141. {
  142. "$project":
  143. {
  144. "_id": False,
  145. "instance_id": True,
  146. "wmis_qubo": True,
  147. "embeddings": True
  148. }
  149. }
  150. ])
  151. def __iter__(self):
  152. return self
  153. def __next__(self):
  154. return self.__query.next()
  155. class WMIS_solver_input_scope_query (WMIS_solver_input_scope_query_raw):
  156. def __next__(self):
  157. doc = super(WMIS_solver_input_scope_query, self).__next__()
  158. data = {}
  159. data["instance_id"] = doc["instance_id"]
  160. data["qubo_id"] = doc["wmis_qubo"]["_id"]
  161. data["qubo"] = read_raw_qubo(doc["wmis_qubo"]["qubo"])
  162. data["embeddings_id"] = doc["embeddings"]["_id"]
  163. data["embeddings"] = []
  164. for raw_emb in doc["embeddings"]["list"]:
  165. data["embeddings"].append(read_raw_embedding(raw_emb))
  166. return data
  167. def load_embedding(collection, qubo_id, solver_graph_id):
  168. doc = collection.find_one(
  169. {
  170. "qubo": bson.ObjectId(qubo_id),
  171. "solver_graph": bson.ObjectId(solver_graph_id)
  172. }
  173. )
  174. if doc == None:
  175. return None
  176. if doc["embeddings"] == None:
  177. return None
  178. if len(doc["embeddings"]) == 0:
  179. return None
  180. return read_raw_embedding(doc["embeddings"][0])
  181. def get_id_of_solver_graph(collection, nx_graph_data):
  182. doc = collection.find_one({"data": nx_graph_data})
  183. if doc == None:
  184. return None
  185. return doc["_id"]
  186. def load_WMIS_qubo_of_instance(collection, instance_id):
  187. doc = load_WMIS_qubo_of_instance_raw(collection, instance_id)
  188. if doc == None:
  189. return None
  190. return read_raw_qubo(doc["qubo"]), doc["_id"]
  191. def get_WMIS_qubo_id_of_instance(collection, instance_id):
  192. doc = load_WMIS_qubo_of_instance_raw(collection, instance_id)
  193. if doc == None:
  194. return None
  195. return doc["_id"]
  196. def load_WMIS_qubo_of_instance_raw(collection, instance_id):
  197. return collection.find_one({"instance": bson.ObjectId(instance_id)})
  198. def read_raw_qubo(raw_qubo):
  199. qubo = {}
  200. for entry in raw_qubo:
  201. energy = entry[1]
  202. raw_coupler = entry[0]
  203. node1 = tuple(raw_coupler[0])
  204. node2 = tuple(raw_coupler[1])
  205. qubo[(node1, node2)] = energy
  206. return qubo
  207. def read_raw_embedding(raw_embedding):
  208. emb = {}
  209. for entry in raw_embedding:
  210. emb[tuple(entry[0])] = entry[1]
  211. return emb