-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver2helper.py
277 lines (234 loc) · 10.2 KB
/
solver2helper.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import networkx as nx
import os
import random
from multiprocessing import Pool
from random import choice
###########################################
# Change this variable to the path to
# the folder containing all three input
# size category folders
###########################################
path_to_inputs = "./all_inputs"
###########################################
# Change this variable if you want
# your outputs to be put in a
# different folder
###########################################
path_to_outputs = "./all_outputs"
def parse_input(folder_name):
'''
Parses an input and returns the corresponding graph and parameters
Inputs:
folder_name - a string representing the path to the input folder
Outputs:
(graph, num_buses, size_bus, constraints)
graph - the graph as a NetworkX object
num_buses - an integer representing the number of buses you can allocate to
size_buses - an integer representing the number of students that can fit on a bus
constraints - a list where each element is a list vertices which represents a single rowdy group
'''
graph = nx.read_gml(folder_name + "/graph.gml")
parameters = open(folder_name + "/parameters.txt")
num_buses = int(parameters.readline())
size_bus = int(parameters.readline())
constraints = []
for line in parameters:
line = line[1: -2]
curr_constraint = [num.replace("'", "") for num in line.split(", ")]
constraints.append(curr_constraint)
return graph, num_buses, size_bus, constraints
def solve(graph, num_buses, bus_size, constraints):
#TODO: Write this method as you like. We'd recommend changing the arguments here as well
print('start solve')
num_kids = len(graph.nodes())
max_bus_size = min(bus_size, num_kids - num_buses + 1)
for i in range(len(constraints)):
constraints[i] = set(constraints[i])
#constraints = sorted(constraints, key=lambda c: -len(c))
def isrowdy(group):
for constraint in constraints:
if constraint.issubset(group):
return constraint
return None
clusters = []
copy = graph.copy()
while copy.nodes():
clustering = nx.clustering(copy)
start = max([entry for entry in clustering.items()], key=lambda e: e[1])[0]
print('\tinitial node: ' + start, end='')
cluster = set([start])
fringe = set(copy.neighbors(start))
fgraph = nx.Graph()
fgraph.add_node(start)
for node in fringe:
fgraph.add_node(node)
for neighbor in copy.neighbors(node):
if neighbor in cluster:
fgraph.add_edge(node, neighbor)
while fringe and len(cluster) < max_bus_size:
degrees = fgraph.degree()
candidates = sorted(fringe, key=lambda node: -(degrees[node] + clustering[node]))
index = -1
for i in range(len(candidates)):
if not isrowdy(cluster.union(set([candidates[i]]))):
index = i
break
if index == -1:
break
candidate = candidates[index]
fringe.remove(candidate)
cluster.add(candidate)
for neighbor in copy.neighbors(candidate):
if neighbor in fringe:
fgraph.add_edge(candidate, neighbor)
elif neighbor not in cluster:
fringe.add(neighbor)
fgraph.add_node(neighbor)
for neighbor_neighbor in copy.neighbors(neighbor):
if neighbor_neighbor in cluster:
fgraph.add_edge(neighbor, neighbor_neighbor)
cgraph = nx.Graph()
for node in cluster:
cgraph.add_node(node)
for neighbor in copy.neighbors(node):
if neighbor in cluster:
cgraph.add_edge(node, neighbor)
while False:
cdegrees = cgraph.degree()
loneliest = min(cluster, key=lambda node: cdegrees[node])
fcopy = fgraph.copy()
fcopy.remove_node(loneliest)
fdegrees = fcopy.degree()
friendly_list = sorted(fringe, key=lambda node: -fdegrees[node])
friendliest = None
difference = cluster.difference(set([loneliest]))
for node in friendly_list:
if fdegrees[node] <= cdegrees[loneliest]:
break
if fdegrees[node] > cdegrees[loneliest] and not isrowdy(difference.union(set([node]))):
friendliest = node
break
if not friendliest:
break
cgraph.remove_node(loneliest)
fgraph.add_node(loneliest)
fgraph.remove_node(friendliest)
cgraph.add_node(friendliest)
cluster.remove(loneliest)
fringe.add(loneliest)
fringe.remove(friendliest)
cluster.add(friendliest)
for neighbor in copy.neighbors(loneliest):
if neighbor in fringe:
fgraph.add_edge(loneliest, neighbor)
elif neighbor not in cluster:
fgraph.add_node(neighbor)
for neighbor_neighbor in copy.neighbors(neighbor):
if neighbor_neighbor in cluster:
fgraph.add_edge(neighbor, neighbor_neighbor)
for neighbor in copy.neighbors(friendliest):
if neighbor in cluster:
cgraph.add_edge(friendliest, neighbor)
clusters.append(cluster)
copy.remove_nodes_from(cluster)
print('\tsize: ' + str(len(cluster)))
clusters = sorted(clusters, key=lambda c: -len(c))
#print(clusters)
buses = []
if len(clusters) < num_buses:
while len(clusters) < num_buses:
source = 0
while source + 1 < len(clusters) and len(clusters[source + 1]) > 1:
source += 1
subgraph = graph.subgraph(clusters[source])
degrees = subgraph.degree()
loneliest = min([node for node in clusters[source]], key=lambda node: degrees[node])
clusters[source].remove(loneliest)
clusters.append(set([loneliest]))
for cluster in clusters:
buses.append(cluster)
elif len(clusters) > num_buses:
for i in range(num_buses - 1):
buses.append(clusters[i])
buses.append(set())
for i in range(num_buses - 1, len(clusters)):
added = False
for j in range(num_buses):
if len(buses[j]) + len(clusters[i]) <= bus_size and not isrowdy(buses[j].union(clusters[i])):
buses[j].update(clusters[i])
added = True
break
if added:
continue
for j in range(num_buses - 1, -1, -1):
if len(buses[j]) + len(clusters[i]) <= bus_size:
buses[j].update(clusters[i])
added = True
break
if added:
continue
for j in range(num_buses - 1, -1, -1):
while clusters[i] and len(buses[j]) < bus_size:
buses[j].add(clusters[i].pop())
if not len(buses[-1]):
source = 0
while source + 1 < len(buses) and len(buses[source + 1]) > 1:
source += 1
subgraph = graph.subgraph(buses[source])
degrees = subgraph.degree()
loneliest = min([node for node in buses[source]], key=lambda node: degrees[node])
buses[source].remove(loneliest)
buses[-1].add(loneliest)
else:
for i in range(num_buses):
buses.append(clusters[i])
print('end solve')
print(buses)
return [list(bus) for bus in buses]
def solve_and_write(params):
output_path = params[4]
sol = solve(params[0], params[1], params[2], params[3])
output_file = open(output_path, "w")
for bus in sol:
output_file.write(str(bus) + '\n')
output_file.close()
def main():
'''
Main method which iterates over all inputs and calls `solve` on each.
The student should modify `solve` to return their solution and modify
the portion which writes it to a file to make sure their output is
formatted correctly.
'''
size_categories = ["medium"]
if not os.path.isdir(path_to_outputs):
os.mkdir(path_to_outputs)
for size in size_categories:
category_path = path_to_inputs + "/" + size
output_category_path = path_to_outputs + "/" + size
category_dir = os.fsencode(category_path)
if not os.path.isdir(output_category_path):
os.mkdir(output_category_path)
problems = []
# put all problem inputs into a list
for input_folder in os.listdir(category_dir):
input_name = os.fsdecode(input_folder)
graph, num_buses, size_bus, constraints = parse_input(category_path + "/" + input_name)
output_path = output_category_path + "/" + input_name + ".out"
problem = (graph, num_buses, size_bus, constraints, output_path)
problems.append(problem)
# in a parallel way, solve all the problems
with Pool(5) as p:
p.map(solve_and_write, problems)
'''
for input_folder in os.listdir(category_dir):
input_name = os.fsdecode(input_folder)
graph, num_buses, size_bus, constraints = parse_input(category_path + "/" + input_name)
print('solving', size, input_name)
solution = solve(graph, num_buses, size_bus, constraints)
output_file = open(output_category_path + "/" + input_name + ".out", "w")
for bus in solution:
output_file.write(str(bus) + '\n')
output_file.close()
'''
if __name__ == '__main__':
main()