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.

102 lines
3.4 KiB

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. return experimentScope
  32. def __getDBClient(dbConfigPath):
  33. dbConf = scriptUtils.readConfig(dbConfigPath)
  34. dbConf["CONNECTION"]
  35. return pymongo.MongoClient(
  36. "mongodb://%s:%s@%s:%s/%s"
  37. % ( dbConf["CONNECTION"]["user"],
  38. dbConf["CONNECTION"]["pw"],
  39. dbConf["CONNECTION"]["url"],
  40. dbConf["CONNECTION"]["port"],
  41. dbConf["CONNECTION"]["database"]),
  42. ssl=True,
  43. ssl_cert_reqs=ssl.CERT_NONE)
  44. def __getDBContext(dbConfigPath):
  45. dbContext = {}
  46. dbContext["client"] = __getDBClient(dbConfigPath)
  47. dbContext["db"] = dbContext["client"]["experiments"]
  48. dbContext["instances"] = dbContext["db"]["instances"]
  49. dbContext["experimentScopes"] = dbContext["db"]["experiment_scopes"]
  50. return dbContext
  51. def __parseArguments():
  52. parser = scriptUtils.ArgParser()
  53. parser.addArg(alias="instances", shortFlag="i", longFlag="instances",
  54. help="number of random kSAT instances", type=int,
  55. ignoreDatabaseConfig=True)
  56. parser.addArg(alias="variables", shortFlag="v", longFlag="variables",
  57. help="number of variables in the ksat instances", type=int,
  58. ignoreDatabaseConfig=True)
  59. parser.addArg(alias="clauses", shortFlag="c", longFlag="clauses",
  60. help="number of clauses in the ksat instances", type=int,
  61. ignoreDatabaseConfig=True)
  62. parser.addArg(alias="variablesPerClause", shortFlag="vpc",
  63. longFlag="variables_per_clause",
  64. help="variables per clause in the kSAT instances", type=int,
  65. default=3, ignoreDatabaseConfig=True)
  66. parser.addArg(alias="dbConfigPath", shortFlag="d", longFlag="db_config",
  67. help="path to the database config file", type=str,
  68. default="database.config", ignoreDatabaseConfig=True)
  69. return parser.parse()
  70. if __name__ == "__main__":
  71. main()