Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Clause import Clause
- class DimacsController:
- """ File format
- p wcnf 4 5 15
- 15 1 -2 4 0
- 15 -1 -2 3 0
- 1 -2 -4 0
- 1 -3 2 0
- 1 1 3 0
- """
- soft = []
- hard = []
- num_literals = 0
- hard_cost = 0
- aux_count = 0
- @staticmethod
- def read_file(file_path):
- for line in open(file_path):
- if "wcnf" in line: # first line
- # Get num
- DimacsController.num_literals = int(line.split()[2])
- # Get hard_cost
- DimacsController.hard_cost = int(line.split()[4])
- else:
- cost = int(line.split()[0])
- if cost == DimacsController.hard_cost: # if hard clause
- DimacsController.hard.append(Clause.clause_from_dimacs_line(line))
- else:
- DimacsController.soft.append(Clause.clause_from_dimacs_line(line))
- # Return thre read data as hash table
- result = {}
- result["hard_cost"] = DimacsController.hard_cost
- result["num_literals"] = DimacsController.num_literals
- result["hard_clauses"] = DimacsController.hard
- result["soft_clauses"] = DimacsController.soft
- DimacsController.aux_count = DimacsController.num_literals
- return result
- @staticmethod
- def create_literal():
- aux_count += 1
- return aux_count
- @staticmethod
- def get_str(soft, hard):
- # calculate num literals
- num_literals = DimacsController.aux_count
- # Calculate num_clauses
- num_clauses = len(hard) + len(soft)
- # Get hard_cost
- hard_cost = hard[0].cost
- # Format first line of dimacs
- f_line = "p wcnf " + str(num_literals) + " " \
- + str(num_clauses) + " " \
- + str(hard_cost) + "\n"
- # Fomrat soft clauses
- soft_clauses = "\n".join(map(str, soft)) + "\n"
- # Format hard claues
- hard_clauses = "\n".join(map(str, hard))
- return f_line + soft_clauses + hard_clauses
- if __name__ == '__main__':
- DimacsController.read_file("dimacs.txt")
- print DimacsController.get_str(DimacsController.soft,
- DimacsController.hard)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement