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.

168 lines
4.8 KiB

6 years ago
  1. import json
  2. from kSAT import kSAT
  3. class SATquboResult:
  4. def __init__(self):
  5. self.__assignments = set()
  6. self.__activeBindings = {}
  7. self.__hasConflicts = False
  8. self.__occurrences = 1
  9. self.__satisfiesInstance = False
  10. self.__assignmentsUpToDate = True
  11. def satisfiesInstance(self):
  12. return self.__satisfiesInstance
  13. def setSatisfiesInstance(self, satInstance):
  14. evaluation = satInstance.checkAssignment(self.getAssignmentsWithoutPositionIndex())
  15. self.__satisfiesInstance = evaluation
  16. def addBinding(self, binding, isActive):
  17. self.__assignmentsUpToDate = False
  18. clause = binding[0]
  19. assignment = binding[1]
  20. varLabel = abs(assignment)
  21. if varLabel not in self.__activeBindings:
  22. self.__activeBindings[varLabel] = []
  23. if not isActive:
  24. return
  25. bindingSign = -1 if assignment < 0 else 1
  26. self.__activeBindings[varLabel].append((clause, bindingSign))
  27. self.__checkForConflictAtVar(varLabel)
  28. def __checkForConflictAtVar(self, varLabel):
  29. if varLabel not in self.__activeBindings:
  30. return
  31. sign = None
  32. for assignment in self.__activeBindings[varLabel]:
  33. if sign != None:
  34. if sign * assignment[1] < 0:
  35. self.__hasConflicts = True
  36. return
  37. sign = assignment[1]
  38. def getAssignments(self):
  39. if not self.__assignmentsUpToDate:
  40. self.__updateAssignments()
  41. return self.__assignments
  42. def __updateAssignments(self):
  43. self.__assignments = set()
  44. for varLabel in self.__activeBindings:
  45. bindings = self.__activeBindings[varLabel]
  46. if not bindings:
  47. self.__assignments.add(-1 * varLabel)
  48. for binding in bindings:
  49. sign = binding[1]
  50. self.__assignments.add(sign * varLabel)
  51. self.__assignmentsUpToDate = True
  52. def getAssignmentsSortedByVarIndex(self):
  53. return sorted(self.getAssignments(), key=abs)
  54. def getAssignmentsWithoutPositionIndex(self):
  55. assignments = self.getAssignmentsSortedByVarIndex()
  56. for i in range(len(assignments)):
  57. assignments[i] = 0 if assignments[i] < 0 else 1
  58. return assignments
  59. def getActiveBindings(self):
  60. return self.__activeBindings
  61. def hasConflicts(self):
  62. return self.__hasConflicts
  63. def setOccurrences(self, occurrences):
  64. self.__occurrences = occurrences
  65. def getOccurrences(self):
  66. return self.__occurrences
  67. def toPrimitive(self):
  68. primitive = {}
  69. primitive["assignments"] = self.getAssignmentsSortedByVarIndex()
  70. primitive["hasConflicts"] = self.__hasConflicts
  71. primitive["activeBindings"] = self.getActiveBindings()
  72. primitive["satisfiesInstance"] = self.__satisfiesInstance
  73. return primitive
  74. def fromPrimitive(self, primitive):
  75. for varLabel in primitive["activeBindings"]:
  76. bindings = primitive["activeBindings"][varLabel]
  77. if not bindings:
  78. self.addBinding((-1, int(varLabel)), False)
  79. for binding in bindings:
  80. clause = binding[0]
  81. sign = binding[1]
  82. self.addBinding((clause, sign * int(varLabel)), True)
  83. self.__satisfiesInstance = primitive["satisfiesInstance"]
  84. def readAssignmentsFromFile(resultFilePath):
  85. resultFile = open(resultFilePath, "r")
  86. assignments = []
  87. line = resultFile.readline()
  88. while line:
  89. if line.strip():
  90. assignments.append(__parseAssignmentFromLine(line))
  91. line = resultFile.readline()
  92. resultFile.close()
  93. return assignments
  94. def __parseAssignmentFromLine(line):
  95. result = json.loads(line)
  96. assignment = [None] * abs(max(result["assignments"], key=abs))
  97. for varAssignment in result["assignments"]:
  98. assignment[abs(varAssignment) - 1] = 0 if varAssignment < 0 else 1
  99. return assignment
  100. def readResultsFromFile(resultFilePath):
  101. resultFile = open(resultFilePath, "r")
  102. results = []
  103. line = resultFile.readline()
  104. while line:
  105. if line.strip():
  106. result = SATquboResult()
  107. result.fromPrimitive(json.loads(line))
  108. results.append(result)
  109. line = resultFile.readline()
  110. return results