Esempio n. 1
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    # Add your forward checking logic here.
    cur_var = state.get_current_variable()

    if not cur_var:
        return True

    cur_val = None

    cur_val = cur_var.get_assigned_value()

    cons_list = state.get_constraints_by_name(cur_var.get_name())

    for con in cons_list:

        connect_var = state.get_variable_by_name(con.get_variable_j_name())

        for val in connect_var.get_domain():

            if not con.check(state, cur_val, val):
                connect_var.reduce_domain(val)

            if len(connect_var.get_domain()) == 0:
                return False

    return True
Esempio n. 2
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    var = state.get_current_variable()
    
    if var!=None:  
        value = var.get_assigned_value()
        constraints=state.get_constraints_by_name(var.get_name());
        for c in constraints:
            if state.get_variable_by_name(c.get_variable_i_name()) is var:
                Y = state.get_variable_by_name(c.get_variable_j_name())
                for val in Y.get_domain():
                    if c.check(state, value, val) is False:
                        Y.reduce_domain(val)
                    if Y.domain_size() is 0:
                        return False
            elif state.get_variable_by_name(c.get_variable_j_name()) is var:
                Y = state.get_variable_by_name(c.get_variable_i_name())
                for val in Y.get_domain():
                    if c.check(state, val, value) is False:
                        Y.reduce_domain(val)
                    if Y.domain_size() is 0:
                        return False
    return True    
Esempio n. 3
0
def forward_checking(state, verbose=False):
    """
    Args:
        state (CSPState)
    """
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False
    # Add your forward checking logic here.
    X = state.get_current_variable()
    if not X:
        return True
    x = X.get_assigned_value()
    # Loop through constraints
    for c in state.get_constraints_by_name(X.get_name()):
        var_i, var_j = c.get_variable_i_name(), c.get_variable_j_name()
        Y_name = var_i if X.get_name() != var_i else var_j
        Y = state.get_variable_by_name(Y_name)
        for v in Y.get_domain():
            if not c.check(state, x, v):
                Y.reduce_domain(v)
        if not Y.get_domain():
            return False
    # Return
    return True
Esempio n. 4
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False
    X = state.get_current_variable()
    if not X:
        return True
    x = X.get_assigned_value()
    counter = 0
    constraints = state.get_constraints_by_name(X.get_name())
    while counter < len(constraints):
        constraint = constraints[counter]
        counter += 1
        W = constraint.get_variable_j_name()
        Z = constraint.get_variable_i_name()
        if W == X.get_name():
            Y = Z
        else:
            Y = W 
        Y = state.get_variable_by_name(Y)
        for y in Y.get_domain():
            if not constraint.check(state, x, y):
                Y.reduce_domain(y)
        if not Y.get_domain():
            return False

    return True
Esempio n. 5
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    var = state.get_current_variable()
    value=None
    if var is not None:
        value = var.get_assigned_value()
        constraints = state.get_constraints_by_name(var.get_name())
        while constraints:
            con = constraints.pop()
            if con.get_variable_i_name() == var.get_name():
                active=con.get_variable_j_name()
            else:
                active=con.get_variable_i_name()
            j=state.get_variable_by_name(active)

            for jval in j.get_domain():

                a=con.check(state,value_i=value,value_j=jval)


                if not a:
                    j.reduce_domain(jval)
                    
                if j.domain_size() == 0:
                    #print "\n------------\nTEST FAILS\n------------"
                    return False
        return True
    return True
Esempio n. 6
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False
    X = state.get_current_variable()
    if not X:
        return True
    x = X.get_assigned_value()
    counter = 0
    constraints = state.get_constraints_by_name(X.get_name())
    while counter < len(constraints):
        constraint = constraints[counter]
        counter += 1
        W = constraint.get_variable_j_name()
        Z = constraint.get_variable_i_name()
        if W == X.get_name():
            Y = Z
        else:
            Y = W
        Y = state.get_variable_by_name(Y)
        for y in Y.get_domain():
            if not constraint.check(state, x, y):
                Y.reduce_domain(y)
        if not Y.get_domain():
            return False

    return True
