def guess_password(password_hash: int, num_chars: int,
                   hash_func: Callable[[str], int]) -> str:
    """Attempt to find the string whose hash matches the given hash via a genetic algorithm.

    :param password_hash: The hash for the original password
    :param num_chars: How many characters to put in the generated passwords
    :param hash_func: The function to be used for hashing; it should take in a string and output its hash
    :return: The optimal result produced by the genetic algorithm
    """
    def gen_passwords():
        """Generate a list of random passwords."""
        population = []
        for i in range(500):
            char_list = []
            for j in range(num_chars):
                char_list.append(get_pass_char())
            population.append(Password(char_list, password_hash, hash_func))
        return population

    # Uncomment the lines in main.py if you want to uncomment these
    #def time_out():
    #print("TIMEOUT ERROR: too much time has passed since beginning the reconstruction. This may have occurred "
    #"because you entered an improper password length.")
    #input("Press any key to exit.")
    #_thread.interrupt_main()

    #tmr = Timer(30.0, time_out)

    #tmr.start()
    best_pass = genetic_algorithm.run(gen_passwords,
                                      lambda best: best.get_fitness() < 1, 0.7,
                                      0.04, 0.6)
    #tmr.cancel()

    return str(best_pass)
Esempio n. 2
0
def run(parameters):
    global idx
    idx += 1
    try:
        res = ga.run(parameters)
    except:
        print('An error occuered with ', parameters)
        errored_params_pool.append(parameters)
        return {}
    print('run', idx, parameters, ' - ', res['run_time'])
    return res
Esempio n. 3
0
mutation_probability = 0.1
mutation_rate = 0.1

# algorithm settings
max_iter = 300
k = 10
stop_thrs = 10
mutation_level = 0.2

run_num = 3
run = 1

individual_size = 120

# In[3]:


parameters = [
    run, population_size, k,\
fitness_function,  crossover_type, crossover_probability, \
elitism_ratio, max_parent_allowance, \
mutation_probability, mutation_rate, max_iter, individual_size,\
stop_thrs, mutation_level \
]

# In[4]:

ga.run(parameters)

# In[ ]:
Esempio n. 4
0
from genetic_algorithm import run

if __name__ == "__main__":
    run()
Esempio n. 5
0

# Problem Definition
problem = structure()
problem.costfunc = sphere
problem.nvar = 5
problem.varmin = -10
problem.varmax = 10

# GA Parameters
params = structure()
params.maxit = 100
params.npop = 20
params.pc = 1
params.gamma = 0.1
params.mu = 0.2  # como temos 5 genes (nvar), sempre vamos alterar 1 na mutacao
params.sigma = 0.1

# Run GA
out = genetic_algorithm.run(problem, params)

# Results
# plt.plot(out.bestcost)
plt.semilogy(out.bestcost)
plt.xlim(0, params.maxit)
plt.xlabel('Iterations')
plt.ylabel('Best Cost')
plt.title('Genetic Algorithm (GA)')
plt.grid(True)
plt.show()