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.

115 lines
3.1 KiB

6 years ago
  1. #!/usr/bin/env python3
  2. import kSAT
  3. import SAT2QUBO
  4. from SATquboResult import SATquboResult
  5. import argparse
  6. from dwave_qbsolv import QBSolv
  7. import os
  8. import collections
  9. import json
  10. from tqdm import tqdm
  11. import scriptUtils
  12. def main():
  13. arguments = __parseArguments()
  14. satInstance = kSAT.kSAT()
  15. print("reading ksat...")
  16. satInstance.readDIMACS(arguments["instancePath"])
  17. print()
  18. result = __runWMISquboOnSatInstance(satInstance)
  19. resultPath = os.path.join(
  20. os.path.join(arguments["resultDir"],
  21. "run%d" % arguments["run"]),
  22. "%s.out" % arguments["instanceFileName"])
  23. print()
  24. print("writing results to file...")
  25. __writeResult(result, resultPath)
  26. def __parseArguments():
  27. parser = scriptUtils.ArgParser()
  28. parser.addInstanceDirArg()
  29. parser.addArg(alias="instancePath", shortFlag="i", longFlag="instance",
  30. help="instance file, has to be in DIMACS format", type=str)
  31. parser.addArg(alias="resultDir", shortFlag="o", longFlag="wmis_result_dir",
  32. help="the wmis result directory", type=str,
  33. ignoreDatabaseConfig=False)
  34. parser.addArg(alias="run", shortFlag="r", longFlag="run",
  35. help="results will get saved unter [instance]_[run].out", type=int)
  36. arguments = parser.parse()
  37. arguments["instanceFileName"] = os.path.basename(arguments["instancePath"])
  38. return arguments
  39. def __runWMISquboOnSatInstance(satInstance):
  40. print("generating wmis qubo...")
  41. qubo = SAT2QUBO.WMISdictQUBO(satInstance)
  42. print()
  43. print("running gbsolv...")
  44. qbresult = QBSolv().sample_qubo(Q=qubo, find_max=True)
  45. print()
  46. print("packing results...")
  47. results = __packResults(satInstance, qbresult)
  48. return results
  49. def __packResults(satInstance, qbresult):
  50. results = []
  51. samples = list(qbresult.samples())
  52. occurrences = qbresult.data_vectors["num_occurrences"]
  53. for i in tqdm(range(len(samples))):
  54. quboResult = __satQuboResultFromSample(samples[i])
  55. quboResult.setOccurrences(occurrences[i])
  56. quboResult.setSatisfiesInstance(satInstance)
  57. results.append(quboResult)
  58. return results
  59. def __writeResult(results, resultPath):
  60. resultDir = os.path.dirname(resultPath)
  61. if not os.path.exists(resultDir):
  62. os.makedirs(resultDir)
  63. resultFile = open(resultPath, "w+")
  64. for result in tqdm(results):
  65. resultFile.write(json.dumps(result.toPrimitive()))
  66. resultFile.write("\n\n")
  67. resultFile.close()
  68. def __satQuboResultFromSample(sample):
  69. result = SATquboResult()
  70. for binding in sample:
  71. isActive = True if sample[binding] == 1 else False
  72. result.addBinding(binding, isActive)
  73. #if sample[binding] == 1:
  74. #result.addActiveBinding(binding)
  75. return result
  76. if __name__ == "__main__":
  77. main()