예제 #1
0
def backtracking(csp, ac_3=False):
    """
    Implement the basic backtracking algorithm to solve a CSP.

    Optional: if ac_3 == True use the AC-3 algorithm for constraint
              propagation. Important: if ac_3 == false don't use
              AC-3!
    :param csp: A csp.ConstrainedSatisfactionProblem object
                representing the CSP to solve
    :return: A csp.ConstrainedSatisfactionProblem, where all Variables
             are set and csp.complete() returns True. (I.e. the solved
             CSP)
    """
    if (csp.complete()):
        return csp
    var = firstUnassigned(csp.variables)
    if (var == None): # falls keine unbelegte Variable existiert ist Rekursionszweig abgeschlossen
        return csp
    for value in var.domain:
        var.set_value(value)
        fail = False # gibt an ob midnestens ein constraint verletzt wurde
        for constraint in csp.get_constraints_for_variable(var):
            if (not constraint.consistent()):
                fail = True
                break
        if (not fail):
            result = backtracking(csp,ac_3)
            if (result != None):
                return result
    var.set_value(None)
    return None
예제 #2
0
def mrv_variable_falsch(csp):
    """ choose the variable with the fewest legal values
    Annahme: wenigste mögliche Werte=meiste Constraints (???)
    """
    count=0
    res=None
    for x in csp.variables:
        constraints_length=len(list(csp.get_constraints_for_variable(x))) #list aus generator machen .... komt mir sehr sehr unschoen vor
        if x.value==None  and constraints_length > count:
            count=constraints_length
            res=x
    return res
예제 #3
0
def mrv_variable(csp):
    """ choose the variable with the fewest legal values
    Vorgehen: gehe werte durch und zähle Möglichkeiten  (bedeutet merhfache belegung)
    """
    res=firstUnassigned(csp.variables)
    if res !=None:
        count=res.domain.__len__  # initialwert auf erste mögliche setzen

        for x in csp.variables:
            constraints=csp.get_constraints_for_variable(x) #list aus generator machen .... komt mir sehr sehr unschoen vor
            if x.value==None:
                xcount=remaining_values(x,csp)
                if xcount < count:
                    count=xcount
                    res=x
    return res
예제 #4
0
def mrv_rec (csp, order):
    if (csp.complete()):
        return (csp, order)
    var = mrv_variable(csp)
    order.append(var)
    if (var == None): # falls keine unbelegte Variable existiert ist Rekursionszweig abgeschlossen
        return (csp, order)
    for value in var.domain:
        var.set_value(value)
        fail = False # gibt an ob midnestens ein constraint verletzt wurde
        for constraint in csp.get_constraints_for_variable(var):
            if (not constraint.consistent()):
                fail = True
                break
        if (not fail):
            result = mrv_rec(csp,order)
            if (result != None):
                return result #hier auf keinen Fall ein Tupel übergeben. war ein ganz dummer fehler
    var.set_value(None)
    return None