-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
136 lines (121 loc) · 5.66 KB
/
solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import os
import sys
sys.path.append('..')
sys.path.append('../..')
import argparse
import utils
import networkx as nx
from solvers.mst import mst_dfs_solve
from solvers.flp import flp_solve
from solvers.local_search import local_search_solve
from solvers.greedy import greedy_shortest_path_solve
from student_utils import *
"""
======================================================================
Complete the following function.
======================================================================
"""
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
"""
G, _ = adjacency_matrix_to_graph(adjacency_matrix)
mst_t, mst_d = mst_dfs_solve(list_of_locations, list_of_homes, starting_car_location, adjacency_matrix)
greedy_t, greedy_d = greedy_shortest_path_solve(list_of_locations, list_of_homes, starting_car_location, adjacency_matrix)
flp_t_0, flp_d_0 = flp_solve(list_of_locations,
list_of_homes,
starting_car_location,
adjacency_matrix, 0)
flp_t_1, flp_d_1 = flp_solve(list_of_locations,
list_of_homes,
starting_car_location,
adjacency_matrix, 1)
flp_t_2, flp_d_2 = flp_solve(list_of_locations,
list_of_homes,
starting_car_location,
adjacency_matrix, 2)
options = [
('FLP0', flp_t_0, flp_d_0),
('FLP1', flp_t_1, flp_d_1),
('FLP2', flp_t_2, flp_d_2),
('MST_DFS', mst_t, mst_d),
('GREEDY_SP', greedy_t, greedy_d),
]
solver_name, solution, dropoffs = min(options, key=lambda x: cost_of_solution(G, x[1], x[2])[0])
print(f'\nUsed {solver_name}\n')
local_t, local_d = local_search_solve(list_of_locations,
list_of_homes,
starting_car_location,
adjacency_matrix,
initial_solution=solution)
return local_t, local_d
"""
======================================================================
No need to change any code below this line
======================================================================
"""
"""
Convert solution with path and dropoff_mapping in terms of indices
and write solution output in terms of names to path_to_file + file_number + '.out'
"""
def convertToFile(path, dropoff_mapping, path_to_file, list_locs):
string = ''
for node in path:
string += list_locs[node] + ' '
string = string.strip()
string += '\n'
dropoffNumber = len(dropoff_mapping.keys())
string += str(dropoffNumber) + '\n'
for dropoff in dropoff_mapping.keys():
strDrop = list_locs[dropoff] + ' '
for node in dropoff_mapping[dropoff]:
strDrop += list_locs[node] + ' '
strDrop = strDrop.strip()
strDrop += '\n'
string += strDrop
utils.write_to_file(path_to_file, string)
def solve_from_file(input_file, output_directory, params=[]):
print('Processing', 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)
basename, filename = os.path.split(input_file)
if not os.path.exists(output_directory):
os.makedirs(output_directory)
output_file = utils.input_to_output(input_file, output_directory)
convertToFile(car_path, drop_offs, output_file, list_locations)
def solve_all(input_directory, output_directory, params=[]):
input_files = utils.get_files_with_extension(input_directory, 'in')
for input_file in input_files:
try:
solve_from_file(input_file, output_directory, params=params)
except:
with open('./err.txt', 'a') as f:
f.write(input_file + '\n')
if __name__=="__main__":
parser = argparse.ArgumentParser(description='Parsing arguments')
parser.add_argument('--all', action='store_true', help='If specified, the solver is run on all files in the input directory. Else, it is run on just the given input file')
parser.add_argument('input', type=str, help='The path to the input file or directory')
parser.add_argument('output_directory', type=str, nargs='?', default='.', help='The path to the directory where the output should be written')
parser.add_argument('params', nargs=argparse.REMAINDER, help='Extra arguments passed in')
args = parser.parse_args()
output_directory = args.output_directory
if args.all:
input_directory = args.input
solve_all(input_directory, output_directory, params=args.params)
else:
input_file = args.input
solve_from_file(input_file, output_directory, params=args.params)