def _generate_random_assignment(self, csp): assignment = Assignment() for var in csp.get_variables(): value = select_randomly_from_list(list(csp.get_domain(var))) assignment.set_assignment(var, value) return assignment
def find_model_for(self, sentence, number_of_flips, probability_of_random_walk): model = Model() cnf_sentence = CNFTransformer().transform(sentence) clauses = CNFClauseGatherer().collect(cnf_sentence) symbols = SymbolsCollector().collect_symbols(sentence) # model <- a random assignment of true/false to the symbols in clauses for symbol in symbols: model = model.extend(symbol, randbool()) # for i = 1 to max_flips do for i in range(number_of_flips): # if model satisfies clauses then return model if self._all_clauses_satisfied(clauses, model): return model # clause <- a randomly selected clause from clauses that is false in model symbols_list = list( self._get_symbols_of_randomly_selected_false_clause( clauses, model)) # with probability p flip the value in model of a randomly selected symbol from clause if random.random() >= probability_of_random_walk: symbol = select_randomly_from_list(symbols_list) # else flip whichever symbol in clause maximizes the number of satisfied clauses else: symbol = self._get_symbol_whose_flip_maximises_satisfied_clauses( clauses, model, symbols_list) model.flip(symbol) return None
def find_model_for(self, sentence, number_of_flips, probability_of_random_walk): model = Model() cnf_sentence = CNFTransformer().transform(sentence) clauses = CNFClauseGatherer().collect(cnf_sentence) symbols = SymbolsCollector().collect_symbols(sentence) # model <- a random assignment of true/false to the symbols in clauses for symbol in symbols: model = model.extend(symbol, randbool()) # for i = 1 to max_flips do for i in range(number_of_flips): # if model satisfies clauses then return model if self._all_clauses_satisfied(clauses, model): return model # clause <- a randomly selected clause from clauses that is false in model symbols_list = list(self._get_symbols_of_randomly_selected_false_clause(clauses, model)) # with probability p flip the value in model of a randomly selected symbol from clause if random.random() >= probability_of_random_walk: symbol = select_randomly_from_list(symbols_list) # else flip whichever symbol in clause maximizes the number of satisfied clauses else: symbol = self._get_symbol_whose_flip_maximises_satisfied_clauses(clauses, model, symbols_list) model.flip(symbol) return None
def _get_symbols_of_randomly_selected_false_clause(self, clauses, model): false_clauses = [] for clause in clauses: if not model.is_true(clause): false_clauses.append(clause) random_false_clause = select_randomly_from_list(false_clauses) return SymbolsCollector().collect_symbols(random_false_clause)
def _get_min_conflict_value_for(self, var, assignment, csp): constraints = csp.get_constraints() duplicate_assignment = assignment.copy() min_conflict = PlusInfinity() result_candidates = [] for value in csp.get_domain(var): duplicate_assignment.set_assignment(var, value) curr_conflict = self._count_conflicts(assignment, constraints) if curr_conflict <= min_conflict: if curr_conflict < min_conflict: result_candidates = [] min_conflict = curr_conflict result_candidates.append(value) if len(result_candidates) != 0: return select_randomly_from_list(result_candidates) else: return None
def solve(self, csp): # current = an initial complete assignment for csp assignment = self._generate_random_assignment(csp) self._notify_state_changed(csp) # for i = 1 to max steps do for i in range(self.max_step): # if current is a solution for csp then return current if assignment.is_solution(csp): return assignment else: # var = a randomly chosen conflicted variable from csp.VARIABLES vars = self._get_conflicted_variables(assignment, csp) var = select_randomly_from_list(vars) # value = the value v for var that minimizes CONFLICTS(var, v, current, csp) value = self._get_min_conflict_value_for(var, assignment, csp) # set var = value in current assignment.set_assignment(var, value) self._notify_state_changed(csp, assignment) # return failure return None