-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqap_solver
executable file
·105 lines (90 loc) · 3.16 KB
/
qap_solver
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
#!/usr/bin/python2
import sys
import math
import os.path
import operator
from time import time
from solution import Solution
import evolutionary as evo
import local_search as local
import input_processor as processor
POPULATION = 10
GENERATIONS = 7
ls = lambda x: local.search(x,43.5)
ils = lambda x: local.eager_search(x, 1)
sa = lambda x: local.annealing(x,1050500)
def timereps(slns, ls):
start = time()
slns = evo.genetic(slns,GENERATIONS,ls)
end = time()
return (end - start,slns)
def run_ls(n, distances, flows, coeff):
solution = Solution(n, distances, flows)
start = time()
local.search(solution, coeff)
end = time()
return (solution, end - start)
def run_sa(n, distances, flows, temp):
solution = Solution(n, distances, flows)
start = time()
local.annealing(solution, temp)
end = time()
return (solution, end - start)
def run_ils(n, distances, flows, coeff):
solution = Solution(n, distances, flows)
start = time()
local.eager_search(solution, coeff)
end = time()
return (solution, end - start)
def run_evo(n, distances, flows, generations=GENERATIONS):
print 's1, s2, s3, t1, t2, t3'
for _ in xrange(0, generations):
solutions1 = map(lambda x: x.randomize(n),
map(
lambda (x,y,z): Solution(x,y,z),
[(n, distances, flows)] * POPULATION
)
)
solutions2 = map(lambda x: x.randomize(n),
map(
lambda (x,y,z): Solution(x,y,z),
[(n, distances, flows)] * POPULATION
)
)
solutions3 = map(lambda x: x.randomize(n),
map(
lambda (x,y,z): Solution(x,y,z),
[(n, distances, flows)] * POPULATION
)
)
(t1,solutions1) = timereps(solutions1,ls)
(t2,solutions2) = timereps(solutions2,sa)
(t3,solutions3) = timereps(solutions3,ils)
print ("{0},{1},{2},{3},{4},{5}".format(
min(solutions1,key=operator.attrgetter('cost')).cost,
min(solutions2,key=operator.attrgetter('cost')).cost,
min(solutions3,key=operator.attrgetter('cost')).cost,
t1,t2,t3
)
)
def main():
if len(sys.argv) < 4 or len(sys.argv) > 4:
sys.exit('Bad arguments')
if os.path.isfile(sys.argv[3]):
file = open(sys.argv[3],'r')
else:
sys.exit('Not a file')
(n, distances, flows) = processor.processInput(file)
if sys.argv[1] == 'ls':
(sln, t) = run_ls(n, distances, flows, int(sys.argv[2]))
print sln.cost, t
elif sys.argv[1] == 'sa':
(sln, t) = run_sa(n, distances, flows, int(sys.argv[2]))
print sln.cost, t
elif sys.argv[1] == 'ils':
(sln, t) = run_ils(n, distances, flows, int(sys.argv[2]))
print sln.cost, t
elif sys.argv[1] == 'evo':
sln = run_evo(n, distances, flows ,int(sys.argv[2]))
if __name__ == "__main__":
main()