Esempio n. 1
0
def plot_mimic_graph(prob_type, flag=0):
    try:
        os.mkdir("./MIMIC")
    except FileExistsError:
        pass
    except OSError as error:
        print("Error creating directory results.")

    print('\n plot_mimic_graph - Progress: ', end="")

    fitness, problem = get_fitness_function(prob_type)
    process_time = []
    fitness_score = []
    if flag == 0:
        population = [100, 200, 300, 500, 700, 1000, 1500]
    else:
        population = [0.1, 0.15, 0.175, 0.2, 0.25, 0.3, 0.5]
    for i in population:
        print(".", end="")
        start = time.time()
        if flag == 0:
            best_state, best_fitness, fitness_curve = mlrose.mimic(
                problem,
                pop_size=i,
                keep_pct=__KEEP_PCT,
                max_iters=__MAX_ITERS,
                max_attempts=__MAX_ATTEMPTS[prob_type],
                random_state=10)
        else:
            best_state, best_fitness, fitness_curve = mlrose.mimic(
                problem,
                pop_size=__POP_SIZE[prob_type],
                keep_pct=i,
                max_iters=__MAX_ITERS,
                max_attempts=__MAX_ATTEMPTS[prob_type],
                random_state=10)
        fitness_score.append(best_fitness)
        process_time.append(time.time() - start)

    plt.figure(200 + flag)
    plt.plot(fitness_score)
    plt.xticks(np.arange(len(population)), [str(p) for p in population])
    plt.xlabel("pop_size constant" if flag == 0 else "keep_pct constant")
    plt.ylabel("fitness score")
    plt.title('MIMIC')
    plt.savefig("MIMIC/" + str(flag) + "Fitness.png")

    plt.figure(210 + flag)
    plt.plot(process_time)
    plt.xticks(np.arange(len(population)), [str(p) for p in population])
    plt.xlabel("pop_size constant" if flag == 0 else "keep_pct constant")
    plt.ylabel("time")
    plt.title('MIMIC')
    plt.savefig("MIMIC/" + str(flag) + "Time.png")
def call_mlrose_curve(algorith_keyword, problem, pop_size=200, mutation_prob=0.1, max_attempts=10, max_iters=np.inf, curve=False\
, random_state=None, schedule=mlrose.GeomDecay(), init_state=None, restarts=0, keep_pct=0.2, fast_mimic=True):
    if algorith_keyword == 'RHC':
        best_state, best_fitness, curve_output = mlrose.random_hill_climb(problem, max_attempts=max_attempts, max_iters=max_iters\
        , restarts=restarts, init_state=init_state, curve=curve, random_state=random_state)
    elif algorith_keyword == 'GA':
        best_state, best_fitness, curve_output = mlrose.genetic_alg(problem, pop_size=pop_size, mutation_prob=mutation_prob\
        , max_attempts=max_attempts, max_iters=max_iters, curve=curve, random_state=random_state)
    elif algorith_keyword == 'SA':
        best_state, best_fitness, curve_output = mlrose.simulated_annealing(problem, schedule=schedule, max_attempts=max_attempts\
        ,max_iters=max_iters, init_state=init_state, curve=curve, random_state=random_state)
    elif algorith_keyword == 'MIMIC':
        print("problem: ", problem, "\npop_size: ", pop_size, "\n",
              "keep_pct: ", keep_pct)
        print("max_attempts: ", max_attempts, "\nmax_iters: ", max_iters,
              "\nrandom_state: ", random_state, "\nfast_mimic: ", fast_mimic)
        best_state, best_fitness, curve_output = mlrose.mimic(problem, pop_size=pop_size, keep_pct=keep_pct\
        , max_attempts=max_attempts, max_iters=max_iters, curve=curve, random_state=random_state)
        print("best_fitness: ", best_fitness)
    else:
        print(
            "\n\nIncorrect 'algorithm_keyword'. Please check the input to the 'call_mlrose' function.\n\n"
        )
        best_state, best_fitness, curve_output = 'incorrect key word', 'incorrect key word', 'incorrect key word'
    return best_state, best_fitness, curve_output
Esempio n. 3
0
def n_queens_mimic(nq_problem, max_iters=np.inf, num_runs=20, verbose=False):
    # HP to vary
    hp_name = 'keep_pct'
    hp_values = [0.2, 0.4, 0.6]

    # other hyperparameters for genetic algorithm
    population_size = 200

    # run for each hp value and append results to list

    fitness_dfs = []
    runs = np.arange(num_runs)

    for hp_value in hp_values:
        keep_pct = hp_value  # set varied HP at beginning of loop

        run_times = np.zeros(num_runs)
        fitness_data = pd.DataFrame()

        for run in runs:
            run_t0 = time()
            best_state, best_fitness, fitness_curve = mlrose.mimic(
                problem=nq_problem,
                pop_size=population_size,
                keep_pct=keep_pct,
                max_attempts=10,
                max_iters=max_iters,
                curve=True,
            )
            run_time = time() - run_t0
            run_times[run] = run_time

            fitness_data = pd.concat(
                [fitness_data, pd.DataFrame(fitness_curve)],
                axis=1,
                sort=False)

        fitness_data.columns = runs
        fitness_data = fitness_data.fillna(method='ffill')
        fitness_dfs.append(fitness_data)

        # calculate and print avg time per run
        avg_run_time = np.average(run_times)
        print("N-Queens - MIMIC avg run time,", hp_value, hp_name, ":",
              avg_run_time)

    # generate plots
    plot_title = "N-Queens MIMIC - " \
        + str(population_size) + " pop, " \
        + ": fit vs iter"
    plotting.plot_fitness_curves(
        fitness_dfs=fitness_dfs,
        hp_values=hp_values,
        hp_name=hp_name,
        title=plot_title,
    )
    plt.savefig('graphs/n_queens_mimic_fitness.png')
    plt.clf()

    return fitness_dfs
Esempio n. 4
0
def compare_methods(num_char):
    global count
    count=0
    fitness_obj=mlrose.CustomFitness(heter_string_fn)
    opt=mlrose.DiscreteOpt(num_char,fitness_obj,maximize=True,max_val=num_char)
    best_state_climb,best_fitness_climb,fitness_curve_climb=mlrose.random_hill_climb(opt,curve=True)
    print('---------------------random hill climb-------------------------')
    print('hill climbing best state for heter-string problem:',best_state_climb)
    print('hill climbing best fitness for heter-string problem:',best_fitness_climb)
    print('hill climbing fitting curve for heter-string problem:',fitness_curve_climb)
    print('number of fitness call used:',count)
    count=0
    print('-------------------simulated annealing-------------------------')
    best_state_ann,best_fitness_ann,fitness_curve_ann=mlrose.simulated_annealing(opt,schedule=mlrose.ExpDecay(),curve=True)
    print('simulated annealing best state for heter-string problem:',best_state_ann)
    print('simulated annealing best fitness for heter-string problem:',best_fitness_ann)
    print('simulated annealing fitting curve for heter-string problem:',fitness_curve_ann)
    print('number of fitness call used:',count)
    count=0
    best_state_ga,best_fitness_ga,fitness_curve_ga=mlrose.genetic_alg(opt,pop_size=200, mutation_prob=0.5,curve=True)
    print('---------------------genetic alg----------------------------')
    print('genetic algorithm best state for heter-string problem:',best_state_ga)
    print('genetic algorithm best fitnees for heter-string problem:',best_fitness_ga)
    print('genetic algorithm fitness curve for heter-string problem:',fitness_curve_ga)
    print('number of fitness call used:',count)
    count=0
    best_state_mimic,best_fitness_mimic,fitness_curve_mimic=mlrose.mimic(opt,pop_size=200,curve=True)
    print('------------------------mimic-------------------------------')
    print('mimic best state for heter-string problem:',best_state_mimic)
    print('mimic best fitness value for heter-string problem:',best_fitness_mimic)
    print('mimic curve for heter-string problem:',fitness_curve_mimic)
    print('number of fitness call used:',count)
    count=0
    plt.figure(figsize=(10,10))
    plt.subplot(221)
    plt.plot(fitness_curve_climb)
    plt.ylabel('fitness')
    plt.xlabel('num_iter')
    plt.ylim(20,50)
    plt.title('random hill climb')
    plt.subplot(222)
    plt.plot(fitness_curve_ann)
    plt.ylabel('fitness')
    plt.xlabel('num_iter')
    plt.ylim(20,50)
    plt.title('simulated annealing')
    plt.subplot(223)
    plt.plot(fitness_curve_ga)
    plt.ylim(20,50)
    plt.ylabel('fitness')
    plt.xlabel('num_iter')
    plt.title('genetic algorithm')
    plt.subplot(224)
    plt.plot(fitness_curve_mimic)
    plt.ylim(20,50)
    plt.title('mimic')
    plt.ylabel('fitness')
    plt.xlabel('num_iter')
    plt.show()
Esempio n. 5
0
def mimic(problem, init_state, max_attempts, max_iters):
    best_state, best_fitness, fitness_curve  = mlrose_hiive.mimic(problem, pop_size=200, keep_pct=0.2, 
                                                          max_attempts = max_attempts, max_iters = max_iters, curve=True, random_state = 1)
    print('mimic')
    print(best_state)
    print(best_fitness)
#     print(fitness_curve)
    return best_state, best_fitness, fitness_curve
