Exemplo n.º 1
0
def __revise_i(constraint_problem: ConstraintProblem, subset: Tuple[Variable, ...], ith_variable: Variable,
               i_subsets_consistent_assignments:  Dict[Tuple[Tuple[Variable, ...], Variable], set]) -> bool:
    revised = False
    i_subsets_constraints = map(constraint_problem.get_constraints_containing_variable, subset)
    i_subsets_constraints = set(chain.from_iterable(i_subsets_constraints))
    i_subsets_constraints.update(constraint_problem.get_constraints_containing_variable(ith_variable))
    subset_domain = [variable.domain for variable in subset]
    for assignment in product(*subset_domain):
        var_were_assigned = dict()
        for variable, value in zip(subset, assignment):
            variable_was_assigned = False if variable.value is None else True
            var_were_assigned[variable] = variable_was_assigned
            if not variable_was_assigned:
                variable.assign(value)
        ith_variable_was_assigned = False if ith_variable.value is None else True
        for value in ith_variable.domain:
            if not ith_variable_was_assigned:
                ith_variable.assign(value)
            for constraint in i_subsets_constraints:
                i_assignment = assignment + (value,)
                if not constraint.is_consistent() and i_assignment in i_subsets_consistent_assignments[(subset,
                                                                                                        ith_variable)]:
                    revised = True
                    i_subsets_consistent_assignments[(subset, ith_variable)].remove(i_assignment)
            if not ith_variable_was_assigned:
                ith_variable.unassign()
        for variable in subset:
            if not var_were_assigned[variable]:
                variable.unassign()

    __reduce_assignment_constraints_domains(constraint_problem, i_subsets_consistent_assignments)
    return revised
def __revise(constraints_problem: ConstraintProblem, variable: Variable,
             neighbor: Variable) -> bool:
    if variable.value is not None:
        return False
    variable_constraints = constraints_problem.get_constraints_containing_variable(
        variable)
    neighbor_constraints = constraints_problem.get_constraints_containing_variable(
        neighbor)
    shared_constraint, *_ = (variable_constraints & neighbor_constraints)
    revised = False
    for value in variable.domain:
        variable.assign(value)
        if not shared_constraint.get_consistent_domain_values(neighbor):
            variable.remove_from_domain(value)
            revised = True
        variable.unassign()
    return revised
def __calculate_weight(constraint_problem: ConstraintProblem,
                       constraints_weights: Dict[Constraint, int]) -> int:
    weight = 0
    for variable in constraint_problem.get_variables():
        unsatisfied_constraints = filterfalse(
            None,
            constraint_problem.get_constraints_containing_variable(variable))
        for unsatisfied_const in unsatisfied_constraints:
            weight += constraints_weights[unsatisfied_const]
    return weight