def test_decoder_basic_1(self): sat_problem = SATProblem() Encoder.encode_basic_clause(self.tbn_problem, sat_problem) Encoder.increment_min_representatives(self.tbn_problem, sat_problem) sat_problem.solve() polymers = Decoder.decode_boolean_values(self.tbn_problem, sat_problem) self.assertEqual(len(polymers), 1)
def test_solve_empty_clause(self): sat_problem = SATProblem() sat_problem.constraint_clauses.append([]) sat_problem.solve(True) success = sat_problem.success result = sat_problem.result self.assertFalse(success) self.assertEqual(0, len(result)) self.assertEqual([], result)
def test_solve_single_clause(self): sat_problem = SATProblem() sat_problem.constraint_clauses.append([1]) sat_problem.solve(True) success = sat_problem.success result = sat_problem.result self.assertTrue(success) self.assertEqual(1, len(result)) self.assertEqual([1], result)
def test_basic(self): tbn_file = open("../input/classic.txt", 'rt') tbn_problem = parse_input_lines(tbn_file.readlines(), []) tbn_file.close() sat_problem = SATProblem() Encoder.encode_basic_clause(tbn_problem, sat_problem) while sat_problem.success: Encoder.increment_min_representatives(tbn_problem, sat_problem) sat_problem.solve() self.assertEqual(sat_problem.min_reps, 4)
def test_solve_basic_clauses(self): sat_problem = SATProblem() sat_problem.constraint_clauses.append([1, 2, 3]) sat_problem.constraint_clauses.append([-2]) sat_problem.constraint_clauses.append([-3]) sat_problem.solve(True) success = sat_problem.success result = sat_problem.result self.assertTrue(success) self.assertEqual(3, len(result)) self.assertEqual([1, -2, -3], result)
def get_stable_config(tbn_lines, constr_lines, gen_count, init_k, celery_task=None): # parse the input to encode it into BindingSite/Monomer classes t0 = time.time() # Celery Task is flag to indicate if a call is made from library or from a celery broker worker API if celery_task is not None: if celery_task.is_aborted(): raise EarlyTerminationException(0, 0) celery_task.update_state(state="PROGRESS", meta={ 'status': "Progress", 'count': 0, 'k': 0 }) tbn_problem = parse_input_lines(tbn_lines, constr_lines) tbn_problem.gen_count = gen_count tbn_problem.init_k = init_k configs = [] # encode problem to SAT solver compatible problem sat_problem = SATProblem() Encoder.encode_basic_clause(tbn_problem, sat_problem) print() print("[1] COMPUTING ORIGINAL STABLE CONFIGURATION:") # Increment solver to minimum polymers before computing while sat_problem.min_reps < tbn_problem.init_k: Encoder.increment_min_representatives(tbn_problem, sat_problem) original_num_reps = 0 # solve the problem (SAT solver) while sat_problem.success: if celery_task is not None: if celery_task.is_aborted(): raise EarlyTerminationException(1, sat_problem.min_reps) celery_task.update_state(state="PROGRESS", meta={ 'status': "Progress", 'count': 1, 'k': sat_problem.min_reps }) else: print("... Checking for k =", sat_problem.min_reps, "polymers") sat_problem.solve(False) if (sat_problem.success): original_num_reps = sat_problem.min_reps Encoder.increment_min_representatives(tbn_problem, sat_problem) if original_num_reps > 0: if celery_task is None: print("Found an original stable configuration with [", original_num_reps, "] polymers.\n") else: if celery_task is None: print("Could not find original stable configuration with [", tbn_problem.init_k, "] polymers.\n") # Printing execution time print("\nCompleted in", time.time() - t0, "seconds.\n") raise MinPolymersExceedEntropyException(tbn_problem.init_k) # Decode the problem into polymers and print results polymers = Decoder.decode_boolean_values(tbn_problem, sat_problem) if celery_task is None: for index, polymer in enumerate(polymers): print("\t" + "Polymer number", index + 1) for monomer in polymer.monomer_list: print("\t\t" + str(monomer)) print() # SOLVING WITH CONSTRAINTS if len(tbn_problem.constraints) != 0: configs = get_stable_configs_using_constraints(tbn_problem, sat_problem, original_num_reps, celery_task) # SOLVING WITHOUT CONSTRAINTS else: # Add original configuration solution to result list configs.append(polymers) # Generate additional unique solutions if tbn_problem.gen_count > 1: additional_configs = get_stable_configs_using_constraints( tbn_problem, sat_problem, original_num_reps, celery_task) # Add results to existing solution list configs.extend(additional_configs) # Printing execution time if celery_task is None: print("\nCompleted in", time.time() - t0, "seconds.\n") return configs, original_num_reps