def read_appropriate_pairs(filename): """ This function reads appropriate_pairs excel file and returns appropriate_pairs as a list of lists It takes 1 argument: i) filename : A string , which is filename of excel file It returns 2 variables: i) from_district : A list , which consists of ids of from_district ii) to_district : A list , which consists of ids of to_district """ #Get the full_path using current_directory current_directory = util.get_current_directory() full_path = current_directory + "/Appropriate_Pairs/" + filename all_data = pd.ExcelFile(full_path) worksheet = all_data.parse('Sheet1') #Get the columns into columns columns = worksheet.columns #It is easier to work with list objects,so cast them to list objects. columns = list(columns) from_district = worksheet['From_District'] to_district = worksheet['To_District'] return from_district , to_district
def read_new_district_names(filename): """ This function gives new_district_names """ current_directory = util.get_current_directory() full_path = current_directory + "/Coords/" + filename all_data = pd.ExcelFile(full_path) worksheet_1 = all_data.parse('Sheet1') #Get the columns into columns columns_1 = worksheet_1.columns #It is easier to work with list objects,so cast them to list objects. columns_1 = list(columns_1) if IS_DEBUG == True: print("The columns in the first worksheet are: " , columns_1) print("The columns in the second workshett are: " , columns_2) #Read all the x_coordinates and y_coordinates old_district_names = worksheet_1['AD'].values #Get rid of empty districts new_district_names = [] for i in range(len(old_district_names)): if type(old_district_names[i]) == type(" "): new_district_names.append(old_district_names[i]) return new_district_names
def read_cloud_solution(filename): """ This function reads solution of the optimization problem which is solved by cplex_cloud """ current_directory = util.get_current_directory() full_path = current_directory + "/Solutions/" + filename solution_file = open(full_path , 'r') content = solution_file.read() is_solution_start = False is_solution_end = False solution_array = [] for i in range(len(content)) : if content[i : i + 5] == "y = [" : is_solution_start = True if is_solution_start == True and is_solution_end == False : if 48 <= ord(content[i]) <= 49 : solution_array.append(int(content[i])) if is_solution_start == True and content[i] == "]" : is_solution_end = True return solution_array
def combine_queries(length_of_appropriate_pairs): """ This function reads all the cleaned query files, then it combines them in an array which consists of len(appropriate_pairs) lists each of which consists of 60 data points It takes no argument. It returns 1 variable: combined_data_points : A list of lists , whose length is len(appropriate_pairs) , each element list has length 60 """ #Get the full path using current_directory current_directory = util.get_current_directory() full_path = current_directory + "/New_Data_Points" #Initialize the variables combined_data_points = [] for i in range(length_of_appropriate_pairs): combined_data_points.append([]) #Iterate over all excel queries and add the data points accordingly for filename in os.listdir(full_path): if filename.endswith(".xlsx") and filename[0] == 'w': d_excel_file = pd.ExcelFile(full_path + "/" + filename) worksheet_1 = d_excel_file.parse('Sheet1' , header = None) for i in range(len(worksheet_1)): if worksheet_1.iloc[i,2] != 0 : combined_data_points[i].append(worksheet_1.iloc[i,2]) else: print("You are not a excel file dute.") return combined_data_points
def read_query_file(filename): """ This function read a query file. It takes 1 argument: i) filename : A string , name of the excel file to read It returns 3 variables: i) from_district : A list which consists of ids of from_district ii) to_district : A list which consists of ids of to_district iii) duration : A list which consists of travel time between from_district and to_district such as "8 mins" """ #Get the full path using current directory current_directory = util.get_current_directory() full_path = current_directory + "/Data_Points/" + filename #Open excel file d_excel_file = pd.ExcelFile(full_path) worksheet = d_excel_file.parse('Sheet1' , header = None) #Initialize the variables from_district = [] to_district = [] duration = [] #Fill the variables usign excel data for i in range(len(worksheet)): from_district.append(worksheet.iloc[i , 0]) to_district.append(worksheet.iloc[i , 1]) duration.append(worksheet.iloc[i , 2]) return from_district , to_district , duration
def read_binary_txt(filename): """ This function reads solution txt which consist of NUMBER_OF_DISTRICT binary values in line 1 It takes 1 argument: filename : A string , which is the name of the txt file It returns 1 variable: solution_array : A list , which consist of NUMBER_OF_DISTRICT binary values """ current_directory = util.get_current_directory() full_path = current_directory + "/Solutions/" + filename #Open the file and read the content of it into the variable "content" file = open(full_path , 'r') content = file.read() #Get rid of \n at the at of the content content = content.strip() #Collect all the elements in array solution_array = [] for element in content: if element != ' ': solution_array.append(element) #Make the elements integer and win the war. for i in range(len(solution_array)): solution_array[i] = int(solution_array[i]) return solution_array
def polygon_coords(filename): """ This function reads x-y data of polygon coordinates and return it """ #Get full_path using current_directory current_directory = util.get_current_directory() full_path = current_directory + "\Coords/" + filename all_data = pd.ExcelFile(full_path) worksheet_1 = all_data.parse('Sheet1') #Get the columns into columns columns_1 = worksheet_1.columns #It is easier to work with list objects,so cast them to list objects. columns_1 = list(columns_1) id_district = worksheet_1['shapeid'].values x_district = worksheet_1['x'].values y_district = worksheet_1['y'].values #Initialize variables lat = [] longs = [] for i in range(util.VIS_NUMBER_OF_DISTRICT): lat.append([]) longs.append([]) for i in range(len(x_district)): if pd.isna(x_district[i]) != True and pd.isna(y_district[i]) != True and pd.isna(id_district[i]) != True : while x_district[i] > 100 : x_district[i] /= 10 while y_district[i] > 100 : y_district[i] /= 10 lat[int(id_district[i]/10)].append(y_district[i]) longs[int(id_district[i]/10)].append(x_district[i]) return lat,longs
def read_availability_matrix(filename): """ This function reads filename.xlsx and returns availability matrix. """ #Get full path using current_directory current_directory = util.get_current_directory() full_path = current_directory + "/Availability_Matrix/" + filename #Open excel file to write excel_file = pd.ExcelFile(full_path) worksheet = excel_file.parse('Sheet1' , header = None) #Initialize availability_matrix availability_matrix = [] for i in range(len(worksheet)): availability_matrix.append([]) for k in range(util.NUMBER_OF_DISTRICT) : availability_matrix[i].append(worksheet.iloc[i][k]) return availability_matrix