Esempio n. 7
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    cur_var = state.get_current_variable()
    if(cur_var == None):
        return True # at root

    ass_val = cur_var.get_assigned_value()
    constraints = state.get_constraints_by_name(cur_var.get_name())

    for const in constraints:
        if(const.get_variable_i_name() == cur_var.get_name()):
            other_var = state.get_variable_by_name(const.get_variable_j_name())
            other_domain = other_var.get_domain()

            for other_val in other_domain:
                if( not const.check(state, value_i=ass_val, value_j=other_val)):
                    other_var.reduce_domain(other_val)

                    if(other_var.domain_size() == 0):
                        return False

    return True
Esempio n. 8
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    # Add your forward checking logic here.
    X = state.get_current_variable()
    if X is None: return True
    x = X.get_assigned_value()
    for c in state.get_constraints_by_name(X.get_name()):
        Y_name = c.get_variable_i_name()
        i_is_y = True
        if Y_name == X.get_name():
            Y_name = c.get_variable_j_name()
            i_is_y = False
        Y = state.get_variable_by_name(Y_name)
        for y in Y.get_domain():
            if i_is_y:
                passed = c.check(state, value_i=y, value_j=x)
            else:
                passed = c.check(state, value_i=x, value_j=y)
            if not passed: Y.reduce_domain(y)
            if len(Y.get_domain()) == 0: return False
    return True
Esempio n. 9
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False
    variable = state.get_current_variable()
    if not variable:
        return True
    variablename = variable.get_name()
    constraints = state.get_constraints_by_name(variablename)
    x = variable.get_assigned_value()
    i = 0
    for constraint in constraints:
        connected_variable_name = constraint.get_variable_j_name()
        connected_variable = state.get_variable_by_name(
            connected_variable_name)
        connected_domain = connected_variable.get_domain()
        if connected_variable.is_assigned() == False:
            for value in connected_domain:
                if not constraint.check(state, x, value):
                    connected_variable.reduce_domain(value)
            if not connected_variable.get_domain():
                return False

    return True
Esempio n. 10
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.

    basic = basic_constraint_checker(state, verbose)

    if not basic:
        return False

    # so far, all the assigned values satisfied the constraints
    # in this state, some variable has been assigned, what we need to
    # to do is check domain of the remaining unassigned variables
    print "Before Check: state = ", state.vd_table()
    new_state = state.copy()
    current_var_name = new_state.get_current_variable_name()
    constraints = new_state.get_constraints_by_name(current_var_name)
    print "current variable :", current_var_name
    for constraint in constraints:
        print constraint
        var_j_name = constraint.get_variable_j_name()
        var_j = new_state.get_variable_by_name(var_j_name)
        domain = var_j.get_domain()
        for value in domain:
            var_j.set_value(value)
            if constraint.check(new_state):
                continue
            elif var_j.domain_size() > 1:
                var_j.reduce_domain(value)
            else:
                return False
    print "After Check: state = ", state.vd_table()
    return True
Esempio n. 11
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    # Add your forward checking logic here.
    # 1. Find X = x being the current assignment.
    # 2. for each Y that is associated with X in a binary constraint; do
    #        for each y in Y's Domain; do
    #            if X=x and Y=y don't hold:
    #                 remove y from Y's Domain
    #                 if Y's Domain is empty:
    #                    return False
    #    return True

    # NOTE: This function is not a pure function. Its side-effect is
    #       to update the state
    curr_var_name = state.get_current_variable_name()
    if not curr_var_name:
        # ROOT
        return True

    for constraint in state.get_constraints_by_name(curr_var_name):
        var_j = state.get_variable_by_name(constraint.get_variable_j_name())
        for val in var_j.get_domain():
            if not constraint.check(state, value_i=None, value_j=val):
                var_j.reduce_domain(val)

            if var_j.domain_size() == 0:
                return False

    return True
Esempio n. 12
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    
    if not basic:
        return False
    
    X = state.get_current_variable()
    if X is None:
        return True
    
    x = X.get_assigned_value()
    
    all_constraints = state.get_constraints_by_name(X.get_name())
    
    for constraint in all_constraints:
        if constraint.get_variable_i_name() == X.get_name():
            Y = state.get_variable_by_name(constraint.get_variable_j_name())
        else:
            Y = state.get_variable_by_name(constraint.get_variable_i_name())
        domain = Y.get_domain()
        for y in domain:
            if not constraint.check(state, x, y):
                Y.reduce_domain(y)
            if Y.domain_size() == 0:
                return False
    return True
Esempio n. 13
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    # Add your forward checking logic here.

    raise NotImplementedError
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    # Add your forward checking logic here.
    
    raise NotImplementedError
Esempio n. 15
0
File: lab4.py Progetto: AIRising/AI
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.

    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    # Add your forward checking logic here.
    X = state.get_current_variable()
    if(not X is None):
        #We proceed
        x = X.get_assigned_value()
        constraints = state.get_constraints_by_name(X.get_name())
    else:
        return True
Esempio n. 16
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.

    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False


    """X = state.get_current_variable()

    if X is not None:
        x = X.get_assigned_value()
        constraints = state.get_constraints_by_name(X.get_name())

        for constraint in constraints:
            if state.get_variable_by_name(constraint.get_variable_i_name()) == X:
                Y_name = constraint.get_variable_j_name()
            else:
                Y_name = constraint.get_variable_i_name()"""

            
                
        


    X = state.get_current_variable()
   
#THIS IS BEYOND FRUSTRATING
 
    if X is not None:  
        x = X.get_assigned_value()
        constraints = state.get_constraints_by_name(X.get_name())
        for constraint in constraints:
            if state.get_variable_by_name(constraint.get_variable_i_name()) == X:
                Y_name = constraint.get_variable_j_name()
            else:
                Y_name = constraint.get_variable_i_name()           
    
            Y = state.get_variable_by_name(Y_name)

            for y in Y.get_domain():
                if constraint.check(state, x, y) is False:
                    Y.reduce_domain(y)
                if Y.domain_size() is 0:
                    return False
    return True
Esempio n. 17
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic: return False
    X = state.get_current_variable()
    if X is None: return True
    x = X.get_assigned_value()
    X_name = X.get_name()
    X_constraints = state.get_constraints_by_name(X_name)
    for c in X_constraints:
        Y_name = c.get_variable_j_name()
        Y = state.get_variable_by_name(Y_name)
        Y_domain = Y.get_domain()
        for y in Y_domain:
            if not c.check(state, value_i = x, value_j = y):
                Y.reduce_domain(y)
        if not Y.get_domain(): return False
    return True
Esempio n. 18
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False
    variable = state.get_current_variable()
    if(variable == None): return True
    value = variable.get_assigned_value()
    constraints = state.get_constraints_by_name(variable.get_name())
    #for every constraint, get all possible values for the other constraint and check if they work
    for constraint in constraints:
        otherVariable = state.get_variable_by_name(constraint.get_variable_j_name())
        domain = otherVariable.get_domain()
        for otherVariableValue in domain:
            if not constraint.check(state, value, otherVariableValue):
                otherVariable.reduce_domain(otherVariableValue)
            if otherVariable.domain_size() == 0:
                return False
    return True
Esempio n. 19
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    # Add your forward checking logic here.
    X = state.get_current_variable()
    if X != None:
        x = X.get_assigned_value()
        constraints = state.get_constraints_by_name(X.get_name())
        for c in constraints:
            Y = state.get_variable_by_name(c.get_variable_j_name())
            for y in Y.get_domain():
                if not c.check(state, x, y):
                    Y.reduce_domain(y)
                if Y.domain_size() == 0:
                    return False
    return True 
Esempio n. 20
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.

    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False
    """X = state.get_current_variable()

    if X is not None:
        x = X.get_assigned_value()
        constraints = state.get_constraints_by_name(X.get_name())

        for constraint in constraints:
            if state.get_variable_by_name(constraint.get_variable_i_name()) == X:
                Y_name = constraint.get_variable_j_name()
            else:
                Y_name = constraint.get_variable_i_name()"""

    X = state.get_current_variable()

    #THIS IS BEYOND FRUSTRATING

    if X is not None:
        x = X.get_assigned_value()
        constraints = state.get_constraints_by_name(X.get_name())
        for constraint in constraints:
            if state.get_variable_by_name(
                    constraint.get_variable_i_name()) == X:
                Y_name = constraint.get_variable_j_name()
            else:
                Y_name = constraint.get_variable_i_name()

            Y = state.get_variable_by_name(Y_name)

            for y in Y.get_domain():
                if constraint.check(state, x, y) is False:
                    Y.reduce_domain(y)
                if Y.domain_size() is 0:
                    return False
    return True
Esempio n. 21
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    # Add your forward checking logic here.

    #Upto this point the function only checks if the constraints are satisfied
    #From here we have to reduce the domain of the variables that appear in the
    #constraints

    current_var = state.get_current_variable()
    value = None
    if current_var is not None:
        var_val = current_var.get_assigned_value()

        cons = state.get_constraints_by_name(current_var.get_name())

        for constraint in cons:
            var_i = state.get_variable_by_name(
                constraint.get_variable_i_name())
            var_j = state.get_variable_by_name(
                constraint.get_variable_j_name())

            if var_i.get_name() == current_var.get_name():
                mVar = var_j
                for value in mVar.get_domain():
                    if not constraint.check(state, var_val, value):
                        mVar.reduce_domain(value)
                        if len(mVar.get_domain()) == 0:
                            return False
            else:
                mVar = var_i
                for value in mVar.get_domain():
                    if not constraint.check(state, value, var_val):
                        mVar.reduce_domain(value)
                        if len(mVar.get_domain()) == 0:
                            return False
    return True
Esempio n. 22
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    var1 = state.get_current_variable()
    if not var1:
        return True
    val1 = var1.get_assigned_value()

    for c in state.get_constraints_by_name(var1.get_name()):
        var2 = state.get_variable_by_name(c.get_variable_j_name())
        for val2 in var2.get_domain():
            if not c.check(state, val1, val2):
                var2.reduce_domain(val2)
        if not var2.get_domain():
            return False

    return True
Esempio n. 23
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False
    # forward checking logic
    X = state.get_current_variable()
    if not X: return True
    x = X.get_assigned_value()
    constraints = state.get_constraints_by_name(X.get_name())
    for con in constraints:
        Y = state.get_variable_by_name(con.get_variable_i_name(
        ) if con.get_variable_i_name() != X.get_name() else con.
                                       get_variable_j_name())
        for y in Y.get_domain():
            if con.check(state, x, y) is False:
                Y.reduce_domain(y)
                if Y.domain_size() is 0:
                    return False

    return True
def forward_checking(state, verbose=False, cur_var_data=None):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    # Now that we've run basic_constraint_checker, we know that all assigned
    #  variables are valid. This means we only need to worry about domains
    # in the next step; no need to concern ourselves with the validity of
    # values.
    if not basic:
        return False
    if 0 > state.variable_index:
        return True
    # MY_CODE
    if cur_var_data is None:
        # This default setting is used when this method alone is used for
        # checking
        cur_var = state.get_current_variable()
        # Guaranteed to have an assigned value because it's the current
        # variable in the state object.
        cur_var_value = cur_var.get_assigned_value()
    else:
        # This alternate setting is used when this method is used as a
        # helper to forward_checking_prop_singleton()
        cur_var, cur_var_value = cur_var_data
    neighbor_constraints = state.get_constraints_by_name(cur_var.get_name())
    for constraint in neighbor_constraints:
        neighbor = state.get_variable_by_name(constraint.get_variable_j_name())
        # BEWARE: This domain is not a reference to the actual domain; it is
        #  a copy! One must call the reduce_domain() function to alter the
        # domain
        neighbor_domain = neighbor.get_domain()
        for domain_possibility in neighbor_domain:
            if not constraint.check(
                    state, value_i=cur_var_value, value_j=domain_possibility):
                neighbor.reduce_domain(domain_possibility)
        if not neighbor.get_domain():
            return False
    return True
Esempio n. 25
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    # Add your forward checking logic here.
    
#    raise NotImplementedError
    X= state.get_current_variable()
    x= None
    if X is not None:
        x= X.get_assigned_value()
    constants =state.get_constraints_by_name(state.get_current_variable_name())
    for constant in constants:
        Y= state.get_variable_by_name(constant.get_variable_j_name())
        for y in Y.copy().get_domain():
            if not constant.check(state, value_i=x, value_j=y):
                Y.reduce_domain(y)
        if Y.domain_size()==0:
            return False
    return True