def mimic(problem_fit, vectorLength, data_dir, iterlist):
    directory = "./" + data_dir + "/curves/"
    if not os.path.exists(directory):
        os.makedirs(directory)
    path1 = './' + data_dir
    path2 = "./" + data_dir + "/curves/"
    kpl = []
    beststate = []
    bestfit = []
    curve = []
    time = []
    iterlistn = []

    for kp in np.linspace(0.1, .5, 3):
        for iters in iterlist:

            iterlistn.append(int(iters))
            start = clock()
            best_state, best_fitness, train_curve = mlrose.mimic(problem_fit, \
                                                                keep_pct=kp,\
                                                                max_attempts=5,\
                                                                max_iters=int(iters), curve=True, random_state=randomSeed)
            end = clock()

            time.append(end - start)
            beststate.append(best_state)
            bestfit.append(best_fitness)
            kpl.append(kp)
            curve.append(train_curve)

            if (verbose == True):
                print(int(iters))
                print(kp)
                print(best_state)
                print(best_fitness)

    ffmi = pd.DataFrame({
        'Keep percentage': kpl,
        'Best Fitness': bestfit,
        'Iterations': iterlistn,
        'Time': time
    })
    mibeststatedf = pd.DataFrame(0.0,
                                 index=range(1, vectorLength + 1),
                                 columns=range(1,
                                               len(beststate) + 1))

    for i in range(1, len(beststate) + 1):
        mibeststatedf.loc[:, i] = beststate[i - 1]

    ffmi.to_csv(os.path.join(path1, 'mi.csv'))
    mibeststatedf.to_csv(os.path.join(path1, 'mistates.csv'))

    for i in range(len(curve)):
        micurvedf = pd.DataFrame(curve[i])
        micurvedf.to_csv(
            os.path.join(path2,
                         'micurve{}_{}.csv'.format(kpl[i], iterlistn[i])))
Esempio n. 7
0
def mimic_optimization(size):
    algo = 'mimic'
    problem = get_problem(size)
    problem.set_mimic_fast_mode(True)

    # Gridsearch params
    max_iters = 500
    pop_size_values = [50, 100, 150, 200]
    keep_pct_values = [0.2, 0.3]
    max_attempts_values = [10, 50, 100, 200]
    n_runs = len(max_attempts_values) * len(pop_size_values) * len(keep_pct_values)

    # Best vals
    pop_size, keep_pct,  max_attempts = None, None, None
    fitness, curves, n_invocations, time = float('-inf'), [], 0, 0

    # Gridsearch
    global eval_count
    run_counter = 0
    for run_pop_size in pop_size_values:
        for run_keep_pct in keep_pct_values:
            for run_max_attempts in max_attempts_values:
                # Print status
                run_counter += 1
                print(f'RUN {run_counter} of {n_runs} [pop_size: {run_pop_size}] [run_keep_pct: {run_keep_pct}] [max_attempts: {run_max_attempts}]')

                # Run problem
                eval_count = 0
                start = timer()
                run_state, run_fitness, run_curves = mlrose_hiive.mimic(problem,
                                                                        pop_size=run_pop_size,
                                                                        keep_pct=run_keep_pct,
                                                                        max_attempts=run_max_attempts,
                                                                        max_iters=max_iters,
                                                                        random_state=42,
                                                                        curve=True)
                end = timer()

                # Save curves and params
                if run_fitness > fitness:
                    pop_size = run_pop_size
                    keep_pct = run_keep_pct
                    max_attempts = run_max_attempts
                    fitness = run_fitness
                    curves = run_curves
                    n_invocations = eval_count
                    time = end - start

    df = pandas.DataFrame(curves, columns=['fitness'])
    df['pop_size'] = pop_size
    df['keep_pct'] = keep_pct
    df['max_attempts'] = max_attempts
    df['max_iters'] = max_iters
    df['n_invocations'] = n_invocations
    df['time'] = time
    df.to_csv(f'{STATS_FOLDER}/{algo}_{size}_stats.csv', index=False)

    print(f'{algo}_{size} run.')
Esempio n. 8
0
    def test_mimic_discrete_min():
        """Test mimic function for a discrete minimization problem"""

        problem = DiscreteOpt(5, OneMax(), maximize=False)
        best_state, best_fitness, _ = mimic(problem, max_attempts=50)

        x = np.array([0, 0, 0, 0, 0])

        assert (np.array_equal(best_state, x) and best_fitness == 0)
Esempio n. 9
0
def mimic(problem, iterations, random_seed, graph_file, graph_title):
    keep_pct = [0.1, 0.25, 0.50]
    best_score = []
    time_taken = []
    fn_evals_taken = []
    global eval_count
    for k in keep_pct:
        fitness = []
        fit_time = []
        fn_evals = []
        for i in iterations:
            eval_count = 0
            start = datetime.datetime.now()
            best_state, best_fitness, _ = mlrose_hiive.mimic(problem, keep_pct=k,
                                                            max_iters=i, random_state=random_seed)
            finish = datetime.datetime.now()
            fitness.append(best_fitness)
            fit_time.append((finish - start).total_seconds())
            fn_evals.append(eval_count)
        # Find the best score achieved in that mutation prob
        best_score.append(max(fitness))
        index = fitness.index(max(fitness))
        # find the time that was taken to achieve that
        time_taken.append(fit_time[index])
        fn_evals_taken.append(fn_evals[index])
        plt.plot(iterations, fitness, label="keep_pct = " + str(k))

    plt.legend(loc="best", title='Proportion of samples kept')
    plt.grid()
    generate_graph(graph_file + "mimic", graph_title + "MIMIC: ", "Iterations", "Fitness")

    # Decays best_score and time_taken
    plt.plot(keep_pct, best_score)
    plt.grid()
    generate_graph(graph_file + "mimic_pct", graph_title + "MIMIC",
                   "Proportion of samples kept", "Best Score Achieved")

    """
    plt.plot(mutation_prob, time_taken)
    plt.grid()
    generate_graph("cp_sa_decay_time", "Continuous Peaks - Genetic Algorithm", "Mutation Probability",
                   "Time taken to achieve that")
    """

    plt.scatter(time_taken, best_score)
    for i, txt in enumerate(keep_pct):
        plt.annotate(s=str(txt), xy=(time_taken[i], best_score[i]))
    plt.legend(loc='best', title='Proportion of samples kept')
    plt.grid()
    generate_graph(graph_file + "mimic_scatter", graph_title + "MIMIC",
                   "Time Taken", "Best Score achieved")

    print('Proportion of samples kept: ', keep_pct)
    print('Best scores reached: ', best_score)
    print('Time taken to do that: ', time_taken)
    print('Function evaluations taken: ', fn_evals_taken)
Esempio n. 10
0
def compare_multi_round_k_color():
    global count
    count = 0
    fitness_obj = mlrose.CustomFitness(k_color_fit)
    opt = mlrose.DiscreteOpt(50, fitness_obj, maximize=True, max_val=8)
    fitness_list_rhc = []
    fitness_list_ann = []
    fitness_list_genetic = []
    fitness_list_mimic = []
    num_sample_rhc = []
    num_sample_ann = []
    num_sample_genetic = []
    num_sample_mimic = []
    for i in range(20):
        best_state_climb, best_fitness_climb, fitness_curve_climb = mlrose.random_hill_climb(
            opt, curve=True)
        fitness_list_rhc.append(best_fitness_climb)
        num_sample_rhc.append(count)
        count = 0
        best_state_ann, best_fitness_ann, fitness_curve_ann = mlrose.simulated_annealing(
            opt, schedule=mlrose.ExpDecay(), curve=True)
        fitness_list_ann.append(best_fitness_ann)
        num_sample_ann.append(count)
        count = 0
        best_state_ga, best_fitness_ga, fitness_curve_ga = mlrose.genetic_alg(
            opt, pop_size=500, mutation_prob=0.5, curve=True)
        fitness_list_genetic.append(best_fitness_ga)
        num_sample_genetic.append(count)
        count = 0
        best_state_mimic, best_fitness_mimic, fitness_curve_mimic = mlrose.mimic(
            opt, pop_size=500, curve=True)
        fitness_list_mimic.append(best_fitness_mimic)
        num_sample_mimic.append(count)
        count = 0
    plt.figure(figsize=(10, 6))
    plt.subplot(121)
    plt.plot(fitness_list_rhc, label='rhc')
    plt.plot(fitness_list_ann, label='ann')
    plt.plot(fitness_list_genetic, label='ga')
    plt.plot(fitness_list_mimic, label='mimic')
    plt.xlabel('rounds')
    plt.ylabel('finess value')
    plt.title('fitness value comparision')
    plt.legend(loc='lower right')
    plt.subplot(122)
    plt.plot(num_sample_rhc, label='rhc')
    plt.plot(num_sample_ann, label='ann')
    plt.plot(num_sample_genetic, label='ga')
    plt.plot(num_sample_mimic, label='mimic')
    plt.xlabel('rounds')
    plt.ylabel('fitness calls')
    plt.title('fitness call number comparision')
    plt.legend(loc='upper right')
    plt.show()
