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.

428 lines
12 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
  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. self.__instance_ids = []
  9. self.__instance_id_iterator = None;
  10. def query(self, scope):
  11. self.__query = self.__database["experiment_scopes"].aggregate([
  12. {
  13. "$match": {"_id": scope}
  14. },
  15. {
  16. "$unwind": "$instances"
  17. },
  18. {
  19. "$lookup":
  20. {
  21. "from": "instances",
  22. "localField": "instances",
  23. "foreignField": "_id",
  24. "as": "instance"
  25. }
  26. },
  27. {
  28. "$unwind": "$instance"
  29. },
  30. {
  31. "$project": {"instance_id": "$instance._id"}
  32. }
  33. ])
  34. self.__instance_ids = []
  35. for doc in self.__query:
  36. self.__instance_ids.append(doc["instance_id"])
  37. self.__instance_id_iterator = iter(self.__instance_ids)
  38. def __iter__(self):
  39. return self
  40. def __next__(self):
  41. instance_id = self.__instance_id_iterator.__next__()
  42. document = self.__database["instances"].find_one({"_id": instance_id})
  43. return self.__document_to_sat(document)
  44. def __document_to_sat(self, document):
  45. sat = kSAT()
  46. for clause in document["clauses"]:
  47. sat.addClause(clause);
  48. return sat, document["_id"]
  49. class WMIS_scope_query_raw:
  50. def __init__(self, database):
  51. self.__database = database
  52. self.__query = None
  53. self.__qubo_ids = []
  54. self.__qubo_id_iterator = None
  55. def query(self, scope):
  56. self.__query = self.__database["experiment_scopes"].aggregate([
  57. {
  58. "$match": {"_id": scope}
  59. },
  60. {
  61. "$unwind": "$instances"
  62. },
  63. {
  64. "$lookup":
  65. {
  66. "from": "wmis_qubos",
  67. "localField": "instances",
  68. "foreignField": "instance",
  69. "as": "qubo"
  70. }
  71. },
  72. {
  73. "$unwind": "$qubo"
  74. },
  75. {
  76. "$project": {"qubo_id": "$qubo._id"}
  77. }
  78. ])
  79. self.__qubo_ids = []
  80. for doc in self.__query:
  81. self.__qubo_ids.append(doc["qubo_id"])
  82. self.__qubo_id_iterator = iter(self.__qubo_ids)
  83. def __len__(self):
  84. return self.query.count_documents({})
  85. def __iter__(self):
  86. return self
  87. def __next__(self):
  88. qubo_filter = {"_id": self.__qubo_id_iterator.__next__()}
  89. return self.__database["wmis_qubos"].find_one(qubo_filter)
  90. class WMIS_scope_query (WMIS_scope_query_raw):
  91. def __next__(self):
  92. doc = super(WMIS_scope_query, self).__next__()
  93. return read_raw_qubo(doc["qubo"]), doc["_id"]
  94. class WMIS_solver_input_scope_query_raw:
  95. def __init__(self, database):
  96. self.__database = database
  97. self.__query = None
  98. self.__ids = []
  99. self.__id_iterator = None;
  100. def query(self, scope, solver_graph_id):
  101. self.__query = self.__database["experiment_scopes"].aggregate([
  102. {
  103. "$match": {"_id": scope}
  104. },
  105. {
  106. "$unwind": "$instances"
  107. },
  108. {
  109. "$project": {"instance_id": "$instances"}
  110. },
  111. {
  112. "$lookup":
  113. {
  114. "from": "wmis_qubos",
  115. "localField": "instance_id",
  116. "foreignField": "instance",
  117. "as": "wmis_qubo"
  118. }
  119. },
  120. {
  121. "$unwind": "$wmis_qubo"
  122. },
  123. {
  124. "$project":
  125. {
  126. "instance_id": True,
  127. "wmis_qubo_id": "$wmis_qubo._id"
  128. }
  129. },
  130. {
  131. "$lookup":
  132. {
  133. "from": "embeddings",
  134. "let":
  135. {
  136. "qubo_id": "$wmis_qubo_id",
  137. "solver_graph_id": bson.ObjectId(solver_graph_id)
  138. },
  139. "pipeline":
  140. [
  141. {
  142. "$project":
  143. {
  144. "qubo": True,
  145. "solver_graph": True,
  146. }
  147. },
  148. {
  149. "$match":
  150. {
  151. "$expr":
  152. {
  153. "$and":
  154. [
  155. {"$eq": ["$qubo", "$$qubo_id"]},
  156. {"$eq": ["$solver_graph", "$$solver_graph_id"]}
  157. ]
  158. }
  159. }
  160. },
  161. {
  162. "$project":
  163. {
  164. "_id": True
  165. }
  166. }
  167. ],
  168. "as": "embeddings_id"
  169. }
  170. },
  171. {
  172. "$unwind": "$embeddings_id"
  173. },
  174. {
  175. "$project":
  176. {
  177. "instance_id": True,
  178. "wmis_qubo_id": True,
  179. "embeddings_id": "$embeddings_id._id"
  180. }
  181. }
  182. ])
  183. self.__ids = []
  184. for doc in self.__query:
  185. self.__ids.append({
  186. "instance_id": doc["instance_id"],
  187. "wmis_qubo_id": doc["wmis_qubo_id"],
  188. "embeddings_id": doc["embeddings_id"]
  189. })
  190. self.__id_iterator = iter(self.__ids)
  191. def __iter__(self):
  192. return self
  193. def __next__(self):
  194. ids = self.__id_iterator.__next__()
  195. doc = {}
  196. doc["instance_id"] = ids["instance_id"]
  197. qubo_filter = {"_id": ids["wmis_qubo_id"]}
  198. doc["wmis_qubo"] = self.__database["wmis_qubos"].find_one(qubo_filter)
  199. embeddings_filter = {"_id": ids["embeddings_id"]}
  200. doc["embeddings"] = self.__database["embeddings"].find_one(embeddings_filter)
  201. return doc
  202. class WMIS_solver_input_scope_query (WMIS_solver_input_scope_query_raw):
  203. def __next__(self):
  204. doc = super(WMIS_solver_input_scope_query, self).__next__()
  205. data = {}
  206. data["instance_id"] = doc["instance_id"]
  207. data["qubo_id"] = doc["wmis_qubo"]["_id"]
  208. data["qubo"] = read_raw_qubo(doc["wmis_qubo"]["qubo"])
  209. data["embeddings_id"] = doc["embeddings"]["_id"]
  210. data["embeddings"] = []
  211. for raw_emb in doc["embeddings"]["embeddings"]:
  212. data["embeddings"].append(read_raw_embedding(raw_emb))
  213. return data
  214. class WMIS_result_scope_query_raw:
  215. def __init__(self, database):
  216. self.__database = database
  217. self.__query = None
  218. def query(self, scope, collection):
  219. self.__query = self.__database["experiment_scopes"].aggregate([
  220. {
  221. "$match": {"_id": scope}
  222. },
  223. {
  224. "$unwind": "$instances"
  225. },
  226. {
  227. "$project": {"instance_id": "$instances"}
  228. },
  229. {
  230. "$lookup":
  231. {
  232. "from": collection,
  233. "localField": "instance_id",
  234. "foreignField": "instance",
  235. "as": "result"
  236. }
  237. },
  238. {
  239. "$unwind": "$result"
  240. },
  241. {
  242. "$replaceRoot": {"newRoot": "$result"}
  243. }
  244. ])
  245. def __iter__(self):
  246. return self
  247. def __next__(self):
  248. return self.__query.next()
  249. class Minisat_run_scope_query_raw:
  250. def __init__(self, database):
  251. self.__database = database
  252. self.__query = None
  253. def query(self, scope, collection):
  254. self.__query = self.__database["experiment_scopes"].aggregate([
  255. {
  256. "$match": {"_id": scope}
  257. },
  258. {
  259. "$unwind": "$instances"
  260. },
  261. {
  262. "$project": {"instance_id": "$instances"}
  263. },
  264. {
  265. "$lookup":
  266. {
  267. "from": collection,
  268. "localField": "instance_id",
  269. "foreignField": "instance",
  270. "as": "run"
  271. }
  272. },
  273. {
  274. "$unwind": "$run"
  275. },
  276. {
  277. "$replaceRoot": {"newRoot": "$run"}
  278. }
  279. ])
  280. def __iter__(self):
  281. return self
  282. def __next__(self):
  283. return self.__query.next()
  284. def load_embedding(collection, qubo_id, solver_graph_id):
  285. doc = collection.find_one(
  286. {
  287. "qubo": bson.ObjectId(qubo_id),
  288. "solver_graph": bson.ObjectId(solver_graph_id)
  289. }
  290. )
  291. if doc == None:
  292. return None
  293. if doc["embeddings"] == None:
  294. return None
  295. if len(doc["embeddings"]) == 0:
  296. return None
  297. return read_raw_embedding(doc["embeddings"][0])
  298. def get_id_of_solver_graph(collection, nx_graph_data):
  299. doc = collection.find_one({"data": nx_graph_data})
  300. if doc == None:
  301. return None
  302. return doc["_id"]
  303. def load_WMIS_qubo_of_instance(collection, instance_id):
  304. doc = load_WMIS_qubo_of_instance_raw(collection, instance_id)
  305. if doc == None:
  306. return None
  307. return read_raw_qubo(doc["qubo"]), doc["_id"]
  308. def get_WMIS_qubo_id_of_instance(collection, instance_id):
  309. doc = load_WMIS_qubo_of_instance_raw(collection, instance_id)
  310. if doc == None:
  311. return None
  312. return doc["_id"]
  313. def load_WMIS_qubo_of_instance_raw(collection, instance_id):
  314. return collection.find_one({"instance": bson.ObjectId(instance_id)})
  315. def read_raw_qubo(raw_qubo):
  316. qubo = {}
  317. for entry in raw_qubo:
  318. energy = entry[1]
  319. raw_coupler = entry[0]
  320. node1 = tuple(raw_coupler[0])
  321. node2 = tuple(raw_coupler[1])
  322. qubo[(node1, node2)] = energy
  323. return qubo
  324. def read_raw_embedding(raw_embedding):
  325. emb = {}
  326. if "embedding" in raw_embedding:
  327. raw_embedding = raw_embedding["embedding"]
  328. for entry in raw_embedding:
  329. emb[tuple(entry[0])] = entry[1]
  330. return emb
  331. def read_raw_wmis_sample_set(raw_sample_set):
  332. sample_set_data = raw_sample_set.copy()
  333. sample_set_data["variable_labels"] = []
  334. for label in raw_sample_set["variable_labels"]:
  335. sample_set_data["variable_labels"].append(tuple(label))
  336. return dimod.SampleSet.from_serializable(sample_set_data)
  337. def get_instance_by_id(collection, id):
  338. doc = collection.find_one({"_id": bson.ObjectId(id)})
  339. sat = kSAT()
  340. for clause in doc["clauses"]:
  341. sat.addClause(clause);
  342. return sat