def _generate_solutions(cls, period, doms): """ Each child gets as domain a set of ShiftAssignables, which is generated by their shift preferences; each solution selects for each child a ShiftAssignable so that all assignments are compatible. Here we configure a problem from the constraint library, then call the solver. The problem takes a mapping, doms, from child_pk to a set of assignables for that child, plus a specification, across children, of which assignables are pairwise consistent The solver yields all compatible sets of assignables """ problem = Problem() prefs = (ShiftPreference.objects.filter( period=period).select_related('shift').select_related('child')) for pref in prefs: for assignable in pref.assignables(): if assignable.is_active: doms[assignable.child.pk].append(assignable) for k, dom in doms.items(): try: problem.addVariable(k, dom) except ValueError: return {} cached_compatibility = functools.lru_cache()( lambda a1, a2: a1.is_compatible_with(a2)) for k1, k2 in itertools.combinations(doms.keys(), 2): problem.addConstraint(cached_compatibility, (k1, k2)) # call the solver return problem.getSolutionIter()
def solve(): problem = Problem() problem.addVariables("sendmory", range(10)) problem.addConstraint(lambda d, e, y: (d + e) % 10 == y, "dey") problem.addConstraint( lambda n, d, r, e, y: (n * 10 + d + r * 10 + e) % 100 == e * 10 + y, "ndrey" ) problem.addConstraint( lambda e, n, d, o, r, y: (e * 100 + n * 10 + d + o * 100 + r * 10 + e) % 1000 == n * 100 + e * 10 + y, "endory", ) problem.addConstraint( lambda s, e, n, d, m, o, r, y: 1000 * s + 100 * e + 10 * n + d + 1000 * m + 100 * o + 10 * r + e == 10000 * m + 1000 * o + 100 * n + 10 * e + y, "sendmory", ) problem.addConstraint(NotInSetConstraint([0]), "sm") problem.addConstraint(AllDifferentConstraint()) solutions = problem.getSolutions() return solutions
def find_mapping(signal_patterns: Set[FrozenSet[str]]) -> Dict[str, str]: # Let’s express this as a constraint satisfaction problem problem = Problem() for signal in "abcdefg": problem.addVariable(signal, "abcdefg") # Each signal wire goes to a different segment problem.addConstraint(AllDifferentConstraint()) # Unambiguous digits based on count of lit segments for digit in {1, 4, 7, 8}: for wire in find_possible_signals_for(signal_patterns, digit): segments = DIGIT_TO_SEGMENTS[digit] problem.addConstraint(InSetConstraint(segments), wire) # Unambiguous segments based on how many times they appear in patterns for signal_wire in {"a", "b", "c", "d", "e", "f", "g"}: count = sum(1 for pattern in signal_patterns if signal_wire in pattern) if count == 4: problem.addConstraint(InSetConstraint(["e"]), signal_wire) elif count == 6: problem.addConstraint(InSetConstraint(["b"]), signal_wire) elif count == 7: problem.addConstraint(InSetConstraint(["d", "g"]), signal_wire) elif count == 8: problem.addConstraint(InSetConstraint(["a", "c"]), signal_wire) elif count == 9: problem.addConstraint(InSetConstraint(["f"]), signal_wire) else: raise ValueError return problem.getSolution()
def solve(): problem = Problem() # Define the variables: 9 rows of 9 variables rangin in 1...9 for i in range(1, 10): problem.addVariables(range(i * 10 + 1, i * 10 + 10), range(1, 10)) # Each row has different values for i in range(1, 10): problem.addConstraint(AllDifferentConstraint(), range(i * 10 + 1, i * 10 + 10)) # Each colum has different values for i in range(1, 10): problem.addConstraint(AllDifferentConstraint(), range(10 + i, 100 + i, 10)) # Each 3x3 box has different values problem.addConstraint(AllDifferentConstraint(), [11, 12, 13, 21, 22, 23, 31, 32, 33]) problem.addConstraint(AllDifferentConstraint(), [41, 42, 43, 51, 52, 53, 61, 62, 63]) problem.addConstraint(AllDifferentConstraint(), [71, 72, 73, 81, 82, 83, 91, 92, 93]) problem.addConstraint(AllDifferentConstraint(), [14, 15, 16, 24, 25, 26, 34, 35, 36]) problem.addConstraint(AllDifferentConstraint(), [44, 45, 46, 54, 55, 56, 64, 65, 66]) problem.addConstraint(AllDifferentConstraint(), [74, 75, 76, 84, 85, 86, 94, 95, 96]) problem.addConstraint(AllDifferentConstraint(), [17, 18, 19, 27, 28, 29, 37, 38, 39]) problem.addConstraint(AllDifferentConstraint(), [47, 48, 49, 57, 58, 59, 67, 68, 69]) problem.addConstraint(AllDifferentConstraint(), [77, 78, 79, 87, 88, 89, 97, 98, 99]) # Some value is given. initValue = [[0, 9, 0, 7, 0, 0, 8, 6, 0], [0, 3, 1, 0, 0, 5, 0, 2, 0], [8, 0, 6, 0, 0, 0, 0, 0, 0], [0, 0, 7, 0, 5, 0, 0, 0, 6], [0, 0, 0, 3, 0, 7, 0, 0, 0], [5, 0, 0, 0, 1, 0, 7, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 9], [0, 2, 0, 6, 0, 0, 0, 5, 0], [0, 5, 4, 0, 0, 8, 0, 7, 0]] for i in range(1, 10): for j in range(1, 10): if initValue[i - 1][j - 1] != 0: problem.addConstraint( lambda var, val=initValue[i - 1][j - 1]: var == val, (i * 10 + j, )) # Get the solutions. solutions = problem.getSolutions() return solutions