def force_max_change(self, max_change, indent=0): print_message("Forcing the pattern to never change by more than " + str(max_change) + " cells", 3, indent=indent) width = len(self.grid[0][0]) height = len(self.grid[0]) duration = len(self.grid) for t in range(1, duration): literals = [] for x in range(width): for y in range(height): literal = str(t) + "_" + str(x) + "_" + str(y) + "_changes" self.clauses.append(implies([self.grid[t][y][x], negate(self.grid[0][y][x])], literal)) self.clauses.append(implies([negate(self.grid[t][y][x]), self.grid[0][y][x]], literal)) literals.append(literal) print_message("Generation " + str(t), 3, indent=indent + 1) self.force_at_most(literals, max_change, indent=indent + 2) print_message("Done\n", 3, indent=indent)
def force_unequal(self, argument_0, argument_1=None): if argument_1 is not None: assert isinstance(argument_0, str) and isinstance(argument_1, str), "force_equal arguments not understood" cell_pair_list = [(argument_0, argument_1)] elif argument_0 == []: return elif isinstance(argument_0[0], str): assert len(argument_0) == 2 and isinstance(argument_0[1], str), "force_equal arguments not understood" cell_pair_list = [argument_0] else: cell_pair_list = argument_0 clause = [] for cell_pair in cell_pair_list: cells_equal = str(cell_pair[0]) + "_equals_" + str(cell_pair[1]) self.clauses.append(implies(cell_pair, cells_equal)) self.clauses.append(implies(map(negate, cell_pair), cells_equal)) clause.append(negate(cells_equal)) self.clauses.append(clause)
def force_transition(self, grid, x, y, t, method): cell = grid[t][y][x] duration = len(grid) if method == 0: src.taocp_variable_scheme.transition_rule(self, grid, x, y, t) elif method == 1: predecessor_cell = grid[(t - 1) % duration][y][x] neighbours = neighbours_from_coordinates(grid, x, y, t, background_grid=self.background_grid) # If any four neighbours were live, then the cell is # dead for four_neighbours in itertools.combinations(neighbours, 4): clause = implies(four_neighbours, negate(cell)) self.clauses.append(clause) # If any seven neighbours were dead, the cell is dead for seven_neighbours in itertools.combinations(neighbours, 7): clause = implies([negate(neighbour) for neighbour in seven_neighbours], negate(cell)) self.clauses.append(clause) # If the cell was dead, and any six neighbours were # dead, the cell is dead for six_neighbours in itertools.combinations(neighbours, 6): clause = implies([negate(predecessor_cell)] + [negate(neighbour) for neighbour in six_neighbours], negate(cell)) self.clauses.append(clause) # If three neighbours were alive and five were dead, # then the cell is live for three_neighbours in itertools.combinations(neighbours, 3): neighbours_counter = collections.Counter(neighbours) neighbours_counter.subtract(three_neighbours) three_neighbours, five_neighbours = list(three_neighbours), list(neighbours_counter.elements()) clause = implies(three_neighbours + [negate(neighbour) for neighbour in five_neighbours], cell) self.clauses.append(clause) # Finally, if the cell was live, and two neighbours # were live, and five neighbours were dead, then the # cell is live (independently of the final neighbour) for two_neighbours in itertools.combinations(neighbours, 2): neighbours_counter = collections.Counter(neighbours) neighbours_counter.subtract(two_neighbours) two_neighbours, five_neighbours = list(two_neighbours), list(neighbours_counter.elements())[1:] clause = implies( [predecessor_cell] + two_neighbours + [negate(neighbour) for neighbour in five_neighbours], cell) self.clauses.append(clause) elif method == 2: predecessor_cell = grid[(t - 1) % duration][y][x] neighbours = neighbours_from_coordinates(self.grid, x, y, t, background_grid=self.background_grid) booleans = [True, False] # For each combination of neighbourhoods for predecessor_cell_alive in booleans: for neighbours_alive in itertools.product(booleans, repeat=8): p = "S" if predecessor_cell_alive else "B" transition = src.rules.transition_from_cells(neighbours_alive) transition_literal = self.rule[p + transition] self.clauses.append(implies( [transition_literal] + [negate(predecessor_cell, not predecessor_cell_alive)] + list( map(negate, neighbours, map(lambda q: not q, neighbours_alive))), cell)) self.clauses.append(implies( [negate(transition_literal)] + [negate(predecessor_cell, not predecessor_cell_alive)] + list( map(negate, neighbours, map(lambda q: not q, neighbours_alive))), negate(cell)))
def define_cardinality_variable(self, literals, at_least, already_defined=None, preprocessing=True): """Generates clauses defining a cardinality variable""" if preprocessing: # Remove "0"s and "1"s literals_copy = [] for literal in literals: if literal in ["0", "1"]: at_least -= int(literal) else: literals_copy.append(literal) literals_copy.sort() else: literals_copy = copy.deepcopy(literals) if already_defined is None: already_defined = [] def cardinality_variable_name(literals, at_least): return "at_least_" + str(at_least) + "_of_" + str(literals) name = cardinality_variable_name(literals_copy, at_least) if name not in already_defined: already_defined.append(name) max_literals = len(literals_copy) # The most literals that could be true max_literals_1 = max_literals // 2 literals_1 = literals_copy[:max_literals_1] variables_to_define_1 = [] # A list of variables we need to define max_literals_2 = max_literals - max_literals_1 literals_2 = literals_copy[max_literals_1:] variables_to_define_2 = [] # A list of variables we need to define # If at_least is obviously too small or too big, give the obvious answer if at_least <= 0: self.clauses.append([name]) elif at_least > max_literals: self.clauses.append([negate(name)]) elif max_literals == 1: literal = literals_copy[0] self.clauses.append([negate(name), literal]) self.clauses.append([name, negate(literal)]) # Otherwise define the appropriate clauses else: if at_least <= max_literals_1: self.clauses.append( implies( cardinality_variable_name(literals_1, at_least), name)) variables_to_define_1.append(at_least) for j in range(1, max_literals_2 + 1): for i in range(1, max_literals_1 + 1): if i + j == at_least: self.clauses.append( implies( [cardinality_variable_name(literals_1, i), cardinality_variable_name(literals_2, j)], name)) variables_to_define_1.append(i) variables_to_define_2.append(j) if at_least <= max_literals_2: self.clauses.append( implies( cardinality_variable_name(literals_2, at_least), name)) variables_to_define_2.append(at_least) if at_least > max_literals_2: i = at_least - max_literals_2 self.clauses.append( implies( negate(cardinality_variable_name(literals_1, i)), negate(name))) variables_to_define_1.append(i) for j in range(1, max_literals_2 + 1): for i in range(1, max_literals_1 + 1): if i + j == at_least + 1: self.clauses.append(implies([ negate(cardinality_variable_name(literals_1, i)), negate(cardinality_variable_name(literals_2, j))], negate(name))) variables_to_define_1.append(i) variables_to_define_2.append(j) if at_least > max_literals_1: j = at_least - max_literals_1 self.clauses.append( implies( negate(cardinality_variable_name(literals_2, j)), negate(name))) variables_to_define_2.append(j) # Remove duplicates from our lists of child variables we need to define variables_to_define_1 = set(variables_to_define_1) variables_to_define_2 = set(variables_to_define_2) # Define the child variables for at_least_1 in variables_to_define_1: self.define_cardinality_variable(literals_1, at_least_1, already_defined, preprocessing=False) for at_least_2 in variables_to_define_2: self.define_cardinality_variable(literals_2, at_least_2, already_defined, preprocessing=False) return name
def transition_rule(search_pattern, grid, x, y, t): """Creates clauses enforcing the transition rule at coordinates x, y, t of grid""" duration = len(grid) # These clauses define variables a_i meaning at least i of the neighbours were alive at time t - 1 definition_clauses(search_pattern, grid, x, y, (t - 1) % duration, "a", at_least=2) definition_clauses(search_pattern, grid, x, y, (t - 1) % duration, "a", at_least=3) definition_clauses(search_pattern, grid, x, y, (t - 1) % duration, "a", at_least=4) cell = literal_name(search_pattern, grid, x, y, t) predecessor_cell = literal_name(search_pattern, grid, x, y, (t - 1) % duration) # These clauses implement the cellular automaton rule # If there are at least 4 neighbours in the previous generation then the cell dies search_pattern.clauses.append( implies( literal_name(search_pattern, grid, x, y, (t - 1) % duration, "a", at_least=4), negate(cell))) # If there aren't at least 2 neighbours in the previous generation then the cell dies search_pattern.clauses.append( implies( negate( literal_name(search_pattern, grid, x, y, (t - 1) % duration, "a", at_least=2)), negate(cell))) # If the predecessor is dead and there aren't at least 3 neighbours then the cell dies search_pattern.clauses.append( implies([ negate(predecessor_cell), negate( literal_name(search_pattern, grid, x, y, (t - 1) % duration, "a", at_least=3)) ], negate(cell))) # If there are exactly 3 neighbours then the cell lives search_pattern.clauses.append( implies([ negate( literal_name(search_pattern, grid, x, y, (t - 1) % duration, "a", at_least=4)), literal_name(search_pattern, grid, x, y, (t - 1) % duration, "a", at_least=3) ], cell)) # If the predecessor is alive and there are at least 2 neighbours but not at least 4 neighbours then the cell lives search_pattern.clauses.append( implies([ predecessor_cell, literal_name(search_pattern, grid, x, y, (t - 1) % duration, "a", at_least=2), negate( literal_name(search_pattern, grid, x, y, (t - 1) % duration, "a", at_least=4)) ], cell))