-
Notifications
You must be signed in to change notification settings - Fork 0
/
mst_solution.py
70 lines (47 loc) · 1.8 KB
/
mst_solution.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
import algorithm
import sanity
import supreme_mst
import construction
T = 495 # number of test cases
old_solutions = []
fold = open("answer.out", "r")
for _ in range(T):
old_solutions = old_solutions + [[int(x) for x in fold.readline().split()]]
fout = open ("answer.out", "w")
for t in range(1, T+1):
fin = open("./instances/" + str(t) + ".in", "r")
N = int(fin.readline())
d = [[] for i in range(N)]
for i in range(N):
d[i] = [int(x) for x in fin.readline().split()]
c = fin.readline()
# find an answer, and put into assign
if t%50 == 0:
print ("*** DIVISION " + str(t) + " ***")
old_sol = sanity.denormalize(old_solutions[t-1], N)
old_score = sanity.weight(old_sol,d)
if old_score == 0 or len(old_sol) < 7:
path = old_sol
else:
print ("*** TRIAL " + str(t) + " ***")
print old_score
new_edges = supreme_mst.mst_solver(d, N)
path = algorithm.find_good_construction(new_edges, c, N, selection_method=construction.domain_randomly_select)
print sanity.weight(path, new_edges)
path = algorithm.targeted_improve(path, new_edges, c, 500)
print sanity.weight(path, new_edges)
path = algorithm.improve(path, new_edges, c, 1, 100)
print sanity.weight(path, new_edges)
# path = algorithm.improve(path, d, c, 2, 20)
post = sanity.weight(path, d)
if post < old_score:
print ("NEW: " + str(post))
print ("OLD: " + str(old_score))
print ("IMPROVEMENT: " + str(old_score-post))
print ("")
if len(old_sol) > 0 and old_score < post:
path = old_sol
post = sanity.weight(old_sol,d)
path = sanity.normalize(path, N)
fout.write("%s\n" % " ".join(map(str, path)))
fout.close()