def solve(heuristic, file): start = time.time() heuristics = {0: "Random", 1: "Jeroslaw", 2: "MOMs"} print("Solving using {} heuristic".format(heuristics[int(heuristic)])) which_method = int(heuristic) # List for data analysis EXCEL # [#unit_clauses, %of reduction from first simplify, #splits, #backtrackings, #time] results = [0, 0, 100., 0, 0, 0] split_count = 0 back_track_count = 0 problem_start_time = time.time() rules = read_files.read_DIMACS_file(file) rules, literals_dict, truth_values = read_files.init_database(rules) results[1] = len(truth_values) old_clauses_count = len(rules) done = False rules_before_split, literals_dict_before_split, truth_values_before_split = {}, {}, {} split_choice, neg_literal = [], [] while done == False: back_track = False # Simplify rules, literals_dict, truth_values, split_choice, neg_literal, \ rules_before_split, literals_dict_before_split, truth_values_before_split, back_track = \ simplify.simplify(rules, literals_dict, truth_values, split_choice, neg_literal, rules_before_split, literals_dict_before_split, truth_values_before_split, back_track) new_clauses_count = len(rules) if split_count > 2000: break if back_track: back_track_count += 1 if new_clauses_count == 0: pretty_print.solution(truth_values) results[3] = split_count results[4] = back_track_count results[5] = float("{0:.2f}".format(time.time() - problem_start_time)) print("Solved in {0:.2f}s".format(time.time() - problem_start_time)) print("# splits {}".format(split_count)) done = True with open("{}.out".format(file), 'w') as f: for truth in truth_values: f.write("{} 0\n".format(truth)) elif old_clauses_count == new_clauses_count and back_track == False: split_count += 1 # Split rules, literals_dict, truth_values, split_choice, neg_literal, \ rules_before_split, literals_dict_before_split, truth_values_before_split = \ split.split(rules, literals_dict, truth_values, split_choice, neg_literal, rules_before_split, literals_dict_before_split, truth_values_before_split, which_method) old_clauses_count = new_clauses_count
def solve(heuristic, file): start = time.time() heuristics = {1: "Basic DPLL -- Random", 2: "Jeroslow-Wang", 3: "MOMs"} print('') print('=============== SAT Solver ================') print("Solving using {} heuristic".format(heuristics[int(heuristic)])) print('...\n') which_method = int(heuristic) rules = read_files.read_DIMACS_file(file) rules, literals_dict, truth_values = read_files.init_database(rules) rules_before_split, literals_dict_before_split, truth_values_before_split = {}, {}, {} split_choice, neg_literal = [], [] old_clauses_count = len(rules) back_track_count = 0 split_count = 0 done = False problem_start_time = time.time() while done == False: back_track = False # Simplify rules, literals_dict, truth_values, split_choice, neg_literal, \ rules_before_split, literals_dict_before_split, truth_values_before_split, back_track, file = \ simplify.simplify(rules, literals_dict, truth_values, split_choice, neg_literal, rules_before_split, literals_dict_before_split, truth_values_before_split, back_track, file) new_clauses_count = len(rules) if back_track: back_track_count += 1 if new_clauses_count == 0: print('The problem has a solution -- SAT') print("Solved in {0:.2f}s".format(time.time() - problem_start_time)) print("Number of splits: {}".format(split_count)) print("Number of backtracks: {}".format(back_track_count)) print('The solution can be found at the file: {}.out'.format( str(file))) print('') done = True with open("{}.out".format(file), 'w') as f: f.write("p cnf {} {}\n".format(len(truth_values), len(truth_values))) for truth in truth_values: f.write("{} 0\n".format(truth)) elif old_clauses_count == new_clauses_count and back_track == False: split_count += 1 # Split rules, literals_dict, truth_values, split_choice, neg_literal, \ rules_before_split, literals_dict_before_split, truth_values_before_split = \ split.split(rules, literals_dict, truth_values, split_choice, neg_literal, rules_before_split, literals_dict_before_split, truth_values_before_split, which_method) old_clauses_count = new_clauses_count
for sdk in range(1, len(sudokus)+1)[0:2]: orig_truth_values = sudokus[sdk] # {} when we submit, sudokus[sdk] for num_truth in range(mean - interval, len(orig_truth_values)): truth_values = set(random.sample(orig_truth_values,num_truth)) problem_start_time = time.time() # List for data analysis EXCEL # [#unit_clauses, %of reduction fro first simplify, #splits, #backtrackings, #time] results = [sdk ,0, 100., 0, 0, 0, 0] split_count = 0 # Position 0: get the #unit_clauses results[1] = len(truth_values) rules_before_split, literals_dict_before_split, truth_values_before_split= {}, {}, {} split_choice, neg_literal = [], [] rules = read_files.read_DIMACS_file("sudoku-rules.txt") rules, literals_dict = read_files.init_database(rules) clauses = len(rules) variables = len(truth_values) print('========= SAT Problem: {}/{} ========='.format(sdk, len(sudokus))) old_len = len(rules) print(' Initial number of rules:', len(rules), '\n') # We need it for data analysis init_len_clauses = len(rules) finish = False while finish == False: bk = False # Simplify
rules_before_split, literals_dict_before_split, truth_values_before_split = {}, {}, {} split_choice, neg_literal = [], [] # SDKS # easy_sdk = "sdks/1000_sudokus.txt" # hard_sdk = 'sdks/damnhard.sdk.txt' # sdk_rules = "sdks/sudoku-rules.txt" # sudokus = read_files.read_sudokus_file(hard_sdk) # truth_values = sudokus[27] # rules = read_files.read_DIMACS_file(sdk_rules) # rules, literals_dict = read_files.init_database(rules) # SAT file = 'sudoku2.txt' DIMACS = read_files.read_DIMACS_file(file) rules, literals_dict, truth_values = read_files.init_database(DIMACS) # Choose heuristic: 0 = Basic DPLL (random), 1 = Jeroslow-Wang method, 2 = MOMs method which_method = 0 print('============ SAT Solver =============') print_heuristic = ['Basic DPLL (random)', 'Jeroslow-Wang method
', 'MOMs method'] print(' Heuristic: ', print_heuristic[which_method]) print('=====================================\n') old_clauses_count = len(rules) back_track_count = 0 split_count = 0 done = False problem_start_time = time.time()