Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.51 KB | None | 0 0
  1. import sys
  2. from Queue import Queue, PriorityQueue
  3. from sets import Set
  4.  
  5.  
  6. # Take in a node, then return the neighbours of the node
  7. def getNeighbours(g, id, rows, cols):
  8.     (i, j) = id
  9.     # In the order UP, DOWN, LEFT, RIGHT
  10.     possibleNodes = [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]
  11.     results = []
  12.     # print(possibleNodes)
  13.     for possibleNode in possibleNodes:
  14.         if valid(g,possibleNode, rows, cols):
  15.             # print(possibleNode)
  16.             results.append(possibleNode)
  17.     # print('Current Node', id)
  18.     # print('Results from GetNeighbours: ', results)
  19.     # results = filter(valid(g,currentNode), possibleNodes)
  20.     print('Neighbours',results)
  21.     # *** MIGHT NOT NEED TO REVERSE ***
  22.     # results = reversed(results)
  23.     return results
  24.  
  25. def valid(g, node, rows, cols):
  26.     (i, j) = node
  27.     if 0 <= i  <= rows and 0 <= j <= cols:
  28.         if g[i][j] != 'X':
  29.             return True
  30.     else:
  31.         return False
  32.  
  33. def drawGrid(g,explored, start, currentNode):
  34.     t = False
  35.     res = currentNode
  36.     g[8][0] = 'A'
  37.     while not t:
  38.         if res == start:
  39.             t = True
  40.         (i,j) = res
  41.         g[i][j] = '*'
  42.         res = explored[res]
  43.     # **Printing the Map**
  44.     for i in range(0, len(g)):
  45.         for j in range(0,len(g[0])):
  46.             print(g[i][j]),
  47.         print('')
  48.  
  49. def nextCost(g, current, next):
  50.     (i,j) = current
  51.     (x,y) = next
  52.     currentVal = int(g[i][j])
  53.     nextVal = int(g[x][y])
  54.  
  55.     # print('CurrVal,NextVal: ',currentVal, nextVal)
  56.     # Return 1 if staying 'level' or going 'downhill'
  57.     if nextVal <= currentVal:
  58.         return 1
  59.     # Return 1 + Difference of points if going 'uphill'
  60.     elif nextVal > currentVal:
  61.         cost = 1 + (nextVal - currentVal)
  62.         return cost
  63.  
  64.  
  65.     # if int(g[x][y] )
  66.     return 1
  67.  
  68. def bfs(g, start, end, rows, cols): # Returns a solution, or a failure
  69.     # print('---BFS started---')
  70.     # initialise the fringe/fringe - A list of nodes we know about but have not visited yet
  71.     fringe = Queue()
  72.     fringe.put(start)
  73.     cost = 0
  74.     # initialise the explored list
  75.     explored = {}
  76.     explored[start] = None
  77.     # loop
  78.     while not fringe.empty():
  79.         # print('---WHILE loop---')
  80.         currentNode = fringe.get()
  81.         # print(currentNode)
  82.         # print(fringe.queue)
  83.         if currentNode == end:
  84.             # print('FOUND IT', currentNode)
  85.             # print(cost)
  86.             drawGrid(g, explored, start, currentNode)
  87.             break
  88.         # if
  89.         for nextNode in getNeighbours(g, currentNode, rows, cols):
  90.             if nextNode not in explored:
  91.                 fringe.put(nextNode)
  92.                 explored[nextNode] = currentNode
  93.                 cost += 1
  94.                 # print('Current:', currentNode)
  95.                 # print('Next:', nextNode)
  96.  
  97.  
  98.  
  99.  
  100. def ucs(g, start, end, rows, cols):
  101.     # print('---UCS started---')
  102.     # initialise the fringe - A list of nodes we know about but have not visited yet
  103.     # different to the Queue used in BFS, a PriorityQueue is used for this algorithm
  104.     fringe = PriorityQueue()
  105.     count = 0
  106.     fringe.put(start, 0, count)
  107.     # initialise the explored list
  108.     explored = {}
  109.     explored[start] = None
  110.     # initialise the cost list
  111.     costList = {}
  112.     costList[start] = 0
  113.     # loop
  114.     while not fringe.empty():
  115.         # print('---WHILE loop---')
  116.         print('Frontier', fringe.queue)
  117.         currentNode = fringe.get()
  118.         print('Queue Chooses:', currentNode)
  119.         if currentNode == end:
  120.             print('FOUND IT', currentNode)
  121.             # print(cost)
  122.             break
  123.  
  124.         for nextNode in getNeighbours(g, currentNode, rows, cols):
  125.             nextNodeCost = nextCost(g, currentNode, nextNode)
  126.             cost = costList[currentNode] + nextNodeCost
  127.             print(cost)
  128.             if nextNode not in costList or cost < costList[nextNode]:
  129.                 count += 1
  130.                 costList[nextNode] = cost
  131.                 explored[nextNode] = currentNode
  132.                 nodePriority = cost
  133.                 fringe.put(nextNode, count, nodePriority)
  134.                 print(fringe.queue)
  135.                 print('Just Explored:', currentNode)
  136.                 print('Count: ', count)
  137.                 print('Added to Fringe:', nextNode)
  138.                 print('')
  139.                 print(' ')
  140.     drawGrid(g, explored, start, currentNode)
  141.  
  142. def astar(g, start, end, rows, cols, heuristic):
  143.     pass
  144.  
  145. def main():
  146.     args = ['null'] * 3
  147.     # Placing arguments in a list
  148.     # FORMAT: args[0] = map, args[1] = bfs/ucs/astar, args[2] = heuristic
  149.     for i in range(1,4):
  150.        try:
  151.            args[i - 1] = sys.argv[i]
  152.        except IndexError:
  153.            pass
  154.  
  155.     # Opening file and storing input
  156.     with open(args[0]) as f:
  157.         lines = [line.split() for line in f]
  158.  
  159.     # print(lines)
  160.  
  161.     # Rows(R) and Columns(C)
  162.     R = int(lines[0][0])-1
  163.     C = int(lines[0][1])-1
  164.  
  165.     # Start Point and End Point
  166.     start = (int(lines[1][0])-1, int(lines[1][1])-1)
  167.     end = (int(lines[2][0])-1, int(lines[2][1])-1)
  168.     # print(start, end)
  169.     # c = Maze(R,C)
  170.     # print(c)
  171.     # Storing the map
  172.     g = []
  173.     for i in range(3, R+4):
  174.         g.append(lines[i])
  175.  
  176.     if args[1] == 'astar':
  177.         if args[2] == 'euclidian':
  178.             pass
  179.         elif args[2] == 'manhattan':
  180.             pass
  181.     elif args[1] == 'bfs':
  182.         bfs(g,start, end, R, C)
  183.     elif args[1] == 'ucs':
  184.         ucs(g,start, end, R, C)
  185.  
  186. if __name__ == '__main__':
  187.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement