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.

105 lines
3.4 KiB

6 years ago
  1. #!/usr/bin/env python3
  2. import numpy as np
  3. import kSAT
  4. from tqdm import tqdm
  5. import math
  6. __VERTEX_WEIGHT__ = 1
  7. __EDGE_WEIGHT__ = -2
  8. def WMISdictQUBO(kSATInstance):
  9. quboInstance = {}
  10. for clauseIndex in tqdm(range(kSATInstance.getNumberOfClauses())):
  11. clause = kSATInstance.getClause(clauseIndex)
  12. # build triangles
  13. for varIndexInClause in range(len(clause)):
  14. label = kSATInstance.getLableOfBinding(clauseIndex,
  15. clause[varIndexInClause])
  16. quboInstance[(label, label)] = __VERTEX_WEIGHT__
  17. for i in range(varIndexInClause + 1, len(clause)):
  18. targetLabel = kSATInstance.getLableOfBinding(clauseIndex,
  19. clause[i])
  20. quboInstance[(label, targetLabel)] = __EDGE_WEIGHT__
  21. # connect conflicts
  22. for conflict in kSATInstance.getConflicts():
  23. quboInstance[(conflict[0], conflict[1])] = __EDGE_WEIGHT__
  24. return quboInstance
  25. class QuboWriter:
  26. def __init__(self, qubo):
  27. self.__labelIndexDict = {}
  28. self.__qubo = qubo
  29. self.__initLabelIndexDict(self.__qubo)
  30. print(self.__labelIndexDict)
  31. def __initLabelIndexDict(self, qubo):
  32. indexCount = 0
  33. for coupler in qubo:
  34. label1 = coupler[0]
  35. label2 = coupler[1]
  36. if label1 not in self.__labelIndexDict:
  37. self.__labelIndexDict[label1] = indexCount
  38. indexCount += 1
  39. if label2 not in self.__labelIndexDict:
  40. self.__labelIndexDict[label2] = indexCount
  41. indexCount += 1
  42. def write(self, filePath):
  43. quboFile = open(filePath, "w")
  44. self.__writeQuboFileHeader(quboFile)
  45. self.__writeQuboFileNodes(quboFile)
  46. self.__writeQuboFileCouplers(quboFile)
  47. quboFile.close()
  48. def __writeQuboFileHeader(self, quboFile):
  49. numberOfNodes = len(self.__labelIndexDict)
  50. numberOfCouplers = len(self.__qubo) - numberOfNodes
  51. quboFile.write("c\n")
  52. quboFile.write("c this is a generated qubo file\n")
  53. quboFile.write("c\n")
  54. quboFile.write("p qubo 0 %d %d %d\n" %(numberOfNodes,
  55. numberOfNodes,
  56. numberOfCouplers))
  57. def __writeQuboFileNodes(self, quboFile):
  58. for label in self.__labelIndexDict:
  59. self.__writeCoupler(quboFile, (label, label))
  60. def __writeCoupler(self, quboFile, coupler):
  61. indices = self.__getNodeIndicesFromCoupler(coupler)
  62. quboFile.write("%d %d %d\n" % (indices[0],
  63. indices[1],
  64. self.__qubo[coupler]))
  65. def __getNodeIndicesFromCoupler(self, coupler):
  66. index1 = self.__labelIndexDict[coupler[0]]
  67. index2 = self.__labelIndexDict[coupler[1]]
  68. if index1 <= index2:
  69. return [index1, index2]
  70. else:
  71. return [index2, index1]
  72. def __writeQuboFileCouplers(self, quboFile):
  73. for coupler in self.__qubo:
  74. if coupler[0] != coupler[1]:
  75. self.__writeCoupler(quboFile, coupler)