Advertisement
Guest User

A star

a guest
Jul 19th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.38 KB | None | 0 0
  1. #grid format
  2. # 0 = navigable space
  3. # 1 = occupied space
  4.  
  5. import random
  6.  
  7. grid = [[0, 1, 0, 0, 0, 0],
  8.         [0, 1, 0, 0, 0, 0],
  9.         [0, 1, 0, 0, 0, 0],
  10.         [0, 1, 0, 0, 0, 0],
  11.         [0, 0, 0, 0, 1, 0]]
  12.  
  13. heuristic = [[9, 8, 7, 6, 5, 4],
  14.              [8, 7, 6, 5, 4, 3],
  15.              [7, 6, 5, 4, 3, 2],
  16.              [6, 5, 4, 3, 2, 1],
  17.              [5, 4, 3, 2, 1, 0]]
  18.  
  19. init = [0,0]                            #Start location is (0,0) which we put it in open list.
  20. goal = [len(grid)-1,len(grid[0])-1]     #Our goal in (4,5) and here are the coordinates of the cell.
  21.  
  22. #Below the four potential actions to the single field
  23.  
  24. delta = [[-1 , 0],   #up
  25.          [ 0 ,-1],   #left
  26.          [ 1 , 0],   #down
  27.          [ 0 , 1]]   #right
  28.  
  29. delta_name = ['^','<','V','>']  #The name of above actions
  30.  
  31. cost = 1   #Each step costs you one
  32.  
  33. drone_height = 60
  34.  
  35.  
  36.  
  37. def search():
  38.     #open list elements are of the type [g,x,y]
  39.    
  40.     closed = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
  41.     action = [[-1 for row in range(len(grid[0]))] for col in range(len(grid))]
  42.     #We initialize the starting location as checked
  43.     closed[init[0]][init[1]] = 1
  44.     expand=[[-1 for row in range(len(grid[0]))] for col in range(len(grid))]
  45.    
  46.     elevation = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
  47.     for i in range(len(grid)):    
  48.         for j in range(len(grid[0])):
  49.             if grid[i][j] == 1:
  50.                 elevation[i][j] = random.randint(1,100)
  51.             else:
  52.                 elevation[i][j] = 0
  53.    
  54.     # we assigned the cordinates and g value
  55.     x = init[0]
  56.     y = init[1]
  57.     g = 0
  58.     h = heuristic[x][y]
  59.     e = elevation[x][y]
  60.     f = g + h + e
  61.    
  62.     #our open list will contain our initial value
  63.     open = [[f, g, h, x, y]]
  64.    
  65.     '''
  66.    We are going to use two flags
  67.    1- found and it will be True when the goal position is found.
  68.    2- resign it will be True if we couldn't find the goal position and explore everything.
  69.    '''
  70.     found  = False   #flag that is set when search complete
  71.     resign = False   #Flag set if we can't find expand
  72.     count = 0
  73.  
  74.     #print('initial open list:')
  75.     #for i in range(len(open)):
  76.             #print('  ', open[i])
  77.     #print('----')
  78.    
  79.     while found is False and resign is False:
  80.        
  81.         #Check if we still have elements in the open list
  82.         if len(open) == 0:    #If our open list is empty, there is nothing to expand.
  83.             resign = True
  84.             print('Fail')
  85.             print('############# Search terminated without success')
  86.             print()
  87.         else:
  88.             #if there is still elements on our list
  89.             #remove node from list
  90.             open.sort()             #sort elements in an increasing order from the smallest g value up
  91.             open.reverse()          #reverse the list
  92.             next = open.pop()       #remove the element with the smallest g value from the list
  93.             #print('list item')
  94.             #print('next')
  95.            
  96.             #Then we assign the three values to x,y and g. Which is our expantion.
  97.             x = next[3]
  98.             y = next[4]
  99.             g = next[1]
  100.             #elvation[x][y] = np.random.randint(100, size=(5,6))
  101.            
  102.             expand[x][y] = count
  103.             count+=1
  104.            
  105.             #Check if we are done
  106.             if x == goal[0] and y == goal[1]:
  107.                 found = True
  108.                 print(next) #The three elements above this "if".
  109.                 print('############## Search is success')
  110.                 print()
  111.                
  112.             else:
  113.                 #expand winning element and add to new open list
  114.                 for i in range(len(delta)):       #going through all our actions the four actions
  115.                     #We apply the actions to x and y with additional delta to construct x2 and y2
  116.                     x2 = x + delta[i][0]
  117.                     y2 = y + delta[i][1]
  118.                     #if x2 and y2 falls into the grid
  119.                     if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 <= len(grid[0])-1:
  120.                         #if x2 and y2 not checked yet and there is not obstacles
  121.                         if closed[x2][y2] == 0 and grid[x2][y2] == 0 and e < drone_height:
  122.                             g2 = g + cost             #we increment the cose
  123.                             h2 = heuristic[x2][y2]
  124.                             e2 = elevation[x2][y2]
  125.                             f2 = g2 + h2 + e2
  126.                            
  127.                             open.append([f2,g2,h2,x2,y2])   #we add them to our open list
  128.                             #print('append list item')
  129.                             #print([g2,x2,y2])
  130.                             #Then we check them to never expand again
  131.                             closed[x2][y2] = 1
  132.                             action[x2][y2] = i
  133.     for i in range(len(expand)):
  134.         print(expand[i])
  135.     print()
  136.     policy=[[' ' for row in range(len(grid[0]))] for col in range(len(grid))]
  137.     x=goal[0]
  138.     y=goal[1]
  139.     policy[x][y]='*'
  140.     while x !=init[0] or y !=init[1]:
  141.         x2=x-delta[action[x][y]][0]
  142.         y2=y-delta[action[x][y]][1]
  143.         policy[x2][y2]= delta_name[action[x][y]]
  144.         x=x2
  145.         y=y2
  146.     for i in range(len(policy)):
  147.         print(policy[i])                          
  148.  
  149. search()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement