Repository navigation
Expand file tree
/
Copy pathsolve_kidneys.py
More file actions
296 lines (242 loc) · 9.92 KB
/
Copy pathsolve_kidneys.py
File metadata and controls
296 lines (242 loc) · 9.92 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import copy
class Graph:
def __init__(self, cardinality, children_indices):
self.cardinality = cardinality
self.children_indices = dict(zip(children_indices, [True for _ in range(len(children_indices))]))
self.incoming_edges = {}
self.iterations = 0
self.outgoing_edges = {}
self.vertices = [index for index in range(cardinality)]
def add_incoming_edge(self, frm, to):
if to in self.incoming_edges:
self.incoming_edges[to].add(frm)
else:
self.incoming_edges[to] = set([frm])
def remove_incoming_edge(self, frm, to):
self.incoming_edges[to].remove(frm)
def add_outgoing_edge(self, frm, to):
if frm in self.outgoing_edges:
self.outgoing_edges[frm].add(to)
else:
self.outgoing_edges[frm] = set([to])
def remove_outgoing_edge(self, frm, to):
self.outgoing_edges[frm].remove(to)
def output_attributes(self):
print self.cardinality
print self.children_indices
print self.incoming_edges
print self.outgoing_edges
print self.vertices
def summary(self):
print "FINAL LENGTH: " + str(len(self.vertices))
print "NUMBER OF ITERATIONS: " + str(self.iterations)
def prune(self):
should_continue = True
def prune_vertex(vertex):
if vertex in self.outgoing_edges:
neighbors = self.outgoing_edges.pop(vertex)
for neighbor in neighbors:
self.remove_incoming_edge(vertex, neighbor)
if vertex in self.incoming_edges:
parents = self.incoming_edges.pop(vertex)
for parent in parents:
self.remove_outgoing_edge(parent, vertex)
while should_continue:
self.iterations += 1
should_continue = False
to_remove = []
for vertex in self.vertices:
if vertex not in self.incoming_edges or len(self.incoming_edges[vertex]) is 0:
prune_vertex(vertex)
to_remove += [vertex]
elif vertex not in self.outgoing_edges or len(self.outgoing_edges[vertex]) is 0:
prune_vertex(vertex)
to_remove += [vertex]
for _ in to_remove:
should_continue = True
self.vertices.remove(_)
def initialize(file_name):
with open(file_name, 'r') as file_instance:
cardinality = int(file_instance.readline())
children_line = file_instance.readline().strip()
if len(children_line) > 0:
children_indices = [int(child) for child in children_line.split(' ')]
else:
children_indices = []
graph_instance = Graph(cardinality, children_indices)
current_index = 0
current_line = file_instance.readline().strip()
while len(current_line) > 0:
neighbor_indices = [int(neighbor) for neighbor in current_line.split(' ')]
for neighbor_index, is_edge in enumerate(neighbor_indices):
if is_edge is 1:
graph_instance.add_incoming_edge(current_index, neighbor_index)
graph_instance.add_outgoing_edge(current_index, neighbor_index)
current_line = file_instance.readline().strip()
current_index += 1
graph_instance.prune()
return graph_instance
def findTwentyCycles(graph, vertexIndex, marked=None):
outgoingEdgesMap = graph.outgoing_edges
if marked == None:
marked = set()
paths = []
unmarkingDFS(vertexIndex, vertexIndex, outgoingEdgesMap, marked, [], 0, paths, 20)
return paths
def unmarkingDFS(startvertex, vertex, outgoingEdgesMap, marked, stack, depth, paths, pathCap):
if len(paths) < pathCap and depth < 5 and vertex not in marked:
marked.add(vertex)
stack.append(vertex)
if startvertex in outgoingEdgesMap[vertex]:
path = copy.copy(stack)
paths.append(path)
outgoingEdgesSet = (outgoingEdgesMap[vertex]) - marked
for outgoingVertex in list(outgoingEdgesSet)[:20]:
unmarkingDFS(startvertex, outgoingVertex, outgoingEdgesMap, marked, stack, depth + 1, paths, pathCap)
marked.remove(vertex)
stack.pop()
def assignCycles(graph):
# maps vertices to their assigned cycle
assignedCycles = {}
def sortfunc(vertex):
if vertex in graph.children_indices:
return 0
else:
return 1
allVertices = sorted(graph.vertices, key=sortfunc)
# iterate through all the vertices, considering one at a time
for vertex in allVertices:
print "PROCESSING VERTEX:", vertex
# find cycles for the vertex
cycles = findTwentyCycles(graph, vertex)
best_cycle = []
best_score = 0
bestdisjointCyclesToAdd = []
bestVerticesToRemoveFromAssigned = []
for cycle in cycles:
# find a set of all the vertices in this cycle that are already assigned
# and compute a score benefit of adding this cycle
overlappingVertices = set()
cycle_add_score = 0
for vertexIndex in cycle:
if vertexIndex in assignedCycles:
overlappingVertices.add(vertexIndex)
if vertexIndex in graph.children_indices:
cycle_add_score += 2
else:
cycle_add_score += 1
# if some vertices overlap we must backtrack
if len(overlappingVertices) > 0:
# the set of removed vertices is the union of all cycles
# of overlapping vertices
removedVertices = set()
for overlappingVertex in overlappingVertices:
removedVertices = removedVertices.union(assignedCycles[overlappingVertex])
# we want to test out new assignments so we copy assignedCycles
assignedCopy = copy.deepcopy(assignedCycles)
# compute the score of removing all vertices of removed vertices
# remove these vertices from assigned copy (temporary destroy)
# the cycles
cycle_remove_score = 0
for toRemoveVertex in removedVertices:
assignedCopy.pop(toRemoveVertex)
if toRemoveVertex in graph.children_indices:
cycle_remove_score += 2
else:
cycle_remove_score += 1
# temporarily assign the cycle under consideration
for current_cycle_vertex in cycle:
assignedCopy[current_cycle_vertex] = cycle
# For each vertex in a cycle destroyed by adding the current cycle
disjointCyclesToAdd = []
disjoint_cycle_add_score = 0
for toRemoveVertex in removedVertices:
# compute cycles disjoint from everything in assignedCopy
disjointCycles = findTwentyCycles(graph, toRemoveVertex, set(assignedCopy.keys()))
best_disjoint_cycle = None
best_disjoint_cycle_score = 0
# consider each disjointCycle
for disjointCycle in disjointCycles:
# compute the score of adding this disjoint cycle
disjointCycleScore = 0
for possibleDisjointCycleVertex in disjointCycle:
if possibleDisjointCycleVertex in graph.children_indices:
disjointCycleScore += 2
else:
disjointCycleScore += 1
# if it is greater than any other disjoint cycle
# FOR this vertex, make it the best
if disjointCycleScore > best_disjoint_cycle_score:
best_disjoint_cycle_score = disjointCycleScore
best_disjoint_cycle = disjointCycle
# if a best cycle exists
if best_disjoint_cycle != None:
# add it to the list of disjoint cycles to add
# IF we choose the current cycle
# assign it to assigned so future disjoint cycles dont
# overlap
disjointCyclesToAdd.append(best_disjoint_cycle)
for local_disjoint_cycle_vertex in best_disjoint_cycle:
assignedCopy[local_disjoint_cycle_vertex] = best_disjoint_cycle
# add this disjoint cycle to the sum of disjoint cycle scores
# FOR this current cycle
disjoint_cycle_add_score += best_disjoint_cycle_score
# the total score of adding this cycle is
# the sum of all the disjoint cycles we get to add
# the sum of the cycle itself
# minus the cost of the cycles we have to remove
total_score = disjoint_cycle_add_score + cycle_add_score - cycle_remove_score
# update everything IF this is the best situation
if total_score > best_score:
best_score = total_score
best_cycle = cycle
bestdisjointCyclesToAdd = disjointCyclesToAdd
bestVerticesToRemoveFromAssigned = removedVertices
# no overlapping vertices
else:
# then we can greedily compute the score, as it is disjoint
if cycle_add_score > best_score:
best_cycle = cycle
best_score = cycle_add_score
# remove the destroyed cycles
for removalVertex in bestVerticesToRemoveFromAssigned:
assignedCycles.pop(removalVertex)
# assign the chosen cycle
for best_cycle_vertex in best_cycle:
assignedCycles[best_cycle_vertex] = best_cycle
# add the replacement disjoint cycles
for disjointCycle in bestdisjointCyclesToAdd:
for disjoint_cycle_vertex in disjointCycle:
assignedCycles[disjoint_cycle_vertex] = disjointCycle
# return a list of tuples
final_cycles_set = set()
for key, value in assignedCycles.iteritems():
final_cycles_set.add(tuple(value))
final_cycles_list = list(final_cycles_set)
return final_cycles_list
def solve_kidneys(filename):
graph = initialize(filename)
cycles_list = assignCycles(graph)
return cycles_list
def format_string(cycles):
if len(cycles) == 0:
return "None"
strCyclesList = [' '.join(str(i) for i in tup) for tup in cycles]
return '; '.join(strCyclesList)
def output():
output = open('solutions.out', 'w')
for i in range(1, 126):
file_name = "instances/" + str(i) + ".in"
print "SOLVING INSTANCE:", str(i)
cycles_list = solve_kidneys(file_name)
instance_string = format_string(cycles_list)
output.write(instance_string + '\n')
def output_instance():
output = open('solutions.out', 'w')
file_name = "instances/" + str(352) + ".in"
cycles_list = solve_kidneys(file_name)
instance_string = format_string(cycles_list)
print(instance_string)
output.write(instance_string + '\n')
output()
# output_instance()