Esempio n. 11
0
def plot_optimization_problem_fitness(fitness_function, iterations, random_state, title):

    start_rhc = timeit.default_timer()
    rhc_best_state, rhc_best_fitness, rch_fitness_curve =  mlrose.random_hill_climb(fitness_function, max_iters=iterations, random_state= random_state, restarts=10, curve=True)
    rhc_elapsed = timeit.default_timer() - start_rhc

    start_sa = timeit.default_timer()
    sa_best_state, sa_best_fitness, sa_fitness_curve =  mlrose.simulated_annealing(fitness_function, max_iters=iterations, random_state= random_state, curve=True)
    sa_elapsed = timeit.default_timer() - start_sa

    start_ga = timeit.default_timer()
    ga_best_state, ga_best_fitness, ga_fitness_curve =  mlrose.genetic_alg(fitness_function, max_iters=iterations, random_state= random_state, curve=True)
    ga_elapsed = timeit.default_timer() - start_ga

    start_mimic = timeit.default_timer()
    mimic_best_state, mimic_best_fitness, mimic_fitness_curve =  mlrose.mimic(fitness_function, max_iters=iterations, random_state= random_state, curve=True)
    mimic_elapsed = timeit.default_timer() - start_mimic

    # Fill in arrays.
    rch_fitness_curve_bf = np.full(iterations, rhc_best_fitness)
    rch_fitness_curve_bf[:rch_fitness_curve.shape[0]] = rch_fitness_curve

    sa_fitness_curve_bf = np.full(iterations, sa_best_fitness)
    sa_fitness_curve_bf[:sa_fitness_curve.shape[0]] = sa_fitness_curve

    ga_fitness_curve_bf = np.full(iterations, ga_best_fitness)
    ga_fitness_curve_bf[:ga_fitness_curve.shape[0]] = ga_fitness_curve

    mimic_fitness_curve_bf = np.full(iterations, mimic_best_fitness)
    mimic_fitness_curve_bf[:mimic_fitness_curve.shape[0]] = mimic_fitness_curve

    # Plot the convergance times.
    plot_ro_algo_times(rhc_elapsed, ga_elapsed, sa_elapsed, mimic_elapsed, title)

    # Plot the fitness over iterations.
    fig = plt.figure(figsize=(8,6))

    plt.plot(rch_fitness_curve_bf, label="RHC")
    plt.plot(sa_fitness_curve_bf, label="SA")
    plt.plot(ga_fitness_curve_bf, label="GA")
    plt.plot(mimic_fitness_curve_bf, label="MIMIC")

    plt.xlabel("Number of Iterations")
    plt.xticks(np.arange(0.0, iterations, step=iterations / 10))

    plt.ylabel("Fitness Function")

    plt.title(title)
    plt.legend(prop={'size':13}, loc='lower right')

    plt.savefig('Charts/OptimizationProblems/' + title + '.png')
    plt.clf()
Esempio n. 12
0
def get_mimic(problem, keep_pct=0.25):

    best_state, best_fitness, fitness_curve = mlrose.mimic(
        problem,
        pop_size=200,
        keep_pct=keep_pct,
        max_attempts=10,
        max_iters=np.inf,
        curve=True,
        random_state=23,
    )

    return best_state, best_fitness, fitness_curve
Esempio n. 13
0
def mimic(problem, init_state, max_attempts, max_iters):
    start_time = time.time()
    best_state, best_fitness, fitness_curve = mlr.mimic(
        problem,
        pop_size=200,
        keep_pct=0.2,
        max_attempts=max_attempts,
        max_iters=max_iters,
        curve=True,
        random_state=RANDOM_STATE)
    end_time = time.time()
    total_time = end_time - start_time

    print('MIMIC')
    print("Elapsed Time", total_time)
    #print(best_state)
    print(best_fitness)
    #     print(fitness_curve)
    return best_state, best_fitness, fitness_curve, total_time
Esempio n. 14
0
def run_MIMIC_2(problem, init_state, **kwargs):
    fit_vals = []
    fit_curves = []
    times = []
    fevals = []

    # run multiple times to get average
    for random_state in random_states:
        start = time.time()

        _, best_fit, fit_curve, evals = mimic(problem,
                                              random_state=random_state,
                                              pop_size=300,
                                              keep_pct=0.2,
                                              **kwargs,
                                              fevals=True,
                                              curve=True)

        fit_vals.append(best_fit)
        fit_curves.append(fit_curve)
        times.append(time.time() - start)
        fevals.append(sum(evals.values()))

    # plot average fitness value
    # now = datetime.now()
    # dt_string = now.strftime("%Y-%m-%d-%H-%M-%S")
    # hack for ease of naming
    problem_name = str(problem.fitness_fn).split('.')[-1].split(' ')[0]
    # chart_name = f"charts/mimic_{problem_name}_{len(init_state)}_{dt_string}"

    # plt.plot(average_curves(fit_curves), label="mimic")
    # plt.title(f"MIMIC {problem_name} ({len(init_state)})")
    # plt.xlabel("step")
    # plt.ylabel("fitness")
    # plt.savefig(chart_name)
    # plt.show()

    avg_fit = np.average(fit_vals)
    avg_time = round(np.mean(times), 2)
    avg_evals = np.mean(fevals)
    print(f"MIMIC {problem_name}: {avg_fit}: {avg_time}: {avg_evals}")
    return avg_fit, avg_time, avg_evals
Esempio n. 15
0
def plot_MIMICpct(problem, problem_fit, max_attempts, mimpop, maxIter, seed, min=False):
    PCT = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
    plt.figure()
    for p in PCT:
        print(p)
        best_state, best_fitness, mimicfitness_curve = mlrose.mimic(problem_fit, keep_pct=p, curve=True,
                                                                    pop_size=mimpop,
                                                                    max_attempts=max_attempts,
                                                                    max_iters=maxIter,
                                                                    random_state=seed)
        print(p, best_fitness)
        if min:
            mimicfitness_curve = np.array(mimicfitness_curve) * -1
        plt.plot(mimicfitness_curve, label='pct = ' + str(p))

    plt.title(problem + " - MIMIC - Keep Percent")
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("Images\\" + problem + " - MIMIC - Keep Percent")
    plt.show()
Esempio n. 16
0
def plot_MIMpop(problem, problem_fit, max_attempts, mimpct, maxIter, seed, min=False):
    plt.figure()
    pop = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
    for p in pop:
        print(p)
        best_state, best_fitness, mimicfitness_curve = mlrose.mimic(problem_fit,
                                                                    pop_size=p,
                                                                    keep_pct=mimpct, curve=True,
                                                                    max_attempts=max_attempts,
                                                                    max_iters=maxIter,
                                                                    random_state=seed)
        print(p, best_fitness)
        if min:
            mimicfitness_curve = np.array(mimicfitness_curve) * -1
        plt.plot(mimicfitness_curve, label='pop size = ' + str(p))

    plt.title(problem + " - MIMIC - Population Size")
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("Images\\" + problem + " - MIMIC - Population Size")
    plt.show()
Esempio n. 17
0
def compare_gen_mimic():
    fitness_obj = mlrose.CustomFitness(n_peak_fit)
    opt = mlrose.DiscreteOpt(1, fitness_obj, maximize=True, max_val=10000)
    global count
    count = 0
    iter_num_counter_ga = []
    fitness_list_ga = []
    iter_num_counter_mimic = []
    fitness_list_mimic = []
    for i in range(20):
        best_state_ga, best_fitness_ga, fitness_curve_ga = mlrose.genetic_alg(
            opt, pop_size=20, mutation_prob=0.5, curve=True)
        iter_num_counter_ga.append(count)
        fitness_list_ga.append(best_fitness_ga)
        count = 0
        best_state_mimic, best_fitness_mimic, fitness_curve_mimic = mlrose.mimic(
            opt, pop_size=20, curve=True)
        iter_num_counter_mimic.append(count)
        fitness_list_mimic.append(best_fitness_mimic)
        count = 0
    plt.figure(figsize=(8, 6))
    plt.subplot(121)
    plt.plot(fitness_list_ga, label='ga')
    plt.plot(fitness_list_mimic, label='mimic')
    plt.xlabel('rounds')
    plt.ylabel('finess value')
    plt.title('fitness value comparision')
    plt.legend(loc='lower right')
    plt.subplot(122)
    plt.plot(iter_num_counter_ga, label='ga')
    plt.plot(iter_num_counter_mimic, label='mimic')
    plt.xlabel('rounds')
    plt.ylabel('fitness call no.')
    plt.title('fitness call number comparision')
    plt.legend(loc='upper right')
    plt.show()
Esempio n. 18
0
        pop_size=200,
        max_attempts=attempts,
        random_state=seed,
        curve=True,
        elite_dreg_ratio=0.5,
        mutation_prob=0.4)
    print(f"        best GA State: {best_ga_state}")
    sub_end = time.time()
    ga_fitnesses.append(best_ga_fitness)
    ga_times.append(sub_end - sub_start)

    ## MIMIC
    sub_start = time.time()
    best_m_state, best_m_fitness, m_curve = mlr.mimic(problem,
                                                      pop_size=200,
                                                      max_attempts=attempts,
                                                      random_state=seed,
                                                      curve=True)
    print(f"        best MIMIC State: {best_m_state}")
    sub_end = time.time()
    m_fitnesses.append(best_m_fitness)
    m_times.append(sub_end - sub_start)

    end = time.time()
    part2_time += end - start
    print(f"        Iteration Time Elapsed: {end-start}\n")

print(f"\n\nPart 2 Time Elapsed: {part2_time}\n")

print(f"######### PART 1 #########\n")
Esempio n. 19
0
def get_random_optimisation(problem,
                            optimisation,
                            problem_type,
                            seed=10,
                            state_fitness_callback=None):
    best_state, best_fitness, fitness_curve = None, None, None
    process_time = []

    if optimisation == Optimisation.RHC:
        start = time.time()
        best_state, best_fitness, fitness_curve = mlrose.random_hill_climb(
            problem,
            max_attempts=__MAX_ATTEMPTS[problem_type],
            max_iters=__MAX_ITERS,
            curve=True,
            random_state=seed,
            state_fitness_callback=state_fitness_callback,
            callback_user_info=['rhc'])
        process_time.append(time.time() - start)

    elif optimisation == Optimisation.SA:
        start = time.time()
        best_state, best_fitness, fitness_curve = mlrose.simulated_annealing(
            problem,
            schedule=mlrose.ExpDecay(exp_const=0.1),
            max_attempts=__MAX_ATTEMPTS[problem_type],
            max_iters=__MAX_ITERS,
            curve=True,
            random_state=seed,
            state_fitness_callback=state_fitness_callback,
            callback_user_info=['sa'])
        process_time.append(time.time() - start)

    elif optimisation == Optimisation.GA:
        start = time.time()
        best_state, best_fitness, fitness_curve = mlrose.genetic_alg(
            problem,
            pop_size=__POP_SIZE[problem_type],
            mutation_prob=__MUTATION_PROBABILITY,
            max_attempts=__MAX_ATTEMPTS[problem_type],
            max_iters=__MAX_ITERS,
            curve=True,
            random_state=seed,
            state_fitness_callback=state_fitness_callback,
            callback_user_info=['ga'])
        process_time.append(time.time() - start)

    elif optimisation == Optimisation.MIMIC:
        start = time.time()
        best_state, best_fitness, fitness_curve = mlrose.mimic(
            problem,
            pop_size=__POP_SIZE[problem_type],
            keep_pct=__KEEP_PCT,
            max_iters=__MAX_ITERS,
            max_attempts=__MAX_ATTEMPTS[problem_type],
            curve=True,
            random_state=seed,
            state_fitness_callback=state_fitness_callback,
            callback_user_info=['mimic'])
        process_time.append(time.time() - start)

    return best_state, best_fitness, np.array(fitness_curve), process_time
Esempio n. 20
0
    def run_complexity(self, fitness_fn, mode=None):
        if mode == 1:
            self.run_ga_hyper_params(fitness_fn)
        elif mode == 2:
            self.run_rhc_hyper_params(fitness_fn)
        elif mode == 3:
            self.run_sa_hyper_params(fitness_fn)
        elif mode == 4:
            self.run_mimic_hyper_params(fitness_fn)
        elif not mode:
            fitness_name = fitness_fn.__class__.__name__
            print("Running %s" % fitness_name)
            init_states = {}
            knap_fitnesses = {}
            tsp_fitnesses = {}
            tries = 1
            for x in 2**np.arange(3, 9):
                n = int(x)
                fitness_dists = mlrose.TravellingSales(distances=get_coords(n))
                tsp_fitnesses[n] = fitness_dists
                edges = []
                for x in range(int(n * 0.75)):
                    a = r.randint(0, n - 1)
                    b = r.randint(0, n - 1)
                    while b == a:
                        b = r.randint(0, n - 1)
                    edges.append((a, b))

                fitness_fn_knap = mlrose.MaxKColor(edges=edges)
                init_states[n] = []
                knap_fitnesses[n] = fitness_fn_knap
                for y in range(tries):
                    init_states[n].append(get_init_state(n))

            for n, init_states_list in init_states.items():
                if fitness_name == 'MaxKColor':
                    fitness_fn = knap_fitnesses[n]
                if fitness_name == 'TravellingSales':
                    fitness_fn = tsp_fitnesses[n]
                print(n)
                print('%s: i=%d' % ('random_hill_climb', n))
                total_score = 0
                total_iter = 0
                start = time.time()
                for init_state in init_states_list:
                    problem = mlrose.DiscreteOpt(length=len(init_state),
                                                 fitness_fn=fitness_fn,
                                                 maximize=True)
                    if fitness_name == 'TravellingSales':
                        problem = mlrose.TSPOpt(length=n,
                                                fitness_fn=fitness_fn)
                    max_attempts = self.get_best_param(
                        problem=fitness_name,
                        algo='random_hill_climb',
                        param='max_attempts')
                    restarts = self.get_best_param(problem=fitness_name,
                                                   algo='random_hill_climb',
                                                   param='restarts')
                    best_state, best_fitness, curve = mlrose.random_hill_climb(
                        problem,
                        max_attempts=max_attempts,
                        max_iters=10000,
                        random_state=1,
                        curve=True,
                        restarts=restarts)
                    total_iter += len(curve)
                    total_score += np.mean(curve)
                end = time.time()
                print('The fitness at the best state is: ',
                      total_score / tries)
                self.track(problem=fitness_name,
                           algo='random_hill_climb',
                           i=n,
                           score=total_score / tries,
                           training_time=(end - start) / tries,
                           max_iter=total_iter / tries)

            for n, init_states_list in init_states.items():
                if fitness_name == 'MaxKColor':
                    fitness_fn = knap_fitnesses[n]
                if fitness_name == 'TravellingSales':
                    fitness_fn = tsp_fitnesses[n]
                print(n)
                print('%s: i=%d' % ('simulated_annealing', n))
                total_score = 0
                total_iter = 0
                start = time.time()
                for init_state in init_states_list:
                    problem = mlrose.DiscreteOpt(length=len(init_state),
                                                 fitness_fn=fitness_fn,
                                                 maximize=True)
                    if fitness_name == 'TravellingSales':
                        problem = mlrose.TSPOpt(length=n,
                                                fitness_fn=fitness_fn)
                    max_attempts = self.get_best_param(
                        problem=fitness_name,
                        algo='simulated_annealing',
                        param='max_attempts')
                    best_state, best_fitness, curve = mlrose.simulated_annealing(
                        problem,
                        max_attempts=max_attempts,
                        max_iters=10000,
                        random_state=1,
                        curve=True)
                    total_score += np.mean(curve)
                    total_iter += len(curve)
                end = time.time()
                print('The fitness at the best state is: ',
                      total_score / tries)
                self.track(problem=fitness_name,
                           algo='simulated_annealing',
                           i=n,
                           score=total_score / tries,
                           training_time=(end - start) / tries,
                           max_iter=total_iter / tries)

            for n, init_states_list in init_states.items():
                if fitness_name == 'MaxKColor':
                    fitness_fn = knap_fitnesses[n]
                if fitness_name == 'TravellingSales':
                    fitness_fn = tsp_fitnesses[n]
                print(n)
                print('%s: i=%d' % ('genetic_alg', n))
                total_score = 0
                total_iter = 0
                start = time.time()
                for init_state in init_states_list:
                    problem = mlrose.DiscreteOpt(length=len(init_state),
                                                 fitness_fn=fitness_fn,
                                                 maximize=True)
                    if fitness_name == 'TravellingSales':
                        problem = mlrose.TSPOpt(length=n,
                                                fitness_fn=fitness_fn)
                    mutation_prob = self.get_best_param(problem=fitness_name,
                                                        algo='genetic_alg',
                                                        param='mutation_prob')
                    pop_size = self.get_best_param(problem=fitness_name,
                                                   algo='genetic_alg',
                                                   param='pop_size')
                    best_state, best_fitness, curve = mlrose.genetic_alg(
                        problem,
                        pop_size=pop_size,
                        mutation_prob=mutation_prob,
                        max_iters=10000,
                        random_state=1,
                        curve=True)
                    total_score += np.mean(curve)
                    total_iter += len(curve)
                end = time.time()
                print('The fitness at the best state is: ',
                      total_score / tries)
                self.track(problem=fitness_name,
                           algo='genetic_alg',
                           i=n,
                           score=total_score / tries,
                           training_time=(end - start) / tries,
                           max_iter=total_iter / tries)

            for n, init_states_list in init_states.items():
                if fitness_name == 'MaxKColor':
                    fitness_fn = knap_fitnesses[n]
                if fitness_name == 'TravellingSales':
                    fitness_fn = tsp_fitnesses[n]
                print('%s: i=%d' % ('mimic', n))
                if n > 256:
                    break
                total_score = 0
                total_iter = 0
                start = time.time()
                for init_state in init_states_list:
                    problem = mlrose.DiscreteOpt(length=len(init_state),
                                                 fitness_fn=fitness_fn,
                                                 maximize=True)
                    if fitness_name == 'TravellingSales':
                        problem = mlrose.TSPOpt(length=n,
                                                fitness_fn=fitness_fn)
                    keep_pct = self.get_best_param(problem=fitness_name,
                                                   algo='mimic',
                                                   param='keep_pct')
                    pop_size = self.get_best_param(problem=fitness_name,
                                                   algo='mimic',
                                                   param='pop_size')
                    best_state, best_fitness, curve = mlrose.mimic(
                        problem,
                        max_iters=10000,
                        random_state=1,
                        curve=True,
                        pop_size=pop_size,
                        keep_pct=keep_pct,
                        max_attempts=10)
                    total_score += np.mean(curve)
                    total_iter += len(curve)
                end = time.time()
                print('The fitness at the best state is: ',
                      total_score / tries)
                self.track(problem=fitness_name,
                           algo='mimic',
                           i=n,
                           score=total_score / tries,
                           training_time=(end - start) / tries,
                           max_iter=total_iter / tries)
Esempio n. 21
0
_, _, curve = mlrose.genetic_alg(
    problem,
    max_attempts=MAX_ATTEMPTS,
    elite_dreg_ratio=1,
    curve=True,
    random_state=RANDOM_SEED,
)
t2 = process_time()
time_list.append((t2 - t1) / len(curve))
curve_list.append(curve)
n_eval.append((np.argmin(curve) + 1) * 200)

# MIMIC
t1 = process_time()
_, _, curve = mlrose.mimic(
    problem, max_attempts=MAX_ATTEMPTS, curve=True, random_state=RANDOM_SEED,
)
t2 = process_time()
time_list.append((t2 - t1) / len(curve))
curve_list.append(curve)
n_eval.append((np.argmin(curve) + 1) * 200)

df = 1 / pd.DataFrame(curve_list).transpose()
df.columns = algo_list
df.plot()
plt.xlabel("Iteration")
plt.ylabel("Fitness")
plt.title("TSP: Fitness curve vs algorithms")
plt.savefig("output/tsp_algo.png")
plt.close()
Esempio n. 22
0
                       use_fast_mimic=True)
mim_stats, mim_curve = mim.run()

columns = ['Time', 'Fitness', 'Population Size', 'Keep Percent']
df=pd.read_csv("./queen/queen_prob/mimic__queen_prob__run_stats_df.csv")
print(df[columns].sort_values(by=['Fitness'], ascending=False))

max_attempts = 10
max_iters = 25
keep_pct=0.25
pop_size = 200
eval_count = 0
best_state, best_fitness, mimic_curve = mlrh.mimic(prob,
                                             max_attempts=max_attempts,
                                             max_iters=max_iters,
                                             random_state=random_state,
                                             pop_size=pop_size,
                                             keep_pct=keep_pct,
                                             curve=True)
print("MIMIC - Total Function Evaluations:", eval_count)
plot_fitness_iteration('fitness_iteration_mimic_queens.png',mimic_curve,
                       "Queens - Mimic: Population Size: {}, Keep Percent: {}".format(pop_size, keep_pct))

# RHC

rhc = mlrh.RHCRunner(problem=prob,
                    experiment_name=experiment_name,
                    output_directory=output_directory,
                    seed=random_state,
                    max_attempts=200,
                    iteration_list=[2500],
Esempio n. 23
0
    #    'path': mlrose.CustomFitness(path, problem_type='discrete'),
    'flipflop': mlrose.FlipFlop(),
    #    'cliffs': mlrose.CustomFitness(cf1, problem_type='discrete'),
    #    'cliffs': mlrose.CustomFitness(is_larger, problem_type='discrete'),
    #    'max2color': mlrose.MaxKColorGenerator.generate(seed=42, number_of_nodes=PROBLEM_LENGTH, max_colors=2),
    #    'mod': mlrose.CustomFitness(cf2, problem_type='discrete')
}

RANDOM_STATE = 42
DEFAULTS = {'random_state': RANDOM_STATE, 'curve': True, 'max_attempts': 10}

ALGORITHMS = {
    'rhc': lambda p: mlrose.random_hill_climb(p, **DEFAULTS),
    'sa': lambda p: mlrose.simulated_annealing(p, **DEFAULTS),
    'ga': lambda p: mlrose.genetic_alg(p, **DEFAULTS),
    'mimic': lambda p: mlrose.mimic(p, **DEFAULTS)
}

results = []

PART_1 = True
PART_2 = True

if PART_1:
    for f_name, fitness in FITNESS_FUNCS.items():
        evaluate_fitness(f_name, fitness, f_name == 'max2color')
        alg2curve = {}
        overall_best_fitness = -1
        for alg_name, alg in ALGORITHMS.items():
            if f_name == 'max2color':
                problem = fitness
Esempio n. 24
0
    def run_mimic_hyper_params(self, fitness_fn):
        fitness_name = fitness_fn.__class__.__name__
        print("Running %s" % fitness_name)
        init_states = {}
        knap_fitnesses = {}
        tsp_fitnesses = {}
        tries = 1
        for x in 2**np.arange(6, 7):
            n = int(x)
            fitness_dists = mlrose.TravellingSales(distances=get_coords(n))
            tsp_fitnesses[n] = fitness_dists
            edges = []
            for x in range(int(n * 0.75)):
                a = r.randint(0, n - 1)
                b = r.randint(0, n - 1)
                while b == a:
                    b = r.randint(0, n - 1)
                edges.append((a, b))

            fitness_fn_knap = mlrose.MaxKColor(edges=edges)
            init_states[n] = []
            knap_fitnesses[n] = fitness_fn_knap
            for y in range(tries):
                init_states[n].append(get_init_state(n))

        for n, init_states_list in init_states.items():
            if fitness_name == 'MaxKColor':
                fitness_fn = knap_fitnesses[n]
            if fitness_name == 'TravellingSales':
                fitness_fn = tsp_fitnesses[n]
            print(n)
            print('%s: i=%d' % ('mimic', n))

            for init_state in init_states_list:
                problem = mlrose.DiscreteOpt(length=len(init_state),
                                             fitness_fn=fitness_fn,
                                             maximize=True)
                if fitness_name == 'TravellingSales':
                    problem = mlrose.TSPOpt(length=n, fitness_fn=fitness_fn)
                for pop_size in range(100, 1000, 100):
                    total_score = 0
                    total_iter = 0
                    best_state, best_fitness, curve = mlrose.mimic(
                        problem,
                        pop_size=pop_size,
                        keep_pct=0.1,
                        max_iters=10000,
                        random_state=1,
                        curve=True,
                        max_attempts=10)
                    total_score += np.max(curve)
                    total_iter += len(curve)
                    print('The fitness at the best state is: ',
                          total_score / tries, '. Pop size: ', pop_size)
                    self.track_best_params(problem=fitness_name,
                                           algo='mimic',
                                           param='pop_size',
                                           score=total_score,
                                           value=pop_size)

                pop_size = self.get_best_param(problem=fitness_name,
                                               algo='mimic',
                                               param='pop_size')
                for keep_pct in range(0, 10, 1):
                    total_score = 0
                    total_iter = 0
                    best_state, best_fitness, curve = mlrose.mimic(
                        problem,
                        pop_size=pop_size,
                        keep_pct=1.0 * keep_pct / 10,
                        max_iters=10000,
                        random_state=1,
                        curve=True,
                        max_attempts=10)
                    total_score += np.max(curve)
                    total_iter += len(curve)
                    print('The fitness at the best state is: ',
                          total_score / tries, '. keep_pct: ',
                          1.0 * keep_pct / 10)
                    self.track_best_params(problem=fitness_name,
                                           algo='mimic',
                                           param='keep_pct',
                                           score=total_score,
                                           value=1.0 * keep_pct / 10)
Esempio n. 25
0
def GenerateFitnessCurves(problem_fit, ma, sa_sched=None, gapop=200, gamut=0.5, mimpop=200, mimpct=0.2, maxIter=1000,
                          seed=1, min=False, input_size=-1, restarts=0):
    global eval_count, iter_total, algo_in, start, times, evals

    start = time.time()
    eval_count, iter_total, times, evals = 0, 0, [], []
    algo_in = 'RHC'
    print('RHC')
    best_state, best_fitness, rhcfitness_curve = mlrose.random_hill_climb(problem_fit, curve=True, max_attempts=ma,
                                                                          state_fitness_callback=callback_func,
                                                                          callback_user_info=[], restarts=restarts,
                                                                          random_state=seed, max_iters=maxIter)
    rhc_times = copy.deepcopy(times)
    rhc_evals = copy.deepcopy(evals)
    print('RHC',
          input_size,
          seed,
          best_fitness,
          np.where(rhcfitness_curve == best_fitness)[0][0],  # iter of best fitness
          iter_total,  # total iters
          np.where(rhcfitness_curve == best_fitness)[0][0],  # num evals for best fitness
          eval_count,  # total evals
          # round(rhc_times[np.where(rhcfitness_curve == best_fitness)[0][0]], 4),  # time of best fitness
          round(rhc_times[-1], 4)  # total time
          )
    eval_count, iter_total, times, evals = 0, 0, [], []
    algo_in = 'NOT_RHC'
    print('SA')

    if sa_sched:
        best_state, best_fitness, safitness_curve = mlrose.simulated_annealing(problem_fit, schedule=sa_sched,
                                                                               state_fitness_callback=callback_func,
                                                                               callback_user_info=[],
                                                                               curve=True, max_attempts=ma,
                                                                               random_state=seed, max_iters=maxIter)
    else:
        best_state, best_fitness, safitness_curve = mlrose.simulated_annealing(problem_fit,  # schedule=SA_schedule,
                                                                               curve=True, max_attempts=ma,
                                                                               state_fitness_callback=callback_func,
                                                                               callback_user_info=[],
                                                                               random_state=seed, max_iters=maxIter)
    sa_times = copy.deepcopy(times)
    sa_evals = copy.deepcopy(evals)

    print('SA',
          input_size,
          seed,
          best_fitness,
          np.where(safitness_curve == best_fitness)[0][0],  # iter of best fitness
          len(safitness_curve),  # total iters
          evals[np.where(safitness_curve == best_fitness)[0][0]],  # num evals for best fitness
          eval_count,  # total evals
          round(sa_times[np.where(safitness_curve == best_fitness)[0][0]], 4),  # time of best fitness
          round(sa_times[-1], 4)  # total time
          )
    eval_count, iter_total, times, evals = 0, 0, [], []

    print('GA')

    best_state, best_fitness, gafitness_curve = mlrose.genetic_alg(problem_fit, curve=True, pop_size=gapop,
                                                                   mutation_prob=gamut, max_attempts=ma,
                                                                   state_fitness_callback=callback_func,
                                                                   callback_user_info=[],

                                                                   random_state=seed, max_iters=maxIter)
    ga_times = copy.deepcopy(times)
    ga_evals = copy.deepcopy(evals)

    print('GA', input_size,
          seed,
          best_fitness,
          np.where(gafitness_curve == best_fitness)[0][0],  # iter of best fitness
          len(gafitness_curve),  # total iters
          evals[np.where(gafitness_curve == best_fitness)[0][0]],  # num evals for best fitness
          eval_count,  # total evals
          round(ga_times[np.where(gafitness_curve == best_fitness)[0][0]], 4),  # time of best fitness
          round(ga_times[-1], 4)  # total time
          )

    eval_count, iter_total, times, evals = 0, 0, [], []

    print('MIMIC')

    best_state, best_fitness, mimicfitness_curve = mlrose.mimic(problem_fit, curve=True, max_attempts=ma,
                                                                pop_size=mimpop, keep_pct=mimpct,
                                                                state_fitness_callback=callback_func,
                                                                callback_user_info=[],
                                                                random_state=seed, max_iters=maxIter)
    mim_times = copy.deepcopy(times)
    mim_evals = copy.deepcopy(evals)
    print('MIMIC', input_size,
          seed,
          best_fitness,
          np.where(mimicfitness_curve == best_fitness)[0][0],  # iter of best fitness
          len(mimicfitness_curve),  # total iters
          evals[np.where(mimicfitness_curve == best_fitness)[0][0]],  # num evals for best fitness
          eval_count,  # total evals
          round(mim_times[np.where(mimicfitness_curve == best_fitness)[0][0]], 4),  # time of best fitness
          round(mim_times[-1], 4)  # total time
          )

    if min:
        # To Maximize TSP, need to make everything negative
        gafitness_curve = np.array(gafitness_curve) * -1
        rhcfitness_curve = np.array(rhcfitness_curve) * -1
        safitness_curve = np.array(safitness_curve) * -1
        mimicfitness_curve = np.array(mimicfitness_curve) * -1
        return gafitness_curve, rhcfitness_curve, safitness_curve, mimicfitness_curve, ga_times, rhc_times, sa_times, mim_times, rhc_evals, sa_evals, ga_evals, mim_evals
    else:
        return gafitness_curve, rhcfitness_curve, safitness_curve, mimicfitness_curve, ga_times, rhc_times, sa_times, mim_times, rhc_evals, sa_evals, ga_evals, mim_evals
Esempio n. 26
0
df.plot()
plt.xlabel("Iteration")
plt.ylabel("Fitness")
plt.title("OneMax: Fitness curve vs population size in GA")
plt.savefig("output/onemax_ga_pop.png")
plt.close()

#%% tuning for MIMIC

curve_list = []
pop_sizes = [50, 100, 200]
for p in pop_sizes:
    _, _, curve = mlrose.mimic(
        problem,
        max_attempts=MAX_ATTEMPTS,
        max_iters=50,
        pop_size=p,
        curve=True,
        random_state=RANDOM_SEED,
    )
    curve_list.append(curve)

df = pd.DataFrame(curve_list).transpose()
df.columns = pop_sizes
df.plot()
plt.xlabel("Iteration")
plt.ylabel("Fitness")
plt.title("OneMax: Fitness curve vs population size in MIMIC")
plt.savefig("output/onemax_mimic_pop.png")
plt.close()

#%% Putting together
Esempio n. 27
0
def ContinuousPeaks():
    rs = 2  # random_state
    ma = 100  # max_attempts

    #fitness = mlrose.FourPeaks(t_pct=0.15)
    fitness = mlrose.ContinuousPeaks(t_pct=0.15)
    problem_fit = mlrose.DiscreteOpt(length=50,
                                     fitness_fn=fitness,
                                     maximize=True,
                                     max_val=2)

    # Fitness curve
    alltime = []
    import time
    start = time.time()
    best_state, best_fitness, gafitness_curve = mlrose.genetic_alg(
        problem_fit,
        curve=True,
        pop_size=200,
        mutation_prob=0.5,
        max_attempts=ma,
        random_state=rs)
    end = time.time()
    alltime.append((end - start))

    start = time.time()
    best_state, best_fitness, rhcfitness_curve = mlrose.random_hill_climb(
        problem_fit, curve=True, max_attempts=ma, random_state=rs)
    end = time.time()
    alltime.append((end - start))

    start = time.time()
    #SA_schedule = mlrose.ArithDecay()
    best_state, best_fitness, safitness_curve = mlrose.simulated_annealing(
        problem_fit,  #schedule=SA_schedule,
        curve=True,
        max_attempts=ma,
        random_state=rs)
    end = time.time()
    alltime.append((end - start))

    start = time.time()
    best_state, best_fitness, mimicfitness_curve = mlrose.mimic(
        problem_fit, curve=True, max_attempts=ma, random_state=rs)
    end = time.time()
    alltime.append((end - start))

    # Plot time comparison
    plt.figure()
    algorithms = ['GA', 'RHC', 'SA', 'MIMIC']
    plt.bar(algorithms, alltime)
    plt.title("Running time for CPP (seconds)")
    plt.ylabel('Time (s)')
    plt.xlabel('Random search algorithms')
    plt.tight_layout()
    i = 0
    for a in algorithms:
        plt.text(a,
                 alltime[i] + 0.05,
                 '%.2f' % alltime[i],
                 ha='center',
                 va='bottom',
                 fontsize=11)
        i += 1
    plt.savefig("Running time for CPP")
    plt.show()

    plt.figure()
    plt.title("CPP fitness vs iterations")
    plt.plot(gafitness_curve, label='GA', color='r')
    plt.plot(rhcfitness_curve, label='RHC', color='b')
    plt.plot(safitness_curve, label='SA', color='orange')
    plt.plot(mimicfitness_curve, label='MIMIC', color='g')
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("CPP fitness curve")
    plt.show()

    # MIMIC Fitness vs Iterations as cpt changes
    CPT = [0.1, 0.3, 0.5, 0.7, 0.9]
    plt.figure()
    for c in CPT:
        best_state, best_fitness, mimicfitness_curve = mlrose.mimic(
            problem_fit,
            keep_pct=c,
            curve=True,
            max_attempts=ma,
            random_state=rs)
        plt.plot(mimicfitness_curve, label='pct = ' + str(c))

    plt.title("CPP using MIMIC with different values of pct parameter")
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("CPP MIMIC parameter")
    plt.show()

    # GA Fitness vs Iterations as mutation prob changes
    Mutate = [0.1, 0.3, 0.5, 0.7, 0.9]
    plt.figure()
    for m in Mutate:
        best_state, best_fitness, gafitness_curve = mlrose.genetic_alg(
            problem_fit,
            mutation_prob=m,
            curve=True,
            max_attempts=ma,
            random_state=rs)
        plt.plot(gafitness_curve, label='mutate=' + str(m))

    plt.title("CPP using GA with  different values of mutation probability")
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("CPP GA parameter")
    plt.show()

    # SA Fitness vs Iterations as schedule changes
    # schedule = mlrose.GeomDecay(init_temp=10, decay=0.95, min_temp=1)

    init_temp = 1.0
    decay_r = [0.15, 0.35, 0.55, 0.75, 0.95]
    plt.figure()
    for d in decay_r:
        SAschedule = mlrose.GeomDecay(init_temp=10, decay=d, min_temp=1)
        best_state, best_fitness, safitness_curve = mlrose.simulated_annealing(
            problem_fit,
            schedule=SAschedule,
            curve=True,
            max_attempts=ma,
            random_state=rs)
        plt.plot(safitness_curve, label='decay rate = ' + str(d))

    plt.title("CPP using SA with different values of decay rate")
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("CPP SA parameter")
    plt.show()

    Mutate = [0.1, 0.1, 0.1, 0.1, 0.1]
    pop = [50, 100, 200, 300, 400]
    Mutatepop = [(100, 0.2), (100, 0.5), (100, 0.7), (200, 0.2), (200, 0.5),
                 (200, 0.7), (300, 0.2), (300, 0.5), (300, 0.7)]
    plt.figure()
    for m in Mutatepop:
        best_state, best_fitness, gafitness_curve = mlrose.genetic_alg(
            problem_fit,
            pop_size=m[0],
            mutation_prob=m[1],
            curve=True,
            max_attempts=ma,
            random_state=rs)
        plt.plot(gafitness_curve,
                 label='pop size = ' + str(m[0]) + ', mutation = ' + str(m[1]))

    plt.title("CPP using GA with  different parameters")
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("Knapsack GA parameter mutate pop")
    plt.show()

    temps = [10000000, 1000000, 100000, 10000, 1000, 100, 10, 5]
    plt.figure()
    for t in temps:
        SAschedule = mlrose.GeomDecay(init_temp=t, decay=0.55, min_temp=1)
        best_state, best_fitness, safitness_curve = mlrose.simulated_annealing(
            problem_fit,
            schedule=SAschedule,
            curve=True,
            max_attempts=ma,
            random_state=rs)
        plt.plot(safitness_curve, label='Temperature = ' + str(t))

    plt.title("CPP using SA with different values of temperature")
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("CPP SA temp")
    plt.show()
Esempio n. 28
0
def Knapsack():
    rs = 2  #random state
    ma = 200  #max attempts

    items = 40  # number of items
    random.seed(6)

    weights = []
    values = []
    for i in range(0, items):
        weights.append((random.random() + 0.1) * 30)
        #weights.append(random.randint(1,31))
        #values.append(random.randint(1, 500))
        values.append((random.random() + 0.1) * 500)

    #weights=[9,13,153,50,15,68,27,39,23,52,11,32,24,48,73,42,43,22,7,18,4,30,153,50,15,68,68,27,27,39]
    #values=[150,35,200,60,60,45,60,40,30,10,70,30,15,10,40,70,75,80,20,12,50,10,200,60,60,45,45,60,60,40]
    #print(len(weights))
    #print(weights)

    max_weight_pct = 0.6
    fitness = mlrose.Knapsack(weights, values, max_weight_pct)

    problem_fit = mlrose.DiscreteOpt(length=len(weights),
                                     fitness_fn=fitness,
                                     maximize=True)

    # Fitness curve
    alltime = []
    import time
    start = time.time()
    best_state, best_fitness, gafitness_curve = mlrose.genetic_alg(
        problem_fit,
        pop_size=300,
        mutation_prob=0.7,
        curve=True,
        max_attempts=ma,
        random_state=rs)
    end = time.time()
    alltime.append((end - start))

    start = time.time()
    best_state, best_fitness, rhcfitness_curve = mlrose.random_hill_climb(
        problem_fit, curve=True, max_attempts=ma, random_state=rs)
    end = time.time()
    alltime.append((end - start))

    start = time.time()
    SA_schedule = mlrose.GeomDecay(init_temp=100000, decay=0.95, min_temp=1)
    best_state, best_fitness, safitness_curve = mlrose.simulated_annealing(
        problem_fit,
        schedule=SA_schedule,
        curve=True,
        max_attempts=ma,
        random_state=rs)
    end = time.time()
    alltime.append((end - start))

    start = time.time()
    best_state, best_fitness, mimicfitness_curve = mlrose.mimic(
        problem_fit,
        curve=True,
        max_attempts=ma,
        pop_size=400,
        keep_pct=0.3,
        random_state=rs)
    end = time.time()
    alltime.append((end - start))

    # Plot time comparison
    plt.figure()
    algorithms = ['GA', 'RHC', 'SA', 'MIMIC']
    plt.bar(algorithms, alltime)
    plt.title("Running time for Knapsack problem (seconds)")
    plt.ylabel('Time (s)')
    plt.xlabel('Random search algorithms')
    plt.tight_layout()
    i = 0
    for a in algorithms:
        plt.text(a,
                 alltime[i] + 0.05,
                 '%.2f' % alltime[i],
                 ha='center',
                 va='bottom',
                 fontsize=11)
        i += 1
    plt.savefig("Running time for Knapsack problem")
    plt.show()

    plt.title("Knapsack problem fitness vs iterations")
    plt.plot(gafitness_curve, label='GA', color='r')
    plt.plot(rhcfitness_curve, label='RHC', color='b')
    plt.plot(safitness_curve, label='SA', color='orange')
    plt.plot(mimicfitness_curve, label='MIMIC', color='g')
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("Knapsack fitness curve")
    plt.show()

    # MIMIC Fitness vs Iterations as cpt changes
    CPT = [0.1, 0.3, 0.5, 0.7, 0.9]
    plt.figure()
    for c in CPT:
        best_state, best_fitness, mimicfitness_curve = mlrose.mimic(
            problem_fit,
            keep_pct=c,
            curve=True,
            max_attempts=ma,
            random_state=rs)
        plt.plot(mimicfitness_curve, label='pct = ' + str(c))

    plt.title(
        "Knapsack problem using MIMIC with different values of pct parameter")
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("Knapsack MIMIC parameter")
    plt.show()

    # GA Fitness vs Iterations as mutation prob changes
    Mutate = [0.1, 0.3, 0.5, 0.7, 0.9]
    plt.figure()
    for m in Mutate:
        best_state, best_fitness, gafitness_curve = mlrose.genetic_alg(
            problem_fit,
            mutation_prob=m,
            curve=True,
            max_attempts=ma,
            random_state=rs)
        plt.plot(gafitness_curve, label='mutation = ' + str(m))

    plt.title(
        "Knapsack problem using GA with  different values of mutation probability"
    )
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("Knapsack GA parameter")
    plt.show()

    # SA Fitness vs Iterations as schedule changes
    # schedule = mlrose.GeomDecay(init_temp=10, decay=0.95, min_temp=1)

    init_temp = 1.0
    decay_r = [0.15, 0.35, 0.55, 0.75, 0.95]
    plt.figure()
    for d in decay_r:
        SAschedule = mlrose.GeomDecay(init_temp=100000, decay=d, min_temp=1)
        best_state, best_fitness, safitness_curve = mlrose.simulated_annealing(
            problem_fit,
            schedule=SAschedule,
            curve=True,
            max_attempts=ma,
            random_state=rs)
        plt.plot(safitness_curve, label='decay rate = ' + str(d))

    plt.title("Knapsack problem using SA with different values of decay rate")
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("Knapsack SA parameter")
    plt.show()

    init_temp = 1.0
    temps = [100000, 10000, 1000, 100, 10, 5]
    plt.figure()
    for t in temps:
        SAschedule = mlrose.GeomDecay(init_temp=t, decay=0.95, min_temp=1)
        best_state, best_fitness, safitness_curve = mlrose.simulated_annealing(
            problem_fit,
            schedule=SAschedule,
            curve=True,
            max_attempts=ma,
            random_state=rs)
        plt.plot(safitness_curve, label='Temperature = ' + str(t))

    plt.title("Knapsack problem using SA with different values of temperature")
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("Knapsack SA temp")
    plt.show()

    Mutate = [0.1, 0.1, 0.1, 0.1, 0.1]
    pop = [50, 100, 200, 300, 400]
    Mutatepop = [(100, 0.2), (100, 0.5), (100, 0.7), (200, 0.2), (200, 0.5),
                 (200, 0.7), (300, 0.2), (300, 0.5), (300, 0.7)]
    plt.figure()
    for m in Mutatepop:
        best_state, best_fitness, gafitness_curve = mlrose.genetic_alg(
            problem_fit,
            pop_size=m[0],
            mutation_prob=m[1],
            curve=True,
            max_attempts=ma,
            random_state=rs)
        plt.plot(gafitness_curve,
                 label='pop size = ' + str(m[0]) + ', mutation = ' + str(m[1]))

    plt.title("Knapsack using GA with  different parameters")
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("Knapsack GA parameter mutate pop")
    plt.show()

    temps = [10000000, 1000000, 100000, 10000, 1000, 100, 10, 5]
    plt.figure()
    for t in temps:
        SAschedule = mlrose.GeomDecay(init_temp=t, decay=0.95, min_temp=1)
        best_state, best_fitness, safitness_curve = mlrose.simulated_annealing(
            problem_fit,
            schedule=SAschedule,
            curve=True,
            max_attempts=ma,
            random_state=rs)
        plt.plot(safitness_curve, label='Temperature = ' + str(t))

    plt.title("Knapsack problem using SA with different values of temperature")
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("Knapsack SA temp")
    plt.show()

    CPT = [0.1, 0.3, 0.5, 0.9]
    pp = [(100, 0.2), (100, 0.5), (100, 0.7), (100, 0.9), (200, 0.2),
          (200, 0.5), (200, 0.7), (200, 0.9), (500, 0.2), (500, 0.5),
          (500, 0.7), (500, 0.9)]
    plt.figure()
    Pop = [100, 200, 300, 400, 500]
    for p in Pop:
        best_state, best_fitness, mimicfitness_curve = mlrose.mimic(
            problem_fit,
            pop_size=p,
            keep_pct=0.3,
            curve=True,
            max_attempts=ma,
            random_state=rs)
        plt.plot(mimicfitness_curve, label='pop size = ' + str(p))

    plt.title("Knapsack problem using MIMIC with different parameters")
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("Knapsack MIMIC parameter pop pct")
    plt.show()
Esempio n. 29
0
plt.ylabel("Fitness")
plt.title("FlipFlop: Fitness curve vs population size in GA")
plt.savefig("output/flipflop_ga_pop.png")
plt.close()

print(df.max())

#%% tuning for MIMIC

curve_list = []
nth_pct = [0.2, 0.4]
for p in nth_pct:
    _, _, curve = mlrose.mimic(
        problem,
        max_attempts=MAX_ATTEMPTS,
        max_iters=50,
        keep_pct=p,
        curve=True,
        random_state=RANDOM_SEED,
    )
    curve_list.append(curve)

df = pd.DataFrame(curve_list).transpose()
df.columns = nth_pct
df.plot()
plt.xlabel("Iteration")
plt.ylabel("Fitness")
plt.title("FlipFlop: Fitness curve vs nth percentile in MIMIC")
plt.savefig("output/flipflop_mimic_nth.png")
plt.close()

print(df.max())
Esempio n. 30
0
def run_problem_config(config, OUTPUT_FOLDER="output"):
    if not os.path.exists(OUTPUT_FOLDER):
        os.makedirs(OUTPUT_FOLDER)

    if config['name'] == "nn":
        run_nn_problem_config(config, OUTPUT_FOLDER)
        return

    rhc_run_stats, rhc_run_curves, sa_run_stats, sa_run_curves, ga_run_stats, ga_run_curves, mimic_run_stats, mimic_run_curves = np.full(
        8, None)

    problem = get_problem(config['name'], config['size'])
    name = config['name']
    size = config['size']
    rhc_runner = RHCRunner(problem,
                           experiment_name=f"{size}-{name}-rhc",
                           seed=RS,
                           **config['rhc'])
    sa_runner = SARunner(problem,
                         experiment_name=f"{size}-{name}-sa",
                         seed=RS,
                         decay_list=[GeomDecay],
                         **config['sa'])
    ga_runner = GARunner(problem,
                         experiment_name=f'{size}-{name}-ga',
                         seed=RS,
                         **config['ga'])
    mimic_runner = MIMICRunner(problem=problem,
                               experiment_name=f"{size}-{name}-mimic",
                               seed=RS,
                               use_fast_mimic=True,
                               **config['mimic'])

    problem_size_results = {
        "optimal": [get_optimal_values(name, n) for n in config['sizes']]
    }
    times_results = {}

    if not config['rhc']['skip']:
        print(f"generating rhc results for {size}-{name}...")
        rhc_run_stats, rhc_run_curves = rhc_runner.run()
        rrs = rhc_run_stats[rhc_run_stats['Iteration'] != 0]
        best_restarts = [
            rrs[rrs['Restarts'].eq(i)]['Fitness'].idxmax()
            for i in config['rhc']['restart_list']
        ]
        rrs = rrs[rrs.index.isin(best_restarts)]
        rrs.reset_index(inplace=True, drop=True)
        rhc_run_stats = rrs
        best_run = rrs.query('Fitness == Fitness.max()').query(
            'Restarts == Restarts.max()').iloc[0]
        best_restarts = int(best_run['Restarts'])
        rhc_run_curves = rhc_run_curves.query(
            f"(current_restart == {best_run['current_restart']}) & (Restarts == {best_run['Restarts']})"
        )
        rhc_run_curves.reset_index(inplace=True, drop=True)
        times_results['RHC'] = best_run['Time']
        print(
            f"best restarts: {best_restarts}\tTime: {best_run['Time']}\tIterations:{rhc_run_curves['Iteration'].iloc[-1]}\n"
        )

        fig, axes = plt.subplots(nrows=1, ncols=2)
        rhc_run_curves.plot(title="RHC Convergence",
                            xlabel="Iterations",
                            ylabel="Fitness",
                            ax=axes[0],
                            x="Iteration",
                            y="Fitness")
        rhc_run_stats.plot(title="RHC Restarts",
                           x="Restarts",
                           y="Fitness",
                           ax=axes[1],
                           marker='o',
                           linestyle='--',
                           xlabel="Number of Restarts",
                           ylabel="Fitness")
        fig.suptitle(f"{size}-{name} RHC")
        fig.tight_layout()
        plt.savefig(f"{OUTPUT_FOLDER}/{size}-{name}-rhc_charts.png")

        max_iters = max(config['rhc']['iteration_list'])
        max_attempts = config['rhc']['max_attempts']
        rhc_problem_results = np.zeros(len(config['sizes']))
        for i in range(len(config['sizes'])):
            n = config['sizes'][i]
            start_time = time.time()
            rhc_problem_results[i] = rhc(get_problem(config['name'], n),
                                         max_attempts=max_attempts,
                                         max_iters=max_iters,
                                         random_state=RS,
                                         restarts=best_restarts)[1]
            running_time = time.time() - start_time
            print(
                f"Completed for size {n}\ttime: {running_time}\tFitness: {rhc_problem_results[i]}"
            )
        problem_size_results["RHC"] = rhc_problem_results
        print()

    if not config['sa']['skip']:
        print(f"generating sa results for {size}-{name}...")
        sa_run_stats, sa_run_curves = sa_runner.run()
        sars = sa_run_stats[sa_run_stats['Iteration'] != 0]
        sars.reset_index(inplace=True, drop=True)
        sa_run_stats = sars
        best_run = sars.query('Fitness == Fitness.max()').iloc[0]
        best_temp = best_run['Temperature']
        sa_run_curves = sa_run_curves[sa_run_curves['Temperature'] ==
                                      best_run['Temperature']]
        sa_run_curves.reset_index(inplace=True, drop=True)
        times_results['SA'] = best_run['Time']
        print(
            f"best Temperature: {best_temp}\tTime: {best_run['Time']}\tIterations:{sa_run_curves['Iteration'].iloc[-1]}\n"
        )

        fig, axes = plt.subplots(nrows=1, ncols=2)
        sa_run_curves.plot(title="SA Convergence",
                           xlabel="Iterations",
                           ylabel="Fitness",
                           ax=axes[0],
                           x="Iteration",
                           y="Fitness")
        sa_run_stats.plot(title="SA Initial Temperature",
                          x="schedule_init_temp",
                          y="Fitness",
                          ax=axes[1],
                          marker='o',
                          linestyle='--',
                          xlabel="Temperature",
                          ylabel="Fitness")
        fig.suptitle(f"{size}-{name} SA")
        fig.tight_layout()
        plt.savefig(f"{OUTPUT_FOLDER}/{size}-{name}-sa_charts.png")

        max_iters = max(config['sa']['iteration_list'])
        max_attempts = config['sa']['max_attempts']
        sa_problem_results = np.zeros(len(config['sizes']))
        for i in range(len(config['sizes'])):
            n = config['sizes'][i]
            start_time = time.time()
            sa_problem_results[i] = sa(
                get_problem(config['name'], n),
                max_attempts=max_attempts,
                max_iters=max_iters,
                random_state=RS,
                schedule=GeomDecay(init_temp=best_temp.init_temp))[1]
            running_time = time.time() - start_time
            print(
                f"Completed for size {n}\ttime: {running_time}\tFitness: {sa_problem_results[i]}"
            )
        problem_size_results["SA"] = sa_problem_results
        print()

    if not config['ga']['skip']:
        print(f"generating ga results for {size}-{name}...")
        ga_run_stats, ga_run_curves = ga_runner.run()
        ga_run_stats = ga_run_stats[ga_run_stats['Iteration'] != 0]
        pop_size_results = ga_run_stats.groupby(
            'Population Size')['Fitness'].max()
        mut_rate_results = ga_run_stats.groupby(
            'Mutation Rate')['Fitness'].max()
        ga_groups = ga_run_stats.groupby(['Mutation Rate',
                                          'Population Size']).max()['Fitness']
        best_mut_rate, best_pop_size = ga_groups.idxmax()
        ga_run_curves = ga_run_curves.query(
            f"(`Mutation Rate` == {best_mut_rate}) & (`Population Size` == {best_pop_size})"
        )
        ga_run_stats.reset_index(inplace=True, drop=True)
        ga_run_curves.reset_index(inplace=True, drop=True)
        best_time = ga_run_curves.iloc[-1]['Time']
        print(
            f'best Mutation Rate: {best_mut_rate}\tbest Population Size: {best_pop_size}\tTime: {best_time}\tIterations:{ga_run_curves["Iteration"].iloc[-1]}\n'
        )

        fig, axes = plt.subplots(nrows=1, ncols=3)
        pop_size_results = ga_run_stats.groupby(
            'Population Size')['Fitness'].max()
        mut_rate_results = ga_run_stats.groupby(
            'Mutation Rate')['Fitness'].max()
        ga_run_curves.plot(title="GA Convergence",
                           xlabel="Iterations",
                           ylabel="Fitness",
                           ax=axes[0],
                           x="Iteration",
                           y="Fitness")
        pop_size_results.plot(title="GA Population Sizes",
                              ax=axes[1],
                              marker='o',
                              linestyle='--',
                              xlabel="Population Size",
                              ylabel="Fitness")
        mut_rate_results.plot(title="GA Mutation Rates",
                              ax=axes[2],
                              marker='o',
                              linestyle='--',
                              xlabel="Mutation Rate",
                              ylabel="Fitness")
        fig.suptitle(f"{size}-{name} GA")
        fig.tight_layout()
        plt.savefig(f"{OUTPUT_FOLDER}/{size}-{name}-ga_charts.png")

        max_iters = max(config['ga']['iteration_list'])
        max_attempts = config['ga']['max_attempts']
        ga_problem_results = np.zeros(len(config['sizes']))
        for i in range(len(config['sizes'])):
            n = config['sizes'][i]
            start_time = time.time()
            ga_problem_results[i] = ga(get_problem(config['name'], n),
                                       max_attempts=max_attempts,
                                       max_iters=max_iters,
                                       random_state=RS,
                                       pop_size=int(best_pop_size),
                                       mutation_prob=best_mut_rate)[1]
            running_time = time.time() - start_time
            print(
                f"Completed for size {n}\ttime: {running_time}\tFitness: {ga_problem_results[i]}"
            )
        problem_size_results["GA"] = ga_problem_results
        print()

    if not config['mimic']['skip']:
        print(f"generating mimic results for {size}-{name}...")
        mimic_run_stats, mimic_run_curves = mimic_runner.run()
        mimic_run_stats = mimic_run_stats[mimic_run_stats['Iteration'] != 0]
        pop_size_results = mimic_run_stats.groupby(
            'Population Size')['Fitness'].max()
        keep_percent_results = mimic_run_stats.groupby(
            'Keep Percent')['Fitness'].max()
        mimic_groups = mimic_run_stats.groupby(
            ['Keep Percent', 'Population Size']).max()['Fitness']
        best_keep_percent, best_pop_size = mimic_groups.idxmax()
        mimic_run_curves = mimic_run_curves.query(
            f"(`Keep Percent` == {best_keep_percent}) & (`Population Size` == {best_pop_size})"
        )
        mimic_run_stats.reset_index(inplace=True, drop=True)
        mimic_run_curves.reset_index(inplace=True, drop=True)
        best_time = mimic_run_curves.iloc[-1]['Time']
        times_results['MIMIC'] = best_time
        print(
            f'best Keep Percentage: {best_keep_percent}\tbest Population Size: {best_pop_size}\tTime: {best_time}\tIterations:{mimic_run_curves["Iteration"].iloc[-1]}\n'
        )

        fig, axes = plt.subplots(nrows=1, ncols=3)
        pop_size_results = mimic_run_stats.groupby(
            'Population Size')['Fitness'].max()
        keep_percent_results = mimic_run_stats.groupby(
            'Keep Percent')['Fitness'].max()
        mimic_run_curves.plot(title="MIMIC Convergence",
                              xlabel="Iterations",
                              ylabel="Fitness",
                              ax=axes[0],
                              x="Iteration",
                              y="Fitness")
        pop_size_results.plot(title="MIMIC Population Sizes",
                              ax=axes[1],
                              marker='o',
                              linestyle='--',
                              xlabel="Population Size",
                              ylabel="Fitness")
        keep_percent_results.plot(title="MIMIC Keep Percent",
                                  ax=axes[2],
                                  marker='o',
                                  linestyle='--',
                                  xlabel="Keep Percent",
                                  ylabel="Fitness")
        fig.suptitle(f"{size}-{name} MIMIC")
        fig.tight_layout()
        plt.savefig(f"{OUTPUT_FOLDER}/{size}-{name}-mimic_charts.png")

        max_iters = max(config['mimic']['iteration_list'])
        max_attempts = config['mimic']['max_attempts']
        mimic_problem_results = np.zeros(len(config['sizes']))
        for i in range(len(config['sizes'])):
            n = config['sizes'][i]
            start_time = time.time()
            mimic_problem_results[i] = mimic(get_problem(config['name'], n),
                                             max_attempts=max_attempts,
                                             max_iters=max_iters,
                                             random_state=RS,
                                             pop_size=int(best_pop_size),
                                             keep_pct=best_keep_percent)[1]
            running_time = time.time() - start_time
            print(
                f"Completed for size {n}\ttime: {running_time}\tFitness: {mimic_problem_results[i]}"
            )
        problem_size_results["MIMIC"] = mimic_problem_results
        print()

    fig, axes = plt.subplots(nrows=1, ncols=1)
    problem_size_results = pd.DataFrame(problem_size_results,
                                        index=config['sizes'])
    problem_size_results.index.rename('Problem Size', inplace=True)
    problem_size_results.plot(ax=axes,
                              title=f"{name} Problem Size Results",
                              marker='o',
                              linestyle='--',
                              xlabel="Problem Size",
                              ylabel="Fitness")
    fig.tight_layout()
    plt.savefig(f"{OUTPUT_FOLDER}/{name}-problem_sizes.png")
    html = problem_size_results.to_html(index=True)
    with open(f"{OUTPUT_FOLDER}/{name}-problem_sizes.html", 'w') as fp:
        fp.write(html)

    times_results = pd.Series(times_results).to_frame()
    times_results.columns = ['Time (s)']
    html = times_results.to_html(index=True)
    with open(f"{OUTPUT_FOLDER}/{size}-{name}-times-results.html", 'w') as fp:
        fp.write(html)

    print("Completed running algorithms.\n")
    return rhc_run_stats, rhc_run_curves, sa_run_stats, sa_run_curves, ga_run_stats, ga_run_curves, mimic_run_stats, mimic_run_curves