Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This file contains all the required routines to make an A* search algorithm.
- #
- __authors__ = 'TO_BE_FILLED'
- __group__ = 'TO_BE_FILLED'
- # _________________________________________________________________________________________
- # Intel.ligencia Artificial
- # Grau en Enginyeria Informatica
- # Curs 2016- 2017
- # Universitat Autonoma de Barcelona
- # _______________________________________________________________________________________
- from SubwayMap import *
- from utils import *
- import os
- import math
- import copy
- def expand(path, map):
- """
- It expands a SINGLE station and returns the list of class Path.
- Format of the parameter is:
- Args:
- path (object of Path class): Specific path to be expanded
- map (object of Map class):: All the information needed to expand the node
- Returns:
- path_list (list): List of paths that are connected to the given path.
- """
- path_list = []
- lista_aux = []
- for x in map.connections[path.last].keys():
- lista_aux = Path(path.route.copy())
- path_list.append(lista_aux)
- path_list[-1].add_route(x)
- """
- en lista_aux hacemos copia del path
- """
- return path_list
- def remove_cycles(path_list):
- """
- It removes from path_list the set of paths that include some cycles in their path.
- Format of the parameter is:
- Args:
- path_list (LIST of Path Class): Expanded paths
- Returns:
- path_list (list): Expanded paths without cycles.
- """
- """
- lista_aux = []
- for station in path_list:
- isRepe=False
- ultima_estacion = station.route[-1]
- for i in station.route:
- if ultima_estacion == i and count!=len(station.route):
- isRepe=True
- if isRepe==False:
- lista_aux.append(station.route)
- """
- list_nocycle = []
- for station in path_list:
- lista_sin_repeticiones = set(station.route)
- if len(lista_sin_repeticiones) == len(station.route):
- list_nocycle.append(station)
- """
- 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
- """
- return list_nocycle
- pass
- def insert_depth_first_search(expand_paths, list_of_path):
- """
- expand_paths is inserted to the list_of_path according to DEPTH FIRST SEARCH algorithm
- Format of the parameter is:
- Args:
- expand_paths (LIST of Path Class): Expanded paths
- list_of_path (LIST of Path Class): The paths to be visited
- Returns:
- list_of_path (LIST of Path Class): List of Paths where Expanded Path is inserted
- """
- list_of_path = expand_paths + list_of_path
- return list_of_path
- def depth_first_search(origin_id, destination_id, map):
- """
- Depth First Search algorithm
- Format of the parameter is:
- Args:
- origin_id (int): Starting station id
- destination_id (int): Final station id
- map (object of Map class): All the map information
- Returns:
- list_of_path[0] (Path Class): the route that goes from origin_id to destination_id
- """
- path1 = []
- path1 = [Path([origin_id])]
- while path1[0].last!= destination_id and path1!=[]:
- aux = path1[0]
- expandir = expand(aux, map)
- Rm = remove_cycles(expandir)
- path1 = insert_depth_first_search(Rm, path1[1:])
- if path1!=[]:
- print(path1[0])
- return path1[0]
- else:
- return 'No path'
- def insert_breadth_first_search(expand_paths, list_of_path):
- """
- expand_paths is inserted to the list_of_path according to BREADTH FIRST SEARCH algorithm
- Format of the parameter is:
- Args:
- expand_paths (LIST of Path Class): Expanded paths
- list_of_path (LIST of Path Class): The paths to be visited
- Returns:
- list_of_path (LIST of Path Class): List of Paths where Expanded Path is inserted
- """
- list_of_path += expand_paths
- return list_of_path
- def breadth_first_search(origin_id, destination_id, map):
- """
- Breadth First Search algorithm
- Format of the parameter is:
- Args:
- origin_id (int): Starting station id
- destination_id (int): Final station id
- map (object of Map class): All the map information
- Returns:
- list_of_path[0] (Path Class): The route that goes from origin_id to destination_id
- """
- path2 = []
- path2 = [Path([origin_id])]
- while path2[0].last!= destination_id and path2!=[]:
- aux = path2[0]
- expandir = expand(aux, map)
- Rm = remove_cycles(expandir)
- path2 = insert_breadth_first_search(Rm, path2[1:])
- if path2!=[]:
- return path2[0]
- else:
- return 'No path'
- def calculate_cost(expand_paths, map, type_preference=0):
- """
- Calculate the cost according to type preference
- Format of the parameter is:
- Args:
- expand_paths (LIST of Paths Class): Expanded paths
- map (object of Map class): All the map information
- type_preference: INTEGER Value to indicate the preference selected:
- 0 - Adjacency
- 1 - minimum Time
- 2 - minimum Distance
- 3 - minimum Transfers
- Returns:
- expand_paths (LIST of Paths): Expanded path with updated cost
- """
- pass
- def insert_cost(expand_paths, list_of_path):
- """
- expand_paths is inserted to the list_of_path according to COST VALUE
- Format of the parameter is:
- Args:
- expand_paths (LIST of Path Class): Expanded paths
- list_of_path (LIST of Path Class): The paths to be visited
- Returns:
- list_of_path (LIST of Path Class): List of Paths where expanded_path is inserted according to cost
- """
- pass
- def uniform_cost_search(origin_id, destination_id, map, type_preference=0):
- """
- Uniform Cost Search algorithm
- Format of the parameter is:
- Args:
- origin_id (int): Starting station id
- destination_id (int): Final station id
- map (object of Map class): All the map information
- Returns:
- list_of_path[0] (Path Class): The route that goes from origin_id to destination_id
- """
- pass
- def calculate_heuristics(expand_paths, map, destination_id, type_preference=0):
- """
- Calculate and UPDATE the heuristics of a path according to type preference
- WARNING: In calculate_cost, we didn't update the cost of the path inside the function
- for the reasons which will be clear when you code Astar (HINT: check remove_redundant_paths() function).
- Format of the parameter is:
- Args:
- expand_paths (LIST of Path Class): Expanded paths
- map (object of Map class): All the map information
- type_preference: INTEGER Value to indicate the preference selected:
- 0 - Adjacency
- 1 - minimum Time
- 2 - minimum Distance
- 3 - minimum Transfers
- Returns:
- expand_paths (LIST of Path Class): Expanded paths with updated heuristics
- """
- pass
- def update_f(expand_paths):
- """
- Update the f of a path
- Format of the parameter is:
- Args:
- expand_paths (LIST of Path Class): Expanded paths
- Returns:
- expand_paths (LIST of Path Class): Expanded paths with updated costs
- """
- pass
- def remove_redundant_paths(expand_paths, list_of_path, visited_stations_cost):
- """
- It removes the Redundant Paths. They are not optimal solution!
- If a station is visited and have a lower g in this moment, we should remove this path.
- Format of the parameter is:
- Args:
- expand_paths (LIST of Path Class): Expanded paths
- list_of_path (LIST of Path Class): All the paths to be expanded
- visited_stations_cost (dict): All visited stations cost
- Returns:
- new_paths (LIST of Path Class): Expanded paths without redundant paths
- list_of_path (LIST of Path Class): list_of_path without redundant paths
- """
- pass
- def insert_cost_f(expand_paths, list_of_path):
- """
- expand_paths is inserted to the list_of_path according to f VALUE
- Format of the parameter is:
- Args:
- expand_paths (LIST of Path Class): Expanded paths
- list_of_path (LIST of Path Class): The paths to be visited
- Returns:
- list_of_path (LIST of Path Class): List of Paths where expanded_path is inserted according to f
- """
- pass
- def coord2station(coord, map):
- """
- From coordinates, it searches the closest station.
- Format of the parameter is:
- Args:
- coord (list): Two REAL values, which refer to the coordinates of a point in the city.
- map (object of Map class): All the map information
- Returns:
- possible_origins (list): List of the Indexes of stations, which corresponds to the closest station
- """
- pass
- def Astar(origin_coor, dest_coor, map, type_preference=0):
- """
- A* Search algorithm
- Format of the parameter is:
- Args:
- origin_id (list): Starting station id
- destination_id (int): Final station id
- map (object of Map class): All the map information
- type_preference: INTEGER Value to indicate the preference selected:
- 0 - Adjacency
- 1 - minimum Time
- 2 - minimum Distance
- 3 - minimum Transfers
- Returns:
- list_of_path[0] (Path Class): The route that goes from origin_id to destination_id
- """
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement