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.

92 lines
2.5 KiB

5 years ago
  1. #!/usr/bin/env python3
  2. from util.kSAT import kSAT
  3. from util import SAT2QUBO
  4. import util.script as scriptUtils
  5. import pymongo
  6. import ssl
  7. import json
  8. def main():
  9. args = __parseArguments()
  10. dbContext = scriptUtils.getDBContext(args["dbConfigPath"])
  11. __createQubos(dbContext, args["experimentScope"])
  12. def __parseArguments():
  13. parser = scriptUtils.ArgParser()
  14. parser.addArg(alias="experimentScope", shortFlag="s", longFlag="scope",
  15. help="the experiment experiment scope of interest", type=str)
  16. parser.addArg(alias="dbConfigPath", shortFlag="d", longFlag="db_config",
  17. help="path to the database config file", type=str,
  18. default="database.config")
  19. return parser.parse()
  20. def __createQubos(dbContext, experimentScope):
  21. instances = __queryInstancs(dbContext["db"], experimentScope)
  22. for instance in instances:
  23. sat = kSAT()
  24. for clause in instance["clauses"]:
  25. sat.addClause(clause);
  26. qubo = SAT2QUBO.WMISdictQUBO(sat)
  27. doc = {}
  28. doc["instance"] = instance["_id"]
  29. doc["description"] = {"<qubo>": "<entrys>",
  30. "<entrys>": "<entry><entrys> | <entry> | \"\"",
  31. "<entry>": "<coupler><energy>",
  32. "<energy>": "<real_number>",
  33. "<coupler>": "<node><node>",
  34. "<node>": "<clause><literal>",
  35. "<clause>": "<natural_number>",
  36. "<literal>": "<integer>"}
  37. doc["qubo"] = __qubo2JSON(qubo)
  38. dbContext["db"]["wmis_qubos"].insert_one(doc)
  39. def __qubo2JSON(qubo):
  40. quboJSON = []
  41. for coupler, value in qubo.items():
  42. quboJSON.append([coupler, float(value)])
  43. return quboJSON
  44. def __queryInstancs(db, experimentScope):
  45. return db["experiment_scopes"].aggregate([
  46. {
  47. "$match": {"_id": experimentScope}
  48. },
  49. {
  50. "$unwind": "$instances"
  51. },
  52. {
  53. "$lookup":
  54. {
  55. "from": "instances",
  56. "localField": "instances",
  57. "foreignField": "_id",
  58. "as": "instance"
  59. }
  60. },
  61. {
  62. "$unwind": "$instance"
  63. },
  64. {
  65. "$replaceRoot": {"newRoot": "$instance"}
  66. }
  67. ])
  68. if __name__ == "__main__":
  69. main()