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.

105 lines
3.4 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(sat.writeJSONLike()).inserted_id
  19. experimentScope["instances"].append(instanceId)
  20. dbContext["experimentScopes"].insert_one(experimentScope)
  21. def __prepareExperimentScope(args):
  22. experimentScope = {}
  23. experimentScope["instances"] = []
  24. description = "This experiment contains %d instances " % args["instances"]
  25. description += "each with %d clauses, %d variables " % (args["clauses"],
  26. args["variables"])
  27. description += ("and %d variables per clause (c/v = %.2f)"
  28. %(args["variablesPerClause"],
  29. float(args["clauses"]) / float(args["variables"])))
  30. experimentScope["description"] = description
  31. experimentScope["_id"] = args["experimentId"].strip()
  32. return experimentScope
  33. def __getDBClient(dbConfigPath):
  34. dbConf = scriptUtils.readConfig(dbConfigPath)
  35. dbConf["CONNECTION"]
  36. return pymongo.MongoClient(
  37. "mongodb://%s:%s@%s:%s/%s"
  38. % ( dbConf["CONNECTION"]["user"],
  39. dbConf["CONNECTION"]["pw"],
  40. dbConf["CONNECTION"]["url"],
  41. dbConf["CONNECTION"]["port"],
  42. dbConf["CONNECTION"]["database"]),
  43. ssl=True,
  44. ssl_cert_reqs=ssl.CERT_NONE)
  45. def __getDBContext(dbConfigPath):
  46. dbContext = {}
  47. dbContext["client"] = __getDBClient(dbConfigPath)
  48. dbContext["db"] = dbContext["client"]["experiments"]
  49. dbContext["instances"] = dbContext["db"]["instances"]
  50. dbContext["experimentScopes"] = dbContext["db"]["experiment_scopes"]
  51. return dbContext
  52. def __parseArguments():
  53. parser = scriptUtils.ArgParser()
  54. parser.addArg(alias="instances", shortFlag="i", longFlag="instances",
  55. help="number of random kSAT instances", type=int)
  56. parser.addArg(alias="variables", shortFlag="v", longFlag="variables",
  57. help="number of variables in the ksat instances", type=int)
  58. parser.addArg(alias="clauses", shortFlag="c", longFlag="clauses",
  59. help="number of clauses in the ksat instances", type=int)
  60. parser.addArg(alias="variablesPerClause", shortFlag="vpc",
  61. longFlag="variables_per_clause",
  62. help="variables per clause in the kSAT instances", type=int,
  63. default=3)
  64. parser.addArg(alias="dbConfigPath", shortFlag="d", longFlag="db_config",
  65. help="path to the database config file", type=str,
  66. default="database.config")
  67. parser.addArg(alias="experimentId", shortFlag="id", longFlag="experiment_id",
  68. help="the name or id of the experiment", type=str)
  69. return parser.parse()
  70. if __name__ == "__main__":
  71. main()