Repository navigation
Expand file tree
/
Copy pathch09_ex2.py
More file actions
91 lines (72 loc) · 1.89 KB
/
Copy pathch09_ex2.py
File metadata and controls
91 lines (72 loc) · 1.89 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
#!/usr/bin/env python3
"""Functional Python Programming
Chapter 9, Example Set 2
An example of an optimization problem:
https://www.me.utexas.edu/~jensen/ORMM/models/unit/combinatorics/permute.html
"""
# Cost data
cost_data="""\
14,11,6,20,12,9,4
15,28,34,4,12,24,21
16,31,22,18,31,15,23
20,18,9,15,30,4,18
24,8,24,30,28,25,4
3,23,22,11,5,30,5
13,7,5,10,7,7,32
"""
import csv
import io
def get_cost_matrix():
with io.StringIO(cost_data) as source:
rdr= csv.reader(source)
cost= list(tuple(map(int,row)) for row in rdr)
return cost
from itertools import *
def assignment( cost ):
n= len(cost)
perms = permutations( range(n) )
alt= [ (sum(cost[x][y] for y,x in enumerate(perm)), perm) for perm in perms ]
m= min(alt)[0]
return [ans for s, ans in alt if s == m]
test_assignment="""
>>> from pprint import pprint
>>> cost= get_cost_matrix()
>>> len(cost)
7
>>> pprint(cost)
[(14, 11, 6, 20, 12, 9, 4),
(15, 28, 34, 4, 12, 24, 21),
(16, 31, 22, 18, 31, 15, 23),
(20, 18, 9, 15, 30, 4, 18),
(24, 8, 24, 30, 28, 25, 4),
(3, 23, 22, 11, 5, 30, 5),
(13, 7, 5, 10, 7, 7, 32)]
>>> solutions = assignment(cost)
>>> pprint(solutions)
[(2, 4, 6, 1, 5, 3, 0), (2, 6, 0, 1, 5, 3, 4)]
>>> expected= tuple(map(lambda x:x-1, [3,5,7,2,6,4,1] ) )
>>> expected
(2, 4, 6, 1, 5, 3, 0)
>>> expected in solutions
True
"""
def performance():
"""Takes almost 1 minute."""
import timeit
perf= timeit.timeit( """list(permutations(range(10)))""", """from itertools import permutations""", number=100 )
print( "10!", perf/100 )
test_combinations="""
>>> hands= list(combinations( tuple(product(range(13),'♠♥♦♣')), 5 ))
>>> print( len(hands) )
2598960
"""
__test__ = {
"test_assignment": test_assignment,
"test_combinations": test_combinations,
}
def test():
import doctest
doctest.testmod(verbose=1)
if __name__ == "__main__":
#performance()
test()