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.

100 lines
3.4 KiB

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 h5py
  6. import numpy as np
  7. import pymongo
  8. import ssl
  9. def main():
  10. args = __parseArguments()
  11. dbClient = __getDBClient(args["dbConfigPath"]);
  12. db = dbClient["experiments"]
  13. dbInstances = db["instances"]
  14. dbExperimentScopes = db["experiment_scopes"]
  15. experimentScope = {}
  16. experimentScope["instances"] = []
  17. description = "This experiment contains %d instances " % args["instances"]
  18. description += "each with %d clauses, %d variables " % (args["clauses"],
  19. args["variables"])
  20. description += ("and %d variables per clause (c/v = %.2f)"
  21. %(args["variablesPerClause"],
  22. float(args["clauses"]) / float(args["variables"])))
  23. experimentScope["description"] = description
  24. for i in range(args["instances"]):
  25. print("loop", i)
  26. sat = randomSAT.generateRandomKSAT(args["clauses"],
  27. args["variables"],
  28. args["variablesPerClause"])
  29. instanceId = dbInstances.insert_one(sat.writeJSONLike()).inserted_id
  30. experimentScope["instances"].append(instanceId)
  31. dbExperimentScopes.insert_one(experimentScope)
  32. dbClient.close()
  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 __parseArguments():
  46. parser = scriptUtils.ArgParser()
  47. parser.addArg(alias="instances", shortFlag="i", longFlag="instances",
  48. help="number of random kSAT instances", type=int,
  49. ignoreDatabaseConfig=True)
  50. parser.addArg(alias="variables", shortFlag="v", longFlag="variables",
  51. help="number of variables in the ksat instances", type=int,
  52. ignoreDatabaseConfig=True)
  53. parser.addArg(alias="clauses", shortFlag="c", longFlag="clauses",
  54. help="number of clauses in the ksat instances", type=int,
  55. ignoreDatabaseConfig=True)
  56. parser.addArg(alias="variablesPerClause", shortFlag="vpc",
  57. longFlag="variables_per_clause",
  58. help="variables per clause in the kSAT instances", type=int,
  59. default=3, ignoreDatabaseConfig=True)
  60. parser.addArg(alias="dbConfigPath", shortFlag="d", longFlag="db_config",
  61. help="path to the database config file", type=str,
  62. default="database.config", ignoreDatabaseConfig=True)
  63. return parser.parse()
  64. def __initFile(args):
  65. f = h5py.File(args["fileName"], "w")
  66. __SAT_TYPE__ = np.dtype([('bindings', (np.int32, (args["clauses"],
  67. args["variablesPerClause"])))])
  68. instances = f.create_dataset(name="instances",
  69. shape=(args["instances"],),
  70. dtype=__SAT_TYPE__)
  71. return f, __SAT_TYPE__
  72. if __name__ == "__main__":
  73. main()