예제 #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.
    """

    if len(csp.assignment) == len(csp.variables):
        return True

    variable = select_unassigned_variable(csp)
    value = order_domain_values(csp, variable)
    #print variable
    #print value
    flag = 0
    for x in value:
        csp.variables.begin_transaction()
        if is_consistent(csp, variable, x):
            #print "past is_consistent"
            for var in csp.variables:
                if var == variable:
                    var.assign(x)
                    var.is_assigned()
                    solution = backtrack(csp)
                    if solution != False:
                        return True
        csp.variables.rollback()
    return False
예제 #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 len(csp.assignment) == len(csp.variables):
        return True

    variable = select_unassigned_variable(csp)
    value = order_domain_values(csp, variable)
    #print variable
    #print value
    flag = 0
    for x in value:
        csp.variables.begin_transaction()
        if is_consistent(csp, variable, x):
            #print "past is_consistent"
            for var in csp.variables:
                if var == variable:
                    var.assign(x)
                    var.is_assigned()
                    solution = backtrack(csp)
                    if solution != False:
                        return True
        csp.variables.rollback()
    return False
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
예제 #4
0
파일: p6_solver.py 프로젝트: szzheng/CSE150
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
예제 #5
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
예제 #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
    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
예제 #8
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