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.

112 lines
3.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. #!/usr/bin/env python3
  2. from util import SATquboResult
  3. import argparse
  4. from util.kSAT import kSAT
  5. from util import minisat as mSatU
  6. from util import SATquboResult
  7. import os
  8. import json
  9. def main():
  10. args = __parseArguments()
  11. comparison = __compare(args["instancePath"],
  12. args["minisatResult"],
  13. args["quboResult"])
  14. outputFilePath = os.path.join(args["outputDir"],
  15. args["instanceFileName"] + ".cmp")
  16. __writeComparison(comparison, outputFilePath)
  17. def __parseArguments():
  18. parser = argparse.ArgumentParser()
  19. parser.add_argument("-i", "--instance", help="instance file, has to be in DIMACS format", type=str)
  20. parser.add_argument("-m", "--minisat_result", help="Minisat result file", type=str)
  21. parser.add_argument("-q", "--qubo_result", help="Qubo result file", type=str)
  22. parser.add_argument("-o", "--output", help="Directory to store the comparison (optional)", type=str)
  23. args = parser.parse_args()
  24. arguments = {}
  25. arguments["instancePath"] = args.instance
  26. if arguments["instancePath"] == None:
  27. arguments["instancePath"] = str(input("Instance file: "))
  28. #arguments["instancePath"]
  29. arguments["instanceFileName"] = os.path.basename(arguments["instancePath"])
  30. arguments["minisatResult"] = args.minisat_result
  31. if arguments["minisatResult"] == None:
  32. arguments["minisatResult"] = str(input("Minisat result file: "))
  33. arguments["quboResult"] = args.qubo_result
  34. if arguments["quboResult"] == None:
  35. arguments["quboResult"] = str(input("Qubo result file: "))
  36. arguments["outputDir"] = args.output
  37. if arguments["outputDir"] == None:
  38. arguments["outputDir"] = str(input("Output directory: "))
  39. arguments["outputDir"] = os.path.abspath(arguments["outputDir"])
  40. return arguments
  41. def __compare(instancePath, msatResultFile, quboResultFile):
  42. comparison = {}
  43. sat = kSAT()
  44. sat.readDIMACS(instancePath)
  45. comparison["instance"] = os.path.basename(instancePath)
  46. comparison["minisat_satisfiable"] = __getMinisatSatisfiability(msatResultFile)
  47. comparison["qubo_satisfiable"] = __getQuboSatisfiability(quboResultFile)
  48. comparison["degrees_of_variables"] = sat.getDegreesOfVariables()
  49. comparison["conflicts_per_variable"] = __countConflicts(sat.getConflicts())
  50. return comparison
  51. def __getMinisatSatisfiability(resultFile):
  52. minisatResult = mSatU.readMinisatResult(resultFile)
  53. return minisatResult["satisfiable"]
  54. def __getQuboSatisfiability(resultFile):
  55. results = SATquboResult.readResultsFromFile(resultFile)
  56. for result in results:
  57. if result.satisfiesInstance():
  58. return True
  59. return False
  60. def __writeComparison(comparison, outputFilePath):
  61. if outputFilePath == None:
  62. return
  63. outputFile = open(outputFilePath, "w+")
  64. outputFile.write(json.dumps(comparison) + "\n")
  65. outputFile.close()
  66. def __countConflicts(conflicts):
  67. conflictsPerVariable = {}
  68. for conflict in conflicts:
  69. varLabel = abs(conflict[0][1])
  70. if varLabel not in conflictsPerVariable:
  71. conflictsPerVariable[varLabel] = 1
  72. else:
  73. conflictsPerVariable[varLabel] += 1
  74. return conflictsPerVariable
  75. if __name__ == "__main__":
  76. main()