Esempio n. 26
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    # Add your forward checking logic here.
    var = state.get_current_variable()
    value = None
    if var is not None:  # we are not in the root state
        if var.is_assigned():
            value = var.get_assigned_value()
            #print 'var', var, 'is assigned to value', value
            constraints = state.get_constraints_by_name(var.get_name())
            for c in constraints:
                #print 'Considering constraint', c
                other_var_name = c.get_variable_i_name()
                if other_var_name == var.get_name():
                    other_var_name = c.get_variable_j_name()
                other_var = state.get_variable_by_name(other_var_name)
                #print 'other var', other_var
                for y in other_var.get_domain():
                    if not c.check(state, value, y):
                        #print 'constraint', c, 'failed for', value, y, 'remove it from domain'
                        other_var.reduce_domain(y)
                    if other_var.domain_size() == 0:
                        #print 'Domain is empty, return false'
                        return False
            #print 'Evrything was ok, return True'
            return True
        else:
            #print 'var', var, 'is not assigned to any value, return false'
            return True
    else:
        #print 'We  are at root state, return True'
        return True
Esempio n. 27
0
def forward_checking(state, verbose=False):
    global cantLlamadas
    cantLlamadas += 1

    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    # Add your forward checking logic here.
    Xvar = state.get_current_variable()
    if (Xvar == None):
        return True

    xval = Xvar.get_assigned_value()
    constraints = state.get_constraints_by_name(Xvar.get_name())

    for const in constraints:
        if (const.get_variable_i_name() == Xvar.get_name()):
            YvarName = const.get_variable_j_name()
            Yvar = state.get_variable_by_name(YvarName)
            for yval in Yvar.get_domain():
                if (not const.check(state, value_i=xval, value_j=yval)):
                    Yvar.reduce_domain(yval)
        elif (const.get_variable_j_name() == Xvar.get_name()):
            YvarName = const.get_variable_i_name()
            Yvar = state.get_variable_by_name(YvarName)
            for yval in Yvar.get_domain():
                if (not const.check(state, value_i=yval, value_j=xval)):
                    Yvar.reduce_domain(yval)

        if (Yvar.domain_size() == 0):
            return False

    #Si no devolvi False todavia es que ninguna restriccion fallo
    return True
Esempio n. 28
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    #X is the variable currently being assigned
    X = state.get_current_variable()
    value = None

    if X is None:
        return True  #Thus this is the root of the search tree, since forward checking could only be done when there are variables to be assigned

    x = X.get_assigned_value()

    #All the binary constraints associated with X
    constraints_list = state.get_constraints_by_name(X.get_name())

    #For each constraint
    for const in constraints_list:
        #Y is the variable connected to X

        #Choosing the connected Variable Y
        if (const.get_variable_i_name != X.get_name()):
            Y = state.get_variable_by_name(const.get_variable_i_name())
        else:
            Y = state.get_variable_by_name(const.get_variable_j_name())

        for y in Y.get_domain():
            #if not fulfilling constraint, remove y
            if not const.check(state, x, y):
                Y.reduce_domain(y)
            if (Y.domain_size() == 0):
                return False

    return True
Esempio n. 29
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False
    
    # Add your forward checking logic here.
    var = state.get_current_variable()
    value = None
    if not var: return True
    value = var.get_assigned_value()
    for constraint in state.get_constraints_by_name(var.get_name()):
        #print(state)
        my_constraint1=constraint.get_variable_i_name()
        my_constraint2=constraint.get_variable_j_name()
        my_constraint1_var=state.get_variable_by_name(my_constraint1)
        my_constraint2_var=state.get_variable_by_name(my_constraint2)
        my_constraint1_val=my_constraint1_var.get_assigned_value()
        my_constraint2_val=my_constraint2_var.get_assigned_value()
        if my_constraint1_var!=var:
            my_constraint_var=my_constraint1_var
        else:
            my_constraint_var=my_constraint2_var
        #my_constraint_var=state.get_variable_by_name(constraint.get_variable_i_name() if constraint.get_variable_i_name() != var.get_name() else constraint.get_variable_j_name())        
        for varvalue in my_constraint_var.get_domain():
            if constraint.check(state,value,varvalue)==False:
                    #print(type(my_constraint_var.get_domain()))
                my_constraint_var.reduce_domain(varvalue)
                    #print(my_constraint_var.get_domain())
            if my_constraint_var.domain_size()==0:
                return False
                    
                
        
    return True
