-def monoalphabetic_break_hillclimbing(message, max_iterations=10000000,
- alphabet=None, fitness=Pletters):
- ciphertext = unaccent(message).lower()
- if not alphabet:
- alphabet = list(string.ascii_lowercase)
- random.shuffle(alphabet)
- alphabet = cat(alphabet)
- return monoalphabetic_break_hillclimbing_worker(ciphertext, alphabet,
- max_iterations, fitness)
-
-def monoalphabetic_break_hillclimbing_mp(message, workers=10,
- max_iterations = 10000000, alphabet=None, fitness=Pletters, chunksize=1):
+# def monoalphabetic_break_hillclimbing(message, max_iterations=10000000,
+# alphabet=None, fitness=Pletters):
+# ciphertext = unaccent(message).lower()
+# if not alphabet:
+# alphabet = list(string.ascii_lowercase)
+# random.shuffle(alphabet)
+# alphabet = cat(alphabet)
+# return monoalphabetic_break_hillclimbing_worker(ciphertext, alphabet,
+# max_iterations, fitness)
+
+# def monoalphabetic_break_hillclimbing_mp(message, workers=10,
+# max_iterations = 10000000, alphabet=None, fitness=Pletters, chunksize=1):
+# worker_args = []
+# ciphertext = unaccent(message).lower()
+# for i in range(workers):
+# if alphabet:
+# this_alphabet = alphabet
+# else:
+# this_alphabet = list(string.ascii_lowercase)
+# random.shuffle(this_alphabet)
+# this_alphabet = cat(this_alphabet)
+# worker_args.append((ciphertext, this_alphabet, max_iterations, fitness))
+# with Pool() as pool:
+# breaks = pool.starmap(monoalphabetic_break_hillclimbing_worker,
+# worker_args, chunksize)
+# return max(breaks, key=lambda k: k[1])
+
+# def monoalphabetic_break_hillclimbing_worker(message, alphabet,
+# max_iterations, fitness):
+# def swap(letters, i, j):
+# if i > j:
+# i, j = j, i
+# if i == j:
+# return letters
+# else:
+# return (letters[:i] + letters[j] + letters[i+1:j] + letters[i] +
+# letters[j+1:])
+# best_alphabet = alphabet
+# best_fitness = float('-inf')
+# for i in range(max_iterations):
+# alphabet = swap(best_alphabet, random.randrange(26), random.randrange(26))
+# cipher_translation = ''.maketrans(string.ascii_lowercase, alphabet)
+# plaintext = message.translate(cipher_translation)
+# if fitness(plaintext) > best_fitness:
+# best_fitness = fitness(plaintext)
+# best_alphabet = alphabet
+# print(i, best_alphabet, best_fitness, plaintext[:50])
+# return best_alphabet, best_fitness
+
+
+def monoalphabetic_break_hillclimbing(message,
+ max_iterations=20000,
+ plain_alphabet=None,
+ cipher_alphabet=None,
+ fitness=Pletters, chunksize=1):
+ return simulated_annealing_break(message,
+ workers=1,
+ initial_temperature=0,
+ max_iterations=max_iterations,
+ plain_alphabet=plain_alphabet,
+ cipher_alphabet=cipher_alphabet,
+ fitness=fitness, chunksize=chunksize)
+
+
+def monoalphabetic_break_hillclimbing_mp(message,
+ workers=10,
+ max_iterations=20000,
+ plain_alphabet=None,
+ cipher_alphabet=None,
+ fitness=Pletters, chunksize=1):
+ return simulated_annealing_break(message,
+ workers=workers,
+ initial_temperature=0,
+ max_iterations=max_iterations,
+ plain_alphabet=plain_alphabet,
+ cipher_alphabet=cipher_alphabet,
+ fitness=fitness, chunksize=chunksize)
+
+
+def simulated_annealing_break(message, workers=10,
+ initial_temperature=200,
+ max_iterations=20000,
+ plain_alphabet=None,
+ cipher_alphabet=None,
+ fitness=Pletters, chunksize=1):