def make_constraints(nwork, words): # unary constraints for variable in nwork.variables: if variable.meta['leading']: # if it's the first char in a word, it can't be 0 # as one of the conditions is no leading zeros! nwork.unary_constraints.append( UnaryConstraint(variable, lambda x: x != 0)) # 'regular'/binary constraints constraints = [] for variable in nwork.variables: if variable.meta['surrogate']: other_var = nwork.get_variable(words[-1], variable.meta['column']) constraints.append( Constraint([other_var, variable], lambda x, y: x == y)) # no variable may have the same value as a different variable if variable.meta['char'] is not None: for other_var in nwork.variables: if other_var is variable: continue constraints.append( Constraint([variable, other_var], lambda x, y: x != y)) # add constraints to network nwork.constraints += constraints
def __init__(self, name, scope, satisfyingAssignments): '''Init by specifying a name and a set variables the constraint is over. Along with a list of satisfying assignments. Each satisfying assignment is itself a list, of length equal to the number of variables in the constraints scope. If sa is a single satisfying assignment, e.g, sa=satisfyingAssignments[0] then sa[i] is the value that will be assigned to the variable scope[i]. Example, say you want to specify a constraint alldiff(A,B,C,D) for three variables A, B, C each with domain [1,2,3,4] Then you would create this constraint using the call c = TableConstraint('example', [A,B,C,D], [[1, 2, 3, 4], [1, 2, 4, 3], [1, 3, 2, 4], [1, 3, 4, 2], [1, 4, 2, 3], [1, 4, 3, 2], [2, 1, 3, 4], [2, 1, 4, 3], [2, 3, 1, 4], [2, 3, 4, 1], [2, 4, 1, 3], [2, 4, 3, 1], [3, 1, 2, 4], [3, 1, 4, 2], [3, 2, 1, 4], [3, 2, 4, 1], [3, 4, 1, 2], [3, 4, 2, 1], [4, 1, 2, 3], [4, 1, 3, 2], [4, 2, 1, 3], [4, 2, 3, 1], [4, 3, 1, 2], [4, 3, 2, 1]]) as these are the only assignments to A,B,C respectively that satisfy A+B=C. ''' Constraint.__init__(self, name, scope) self._name = "TableCnstr_" + name self.satAssignments = satisfyingAssignments
def __init__(self, name, scope, required_values, lower_bound, upper_bound): Constraint.__init__(self, name, scope) self._name = "NValues_" + name self._required = required_values self._lb = lower_bound self._ub = upper_bound
def __init__(self, name, qi, qj, i, j): self._name = "Queen_" + name # print "qi:", qi.getValue() # print "qj:", qj.getValue() scope = [qi, qj] Constraint.__init__(self, name, scope) table = [] for x in qi.domain(): for y in qj.domain(): diag = abs(x - y) == abs(i - j) if not diag and x != y: table.append([x, y]) self.satAssignments = table
def __init__(self, name, qi, qj, i, j): scope = [qi, qj] Constraint.__init__(self, name, scope) self._name = "QueenCnstr_" + name self.i = i self.j = j
def __init__(self, name, scope): Constraint.__init__(self, name, scope) self._name = "AllDiff_" + name
def __init__(self, name, scope): if len(scope) != 2: print "Error Neq Constraints are only between two variables" Constraint.__init__(self, name, scope) self._name = "NeqCnstr_" + name
def __init__(self, name, scope, flights): Constraint.__init__(self,name, scope) self._name = "coverAllFlight_" + name self._scope = scope self._flights = flights
def __init__(self, name, scope, values): Constraint.__init__(self, name, scope) self._name = "all_Once " + name self._scope = scope self._values = values
def __init__(self, name, scope, rest_frequence): Constraint.__init__(self, name, scope) self._name = "tafterc" + name self.rf = rest_frequence
def __init__(self, name, scope, close_distance_buildings): Constraint.__init__(self, name, scope) self._name = "tafterc" + name self.cdb = close_distance_buildings
def __init__(self, name, scope, lec_list, tut_list): Constraint.__init__(self, name, scope) self._name = "tafterc" + name self.lec_list = lec_list self.tut_list = tut_list
def __init__(self, name, scope, i, j): if len(scope) != 2: print("Error Neq Constraints are only between two variables") Constraint.__init__(self, name, scope) self._name = "NeqCnstr_" + name self._abs_diff = abs(i - j)
def run_map_coloring(): # Generates random points coords = [] for _ in range(POINTS_NUMBER): point = [rd.randint(1, F_WIDTH), rd.randint(1, F_HEIGHT)] if point not in coords: coords.append(point) coords = [[x * LINE_DIS, y * LINE_DIS] for x, y in coords] gui = GUI(F_WIDTH, F_HEIGHT) gui.draw_background() # Connect points randomly lines = [] for _ in range(3): rd.shuffle(coords) for X in coords: # Sort by euclidean distance and get the first nearest dstn = sorted(coords, key=lambda point: math.dist(point, X)) for Y in dstn[1:]: # If two points on the line have been already chosen if [X, Y] in lines or [Y, X] in lines: continue # Check if current line intersects any other if any(map(lambda x: intersect(X, Y, *x), lines)): continue gui.draw_line(*X, *Y) lines.append([X, Y]) break # Prepare data for csp # Does not belong to problem solving grouped = {str(c): [] for c in coords} for [s, ends] in lines: if str(s) in grouped: grouped[str(s)].append(ends) # Initiate constraints and variables for map coloring problem VAR = ['red', 'blue', 'green', 'black', 'orange', 'yellow'] CONSTRAINTS = [] for x1 in grouped.keys(): for x2 in grouped[x1]: CONSTRAINTS.append( Constraint( [str(x1), str(x2)], lambda x, x1=x1, x2=x2: x[str(x1)][0] != x[str(x2)][0])) ''' ''' # Solve csp problem csp = CSP(list(VAR), {str(i): list(VAR) for i in grouped.keys()}, CONSTRAINTS) csp.solve_backtracking() # csp.solve_forward_checking() # csp.solve_ac3() if len(csp.sols) == 0: print('Solution does not exists') # gui.draw_color_points(coords, [x[0] for x in csp.doms.values()]) else: gui.draw_color_points(coords, [x[0] for x in csp.sols[0].values()]) # pass gui.root.mainloop()
def __init__(self, name, scope, sp): if len(scope) != 2: print("Error BinaryClassConstraint are only between two variables") Constraint.__init__(self, name, scope) self._name = "BinaryCnstr_" + name self.sp = sp
def __init__(self, name, scope, value): Constraint.__init__(self, name, scope) self._name = "checkAllFlights" + name self._value = value