-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaster.py
More file actions
202 lines (183 loc) · 5.47 KB
/
master.py
File metadata and controls
202 lines (183 loc) · 5.47 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
'''
Created on 04/09/2012
@author: rafa
'''
import multiprocessing
import operator
from collections import Counter
from math import factorial
from functools import reduce
import Bot
import itertools
import time
import itertools
# Unique permutations algorithms http://stackoverflow.com/a/6571976/2399397
def cmp(a, b):
return (a > b) - (a < b)
def next_permutationS(l):
'''Changes a list to its next permutation, in place.
Returns true unless wrapped around so result is lexicographically smaller. '''
n = len(l)
#Step 1: Find tail
last = n-1 #tail is from `last` to end
while last>0:
if l[last-1] < l[last]: break
last -= 1
#Step 2: Increase the number just before tail
if last>0:
small = l[last-1]
big = n-1
while l[big] <= small: big -= 1
l[last-1], l[big] = l[big], small
#Step 3: Reverse tail
i = last
j = n-1
while i < j:
l[i], l[j] = l[j], l[i]
i += 1
j -= 1
return last>0
def next_permutationB(seq, pred=cmp):
"""
This function is taken from this blog post:
http://blog.bjrn.se/2008/04/lexicographic-permutations-using.html
Like C++ std::next_permutation() but implemented as
generator. Yields copies of seq."""
def reverse(seq, start, end):
# seq = seq[:start] + reversed(seq[start:end]) + \
# seq[end:]
end -= 1
if end <= start:
return
while True:
seq[start], seq[end] = seq[end], seq[start]
if start == end or start+1 == end:
return
start += 1
end -= 1
if not seq:
raise StopIteration
try:
seq[0]
except TypeError:
raise TypeError("seq must allow random access.")
first = 0
last = len(seq)
seq = seq[:]
# Yield input sequence as the STL version is often
# used inside do {} while.
yield seq
if last == 1:
raise StopIteration
while True:
next = last - 1
while True:
# Step 1.
next1 = next
next -= 1
if pred(seq[next], seq[next1]) < 0:
# Step 2.
mid = last - 1
while not (pred(seq[next], seq[mid]) < 0):
mid -= 1
seq[next], seq[mid] = seq[mid], seq[next]
# Step 3.
reverse(seq, next1, last)
# Change to yield references to get rid of
# (at worst) |seq|! copy operations.
yield seq[:]
break
if next == first:
raise StopIteration
raise StopIteration
def unique(iterable):
seen = set()
for x in iterable:
if x in seen: continue
seen.add(x)
yield x
def npermutations(word):
# How to calculate unique permutations of a word http://math.stackexchange.com/a/391835
letters = {}
for letter in word:
try:
letters[letter] += 1
except KeyError:
letters[letter] = 1
divisor = 1
for k, v in letters.items():
if v > 1:
divisor *= factorial(v)
return factorial(len(word)) / divisor
class Master(object):
'''
classdocs
'''
def __init__(self, base):
'''
Constructor
'''
self.cores = 8
self.base = base
self.intentos = set()
self.resultados = set()
self.dicc = open("diccionario_venezuela")
#self.dicc = open("alternativo")
self.diccL = set()
for i in range(0, 71937):
self.diccL.add(self.dicc.readline().strip())
self.lockM = multiprocessing.Lock()
self.botsS = multiprocessing.Semaphore(self.cores)
self.permutaciones = next_permutationB(list(self.base))
self.lenght = int(npermutations(base))
self.bots = list()
def crearBot(self, i, i2):
self.lockD.acquire()
bot = self.bots.pop()
self.lockD.release()
self.lockP.acquire()
bot.intento = self.permutaciones.pop()
self.lockP.release()
bot.buscar()
self.lockD.acquire()
self.bots.insert(0, bot)
self.lockD.release()
self.botsS.release()
def alternativo(self):
inicio = time.time()
print("iniciar")
hilo = []
for i in range(0, self.cores):
self.botsS.acquire()
for i in range(0, self.cores):
self.lockM.acquire()
print("Bot" + str(i) + ": Saliendo")
self.lockM.release()
ini = int((i * self.lenght) / self.cores)
fini = int((self.lenght / self.cores) * (i + 1))
#print(ini, fini, fini - ini, self.lenght)
b = Bot.Bot("bot" + str(i), self.diccL,
itertools.islice(self.permutaciones, ini, fini), self)
b.start()
self.lockM.acquire()
print("todos los bots despachados")
self.lockM.release()
for i in range(0, self.cores):
self.botsS.acquire()
self.lockM.acquire()
print("termino")
print (time.time() - inicio)
self.lockM.release()
self.botsS.release()
def quedan(self):
if self.permutaciones.__len__() > 1:
return True
def iniciar(self):
i = 1
inicio = time.time()
print("iniciar")
while (self.quedan()):
self.botsS.acquire()
i = i + 1
print("termino")
print((time.time() - inicio) * 1000)