コード例 #1
0
ファイル: AStarManhathan.py プロジェクト: armandosrz/csci523
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
コード例 #2
0
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
コード例 #3
0
ファイル: p2.py プロジェクト: armandosrz/Learning_Python
def AStar():
	# Set up inital condition.
	# Ask for all requiered input.

	matrix = list("123456780")
	randmatrix = []
	for i in range(len(matrix)):
		index = randint(0, len(matrix)-1)
		randmatrix.append(int(matrix.pop(index)))

	print randmatrix
	matrixWithMoves = createMoves(randmatrix)
	print matrixWithMoves
	initialState = State(matrixWithMoves)
	initialState.f = manhathanDistance(matrixWithMoves)


	if initialState.isGoal():
		return initialState
	frontier = PriorityQueue()
	"""
		A set is an unordered data type with no repited elements.
		It is use for easy comparision of membership
	"""
	explored = {}
	frontier.enqueue(initialState)
	count  = 1
	while frontier:
		"""
		count += 1
		if count == 5:
			break
		"""
		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)
			#elif frontier.__getitem__(child).g > child.g:
			#	continue
			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)
			"""

			if (child.to_string() not in explored) and (child not in frontier):
				frontier.enqueue(child)
			elif child.to_string() in explored:
				continue
			elif child in frontier:
				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)
					#print ts.to_string() + "---" + str(ts.f)
			"""
	"""
	while frontier:
		x = frontier.dequeue()
		print x.to_string() + " " + str(x.f)
	"""
	"""