Advertisement
Guest User

SearchAlgorytm

a guest
Feb 29th, 2020
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.35 KB | None | 0 0
  1. # This file contains all the required routines to make an A* search algorithm.
  2. #
  3. __authors__ = 'TO_BE_FILLED'
  4. __group__ = 'TO_BE_FILLED'
  5. # _________________________________________________________________________________________
  6. # Intel.ligencia Artificial
  7. # Grau en Enginyeria Informatica
  8. # Curs 2016- 2017
  9. # Universitat Autonoma de Barcelona
  10. # _______________________________________________________________________________________
  11.  
  12. from SubwayMap import *
  13. from utils import *
  14. import os
  15. import math
  16. import copy
  17.  
  18.  
  19. def expand(path, map):
  20.     """
  21.     It expands a SINGLE station and returns the list of class Path.
  22.     Format of the parameter is:
  23.        Args:
  24.            path (object of Path class): Specific path to be expanded
  25.            map (object of Map class):: All the information needed to expand the node
  26.        Returns:
  27.            path_list (list): List of paths that are connected to the given path.
  28.    """
  29.     path_list = []
  30.     lista_aux = []
  31.    
  32.     for x in map.connections[path.last].keys():
  33.        
  34.         lista_aux = Path(path.route.copy())
  35.         path_list.append(lista_aux)
  36.         path_list[-1].add_route(x)      
  37.        
  38.         """
  39.        en lista_aux hacemos copia del path
  40.        
  41.        
  42.        """
  43.            
  44.     return path_list
  45.    
  46.    
  47.  
  48.  
  49. def remove_cycles(path_list):
  50.     """
  51.     It removes from path_list the set of paths that include some cycles in their path.
  52.     Format of the parameter is:
  53.        Args:
  54.            path_list (LIST of Path Class): Expanded paths
  55.        Returns:
  56.            path_list (list): Expanded paths without cycles.
  57.    """
  58.    
  59.     """
  60.    lista_aux = []
  61.    
  62.    
  63.    for station in path_list:
  64.        isRepe=False
  65.        ultima_estacion = station.route[-1]
  66.        for i in station.route:
  67.            
  68.            if ultima_estacion == i and count!=len(station.route):
  69.                isRepe=True
  70.        if isRepe==False:
  71.            lista_aux.append(station.route)
  72.            
  73.    """    
  74.     list_nocycle = []
  75.     for station in path_list:
  76.         lista_sin_repeticiones = set(station.route)
  77.         if len(lista_sin_repeticiones) == len(station.route):
  78.             list_nocycle.append(station)
  79.     """
  80.    si la longitud del set es mas pequeño que la del x.route, quiere decir que hay un elemento repetido, ya que en el set no existen valores iguales
  81.    """
  82.     return list_nocycle
  83.     pass
  84.  
  85.  
  86. def insert_depth_first_search(expand_paths, list_of_path):
  87.     """
  88.     expand_paths is inserted to the list_of_path according to DEPTH FIRST SEARCH algorithm
  89.     Format of the parameter is:
  90.        Args:
  91.            expand_paths (LIST of Path Class): Expanded paths
  92.            list_of_path (LIST of Path Class): The paths to be visited
  93.        Returns:
  94.            list_of_path (LIST of Path Class): List of Paths where Expanded Path is inserted
  95.    """
  96.    
  97.     list_of_path = expand_paths + list_of_path
  98.     return list_of_path
  99.    
  100.    
  101.  
  102.  
  103. def depth_first_search(origin_id, destination_id, map):
  104.     """
  105.     Depth First Search algorithm
  106.     Format of the parameter is:
  107.        Args:
  108.            origin_id (int): Starting station id
  109.            destination_id (int): Final station id
  110.            map (object of Map class): All the map information
  111.        Returns:
  112.            list_of_path[0] (Path Class): the route that goes from origin_id to destination_id
  113.    """
  114.     path1 = []
  115.     path1 = [Path([origin_id])]
  116.    
  117.     while path1[0].last!= destination_id and path1!=[]:
  118.         aux = path1[0]
  119.         expandir = expand(aux, map)
  120.         Rm = remove_cycles(expandir)
  121.         path1 = insert_depth_first_search(Rm, path1[1:])
  122.     if path1!=[]:
  123.         print(path1[0])
  124.         return path1[0]
  125.     else:
  126.         return 'No path'
  127.    
  128.  
  129.  
  130. def insert_breadth_first_search(expand_paths, list_of_path):
  131.     """
  132.        expand_paths is inserted to the list_of_path according to BREADTH FIRST SEARCH algorithm
  133.        Format of the parameter is:
  134.           Args:
  135.               expand_paths (LIST of Path Class): Expanded paths
  136.               list_of_path (LIST of Path Class): The paths to be visited
  137.           Returns:
  138.               list_of_path (LIST of Path Class): List of Paths where Expanded Path is inserted
  139.    """
  140.     list_of_path += expand_paths
  141.     return list_of_path
  142.    
  143.  
  144.  
  145. def breadth_first_search(origin_id, destination_id, map):
  146.     """
  147.     Breadth First Search algorithm
  148.     Format of the parameter is:
  149.        Args:
  150.            origin_id (int): Starting station id
  151.            destination_id (int): Final station id
  152.            map (object of Map class): All the map information
  153.        Returns:
  154.            list_of_path[0] (Path Class): The route that goes from origin_id to destination_id
  155.    """
  156.     path2 = []
  157.     path2 = [Path([origin_id])]
  158.    
  159.     while path2[0].last!= destination_id and path2!=[]:
  160.         aux = path2[0]
  161.         expandir = expand(aux, map)
  162.         Rm = remove_cycles(expandir)
  163.         path2 = insert_breadth_first_search(Rm, path2[1:])
  164.     if path2!=[]:
  165.        
  166.         return path2[0]
  167.     else:
  168.         return 'No path'
  169.    
  170.    
  171.  
  172.  
  173. def calculate_cost(expand_paths, map, type_preference=0):
  174.     """
  175.         Calculate the cost according to type preference
  176.         Format of the parameter is:
  177.            Args:
  178.                expand_paths (LIST of Paths Class): Expanded paths
  179.                map (object of Map class): All the map information
  180.                type_preference: INTEGER Value to indicate the preference selected:
  181.                                0 - Adjacency
  182.                                1 - minimum Time
  183.                                2 - minimum Distance
  184.                                3 - minimum Transfers
  185.            Returns:
  186.                expand_paths (LIST of Paths): Expanded path with updated cost
  187.    """
  188.     pass
  189.  
  190.  
  191. def insert_cost(expand_paths, list_of_path):
  192.     """
  193.        expand_paths is inserted to the list_of_path according to COST VALUE
  194.        Format of the parameter is:
  195.           Args:
  196.               expand_paths (LIST of Path Class): Expanded paths
  197.               list_of_path (LIST of Path Class): The paths to be visited
  198.           Returns:
  199.               list_of_path (LIST of Path Class): List of Paths where expanded_path is inserted according to cost
  200.    """
  201.     pass
  202.  
  203.  
  204. def uniform_cost_search(origin_id, destination_id, map, type_preference=0):
  205.     """
  206.     Uniform Cost Search algorithm
  207.     Format of the parameter is:
  208.        Args:
  209.            origin_id (int): Starting station id
  210.            destination_id (int): Final station id
  211.            map (object of Map class): All the map information
  212.        Returns:
  213.            list_of_path[0] (Path Class): The route that goes from origin_id to destination_id
  214.    """
  215.     pass
  216.  
  217.  
  218. def calculate_heuristics(expand_paths, map, destination_id, type_preference=0):
  219.     """
  220.     Calculate and UPDATE the heuristics of a path according to type preference
  221.     WARNING: In calculate_cost, we didn't update the cost of the path inside the function
  222.              for the reasons which will be clear when you code Astar (HINT: check remove_redundant_paths() function).
  223.     Format of the parameter is:
  224.        Args:
  225.            expand_paths (LIST of Path Class): Expanded paths
  226.            map (object of Map class): All the map information
  227.            type_preference: INTEGER Value to indicate the preference selected:
  228.                            0 - Adjacency
  229.                            1 - minimum Time
  230.                            2 - minimum Distance
  231.                            3 - minimum Transfers
  232.        Returns:
  233.            expand_paths (LIST of Path Class): Expanded paths with updated heuristics
  234.    """
  235.     pass
  236.  
  237.  
  238.  
  239. def update_f(expand_paths):
  240.     """
  241.      Update the f of a path
  242.      Format of the parameter is:
  243.         Args:
  244.             expand_paths (LIST of Path Class): Expanded paths
  245.         Returns:
  246.             expand_paths (LIST of Path Class): Expanded paths with updated costs
  247.    """
  248.     pass
  249.  
  250.  
  251. def remove_redundant_paths(expand_paths, list_of_path, visited_stations_cost):
  252.     """
  253.      It removes the Redundant Paths. They are not optimal solution!
  254.      If a station is visited and have a lower g in this moment, we should remove this path.
  255.      Format of the parameter is:
  256.         Args:
  257.             expand_paths (LIST of Path Class): Expanded paths
  258.             list_of_path (LIST of Path Class): All the paths to be expanded
  259.             visited_stations_cost (dict): All visited stations cost
  260.         Returns:
  261.             new_paths (LIST of Path Class): Expanded paths without redundant paths
  262.             list_of_path (LIST of Path Class): list_of_path without redundant paths
  263.    """
  264.     pass
  265.  
  266.  
  267. def insert_cost_f(expand_paths, list_of_path):
  268.     """
  269.        expand_paths is inserted to the list_of_path according to f VALUE
  270.        Format of the parameter is:
  271.           Args:
  272.               expand_paths (LIST of Path Class): Expanded paths
  273.               list_of_path (LIST of Path Class): The paths to be visited
  274.           Returns:
  275.               list_of_path (LIST of Path Class): List of Paths where expanded_path is inserted according to f
  276.    """
  277.     pass
  278.  
  279.  
  280. def coord2station(coord, map):
  281.     """
  282.        From coordinates, it searches the closest station.
  283.        Format of the parameter is:
  284.        Args:
  285.            coord (list):  Two REAL values, which refer to the coordinates of a point in the city.
  286.            map (object of Map class): All the map information
  287.        Returns:
  288.            possible_origins (list): List of the Indexes of stations, which corresponds to the closest station
  289.    """
  290.     pass
  291.  
  292.  
  293. def Astar(origin_coor, dest_coor, map, type_preference=0):
  294.     """
  295.     A* Search algorithm
  296.     Format of the parameter is:
  297.        Args:
  298.            origin_id (list): Starting station id
  299.            destination_id (int): Final station id
  300.            map (object of Map class): All the map information
  301.            type_preference: INTEGER Value to indicate the preference selected:
  302.                            0 - Adjacency
  303.                            1 - minimum Time
  304.                            2 - minimum Distance
  305.                            3 - minimum Transfers
  306.        Returns:
  307.            list_of_path[0] (Path Class): The route that goes from origin_id to destination_id
  308.    """
  309.     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement