Beispiel #1
0
def solve_hill(problem, problem_name):
    start = timer()
    best_state, best_fitness, fitness_curve = random_hill_climb(problem,
                                                                curve=True,
                                                                restarts=10)
    end = timer()
    plot = plot_curve(problem_name + "-Random Climb", "Iterations", "Fitness",
                      range(fitness_curve.size), fitness_curve).show()
    time = end - start
    num_iters = fitness_curve.size
    return best_fitness, time, num_iters, plot
    print('sa algorithm @ {} iterations'.format(i))
    print(best_fitness)
    print(end - start)
    print(len(c))
    times_sa.append(end - start)
    fitns_sa.append(best_fitness)
# ________________________________________________________________________________________________________________________
# Solve problem using random hill climb
times_rhc = []
fitns_rhc = []
for i in iterations:
    start = time.time()
    best_state, best_fitness, c = mlrose.random_hill_climb(problem_fit,
                                                           max_attempts=10,
                                                           max_iters=i,
                                                           restarts=0,
                                                           init_state=None,
                                                           curve=True,
                                                           random_state=3)

    end = time.time()
    print('rhc algorithm @ {} iterations'.format(i))
    print(best_fitness)
    print(end - start)
    print(len(c))
    times_rhc.append(end - start)
    fitns_rhc.append(best_fitness)
# ________________________________________________________________________________________________________________________
# Solve using MIMIC
times_mim = []
fitns_mim = []