Пример #1
0
def repeated_backward_a_star(start,goal,grid,Tie_val,backward_expanded_states):
	counter = 0
	empty_list = list()

	while start != goal:
		counter += 1
		goal.g_val = 0
		goal.search_val = counter

		start.g_val = infinity
		start.search_val = counter

		open_list = BHeap(tie_val=Tie_val)
		closed_list = list()

		goal.f_val = goal.g_val + heuristic(goal,start)

		open_list.insert(goal)

		optimal_path_reversed, backward_expanded_states = compute_path(goal,start,grid,open_list,closed_list,counter,backward_expanded_states)

		optimal_path = list()

		for i in range(len(optimal_path_reversed)-1,-1,-1):
			item = optimal_path_reversed.pop()
			optimal_path.append(item)

		if open_list.empty():
			print "Cannot reach target"
			return empty_list, 0

		if not optimal_path:
			print "Cannot reach target"
			return empty_list, 0

		start = optimal_path[0]
		for i in range(1,len(optimal_path)):
			# if cost(start,optimal_path[i]) != 1:
			# 	# update increased action costs?
			# 	break
			# else:
			# 	start = optimal_path[i]	
			# print "({0},{1})".format(start.x, start.y)
			start = optimal_path[i]


	print "I reached the target"

	return optimal_path, backward_expanded_states
Пример #2
0
def adaptive_a_star(start,goal,grid,Tie_val,adaptive_expanded_states):
	counter = 0
	empty_list = list()

	while start != goal:
		counter += 1
		start.g_val = 0
		start.search_val = counter

		goal.g_val = infinity
		goal.search_val = counter

		open_list = BHeap(tie_val=Tie_val)
		closed_list = list()

		start.f_val = start.g_val + adaptive_heuristic(start,goal)

		open_list.insert(start)

		optimal_path, adaptive_expanded_states = compute_adaptive_path(start,goal,grid,open_list,closed_list,counter,adaptive_expanded_states)

		if open_list.empty():
			print "Cannot reach target"
			break

		if not optimal_path:
			print "Cannot reach target"
			break

		start = optimal_path[0]
		for i in range(1,len(optimal_path)):
			# if cost(start,optimal_path[i]) != 1:
			# 	# update increased action costs?
			# 	break
			# else:
			# 	start = optimal_path[i]	
			# print "({0},{1})".format(start.x, start.y)
			start = optimal_path[i]

	return optimal_path, adaptive_expanded_states
Пример #3
0
def a_star(start,goal,grid):
	size = len(grid)
	closed_list = list()
	open_list = BHeap() 
	open_list.insert(start)

	came_from = dict()

	start.g_val = 0
	start.f_val = start.g_val + heuristic(start,goal)

	while not open_list.empty():
		# pdb.set_trace()
		current = open_list.delete() # get cell with lowest f_value
		# path.append(current)
		closed_list.append(current)

		if current.equals(goal):
			# pdb.set_trace()def repeated_backward_a_star(start,goal,grid,Tie_val):

			return reconstruct_path(came_from,goal,size)

		for neighbor in get_neighbors(current,grid):
			# pdb.set_trace()
			if in_closed_list(closed_list,neighbor):
			# for item in closed_list:
				# if neighbor.equals(item):
					continue

			temp_g_score = current.g_val + 1 # cost(current,neighbor) will always be 1

			if temp_g_score < neighbor.g_val:
				came_from[neighbor.hash_value] = current
				neighbor.g_val = temp_g_score
				neighbor.f_val = neighbor.g_val + heuristic(neighbor,goal)
				if not open_list.contains(neighbor):
					open_list.insert(neighbor)

	empty_list = list()
	return empty_list
from binary_heap import BHeap
if __name__ == "__main__":
    a = [None] * 1
    a.append([90, 'watermelon'])
    a.append([80, 'pear'])
    a.append([70, 'melon'])
    a.append([50, 'lime'])
    a.append([60, 'mango'])
    a.append([20, 'cherry'])
    a.append([30, 'grape'])
    a.append([35, 'orange'])
    a.append([10, 'apricot'])
    a.append([15, 'banana'])
    a.append([45, 'lemon'])
    a.append([40, 'kiwi'])
    b = BHeap(a)

    print('Before heap construction :')
    b.print_heap()
    b.create_heap()

    print('Minimum Heap : ')
    b.print_heap()

    print('After deleting minimum value ')
    print(b.delete_min())
    b.print_heap()
    b.insert([5, 'apple'])
    print('Inserting 5')
    b.print_heap()
Пример #5
0
from binary_heap import BHeap
import math
import pprint
pp = pprint.PrettyPrinter()

from agent import Agent
from cell import Cell

infinity = 1000000

fringe = BHeap()
costs = {}


def print_grid(grid, agent, goal):
    '''
	Use the function to print the world state to the terminal.
	agent is your Agent, goal is the goal Cell.
	'''
    matrix = [['x' for i in range(len(grid[0]))] for j in range(len(grid[1]))]
    for i in range(10):
        for j in range(10):
            if i == agent.x and j == agent.y:
                matrix[i][j] = 'A'
            elif i == goal.x and j == goal.y:
                matrix[i][j] = 'G'
            elif grid[i][j].status == 'u':
                matrix[i][j] = ' '
            else:
                matrix[i][j] = 'X'