diff --git a/.gitignore b/.gitignore index 6a18ad4..7316d1f 100644 --- a/.gitignore +++ b/.gitignore @@ -94,3 +94,7 @@ ENV/ # Rope project settings .ropeproject + +#### config files +# +database.config diff --git a/collectCmpStatus.py b/collectCmpStatus.py index a951c2b..826b72d 100755 --- a/collectCmpStatus.py +++ b/collectCmpStatus.py @@ -7,7 +7,7 @@ import json import numpy as np import matplotlib.pyplot as plt import configparser -import scriptUtils +from util import script as scriptUtils def main(): args = __parseArguments() diff --git a/compareMinisatAndWMISresult.py b/compareMinisatAndWMISresult.py index c500cc7..072fc1e 100755 --- a/compareMinisatAndWMISresult.py +++ b/compareMinisatAndWMISresult.py @@ -1,10 +1,10 @@ #!/usr/bin/env python3 -import SATquboResult +from util import SATquboResult import argparse -from kSAT import kSAT -import minisatUtils as mSatU -import SATquboResult +from util.kSAT import kSAT +from util import minisat as mSatU +from util import SATquboResult import os import json diff --git a/compareRuns.py b/compareRuns.py index 57915ca..18b7180 100755 --- a/compareRuns.py +++ b/compareRuns.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 -import scriptUtils -import compare +from util import script as scriptUtils +from util import compare import glob import os diff --git a/comparisonStats.py b/comparisonStats.py index f0d476f..3596e02 100755 --- a/comparisonStats.py +++ b/comparisonStats.py @@ -7,7 +7,7 @@ import json import numpy as np import matplotlib.pyplot as plt import configparser -import scriptUtils +import util.script as scriptUtils def main(): args = __parseArguments() diff --git a/generateRandomKsatDataSet.py b/generateRandomKsatDataSet.py index 9d28e14..fd95c75 100755 --- a/generateRandomKsatDataSet.py +++ b/generateRandomKsatDataSet.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 -import randomSAT -import kSAT +from util import randomSAT +from util import kSAT import argparse import configparser import os diff --git a/generateRandomKsatDataSet_db.py b/generateRandomKsatDataSet_db.py new file mode 100755 index 0000000..626d3d2 --- /dev/null +++ b/generateRandomKsatDataSet_db.py @@ -0,0 +1,100 @@ +#!/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() diff --git a/runMinisatOnDataset.py b/runMinisatOnDataset.py new file mode 100644 index 0000000..dc8cd68 --- /dev/null +++ b/runMinisatOnDataset.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + +from util import script as scrUt +import configparser +import os + +def main(): + args = __parseArgs() + + print(args) + +def __parseArguments(): + parser = scrUt.ArgParser() + + parser.addInstanceDirArg() + + parser.addArg(alias="instanceDir", shortFlag="i", longFlag="instance_dir", + help="the directory with all instance files", type=str) + + parser.addArg(alias="outputDir", shortFlag="o", longFlag="output_dir", + help="the directory to store the minisat results for each instance", + type=str) + + parser.addArg(alias="configFile", shortFlag="c", longFlag="config", + help="config file (default: ./satlab.config)", + type=str, default=os.path.join(".", "satlab.config")) + + arguments = parser.parse() + + +if __name__ == "__main__": + main() diff --git a/runWMISquboOnSatInstance.py b/runWMISquboOnSatInstance.py index e49ebb2..6c14b72 100755 --- a/runWMISquboOnSatInstance.py +++ b/runWMISquboOnSatInstance.py @@ -1,15 +1,15 @@ #!/usr/bin/env python3 -import kSAT -import SAT2QUBO -from SATquboResult import SATquboResult +from util import kSAT +from util import SAT2QUBO +from util.SATquboResult import SATquboResult import argparse from dwave_qbsolv import QBSolv import os import collections import json from tqdm import tqdm -import scriptUtils +from util import script as scriptUtils def main(): arguments = __parseArguments() diff --git a/satUnsatConflictsPerVariableStats.py b/satUnsatConflictsPerVariableStats.py index 5a30d4e..9112145 100755 --- a/satUnsatConflictsPerVariableStats.py +++ b/satUnsatConflictsPerVariableStats.py @@ -7,7 +7,7 @@ import json import numpy as np import matplotlib.pyplot as plt import collections -import scriptUtils +from script import script as scriptUtils def main(): args = __parseArguments() diff --git a/testMinisatResults.py b/testMinisatResults.py index f1c0566..bb61888 100755 --- a/testMinisatResults.py +++ b/testMinisatResults.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 -import kSAT -import minisatUtils as mSatU +from util import kSAT +from util import minisat as mSatU import argparse diff --git a/testRandomSat.py b/testRandomSat.py index 7d98a05..10e78c8 100755 --- a/testRandomSat.py +++ b/testRandomSat.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 -import randomSAT as rdSAT +from util import randomSAT as rdSAT + kSATInstance = rdSAT.generateRandomKSAT(5, 4, 3) diff --git a/testSAT2QUBO.py b/testSAT2QUBO.py index 8fda346..2637da0 100755 --- a/testSAT2QUBO.py +++ b/testSAT2QUBO.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 -import SAT2QUBO as s2q -import randomSAT as rs +from util import SAT2QUBO as s2q +from util import randomSAT as rs ksatInstance = rs.generateRandomKSAT(2, 4, 3) diff --git a/util/SAT2QUBO.py b/util/SAT2QUBO.py index a87290b..79ff267 100644 --- a/util/SAT2QUBO.py +++ b/util/SAT2QUBO.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 import numpy as np -import kSAT +from . import kSAT from tqdm import tqdm import math diff --git a/util/SATquboResult.py b/util/SATquboResult.py index 942a0e1..752bc2c 100644 --- a/util/SATquboResult.py +++ b/util/SATquboResult.py @@ -1,5 +1,5 @@ import json -from kSAT import kSAT +from .kSAT import kSAT class SATquboResult: diff --git a/util/__init__.py b/util/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/util/kSAT.py b/util/kSAT.py index 47ac0a5..a8faaea 100644 --- a/util/kSAT.py +++ b/util/kSAT.py @@ -150,7 +150,20 @@ class kSAT: line = inputFile.readline() inputFile.close() - + + def writeJSONLike(self): + jsonLike = {} + + jsonLike["clauses"] = self.__clauses + + return jsonLike + + def readJSONLike(self, jsonLike): + self.reset() + + for clause in jsonLike["clauses"]: + self.addClause(clause) + def reset(self): self.__clauses = [] diff --git a/util/minisatUtils.py b/util/minisat.py similarity index 100% rename from util/minisatUtils.py rename to util/minisat.py diff --git a/util/randomSAT.py b/util/randomSAT.py index 8ce050e..6a7d625 100644 --- a/util/randomSAT.py +++ b/util/randomSAT.py @@ -2,7 +2,7 @@ import numpy as np import random -import kSAT +from . import kSAT def generateRandomKSAT(numberOfClauses, numberOfVariables, diff --git a/util/scriptUtils.py b/util/script.py similarity index 98% rename from util/scriptUtils.py rename to util/script.py index 71e361e..51d4159 100644 --- a/util/scriptUtils.py +++ b/util/script.py @@ -54,8 +54,7 @@ class ArgParser: if self.__instanceDirArgSet: self.__config = readConfig(os.path.join(args["dataset_dir"], "dataset.config")) - - self.__parseDatasetConfig() + self.__parseDatasetConfig() for alias, flag in self.__flags.items(): self.__parsedArgs[alias] = self.__processFlag(args, flag) diff --git a/verifyWMISresult.py b/verifyWMISresult.py index 820f17e..f3ec7d1 100755 --- a/verifyWMISresult.py +++ b/verifyWMISresult.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 -from kSAT import kSAT -import SATquboResult +from util.kSAT import kSAT +from util import SATquboResult import argparse def main():