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.

144 lines
4.5 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. #!/usr/bin/env python3
  2. from util import randomSAT
  3. from util import kSAT
  4. import util.script as scriptUtils
  5. import pymongo
  6. import ssl
  7. def main():
  8. args = __parseArguments()
  9. dbContext = __getDBContext(args["dbConfigPath"])
  10. __generateExperiment(args, dbContext)
  11. dbContext["client"].close()
  12. def __generateExperiment(args, dbContext):
  13. experimentScope = __prepareExperimentScope(args)
  14. for i in range(args["instances"]):
  15. sat = randomSAT.generateRandomKSAT(args["clauses"],
  16. args["variables"],
  17. args["variablesPerClause"])
  18. instanceId = dbContext["instances"].insert_one(__packDocument(sat)).inserted_id
  19. experimentScope["instances"].append(instanceId)
  20. dbContext["experimentScopes"].insert_one(experimentScope)
  21. def __packDocument(instance):
  22. doc = instance.writeJSONLike();
  23. doc["degrees_of_variables"] = __packDegreesOfVariables(instance)
  24. doc["conflicts"] = __packConflicts(instance)
  25. return doc
  26. def __packDegreesOfVariables(instance):
  27. doc = []
  28. for var, degree in instance.getDegreesOfVariables().items():
  29. doc.append({"var": var, "deg": degree})
  30. return doc
  31. def __packConflicts(instance):
  32. doc = [{"var": i + 1,
  33. "clauses": {
  34. "pos_lit": [],
  35. "neg_lit": []}} for i in range(instance.getNumberOfVariables())]
  36. for conflict in instance.getConflicts():
  37. for binding in conflict:
  38. clause = binding[0]
  39. lit = binding[1]
  40. varIndex = abs(lit) - 1
  41. if lit > 0:
  42. if clause not in doc[varIndex]["clauses"]["pos_lit"]:
  43. doc[varIndex]["clauses"]["pos_lit"].append(clause)
  44. else:
  45. if clause not in doc[varIndex]["clauses"]["neg_lit"]:
  46. doc[varIndex]["clauses"]["neg_lit"].append(clause)
  47. return doc
  48. def __prepareExperimentScope(args):
  49. experimentScope = {}
  50. experimentScope["instances"] = []
  51. description = "This experiment contains %d instances " % args["instances"]
  52. description += "each with %d clauses, %d variables " % (args["clauses"],
  53. args["variables"])
  54. description += ("and %d variables per clause (c/v = %.2f)"
  55. %(args["variablesPerClause"],
  56. float(args["clauses"]) / float(args["variables"])))
  57. experimentScope["description"] = description
  58. experimentScope["_id"] = args["experimentId"].strip()
  59. return experimentScope
  60. def __getDBClient(dbConfigPath):
  61. dbConf = scriptUtils.readConfig(dbConfigPath)
  62. dbConf["CONNECTION"]
  63. return pymongo.MongoClient(
  64. "mongodb://%s:%s@%s:%s/%s"
  65. % ( dbConf["CONNECTION"]["user"],
  66. dbConf["CONNECTION"]["pw"],
  67. dbConf["CONNECTION"]["url"],
  68. dbConf["CONNECTION"]["port"],
  69. dbConf["CONNECTION"]["database"]),
  70. ssl=True,
  71. ssl_cert_reqs=ssl.CERT_NONE)
  72. def __getDBContext(dbConfigPath):
  73. dbContext = {}
  74. dbContext["client"] = __getDBClient(dbConfigPath)
  75. dbContext["db"] = dbContext["client"]["experiments"]
  76. dbContext["instances"] = dbContext["db"]["instances"]
  77. dbContext["experimentScopes"] = dbContext["db"]["experiment_scopes"]
  78. return dbContext
  79. def __parseArguments():
  80. parser = scriptUtils.ArgParser()
  81. parser.addArg(alias="instances", shortFlag="i", longFlag="instances",
  82. help="number of random kSAT instances", type=int)
  83. parser.addArg(alias="variables", shortFlag="v", longFlag="variables",
  84. help="number of variables in the ksat instances", type=int)
  85. parser.addArg(alias="clauses", shortFlag="c", longFlag="clauses",
  86. help="number of clauses in the ksat instances", type=int)
  87. parser.addArg(alias="variablesPerClause", shortFlag="vpc",
  88. longFlag="variables_per_clause",
  89. help="variables per clause in the kSAT instances", type=int,
  90. default=3)
  91. parser.addArg(alias="dbConfigPath", shortFlag="d", longFlag="db_config",
  92. help="path to the database config file", type=str,
  93. default="database.config")
  94. parser.addArg(alias="experimentId", shortFlag="id", longFlag="experiment_id",
  95. help="the name or id of the experiment", type=str)
  96. return parser.parse()
  97. if __name__ == "__main__":
  98. main()