Exemple #1
0
def AStar(matrix):
    # Set up inital condition.
    # Ask for all requiered input.
    global statesCount
    """
	matrix = map(int,list("123456780"))
	print matrix
	test = map(int, list("704512836"))
	matrixWithMoves = createMoves(matrix)
	print test
	"""
    initialState = State(matrix)
    initialState.f = manhathanDistance(matrix)

    if initialState.isGoal():
        return initialState
    frontier = PriorityQueue()
    explored = {}
    frontier.enqueue(initialState)
    statesCount = 1
    while frontier:
        state = frontier.dequeue()
        # print state.to_string() + " " + str(state.f)
        explored[state.to_string()] = 1
        if state.isGoal():
            return state
        children = createStatesManhatan(state)
        for child in children:
            if child.to_string() in explored:
                continue
            if child not in frontier:
                frontier.enqueue(child)
                statesCount += 1
            else:
                ch = frontier.__getitem__(child)
                if ch.f > child.f:
                    # print child.to_string() + " " + str(ch.f) + " " + str(child.f)
                    frontier.__delitem__(child)
                    frontier.enqueue(child)
                    ts = frontier.__getitem__(child)
    return None
def hc_Steepest(matrix):
	# Set up inital condition.
	currentState = State(matrix)
	currentState.f = manhathanDistance(matrix)
	nextState = State(matrix)
	nextState.f = copy(currentState.f)
	statesCount = 1

	while True:
		children = createStatesManhatan(currentState)
		for child in children:
			statesCount +=1
			if child.f < nextState.f:
				nextState = child
		if nextState.f >= currentState.f:
			if nextState.isGoal():
				return "Goal", nextState, statesCount
			else:
				return 'Local Maxima', nextState, statesCount
		currentState = copy(nextState)
	return None
def AStar(matrix):

	# Set up inital condition.
	# Ask for all requiered input.
	global statesCount
	statesCount = 1
	"""
	matrix = map(int,list("123456780"))
	print matrix
	matrixWithMoves = createMoves(matrix)
	print matrixWithMoves
	"""
	initialState = State(matrix)
	initialState.f = missplacedTitles(matrix)


	if initialState.isGoal():
		return initialState
	frontier = PriorityQueue()
	explored = {}
	frontier.enqueue(initialState)
	while frontier:
		state = frontier.dequeue()
		explored[state.to_string()] = 1
		if state.isGoal():
			return state
		children = createStatesMissplaced(state)
		for child in children:
			if child.to_string() in explored:
				continue
			if child not in frontier:
				frontier.enqueue(child)
				statesCount += 1
			else:
				ch = frontier.__getitem__(child)
				if ch.f > child.f:
					frontier.__delitem__(child)
					frontier.enqueue(child)
					# ts = frontier.__getitem__(child)
	return None
def hc_RandomRestart(matrix, count):
	# Set up inital condition.
	# Ask for all requiered input.
	currentState = State(copy(matrix))
	currentState.f = manhathanDistance(copy(matrix))
	nextState = State(copy(matrix))
	nextState.f = copy(currentState.f)
	statesCount = count

	while True:
		children = createStatesManhatan(currentState)
		for child in children:
			statesCount +=1
			if child.f < nextState.f:
				nextState = child
		if nextState.f >= currentState.f:
			if nextState.isGoal():
				return "Goal", nextState, statesCount
			else:
				return hc_RandomRestart(createMovesRandomRestart(copy(matrix)), statesCount)
		currentState = copy(nextState)
	return None