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.

121 lines
3.6 KiB

6 years ago
  1. #!/usr/bin/env python3
  2. import argparse
  3. import os
  4. import glob
  5. import json
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8. import configparser
  9. import scriptUtils
  10. def main():
  11. args = __parseArguments()
  12. __stats(args["comparisonDir"], args["outputDir"])
  13. def __parseArguments():
  14. parser = scriptUtils.ArgParser()
  15. parser.addInstanceDirArg()
  16. parser.addArg(alias="comparisonDir", shortFlag="c", longFlag="comparison_dir",
  17. help="the direcotry with all comparison files", type=str)
  18. parser.addArg(alias="outputDir", shortFlag="s", longFlag="comparison_stats_dir",
  19. help="Directory to store the stats", type=str)
  20. arguments = parser.parse()
  21. arguments["datasetDir"] = os.path.abspath(arguments["datasetDir"])
  22. arguments["comparisonDir"] = os.path.join(arguments["datasetDir"],
  23. arguments["comparisonDir"])
  24. arguments["outputDir"] = os.path.join(arguments["datasetDir"],
  25. arguments["outputDir"])
  26. return arguments
  27. def __stats(comparisonDir, outputDir):
  28. runs = glob.glob(os.path.join(comparisonDir, "run*"))
  29. for run in runs:
  30. stats = __collectStats(run)
  31. runOutputDir = os.path.join(outputDir, os.path.basename(run))
  32. __writeStats(stats, runOutputDir)
  33. def __collectStats(comparisonDir):
  34. files = glob.glob(os.path.join(comparisonDir, "*.cmp"))
  35. stats = {}
  36. stats["match"] = 0
  37. stats["false_positive"] = 0
  38. stats["false_negative"] = 0
  39. stats["unsat"] = 0
  40. for path in files:
  41. comparison = __readComparison(path)
  42. minisat_satisfiable = comparison["minisat_satisfiable"]
  43. qubo_satisfiable = comparison["qubo_satisfiable"]
  44. if minisat_satisfiable == qubo_satisfiable:
  45. stats["match"] += 1
  46. elif minisat_satisfiable == False and qubo_satisfiable == True:
  47. stats["false_positive"] += 1
  48. elif minisat_satisfiable == True and qubo_satisfiable == False:
  49. stats["false_negative"] += 1
  50. if not minisat_satisfiable:
  51. stats["unsat"] += 1
  52. return stats
  53. def __readComparison(path):
  54. cmpFile = open(path, "r")
  55. comparison = json.load(cmpFile)
  56. cmpFile.close()
  57. return comparison
  58. def __writeStats(stats, outputDir):
  59. if not os.path.exists(outputDir):
  60. os.makedirs(outputDir)
  61. fig = plt.figure()
  62. ax = fig.add_subplot(111)
  63. numInstances = stats["match"] + stats["false_negative"] + stats["false_positive"]
  64. matchBar = ax.bar(x=0, height=stats["match"])
  65. falsePositiveBar = ax.bar(x=1, height=stats["false_positive"])
  66. falseNegativeBar = ax.bar(x=1,
  67. height=stats["false_negative"],
  68. bottom=stats["false_positive"])
  69. ax.axhline(y=stats["match"], linestyle="--", color="gray")
  70. ax.axhline(y=stats["false_negative"], linestyle="--", color="gray")
  71. plt.ylabel("SAT Instanzen")
  72. plt.title("Verlgeich Minisat / WMIS qubo mit qbsolv")
  73. plt.xticks([0, 1], ("Gleiches Ergebnis", "Unterschiedliches Ergebnis"))
  74. plt.yticks([0, stats["match"], stats["false_negative"], numInstances])
  75. plt.legend((matchBar, falsePositiveBar, falseNegativeBar),
  76. ("Gleiches Ergebnis",
  77. "False Positive",
  78. "False Negative"))
  79. plt.savefig(os.path.join(outputDir, "stats.png"))
  80. if __name__ == "__main__":
  81. main()