Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. from Clause import Clause
  2.  
  3. class DimacsController:
  4. """ File format
  5. p wcnf 4 5 15
  6. 15 1 -2 4 0
  7. 15 -1 -2 3 0
  8. 1 -2 -4 0
  9. 1 -3 2 0
  10. 1 1 3 0
  11. """
  12.  
  13. soft = []
  14. hard = []
  15. num_literals = 0
  16. hard_cost = 0
  17. aux_count = 0
  18.  
  19. @staticmethod
  20. def read_file(file_path):
  21. for line in open(file_path):
  22. if "wcnf" in line: # first line
  23. # Get num
  24. DimacsController.num_literals = int(line.split()[2])
  25. # Get hard_cost
  26. DimacsController.hard_cost = int(line.split()[4])
  27. else:
  28. cost = int(line.split()[0])
  29. if cost == DimacsController.hard_cost: # if hard clause
  30. DimacsController.hard.append(Clause.clause_from_dimacs_line(line))
  31. else:
  32. DimacsController.soft.append(Clause.clause_from_dimacs_line(line))
  33.  
  34. # Return thre read data as hash table
  35. result = {}
  36. result["hard_cost"] = DimacsController.hard_cost
  37. result["num_literals"] = DimacsController.num_literals
  38. result["hard_clauses"] = DimacsController.hard
  39. result["soft_clauses"] = DimacsController.soft
  40. DimacsController.aux_count = DimacsController.num_literals
  41. return result
  42.  
  43.  
  44. @staticmethod
  45. def create_literal():
  46. aux_count += 1
  47. return aux_count
  48.  
  49. @staticmethod
  50. def get_str(soft, hard):
  51. # calculate num literals
  52. num_literals = DimacsController.aux_count
  53.  
  54. # Calculate num_clauses
  55. num_clauses = len(hard) + len(soft)
  56.  
  57. # Get hard_cost
  58. hard_cost = hard[0].cost
  59.  
  60. # Format first line of dimacs
  61. f_line = "p wcnf " + str(num_literals) + " " \
  62. + str(num_clauses) + " " \
  63. + str(hard_cost) + "\n"
  64. # Fomrat soft clauses
  65. soft_clauses = "\n".join(map(str, soft)) + "\n"
  66. # Format hard claues
  67. hard_clauses = "\n".join(map(str, hard))
  68. return f_line + soft_clauses + hard_clauses
  69.  
  70.  
  71. if __name__ == '__main__':
  72. DimacsController.read_file("dimacs.txt")
  73. print DimacsController.get_str(DimacsController.soft,
  74. DimacsController.hard)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement