Ejemplo n.º 1
0
def solve_mimic(problem, problem_name):
    start = timer()
    best_state, best_fitness, fitness_curve = mimic(problem, curve=True)
    end = timer()
    plot = plot_curve(problem_name + "-MIMIC", "Iterations", "Fitness",
                      range(fitness_curve.shape[0]),
                      np.max(fitness_curve, axis=1)).show()
    time = end - start
    num_iters = fitness_curve.shape[0]
    return best_fitness, time, num_iters, plot
Ejemplo n.º 2
0
        fitness = mlrose.FlipFlop()
        istate = np.array(np.zeros(p), dtype=int)

        problem = mlrose.DiscreteOpt(length=p,
                                     fitness_fn=fitness,
                                     maximize=True,
                                     max_val=2)

        schedule = mlrose.ExpDecay(init_temp=0.5,
                                   exp_const=0.005,
                                   min_temp=0.001)

        best_state, best_fitness, fitness_curve = mlrose.mimic(
            problem,
            max_attempts=500,
            pop_size=500,
            keep_pct=0.3,
            max_iters=1000,
            curve=True)

        # Define alternative N-Queens fitness function for maximization problem
        # Initialize custom fitness function object
        # Define initial state

        print('The best state found is: ', best_state)
        print('The fitness at the best state is: ', best_fitness)
        print(f'fitness curve: {fitness_curve}')

        final_results_maxs.append(np.max(fitness_curve))
        final_results_it.append(np.argmax(fitness_curve))
    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 = []
for i in iterations:
    start = time.time()
    best_state, best_fitness, c = mlrose.mimic(problem_fit,
                                               pop_size=200,
                                               keep_pct=0.4,
                                               max_attempts=5,
                                               max_iters=i,
                                               curve=True,
                                               random_state=3,
                                               fast_mimic=True)

    end = time.time()
    print('MIMIC algorithm @ {} iterations'.format(i))
    print(best_fitness)
    print(end - start)
    print(len(c))
    times_mim.append(end - start)
    fitns_mim.append(best_fitness)
# ________________________________________________________________________________________________________________________

plot_fitness_curve(x_axis=iterations,
                   curve_rhc=fitns_rhc,