-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalltest2.py
119 lines (86 loc) · 2.49 KB
/
alltest2.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
#!/usr/bin/python
import mcavacamisa
import random
import time
import itertools
import numpy
import multiprocessing
def perm_it(start,N=float("inf")):
it = start
i = 0
yield (it,0)
k = True
while k:
k=next_permutation(it)
i+=1
if(i>N): break
yield (it,i)
def nth(iterable, n, default=None):
"Returns the nth item or a default value"
return next(itertools.islice(iterable, n, None), default)
def next_permutation(a):
"""Generate the lexicographically next permutation inplace.
https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
Return false if there is no next permutation.
"""
# Find the largest index i such that a[i] < a[i + 1]. If no such
# index exists, the permutation is the last permutation
for i in reversed(range(len(a) - 1)):
if a[i] < a[i + 1]:
break # found
else: # no break: not found
return False # no next permutation
# Find the largest index j greater than i such that a[i] < a[j]
j = next(j for j in reversed(range(i + 1, len(a))) if a[i] < a[j])
# Swap the value of a[i] with that of a[j]
a[i], a[j] = a[j], a[i]
# Reverse sequence from a[i + 1] up to and including the final element a[n]
a[i + 1:] = reversed(a[i + 1:])
return True
def work(start,i,N):
mano = start
nomep = "Processo "+str(i)
print nomep, "- Starting from ",mano
mcavacamisa.play((mano[0:10],mano[10:20]),nomep,fast=False)
for m in perm_it(start,N):
i=m[1]
if (i%10000==0): print i
mano = m[0]
mcavacamisa.play((mano[0:10],mano[10:20]),nomep)
N=800000
procs = 4
mazzo = [0]*14
mazzo.extend([1,1,2,2,3,3])
#mani=[[0,0,0,0,0,0,3,0,1,0,0,1,0,0,3,0,2,0,2,0]]
mazzobig=[0]*19
mazzobig.append(1)
mazzobig.extend([0]*9)
mazzobig.extend([1,1,1,2,2,2,2,3,3,3,3])
#mazzobig = [0,2,0,0,0,1,0,0,0,3,0,0,0,3,0,0,0,2,0,3,0,0,2,0,0,3,0,0,1,0,2,1,0,0,0,0,1,0,0,0]
print mazzo
#mazzi = [list(nth(perm_it(mazzo),N*i)) for i in range(procs)]
start = time.time()
mazzi = []
i = 0
for m in perm_it(mazzo):
i=m[1]
if (i%N==0):
mano = list(m[0])
mazzi.append(mano)
print mazzi
end = time.time()
print(end - start)
start = time.time()
jobs = []
for i in range(0, procs):
out_list = list()
process = multiprocessing.Process(target=work, args=(mazzi[i],i,N))
jobs.append(process)
# Start the processes (i.e. calculate the random number lists)
for j in jobs:
j.start()
# Ensure all of the processes have finished
for j in jobs:
j.join()
end = time.time()
print(end - start)