def improve_from_file(input_file, output_directory, parapms=[]): print('Processing %s' % (input_file)) input_data = utils.read_file(input_file) num_of_locations, num_houses, list_locations, list_houses, starting_car_location, adjacency_matrix = data_parser( input_data) car_path, drop_offs = solve(list_locations, list_houses, starting_car_location, adjacency_matrix, params=params) G, _ = adjacency_matrix_to_graph(adjacency_matrix) new_cost, _ = student_utils.cost_of_solution(G, car_path, drop_offs) basename, filename = os.path.split(input_file) output_filename = utils.input_to_output(filename, "") output_file = f'{output_directory}/{output_filename}' output_data = utils.read_file(output_file) car_cycle = convert_locations_to_indices(car_cycle, list_locations) old_cost, _ = student_utils.cost_of_solution(G, car_cycle, drop_offs) if new_cost < old_cost: print(input_file, "improved from", old_cost, 'to', new_cost) if not os.path.exists(output_directory): os.makedirs(output_directory) convertToFile(car_path, drop_offs, output_file, list_locations) else: print("No improvments made for", input_file)
def naive_solver(list_of_locations, list_of_homes, starting_car_location, adjacency_matrix): G, _ = adjacency_matrix_to_graph(adjacency_matrix) car_path = [int(starting_car_location)] drop_off = {int(starting_car_location): [int(h) for h in list_of_homes]} cost, _ = student_utils.cost_of_solution(G, car_path, drop_off) utils.write_data_to_file('logs/naive.log', [cost], separator='\n', append=True) return car_path, drop_off
def two_opt_solver(list_of_locations, list_of_homes, starting_car_location, adjacency_matrix): G, _ = adjacency_matrix_to_graph(adjacency_matrix) all_pairs_shortest_path = dict(nx.floyd_warshall(G)) _, visit_order = nearest_neighbor_tour(list_of_homes, starting_car_location, all_pairs_shortest_path, G) visit_order = two_opt(visit_order, all_pairs_shortest_path) car_path = generate_full_path(visit_order, G) drop_off = find_drop_off_mapping(car_path, list_of_homes, all_pairs_shortest_path) cost, _ = student_utils.cost_of_solution(G, car_path, drop_off) print(len(list_of_locations), 'locations', 'two_opt:', cost) return car_path, drop_off
def greedy_solver(list_of_locations, list_of_homes, starting_car_location, adjacency_matrix): G, _ = adjacency_matrix_to_graph(adjacency_matrix) all_pairs_shortest_path = dict(nx.floyd_warshall(G)) car_path, visit_order = nearest_neighbor_tour(list_of_homes, starting_car_location, all_pairs_shortest_path, G) drop_off = find_drop_off_mapping(car_path, list_of_homes, all_pairs_shortest_path) cost, _ = student_utils.cost_of_solution(G, car_path, drop_off) utils.write_data_to_file('logs/greedy.log', [cost], separator='\n', append=True) print(len(list_of_locations), 'locations', 'greedy:', cost) return car_path, drop_off
def evaluate(G, path, home_idxs, verbose=False): """ Assigns optimal dropoff locations for each TA, given a path that the car is going to take, and calculate the total energy expended. This is the metric that neighbors are going to be evaluated on. """ dropoffs = assign_dropoffs(G, path, home_idxs) cost, msg = cost_of_solution(G, path, dropoffs, shortest=all_pairs_dists) if verbose: print(msg) return cost
def solve_some(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, 'in') num_so_far = 0 score = 0 for input_file in input_files: num_so_far += 1 G, car_cycle, dropoff_mapping = solve_from_file_score( input_file, output_directory) this_score = student_utils.cost_of_solution(G, car_cycle, dropoff_mapping)[0] graph_total = sum(G[u][v]['weight'] for (u, v) in G.edges) score += graph_total / this_score if num_so_far == 10: print("score:") print(score) break
def solve(list_of_locations, list_of_homes, starting_car_location, adjacency_matrix, params=[]): """ Write your algorithm here. Input: list_of_locations: A list of locations such that node i of the graph corresponds to name at index i of the list list_of_homes: A list of homes starting_car_location: The name of the starting location for the car adjacency_matrix: The adjacency matrix from the input file Output: A list of locations representing the car path A dictionary mapping drop-off location to a list of homes of TAs that got off at that particular location NOTE: both outputs should be in terms of indices not the names of the locations themselves """ graph = adjacency_matrix_to_graph(adjacency_matrix)[ 0] ## maybe we want a graph object from network x instead mapping = dict(zip(graph, list_of_locations)) graph = netx.relabel_nodes(graph, mapping) list_of_locations = [str(i) for i in list_of_locations] searchAgent = search.SearchAgent(adjacency_matrix, list_of_homes, starting_car_location, list_of_locations) result = searchAgent.astar() # order_approx_agent = orderApproximators.OrderApproximator(adjacency_matrix, list_of_homes, starting_car_location, # list_of_locations) # result = order_approx_agent.steiner_aneal() # print("Steiner MST approx", cost_of_solution(graph, result[0], result[1])) # nn_agent = nearest_neighbors.NearestNeighbors(adjacency_matrix, list_of_homes, starting_car_location, list_of_locations) # result = nn_agent.get_dropoff_ordering_ns() # print("Nearest neighbour3 approx", cost_of_solution(graph, result[0], result[1])) """steiner_approx_solver = SteinerApproxSolver.SteinerApproxSolver(adjacency_matrix, list_of_homes, starting_car_location, list_of_locations) brr=steiner_approx_solver.solveSteinerTreeDTH() steiner_approx_solver_order = cost_of_solution(graph,brr[0],brr[1]) print(steiner_approx_solver_order)""" return result + [cost_of_solution(graph, result[0], result[1])[0]]
def ant_colony(list_of_locations, list_of_homes, starting_car_location, adjacency_matrix): G, _ = adjacency_matrix_to_graph(adjacency_matrix) all_pairs_shortest_path = dict(nx.floyd_warshall(G)) _, tour = nearest_neighbor_tour(list_of_homes, starting_car_location, all_pairs_shortest_path, G) tour = tour[1:] newGraph = build_tour_graph(G, tour, all_pairs_shortest_path) solution = ant_colony_tour(newGraph, starting_car_location) car_path = generate_full_path(solution, G) drop_off = find_drop_off_mapping(car_path, list_of_homes, all_pairs_shortest_path) cost, _ = student_utils.cost_of_solution(G, car_path, drop_off) utils.write_data_to_file('logs/ant_colony.log', [cost], separator='\n', append=True) print(len(list_of_locations), 'locations', 'ant_colony:', cost) return car_path, drop_off
def mst_solver(list_of_locations, list_of_homes, starting_car_location, adjacency_matrix): G, _ = adjacency_matrix_to_graph(adjacency_matrix) all_pairs_shortest_path = dict(nx.floyd_warshall(G)) verticies = set(list_of_homes) verticies.add(int(starting_car_location)) verticies = list(verticies) newGraph = build_tour_graph(G, verticies, all_pairs_shortest_path) mst = nx.minimum_spanning_tree(newGraph) mst_tour = list( nx.dfs_preorder_nodes(newGraph, source=int(starting_car_location))) mst_tour.append(int(starting_car_location)) car_path = generate_full_path(mst_tour, G) drop_off = find_drop_off_mapping(car_path, list_of_homes, all_pairs_shortest_path) cost, _ = student_utils.cost_of_solution(G, car_path, drop_off) utils.write_data_to_file('logs/mst.log', [cost], separator='\n', append=True) print(len(list_of_locations), 'locations', 'mst:', cost) return car_path, drop_off
def runner(list_of_locations, list_of_homes, starting_car_location, num_clusters, adjacency_matrix, linkage): g, msg = adjacency_matrix_to_graph(adjacency_matrix) home_indices = home_names_to_indices(list_of_homes, list_of_locations) #print('number of homes is '+ str(len(home_indices))) home_distance_matrix = make_home_distance_matrix(g, home_indices) distance_matrix = make_distance_matrix(g, len(list_of_locations)) clustering = make_clusters(num_clusters, linkage, home_distance_matrix) #clustering = make_feat_clusters(num_clusters, home_distance_matrix) #print('number of clustering is '+ str(len(home_indices))) clusters = key_to_clusters(clustering, home_indices) bstops = all_bus_stop(clusters, distance_matrix) #print('we will have ' + str(len(bstops)) + ' bus stops') starting_index = index_of_start(starting_car_location, list_of_locations) stops = [starting_index] + bstops distance_matrix_of_stops = make_home_distance_matrix(g, stops) bus_route = route(distance_matrix_of_stops, 0) for i in range(len(bus_route)): bus_route[i] = stops[bus_route[i]] complete_route = bus_stop_routing_to_complete_routing(bus_route, g) dropoffs = dropoff_dict(clusters, bstops) cost, m = stu.cost_of_solution(g, complete_route, dropoffs) return complete_route, dropoffs, cost
def greedy_clustering_three_opt_best_ratio(list_of_locations, list_of_homes, starting_car_location, adjacency_matrix): def findsubsets(s, n): result = [] for i in range(n): ls = [list(x) for x in list(itertools.combinations(s, i + 1))] result.extend(ls) return result G, _ = adjacency_matrix_to_graph(adjacency_matrix) shortest = dict(nx.floyd_warshall(G)) tour = [int(starting_car_location)] stops = [int(starting_car_location)] remain_bus_stop = set([int(l) for l in list_of_locations]) remain_bus_stop.remove(int(starting_car_location)) drop_off_map = find_drop_off_mapping(tour, list_of_homes, shortest) walk_cost = calc_walking_cost(drop_off_map, shortest) drive_cost = calc_driving_cost(tour, shortest) bestCost = walk_cost + drive_cost while True: bestTour = None bestStop = None best_ratio = 0 bstops = findsubsets(remain_bus_stop, 3) for bstop in bstops: new_tour = stops + bstop new_drop_off_map = find_drop_off_mapping(new_tour, list_of_homes, shortest) _, new_tour = nearest_neighbor_tour(new_tour, starting_car_location, shortest, G) new_tour = three_opt(new_tour, shortest) new_walk_cost = calc_walking_cost(new_drop_off_map, shortest) walk_cost_decrease = walk_cost - new_walk_cost new_drive_cost = calc_driving_cost(new_tour, shortest) drive_cost_increase = new_drive_cost - drive_cost #print(walk_cost_decrease, drive_cost_increase) if drive_cost_increase > 0 and best_ratio < (walk_cost_decrease / drive_cost_increase): bestStop = bstop best_ratio = walk_cost_decrease / drive_cost_increase bestTour = new_tour walk_cost = new_walk_cost drive_cost = new_drive_cost bestCost = new_walk_cost + new_drive_cost if best_ratio > 0: for b in bestStop: remain_bus_stop.remove(int(b)) tour = bestTour stops = stops + bestStop sys.stdout.write(str(bestCost) + '\n') # same as print sys.stdout.flush() else: break car_path = generate_full_path(tour, G) drop_off = find_drop_off_mapping(tour, list_of_homes, shortest) cost, _ = student_utils.cost_of_solution(G, car_path, drop_off) utils.write_data_to_file('logs/greedy_clustering_three_opt.log', [cost], separator='\n', append=True) print(len(list_of_locations), 'locations', 'greedy_clustering_three_opt:', cost) return car_path, drop_off
def greedy_clustering_two_opt(list_of_locations, list_of_homes, starting_car_location, adjacency_matrix, bus_stop_look_ahead): def findsubsets(s, n): result = [] for i in range(n): ls = [list(x) for x in list(itertools.combinations(s, i + 1))] result.extend(ls) return result G, _ = adjacency_matrix_to_graph(adjacency_matrix) shortest = dict(nx.floyd_warshall(G)) tour = [int(starting_car_location)] #stops = [int(starting_car_location)] remain_bus_stop = set([int(l) for l in list_of_locations]) remain_bus_stop.remove(int(starting_car_location)) drop_off_map = find_drop_off_mapping(tour, list_of_homes, shortest) min_walk_cost = calc_walking_cost(drop_off_map, shortest) min_drive_cost = calc_driving_cost(tour, shortest) minCost = min_walk_cost + min_drive_cost while True: bestTour = None bestStop = None bestCost = minCost bstops = findsubsets(remain_bus_stop, bus_stop_look_ahead) #print("number of stops",len(bstops), flush = True) for bstop in bstops: new_tour = tour + bstop new_drop_off_map = find_drop_off_mapping(new_tour, list_of_homes, shortest) new_tour = fast_nearest_neighbor_tour(new_tour, starting_car_location, shortest) new_tour = two_opt(new_tour, shortest) new_walk_cost = calc_walking_cost(new_drop_off_map, shortest) new_drive_cost = calc_driving_cost(new_tour, shortest) new_cost = new_walk_cost + new_drive_cost if new_cost < bestCost: bestStop = bstop bestCost = new_cost bestTour = new_tour if bestCost < minCost: for b in bestStop: remain_bus_stop.remove(b) minCost = bestCost tour = bestTour print(minCost, flush=True) #sys.stdout.write(str(minCost) + '\n') # same as print #sys.stdout.flush() else: break tour = three_opt(tour, shortest) car_path = generate_full_path(tour, G) drop_off = find_drop_off_mapping(tour, list_of_homes, shortest) cost, _ = student_utils.cost_of_solution(G, car_path, drop_off) utils.write_data_to_file('logs/greedy_clustering_three_opt.log', [cost], separator='\n', append=True) print(len(list_of_locations), 'locations', 'greedy_clustering_two_opt:', cost) return car_path, drop_off
def get_score_status(): input_dir = '../inputs/' output_dir = '../outputs/' optimal_dir = output_dir + 'optimal/' suboptimal_dir = output_dir + 'suboptimal/' d = {} optimal_solutions = {} suboptimal_solutions = {} scores = {} for f in os.listdir(optimal_dir): optimal_solutions[f.strip('.out')] = f for f in os.listdir(suboptimal_dir): temp = 0 for i in range(len(f)): if f[i] == '_': if temp == 1: suboptimal_solutions[f[:i]] = f break temp += 1 for f in os.listdir(input_dir): problem = f.strip('.in') if problem not in optimal_solutions and problem not in suboptimal_solutions: d[problem] = ['Unsolved', -1.0, -1.0] else: nodes, houses, start, adj_mat = util.readInput(input_dir + f) trivial_dropoff = {} trivial_path = [start] trivial_dropoff[start] = houses solver = ilp.graphSolver(nodes, houses, start, adj_mat, False) if problem in optimal_solutions: output_file = open(optimal_dir + optimal_solutions[problem], 'r') path = output_file.readline().strip().split(' ') output_file.close() #curr_cost = solver.fitness(path) curr_cost = student_utils.cost_of_solution(solver.G, path, solver.get_pedestrian_walks(path)) if (curr_cost[0] == 'infinite'): print(problem, 'infinite') continue curr_cost = float(curr_cost[0]) trivial_cost = student_utils.cost_of_solution(solver.G, trivial_path, trivial_dropoff) trivial_cost = float(trivial_cost[0]) score = (curr_cost/trivial_cost)*100 if not scores.get(score, False): scores[score] = [(problem, curr_cost)] else: scores[score].append((problem, curr_cost)) elif problem in suboptimal_solutions: output_file = open(suboptimal_dir + suboptimal_solutions[problem], 'r') path = output_file.readline().strip().split(' ') output_file.close() gap = '' for l in suboptimal_solutions[problem].strip('.out')[::-1]: if l == '_': break gap += l gap = float(gap[::-1]) #curr_cost = solver.fitness(path) curr_cost = student_utils.cost_of_solution(solver.G, path, solver.get_pedestrian_walks(path)) if (curr_cost[0] == 'infinite'): print(problem, 'infinite') continue curr_cost = float(curr_cost[0]) trivial_cost = student_utils.cost_of_solution(solver.G, trivial_path, trivial_dropoff) trivial_cost = float(trivial_cost[0]) score = (curr_cost/trivial_cost)*100 if not scores.get(score, False): scores[score] = [(problem, curr_cost)] else: scores[score].append((problem, curr_cost)) print(problem, score, curr_cost) return scores
def cost(self, car_cycle, dropoff_mapping): return su.cost_of_solution(self.G, car_cycle, dropoff_mapping)