Esempio n. 30
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    X = state.get_current_variable()
    if not X:
        return True
    
    x = X.get_assigned_value()
    #i = 0
    constraints = state.get_constraints_by_name(X.get_name())
    
    for i in xrange(len(constraints)):
        constraint = constraints[i]
        #i += 1
        tempX = constraint.get_variable_i_name()
        tempY = constraint.get_variable_j_name()
        
        if tempX == X.get_name():
            Y = tempY
        else:
            Y = tempX
            
        Y = state.get_variable_by_name(Y)
        
        for y in Y.get_domain():
            if not constraint.check(state, x, y):
                Y.reduce_domain(y)
                
        if not Y.get_domain():
            return False

    return True
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    # Add your forward checking logic here.
    Xinstance = state.get_current_variable()
    if Xinstance is not None:
        Xvar = Xinstance.get_name()
        xval = Xinstance.get_assigned_value()

        all_binary_constraints = state.get_constraints_by_name(Xvar)
        for binary_constraint in all_binary_constraints:
            Yvar = binary_constraint.get_variable_j_name()
            Yinstance = state.get_variable_by_name(Yvar)
            for yval in Yinstance.get_domain():
                if not binary_constraint.check(state, value_i=xval, value_j=yval):
                    Yinstance.reduce_domain(yval)
                if Yinstance.domain_size() == 0:
                    return False

    return True
Esempio n. 32
0
def forward_checking(state, verbose=False):
    # Before running Forward checking we must ensure
    # that constraints are okay for this state.
    # print "-"*10, "Inside forward_checking", "-"*10
    basic = basic_constraint_checker(state, verbose)
    if not basic:
        return False

    # return True if it's initial state
    if state.variable_index < 0:    return True

    # print "Current Varialbe: {}, in State: \n{}".format(state.get_current_variable_name(), state.vd_table())

    cur_var = state.get_current_variable()

    # current variable is carried over when csp.solve() creates children
    cur_var_name = state.get_current_variable_name()
    cur_var_value = cur_var.get_assigned_value()
    constraints = state.get_all_constraints()

    # print "state current name: {}".format(state.get_current_variable_name())

    # if cur_var_name == "5":
    #     for i in constraints:
    #         if cur_var_name == i.get_variable_i_name():
    #             if i.get_variable_j_name() == "4":
    #                 print i#, "\t", i.get_variable_i_name(), i.get_variable_j_name()
    #                 print i.check(state, "B", "O")
    count_total = constraints.__len__()
    count = 0
    count_cont = 0
    for constraint in constraints:
        i_name = constraint.get_variable_i_name()
        j_name = constraint.get_variable_j_name()

        # !!! I assume as long as there is a constraint for two variables, they are connected to each other
        if cur_var_name == i_name:
            connected_name = j_name
        elif cur_var_name == j_name:
            connected_name = i_name
        else:
            count_cont += 1
            continue
        count += 1
        # print "the {}/{} in constraint in constraints\n".format(count, count_total)
        connected_var = state.get_variable_by_name(connected_name)
        # print "connected_var: {}\n".format(connected_var)
        # if not connected_var.is_assigned(): # comment this line, and unindent the next command and for loop
        connected_domains = connected_var.get_domain()

        # print "variable: {}, with domain: {}\n".format(connected_name, connected_var.get_domain())

        for domain in connected_domains:
            # in instruction, he says that constraints are symmetrical, so I assume I just need to check one direction
            if not constraint.check(state, cur_var_value, domain):
                # print "constraint.check in domain-in-connected_domains: {}".format("False")
                # print "before reduce_domain: {}, __len__(): {}".format(connected_var, connected_var.domain_size())

                connected_var.reduce_domain(domain)
                # print "\nremove domain: {} from variable: {} at state: {} \n".format(domain, connected_var.get_name() ,state.variable_index)
            #     print "after reduce_domain: {}, __len__(): {}\n".format(connected_var, connected_var.domain_size())
            #
            # print "constraint.check in domain-in-connected_domains: {}".format("True")

            if connected_var.domain_size() == 0:
                # print "return False implemented\n"
                # print "-" * 10, "End --- Inside forward_checking", "-" * 10, "\n"
                return False
    # print "return True implemented\n"
    # print "-" * 10, "End --- Inside forward_checking", "-" * 10, "\n"
    return True

    # Add your forward checking logic here.

    raise NotImplementedError