Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.72 KB | None | 0 0
  1. import Queue
  2. import operator
  3.  
  4. """Consist of x and y co-ordinates of a particular location"""
  5.  
  6.  
  7. class Node:
  8.  
  9.     def __init__(self, x, y):
  10.         self.x = x
  11.         self.y = y
  12.         # self.z = z
  13.  
  14.     def __repr__(self):
  15.         return str(self.x) + "," + str(self.y)
  16.  
  17.     def __eq__(self, other):
  18.         if other != None:
  19.             return str(self.x) == str(other.x) and str(self.y) == str(other.y)
  20.         return True
  21.  
  22.     def __ne__(self, other):
  23.         if other != None:
  24.             return str(self.x) != str(other.x) or str(self.y) != str(other.y)
  25.         return True
  26.  
  27.     def __cmp__(self, other):
  28.         return str(self.x) == str(other.x) and str(self.y) == str(other.y)
  29.  
  30.     def __hash__(self):
  31.         return (hash(self.x)) + (2 * hash(self.y))
  32.  
  33.     def is_valid_location(self, i, j, col, row):
  34.         if 0 <= i < int(col) and 0 <= j < int(row):
  35.             return True
  36.         return False
  37.  
  38.     def get_north(self, col, row):
  39.         if self.is_valid_location(self.x, self.y - 1, col, row):
  40.             return Node(self.x, self.y - 1)
  41.         return None
  42.  
  43.     def get_north_east(self, col, row):
  44.         if self.is_valid_location(self.x + 1, self.y - 1, col, row):
  45.             return Node(self.x + 1, self.y - 1)
  46.         return None
  47.  
  48.     def get_north_west(self, col, row):
  49.         if self.is_valid_location(self.x - 1, self.y - 1, col, row):
  50.             return Node(self.x - 1, self.y - 1)
  51.         return None
  52.  
  53.     def get_west(self, col, row):
  54.         if self.is_valid_location(self.x - 1, self.y, col, row):
  55.             return Node(self.x - 1, self.y)
  56.         return None
  57.  
  58.     def get_south_west(self, col, row):
  59.         if self.is_valid_location(self.x - 1, self.y + 1, col, row):
  60.             return Node(self.x - 1, self.y + 1)
  61.         return None
  62.  
  63.     def get_south(self, col, row):
  64.         if self.is_valid_location(self.x + 1, self.y, col, row):
  65.             return Node(self.x + 1, self.y)
  66.         return None
  67.  
  68.     def get_south_east(self, col, row):
  69.         if self.is_valid_location(self.x + 1, self.y + 1, col, row):
  70.             return Node(self.x + 1, self.y + 1)
  71.         return None
  72.  
  73.     def get_east(self, col, row):
  74.         if self.is_valid_location(self.x, self.y + 1, col, row):
  75.             return Node(self.x, self.y + 1)
  76.         return None
  77.  
  78.  
  79. class Search:
  80.     actions = {"north": "get_north", "east": "get_east", "south": "get_south", "west": "get_west", "north_east":
  81.         "get_north_east", "north_west": "get_north_west", "south_east": "get_south_east",
  82.                "south_west": "get_south_west"}
  83.  
  84.     def __init__(self, type_of_search, landing_site, target_sites, state, max_z_elevation, col, row):
  85.         self.type_of_search = type_of_search
  86.         self.landing_site = landing_site
  87.         self.target_sites = target_sites
  88.         self.state = state
  89.         self.max_z_elevation = max_z_elevation
  90.         self.col = col
  91.         self.row = row
  92.  
  93.     def __repr__(self):
  94.         return "Type of search: " + str(self.type_of_search) + " Landing Site: " + str(
  95.             self.landing_site) + " Target sites: " + \
  96.                str(self.target_sites) + " Max Z elevation:" + str(self.max_z_elevation)
  97.  
  98.     def goal_test(self, curr_site, target_site):
  99.         return curr_site == target_site
  100.  
  101.     def find_route(self, no_of_target_sites):
  102.         if self.type_of_search == "BFS":
  103.             for i in range(int(no_of_target_sites)):
  104.                 self.bfs(self.target_sites[i])
  105.         if self.type_of_search == "UCS":
  106.             for i in range(int(no_of_target_sites)):
  107.                 self.ucs(self.target_sites[i])
  108.         if self.type_of_search == "A*":
  109.             for i in range(int(no_of_target_sites)):
  110.                 self.a_star(self.target_sites[i])
  111.  
  112.     def print_route(self, child_parent_dict, target_site):
  113.         route = []
  114.         present_site = target_site
  115.         while present_site != self.landing_site:
  116.             route.append(present_site)
  117.             present_site = child_parent_dict[present_site]
  118.         route.append(self.landing_site)
  119.         route = route[::-1]
  120.         print(route)
  121.  
  122.     def bfs(self, target_site):
  123.         frontier = Queue.Queue()
  124.         explored = []
  125.         child_parent_dict = {}
  126.         if self.goal_test(self.landing_site, target_site):
  127.             self.print_route(None, self.landing_site)
  128.             return
  129.         frontier.put(self.landing_site)
  130.         while not frontier.empty():
  131.             current_site = frontier.get()
  132.             explored.append(current_site)
  133.             for action in self.actions:
  134.                 site = eval(
  135.                     "current_site" + "." + self.actions.get(action) + "(" + self.col + ", " + self.row + ")")
  136.                 if site is not None and abs(
  137.                         int(self.state[site.y][site.x]) - int(self.state[current_site.y][current_site.x])) <= int(
  138.                     self.max_z_elevation):
  139.                     if site not in explored:
  140.                         if self.goal_test(site, target_site):
  141.                             child_parent_dict[site] = current_site
  142.                             self.print_route(child_parent_dict, site)
  143.                             return
  144.                         frontier.put(site)
  145.                         child_parent_dict[site] = current_site
  146.         print("FAIL")
  147.         return
  148.  
  149.     def ucs(self, target_site):
  150.         priority_frontier = {self.landing_site: 0}
  151.         explored = []
  152.         child_parent_dict = {}
  153.         path_cost = 0
  154.         while True:
  155.             if not priority_frontier:
  156.                 print("FAIL")
  157.                 return
  158.             sites_sorted_by_path_cost = sorted(priority_frontier.items(), key=operator.itemgetter(1))
  159.             current_site = sites_sorted_by_path_cost[0][0]
  160.             del priority_frontier[current_site]
  161.             if self.goal_test(current_site, target_site):
  162.                 self.print_route(child_parent_dict, current_site)
  163.                 return
  164.             explored.append(current_site)
  165.             for action in self.actions:
  166.                 site = eval("current_site" + "." + self.actions.get(action) + "(" + self.col + ", " + self.row + ")")
  167.                 if site not in explored and site not in priority_frontier:
  168.                     if site is not None and abs(
  169.                             int(self.state[site.y][site.x]) - int(self.state[current_site.y][current_site.x])) <= int(
  170.                             self.max_z_elevation):
  171.                         child_parent_dict[site] = current_site
  172.                         if action == "north_east" or action == "north_west" or action == "south_east" or action == \
  173.                                 "south_west":
  174.                             path_cost += 14
  175.                             priority_frontier[site] = path_cost
  176.                         else:
  177.                             path_cost += 10
  178.                             priority_frontier[site] = path_cost
  179.                 elif priority_frontier.get(site) > path_cost:
  180.                     child_parent_dict[site] = current_site
  181.                     priority_frontier[site] = path_cost
  182.  
  183.     def a_star(self, target_site):
  184.         return "FAIL"
  185.  
  186.  
  187. inputFile = open("Test.txt")
  188. typeOfSearch = inputFile.readline().strip()
  189. col, row = inputFile.readline().split()
  190. xL, yL = inputFile.readline().split()
  191. start = Node(int(xL), int(yL))
  192. zElevation = inputFile.readline().strip()
  193. noOfTargetSites = inputFile.readline().strip()
  194. targetSites = []
  195. for i in range(int(noOfTargetSites)):
  196.     x, y = inputFile.readline().split()
  197.     n = Node(x, y)
  198.     targetSites.append(n)
  199. searchSpace = [line.split() for line in inputFile.readlines()]
  200.  
  201. s = Search(typeOfSearch, start, targetSites, searchSpace, zElevation, col, row)
  202. print(s)
  203. s.find_route(noOfTargetSites)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement