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.

325 lines
8.4 KiB

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