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.

96 lines
3.2 KiB

3 years ago
  1. """
  2. This script is written for running Uwe Schöning's WalkSAT algorithm
  3. [Uwe Schöning:
  4. A probabilistic algorithm for k-SAT and constraint satisfaction problems.
  5. In: Proceedings of FOCS 1999, IEEE, pages 410414.]
  6. via the command line
  7. by specifying the instance to solve, and the seed to use.
  8. Uwe Schöning's WalkSAT algorithm can be seen as a special case of the probSAT algorithm
  9. [Adrian Balint, Uwe Schöning:
  10. Choosing Probability Distributions for Stochastic Local Search and the Role of Make versus Break.
  11. In: Lecture Notes in Computer Science, 2012, Volume 7317, Theory and Applications of Satisfiability Testing - SAT 2012, pages 16-29.
  12. https://www.uni-ulm.de/fileadmin/website_uni_ulm/iui.inst.190/Mitarbeiter/balint/SAT2012.pdf]
  13. with exponential function (break-only-exp-algorithm) and c_b = 1.
  14. The script will record the number of flips, the time used, and the seed used.
  15. """
  16. import argparse
  17. from subprocess import Popen, PIPE
  18. from time import sleep
  19. from os.path import exists
  20. import sys
  21. from timeit import default_timer as timer
  22. import pathlib as pl
  23. def run_alg(instance):
  24. solve_instance_path = pl.Path(__file__).absolute().parent / "util" / "solve_instance.sh"
  25. p = Popen(["bash", str(solve_instance_path), instance], stdin = PIPE, stdout = PIPE, stderr=PIPE)
  26. output, err = p.communicate()
  27. rc = p.returncode
  28. err = err.decode("utf-8") if err else ""
  29. if err != "":
  30. print(err, file=sys.stderr)
  31. return output.decode("utf-8")
  32. def parse_result(res_str):
  33. res = {}
  34. for line in res_str.splitlines():
  35. if line.startswith("c restarts"):
  36. res["restarts"] = int(line.split(":")[1].strip().split(" ")[0])
  37. elif line.startswith("c conflicts"):
  38. res["conflicts"] = int(line.split(":")[1].strip().split(" ")[0])
  39. elif line.startswith("c decisions"):
  40. res["decisions"] = int(line.split(":")[1].strip().split(" ")[0])
  41. elif line.startswith("c propagations"):
  42. res["propagations"] = int(line.split(":")[1].strip().split(" ")[0])
  43. elif line.startswith("c nb reduced Clauses"):
  44. res["reduced_clauses"] = int(line.split(":")[1])
  45. elif line.startswith("c CPU time"):
  46. res["cpu_time"] = float(line.split(":")[1].strip().split(" ")[0])
  47. elif line.startswith("s"):
  48. res["result"] = line.split(" ")[1].strip()
  49. return res
  50. def parse_instance_path(ipath):
  51. name = pl.Path(ipath).name.strip()
  52. instance = name[:-len(".cnf.bz2")]
  53. return instance
  54. if __name__ == '__main__':
  55. parser = argparse.ArgumentParser()
  56. parser.add_argument("-s", "--seed", type=int, default=1909, help="The seed.")
  57. parser.add_argument("-i", "--instance", help="The instance.")
  58. args = parser.parse_args()
  59. res_str = run_alg(str(pl.Path(args.instance).absolute()))
  60. iname = parse_instance_path(args.instance)
  61. res = parse_result(res_str)
  62. res_vals = [ iname
  63. ,res["restarts"]
  64. ,res["conflicts"]
  65. ,res["decisions"]
  66. ,res["propagations"]
  67. ,res["reduced_clauses"]
  68. ,res["cpu_time"]
  69. ,res["result"]]
  70. print(','.join(list(map(str, res_vals))))