Ejemplo n.º 1
0
def backtrack(csp):
    """Performs the backtracking search for the given csp.

    If there is a solution, this method returns True; otherwise, it returns False.
    """
    ''' Initial completion check '''
    complete = is_complete(csp)
    if (complete):
        return complete

    #print len(csp.variables) + " length"
    currentVariable = select_unassigned_variable(csp)

    if not (currentVariable is None):
        #print str(currentVariable.name)
        for dv in order_domain_values(csp, currentVariable):
            domainValue = dv
            #print "domainValue: " + str(domainValue)

            if (is_consistent(csp, currentVariable, domainValue)):

                #print "assign"
                csp.variables.begin_transaction()
                ''' assign variable '''

                currentVariable.assign(domainValue)

                result = backtrack(csp)
                if not (result == False):
                    return True

                csp.variables.rollback()

    return False
Ejemplo n.º 2
0
def backtrack(csp):
    """Performs the backtracking search for the given csp.

    If there is a solution, this method returns True; otherwise, it returns False.
    """

    if is_complete(csp):
        return True

    # get the next unassigned variable
    var = select_unassigned_variable(csp)

    result = False
    orderedDomain = order_domain_values(csp, var)
    # for each value in var
    for value in orderedDomain:
        if is_consistent(csp, var, value):

            csp.variables.begin_transaction()

            var.assign(value)
            
            if inference(csp, var):
                result = backtrack(csp)
                if result:
                    return result

            csp.variables.rollback()
    
    return None
Ejemplo n.º 3
0
def backtrack(csp):
    """Performs the backtracking search for the given csp.

    If there is a solution, this method returns True; otherwise, it returns False.
    """

    # TODO copy from p3
    solution = is_complete(csp)

    if solution:
        return solution

    nextVar = select_unassigned_variable(csp)

    # solution = False here

    for i in order_domain_values(csp, nextVar):
        if is_consistent(csp, nextVar, i):
            csp.variables.begin_transaction()
            nextVar.assign(i)

            if backtrack(csp):
                return True

            csp.variables.rollback()

    return solution
Ejemplo n.º 4
0
def backtrack(csp):
    """Performs the backtracking search for the given csp.

    If there is a solution, this method returns True; otherwise, it returns False.
    """

    # TODO implement this
    solution = is_complete(csp)

    if solution:
        return solution

    nextVar = select_unassigned_variable(csp)

    # solution = False here

    for i in order_domain_values(csp, nextVar):
        if is_consistent(csp, nextVar, i):
            csp.variables.begin_transaction()
            nextVar.assign(i)

            if backtrack(csp):
                return True

            csp.variables.rollback()

    return solution
def backtrack(csp):
    """Performs the backtracking search for the given csp.

    If there is a solution, this method returns True; otherwise, it returns False.
    """
    #print "Finding Match"
    # TODO implement this
    if is_complete(csp):
        return True
    else:
        variable = select_unassigned_variable(csp)
        #print variable
        for value in order_domain_values(csp, variable):
            #print value
            if is_consistent(csp, variable, value):
                #print "Found Valid"
                csp.variables.begin_transaction()
                variable.assign(value)
                if backtrack(csp):
                    return True
                csp.variables.rollback()
        return False
Ejemplo n.º 6
0
def backtrack(csp):
	"""Performs the backtracking search for the given csp.

	If there is a solution, this method returns True; otherwise, it returns False.
	"""

	# TODO implement this
	#pass

	''' Initial completion check '''
	complete = is_complete(csp)
	if (complete):
		return complete

	#print len(csp.variables) + " length"
	currentVariable = select_unassigned_variable(csp)

	if not(currentVariable is None):
		#print str(currentVariable.name)
		for dv in order_domain_values(csp, currentVariable):
			domainValue = dv
			#print "domainValue: " + str(domainValue)

			if (is_consistent(csp, currentVariable, domainValue)):

				#print "assign"
				csp.variables.begin_transaction()

				''' assign variable '''

				currentVariable.assign(domainValue)

				result = backtrack(csp)
				if not(result == False):
					return True

				csp.variables.rollback()

	return False
Ejemplo n.º 7
0
def select_unassigned_variable(csp):
    """Selects the next unassigned variable, or None if there is no more unassigned variables
    (i.e. the assignment is complete).

    This method implements the minimum-remaining-values (MRV) and degree heuristic. That is,
    the variable with the smallest number of values left in its available domain.  If MRV ties,
    then it picks the variable that is involved in the largest number of constraints on other
    unassigned variables.
    """
    # TODO implement this

    if is_complete(csp):
        return True

    unassignedList = []
    unassignedFlag = False
    min_val = sys.maxint

    for variable in csp.variables:
        if not variable.is_assigned():
            if len(variable.domain) == min_val:
                unassignedList.append(variable)
                unassignedFlag = True

            elif len(variable.domain) < min_val:
                unassignedList = [variable]
                min_val = len(variable.domain)
                unassignedFlag = True

    if unassignedFlag:
        if (len(unassignedList)) == 1:
            return unassignedList[0]

        else:
            max_constraint = -sys.maxint
            var = None
            for variable in unassignedList:
                count = 0
                for constraint in csp.constraints[variable]:
                    if not constraint.var2.is_assigned(
                    ):  #count only the unassigned variables
                        count += 1

                if count > max_constraint:
                    var = variable
                    max_constraint = count

            return var

        #
        # sortList = sorted(unassignedList, key=itemgetter(1))
        # front = sortList[0]
        # tieList = []
        # tieList.append(front)
        # if(len(sortList) > 1):
        #     for var in range(1, len(sortList)):
        #         if front[1] == sortList[var][1]:
        #             tieList.append(sortList[var])
        #         else:
        #             break
        #
        #  #No repeat take the head
        # if len(tieList) == 1:
        #     return front[0]
        # else:
        #     maxVar = max(tieList, key=lambda i:len(csp.constraints[i[0]]))
        #     #print "maxVar: " + str(maxVar[0])
        #     return maxVar[0]
        # #return front[0]

    else:  # When all the variables are assigned
        return None

    pass