|
|
- #!/usr/bin/env python3
-
- from util import randomSAT
- from util import kSAT
- import util.script as scriptUtils
- import h5py
- import numpy as np
- import pymongo
- import ssl
-
- def main():
- args = __parseArguments()
-
- dbClient = __getDBClient(args["dbConfigPath"]);
- db = dbClient["experiments"]
- dbInstances = db["instances"]
- dbExperimentScopes = db["experiment_scopes"]
-
- experimentScope = {}
- experimentScope["instances"] = []
-
- description = "This experiment contains %d instances " % args["instances"]
- description += "each with %d clauses, %d variables " % (args["clauses"],
- args["variables"])
- description += ("and %d variables per clause (c/v = %.2f)"
- %(args["variablesPerClause"],
- float(args["clauses"]) / float(args["variables"])))
-
- experimentScope["description"] = description
-
-
- for i in range(args["instances"]):
- print("loop", i)
-
- sat = randomSAT.generateRandomKSAT(args["clauses"],
- args["variables"],
- args["variablesPerClause"])
-
- instanceId = dbInstances.insert_one(sat.writeJSONLike()).inserted_id
-
- experimentScope["instances"].append(instanceId)
-
- dbExperimentScopes.insert_one(experimentScope)
-
- dbClient.close()
-
- def __getDBClient(dbConfigPath):
- dbConf = scriptUtils.readConfig(dbConfigPath)
- dbConf["CONNECTION"]
-
- return pymongo.MongoClient(
- "mongodb://%s:%s@%s:%s/%s"
- % ( dbConf["CONNECTION"]["user"],
- dbConf["CONNECTION"]["pw"],
- dbConf["CONNECTION"]["url"],
- dbConf["CONNECTION"]["port"],
- dbConf["CONNECTION"]["database"]),
- ssl=True,
- ssl_cert_reqs=ssl.CERT_NONE)
-
- def __parseArguments():
- parser = scriptUtils.ArgParser()
-
- parser.addArg(alias="instances", shortFlag="i", longFlag="instances",
- help="number of random kSAT instances", type=int,
- ignoreDatabaseConfig=True)
-
- parser.addArg(alias="variables", shortFlag="v", longFlag="variables",
- help="number of variables in the ksat instances", type=int,
- ignoreDatabaseConfig=True)
-
- parser.addArg(alias="clauses", shortFlag="c", longFlag="clauses",
- help="number of clauses in the ksat instances", type=int,
- ignoreDatabaseConfig=True)
-
- parser.addArg(alias="variablesPerClause", shortFlag="vpc",
- longFlag="variables_per_clause",
- help="variables per clause in the kSAT instances", type=int,
- default=3, ignoreDatabaseConfig=True)
-
- parser.addArg(alias="dbConfigPath", shortFlag="d", longFlag="db_config",
- help="path to the database config file", type=str,
- default="database.config", ignoreDatabaseConfig=True)
-
- return parser.parse()
-
- def __initFile(args):
- f = h5py.File(args["fileName"], "w")
-
- __SAT_TYPE__ = np.dtype([('bindings', (np.int32, (args["clauses"],
- args["variablesPerClause"])))])
-
- instances = f.create_dataset(name="instances",
- shape=(args["instances"],),
- dtype=__SAT_TYPE__)
-
- return f, __SAT_TYPE__
-
- if __name__ == "__main__":
- main()
|