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.

104 lines
2.7 KiB

  1. #!python3
  2. from pysat.formula import CNF
  3. from pysat.solvers import Glucose4
  4. import pathlib as pl
  5. import random
  6. import bz2
  7. import sys
  8. import argparse
  9. def main():
  10. args = parse_args()
  11. instances = get_instance_paths(pl.Path(args.instance_dir))
  12. global CORE_DIR
  13. CORE_DIR = pl.Path(args.cores_dir)
  14. global EXTENDED_DIR
  15. EXTENDED_DIR = pl.Path(args.extensions_dir)
  16. print(CORE_DIR)
  17. for instance in instances:
  18. run(instance, args.nr_per_instance)
  19. def parse_args():
  20. parser = argparse.ArgumentParser()
  21. parser.add_argument( "-i"
  22. ,"--instance_dir"
  23. ,type=str
  24. ,default="./instances"
  25. ,help="directory containing the instances")
  26. parser.add_argument( "-c"
  27. ,"--cores_dir"
  28. ,type=str
  29. ,default="./cores"
  30. ,help="directory containing the cores")
  31. parser.add_argument( "-e"
  32. ,"--extensions_dir"
  33. ,type=str
  34. ,default="./extensions"
  35. ,help="directory containing the core extensions")
  36. parser.add_argument( "-n"
  37. ,"--nr_per_instance"
  38. ,type=int
  39. ,default=1
  40. ,help="number of extensions per instance")
  41. return parser.parse_args()
  42. def get_instance_paths(instances_dir):
  43. instances = []
  44. for p in instances_dir.iterdir():
  45. if str(p).endswith(".cnf.bz2"):
  46. instances.append(p)
  47. return instances
  48. def run(instance, calls_per_instance):
  49. ipath = pl.Path(instance)
  50. iname = ipath.stem[:-4]
  51. f = CNF(str(ipath))
  52. for i in range(calls_per_instance):
  53. cores = parse_cores(iname)
  54. print(iname)
  55. print(len(f.clauses), len(cores))
  56. print("{0:.3g}".format(len(cores) * 100 / len(f.clauses)))
  57. print()
  58. def parse_cores(instance):
  59. cores = []
  60. with bz2.open(CORE_DIR / (instance + ".cores.bz2"), "tr") as cores_file:
  61. cores = list(map(lambda l: l.strip(), cores_file.readlines()))
  62. cores = list(map(lambda l: l.split(","), cores))
  63. for i in range(len(cores)):
  64. cores[i] = list(map(int, cores[i]))
  65. cores[i] = list(map(lambda lit: -1 * lit, cores[i]))
  66. return cores
  67. def next_file_path(name):
  68. counter = 0
  69. ext_file_path = EXTENDED_DIR / ("{}_{:04}.cnf.bz2".format(name, counter))
  70. while ext_file_path.exists():
  71. counter += 1
  72. ext_file_path = EXTENDED_DIR / ("{}_{:04}.cnf.bz2".format(name, counter))
  73. return ext_file_path, counter
  74. if __name__ == "__main__":
  75. main()