Exemple #1
0
def queens_problem(n=8):
    queens_max = lambda state: sum(np.arange(len(state))) - mlrose.Queens(
    ).evaluate(state)
    fitness_queens = mlrose.CustomFitness(queens_max)
    return mlrose.DiscreteOpt(length=n,
                              fitness_fn=fitness_queens,
                              maximize=True,
                              max_val=n)
Exemple #2
0
    def run_sa_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' % ('simulated_annealing', 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 max_attempts in range(10, 110, 10):
                    total_score = 0
                    total_iter = 0
                    best_state, best_fitness, curve = mlrose.simulated_annealing(
                        problem,
                        max_attempts=max_attempts,
                        max_iters=10000,
                        random_state=1,
                        curve=True)
                    total_score += np.max(curve)
                    total_iter += len(curve)
                    print('The fitness at the best state is: ',
                          total_score / tries, '. Max Attempts: ',
                          max_attempts)
                    self.track_best_params(problem=fitness_name,
                                           algo='simulated_annealing',
                                           param='max_attempts',
                                           score=total_score,
                                           value=max_attempts)
Exemple #3
0
def get_problem(size=100, t_pct=0.06):
    global orig_fitness_func
    orig_fitness_func = mlrose_hiive.ContinuousPeaks(t_pct)

    fitness = mlrose_hiive.CustomFitness(fitness_func)
    problem = mlrose_hiive.DiscreteOpt(length=size,
                                       fitness_fn=fitness,
                                       maximize=True)

    return problem
Exemple #4
0
def get_flip_flop(size):
    flip_flop = mlrose.FlipFlop()
    state = np.array([0,1,0,1,1,1,1])
    flip_flop.evaluate(state)
    problem = mlrose.DiscreteOpt(
        length=size,
        fitness_fn=flip_flop,
        maximize=True,
        max_val=2 # makes it bit string
    )
    return problem
def get_one_max(size):
    one_max = mlrose.OneMax()
    state = np.array([0, 1, 0, 1, 1, 1, 1])
    one_max.evaluate(state)
    problem = mlrose.DiscreteOpt(
        length=size,
        fitness_fn=one_max,
        maximize=True,
        max_val=2  # makes it bit string
    )
    return problem
Exemple #6
0
def get_continuous_peaks(size):
    continuous_peaks = mlrose.ContinuousPeaks(t_pct=0.1)
    state = np.array([0,1,0,1,1,1,1])
    continuous_peaks.evaluate(state)
    problem = mlrose.DiscreteOpt(
        length=size,
        fitness_fn=continuous_peaks,
        maximize=True,
        max_val=2 # makes it bit string
    )
    return problem
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()
Exemple #8
0
def get_four_peaks(size):
    four_peaks = mlrose.FourPeaks(t_pct=0.1)
    state = np.array([0, 1, 0, 1, 1, 1, 1])
    four_peaks.evaluate(state)
    problem = mlrose.DiscreteOpt(
        length=size,
        fitness_fn=four_peaks,
        maximize=True,
        max_val=2  # makes it bit string
    )
    return problem
Exemple #9
0
def get_problem(name, n):
    if name == "queens":
        problem = queens_problem(n)
    elif name == "four_peaks":
        problem = mlrose.DiscreteOpt(length=n,
                                     fitness_fn=FourPeaks(),
                                     maximize=True,
                                     max_val=2)
    elif name == "flip_flop":
        problem = mlrose.DiscreteOpt(length=n,
                                     fitness_fn=FlipFlop(),
                                     maximize=True,
                                     max_val=2)
    elif name == "six_peaks":
        problem = mlrose.DiscreteOpt(length=n,
                                     fitness_fn=SixPeaks(),
                                     maximize=True,
                                     max_val=2)
    else:
        raise Exception("Invalid Problem Name!")
    problem.set_mimic_fast_mode(True)
    return problem
Exemple #10
0
def get_knapsack(size):
    weights = [10, 5, 2, 8, 15]
    values = [1, 2, 3, 4, 5]
    max_weight_pct = 0.6
    knapsack = mlrose.Knapsack(weights, values, max_weight_pct)
    state = np.array([1, 0, 2, 1, 0])
    knapsack.evaluate(state)
    problem = mlrose.DiscreteOpt(
        length=size,
        fitness_fn=knapsack,
        maximize=True,
        max_val=2  # makes it bit string
    )
    return problem
Exemple #11
0
def main():

    four_peaks_fitness = four_peaks.get_four_peaks()
    four_peaks_tuning_list = four_peaks.get_four_peaks_tuning
    #state = np.array([1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0])
    problem = mlrose.DiscreteOpt(10,
                                 four_peaks_fitness,
                                 maximize=True,
                                 max_val=2)
    problem_size_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
    #problem_size_list = [10, 20]
    best_fitness_list = []
    for size in problem_size_list:
        problem = mlrose.DiscreteOpt(size,
                                     four_peaks_fitness,
                                     maximize=True,
                                     max_val=2)
        experiment_name = 'sa_tuning_size_' + str(size)
        sa = runners.SARunner(problem=problem,
                              experiment_name=experiment_name,
                              output_directory='./',
                              seed=27,
                              iteration_list=[10000],
                              max_attempts=500,
                              temperature_list=[1])
        #decay_list=mlrose.GeomDecay(init_temp=1.1))
        #temperature_list=[1, 10, 50, 100, 250, 500, 1000, 2500, 5000, 10000])
        #temperature_list=[1, 10, 50, 100, 250, 500, 1000, 2500, 5000, 10000])
        # the two data frames will contain the results
        df_run_stats, df_run_curves = sa.run()

        #print(df_run_curves.loc[df_run_curves['Fitness'].idxmax()])
        #curr_best_fitness = df_run_curves.loc[df_run_curves['Fitness'].idxmax()]['Fitness']
        best_fitness_list.append(
            df_run_curves.loc[df_run_curves['Fitness'].idxmax()])
    print(best_fitness_list)
def main():
    verbose = True
    num_runs = 20
    max_iters = 3000

    # define Cont. Peaks fitness function to maximize
    fitness_fn = mlrose.ContinuousPeaks()

    # define optimization problem
    length = 100
    cp_problem = mlrose.DiscreteOpt(
        length=length,
        fitness_fn=fitness_fn,
        maximize=True,
    )

    cp_problem.set_mimic_fast_mode(True)

    # set initial state
    initial_state = np.random.randint(0, 2, size=length)

    # randomized hill climbing
    rhc_fitness_dfs = cont_peaks_rhc(cp_problem, initial_state, max_iters,
                                     num_runs, verbose)
    print('---')

    # simulated annealing
    sa_fitness_dfs = cont_peaks_sa(cp_problem, initial_state, max_iters,
                                   num_runs, verbose)
    print('---')

    # genetic algorithm
    ga_fitness_dfs = cont_peaks_ga(cp_problem, max_iters, num_runs, verbose)
    print('---')

    # MIMIC algorithm
    mimic_fitness_dfs = cont_peaks_mimic(cp_problem, max_iters, num_runs,
                                         verbose)
    print('---')

    # compare algorithm performance
    plotting.compare_algos(
        problem_name='cont_peaks',
        rhc_dfs=rhc_fitness_dfs,
        sa_dfs=sa_fitness_dfs,
        ga_dfs=ga_fitness_dfs,
        mimic_dfs=mimic_fitness_dfs,
    )
def scale_ann(train_features_spam_norm, train_labels_spam,
              test_features_spam_norm, test_labels_spam):
    global count
    train_acc_list = []
    test_acc_list = []
    fitness_call_list = []
    loss_list = []
    for i in range(400, 4001, 400):
        count = 0
        train_features_sub = train_features_spam_norm[:i, :]
        train_labels_sub = train_labels_spam[:i]
        fitness_obj = mlrose.CustomFitness(spam_nn_fit,
                                           train_features=train_features_sub,
                                           train_labels=train_labels_sub)
        opt = mlrose.DiscreteOpt(237, fitness_obj, maximize=True, max_val=1001)
        best_state_spam, best_fitness_spam, _ = mlrose.simulated_annealing(
            opt, schedule=mlrose.ExpDecay(exp_const=0.003), curve=True)
        loss_list.append(best_fitness_spam)
        train_predict = predict(best_state_spam, train_features_sub)
        test_predict = predict(best_state_spam, test_features_spam_norm)
        fitness_call_list.append(count)
        train_acc_list.append(accuracy_score(train_labels_sub, train_predict))
        test_acc_list.append(accuracy_score(test_labels_spam, test_predict))
    plt.figure(figsize=(10, 6))
    plt.subplot(121)
    plt.plot(np.arange(400, 4001, 400), loss_list, label='-1*loss')
    plt.xlabel('training size')
    plt.ylabel('-1*loss')
    plt.title('loss versus training size')
    plt.legend()
    plt.subplot(122)
    plt.plot(np.arange(400, 4001, 400), train_acc_list, label='train')
    plt.plot(np.arange(400, 4001, 400), test_acc_list, label='test')
    plt.xlabel('training size')
    plt.ylabel('accuracy')
    plt.title('accuracy versus training size')
    plt.legend()
    plt.show()
    # fitness calls versus training size
    plt.figure(figsize=(6, 6))
    plt.plot(np.arange(400, 4001, 400), fitness_call_list, label='#.calls')
    plt.xlabel('training size')
    plt.ylabel('fitness calls')
    plt.legend()
    plt.show()
Exemple #14
0
def get_problem(size=100):
    global orig_fitness_func

    seed = 42
    number_of_items_types = size
    max_weight_per_item = 25
    max_value_per_item = 10
    max_weight_pct = 0.35
    max_item_count = 10
    multiply_by_max_item_count = True
    np.random.seed(seed)
    weights = 1 + np.random.randint(max_weight_per_item, size=number_of_items_types)
    values = 1 + np.random.randint(max_value_per_item, size=number_of_items_types)
    orig_fitness_func = mlrose_hiive.Knapsack(weights, values, max_weight_pct=max_weight_pct, max_item_count=max_item_count, multiply_by_max_item_count=multiply_by_max_item_count)

    fitness = mlrose_hiive.CustomFitness(fitness_func)
    problem = mlrose_hiive.DiscreteOpt(length=number_of_items_types, fitness_fn=fitness, maximize=True)
    return problem
Exemple #15
0
 def optimize(self):
     problem_size_space = self.problem_size
     # Initializing the problem
     init_state = np.random.randint(2, size=problem_size_space)
     fitness = mlrose.FourPeaks(t_pct=0.1)
     problem = mlrose.DiscreteOpt(length=problem_size_space,
                                  fitness_fn=fitness,
                                  maximize=True,
                                  max_val=2)
     # SA
     # super().gridSearchSA(problem,'4Peaks',problem_size_space,self.noOfiteration)
     # RHC
     # super().gridSearchRHC(problem,'4Peaks',problem_size_space,self.noOfiteration)
     #GA
     # super().gridSearchGA(problem,'4Peaks',problem_size_space,self.noOfiteration)
     #MIMIC
     super().gridSearchMIMIC(problem, '4Peaks', problem_size_space,
                             self.noOfiteration)
Exemple #16
0
    def optimize(self):
        problem_size_space = self.problem_size

        # Initializing the problem
        init_state = [i for i in range(problem_size_space)]
        fitness = mlrose.Queens()
        problem = mlrose.DiscreteOpt(length=problem_size_space,
                                     fitness_fn=fitness,
                                     maximize=False,
                                     max_val=problem_size_space)
        # SA
        # super().gridSearchSA(problem,'NQueens',problem_size_space,self.noOfiteration)
        # RHC
        # super().gridSearchRHC(problem,'NQueens',problem_size_space,self.noOfiteration)
        #GA
        # super().gridSearchGA(problem,'NQueens',problem_size_space,self.noOfiteration)
        #MIMIC
        super().gridSearchMIMIC(problem, 'NQueens', problem_size_space,
                                self.noOfiteration)
Exemple #17
0
def plot_func_eval(optimisations):
    func_evals = []
    print("\n plot_func_eval: ", end="")

    prob_type = Problem.FOUR_PEAKS
    input_size = [10, 20, 40, 60, 80, 100]
    k = 0
    for i in input_size:
        fitness = mlrose.FourPeaks(t_pct=0.15)
        problem = mlrose.DiscreteOpt(length=i,
                                     fitness_fn=fitness,
                                     maximize=True,
                                     max_val=2)

        func_evals.append([])
        for opt in optimisations:
            state, score, curve, process_time = get_random_optimisation(
                problem, opt, prob_type)
            if len(curve) < __MAX_ATTEMPTS[prob_type] or curve[-1][0] > curve[
                    -(__MAX_ATTEMPTS[prob_type])][0]:
                func_evals[k].append(curve[-1][1])
            else:
                func_evals[k].append(curve[-(__MAX_ATTEMPTS[prob_type])][1])
            print(".", end="")
        k += 1

    func_evals = np.array(func_evals)

    plt.figure(400)
    plt.plot(func_evals[:, 0], label=str(optimisations[0])[13:], color='red')
    plt.plot(func_evals[:, 1], label=str(optimisations[1])[13:], color='blue')
    plt.plot(func_evals[:, 2], label=str(optimisations[2])[13:], color='green')
    plt.plot(func_evals[:, 3],
             label=str(optimisations[3])[13:],
             color='orange')
    plt.legend()
    plt.xticks(np.arange(len(input_size)), [str(z) for z in input_size])
    plt.xlabel("input size")
    plt.ylabel("function evalutations")
    plt.title(str(prob_type)[8:])
    plt.savefig("FITNESS_CURVES" + "/fneval_vs_ipsize_" + str(prob_type)[8:] +
                ".png")
Exemple #18
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()
rhc_repeats = 1
ga_repeats = 200
sa_repeats = 1
mimic_repeats = 1


def fitness_counter(state):
    global eval_count
    fitness = mlrose.FourPeaks(t_pct=0.25)
    eval_count += 1
    return fitness.evaluate(state)


fitness = mlrose.CustomFitness(fitness_counter)
problem = mlrose.DiscreteOpt(length=40,
                             fitness_fn=fitness,
                             maximize=True,
                             max_val=2)

ga_evals_list = []
df_ga_stats_list = []
df_ga_curves_list = []
for r in range(ga_repeats):
    ga = GARunner(problem=problem,
                  experiment_name="ga_test",
                  output_directory="./results/",
                  seed=r,
                  iteration_list=2 ** np.arange(18),
                  max_attempts=50,
                  population_sizes=[300],
                  mutation_rates=[0.2])
    plt.plot(np.arange(400, 4001, 400), train_acc_list, label='train')
    plt.plot(np.arange(400, 4001, 400), test_acc_list, label='test')
    plt.xlabel('training size')
    plt.ylabel('accuracy')
    plt.title('accuracy versus training size')
    plt.legend()
    plt.show()
    # fitness calls versus training size
    plt.figure(figsize=(6, 6))
    plt.plot(np.arange(400, 4001, 400), fitness_call_list, label='#.calls')
    plt.xlabel('training size')
    plt.ylabel('fitness calls')
    plt.legend()
    plt.show()


if __name__ == "__main__":
    train_features_spam_norm, train_labels_spam, test_features_spam_norm, test_labels_spam = split_train_test_spam(
    )
    fitness_obj = mlrose.CustomFitness(spam_nn_fit,
                                       train_features=train_features_spam_norm,
                                       train_labels=train_labels_spam)
    opt = mlrose.DiscreteOpt(237, fitness_obj, maximize=True, max_val=1001)
    rhc(opt)
    s_ann(opt)
    gen_alg(opt)
    method_compare(opt, train_features_spam_norm, train_labels_spam,
                   test_features_spam_norm, test_labels_spam)
    scale_ann(train_features_spam_norm, train_labels_spam,
              test_features_spam_norm, test_labels_spam)
Exemple #21
0
print(f"Attempts:            {attempts}")
print(f"Max Iterations:      {max_it}")
print(f"Problem Sizes:       {test_range}")
print(f"Last Problem Size:   {final_prob_len}\n\n")

part2_time = 0.0

print(f"\n######### PART 2 #########\n")

for i in test_range:
    start = time.time()
    init = np.random.choice(2**(i + 1), size=i, replace=False)
    print(f"Running for subproblem size: {i}\n        Initialization: {init}")

    problem = mlr.DiscreteOpt(length=i,
                              fitness_fn=fitness_cust,
                              maximize=True,
                              max_val=2**(i + 1))

    ## Randomized Hill Climbing
    sub_start = time.time()
    best_rhc_state, best_rhc_fitness, rhc_curve = mlr.random_hill_climb(
        problem,
        max_attempts=attempts,
        random_state=seed,
        curve=True,
        init_state=init,
        restarts=500)
    print(f"        best RHC State: {best_rhc_state}")
    sub_end = time.time()
    rhc_fitnesses.append(best_rhc_fitness)
    rhc_times.append(sub_end - sub_start)
def main():
    ## SET SOME PARAMS TO USE GLOBALLY
    max_iters_list = [50, 100, 1000]  #,32,64,128,256,512,1024]
    max_iters_list_full = [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
    rand_list = [1, 11, 22]  #,44,55,66,77,88,99]
    rand_list_full = [0, 11, 22, 33, 44, 55, 66, 77, 88, 99]
    input_location = 'data/'
    output_location = 'outputs/'
    chart_output_location = 'charts/'
    prefix = '5th_'

    ## DEFINE PROBLEMS TO SOLVE
    # Traveling Salesman Problem (TSP)
    space_length = 1000
    cities_cnt = 200
    coords_list, x, y = create_TSP(space_length,
                                   cities_cnt,
                                   return_lists_too=True)
    plt.plot(x, y, 'o')
    plt.savefig(chart_output_location + 'TPS_visual' + '.png')
    fitness_coords = mlrose.TravellingSales(coords=coords_list)
    problem_TSP = mlrose.TSPOpt(length=len(coords_list),
                                fitness_fn=fitness_coords,
                                maximize=False)

    # 4 Peaks
    t_pct = 0.1
    length = 200
    fitness_4_peaks = mlrose.FourPeaks(t_pct=t_pct)
    problem_4P = mlrose.DiscreteOpt(length=length,
                                    fitness_fn=fitness_4_peaks,
                                    maximize=True,
                                    max_val=2)
    problem_4P_small = mlrose.DiscreteOpt(length=50,
                                          fitness_fn=fitness_4_peaks,
                                          maximize=True,
                                          max_val=2)
    problem_4P_big = mlrose.DiscreteOpt(length=1000,
                                        fitness_fn=fitness_4_peaks,
                                        maximize=True,
                                        max_val=2)

    # Continuous Peaks
    t_pct = 0.1
    length = 200
    fitness_cont_peaks = mlrose.ContinuousPeaks(t_pct=t_pct)
    problem_cont_peaks = mlrose.DiscreteOpt(length=length,
                                            fitness_fn=fitness_cont_peaks,
                                            maximize=True,
                                            max_val=2)

    # Flip Flop
    length = 200
    fitness_FF = mlrose.FlipFlop()
    problem_FF = mlrose.DiscreteOpt(length=length,
                                    fitness_fn=fitness_FF,
                                    maximize=True,
                                    max_val=2)
    problem_FF_small = mlrose.DiscreteOpt(length=50,
                                          fitness_fn=fitness_FF,
                                          maximize=True,
                                          max_val=2)
    problem_FF_big = mlrose.DiscreteOpt(length=1000,
                                        fitness_fn=fitness_FF,
                                        maximize=True,
                                        max_val=2)

    # Knapsack
    length = 200
    weights, values = create_Knapsack(length)
    weights_big, values_big = create_Knapsack(1000)
    weights_small, values_small = create_Knapsack(50)
    fitness_KS = mlrose.Knapsack(weights, values, max_weight_pct=0.65)
    fitness_KS_big = mlrose.Knapsack(weights_big,
                                     values_big,
                                     max_weight_pct=0.65)
    fitness_KS_small = mlrose.Knapsack(weights_small,
                                       values_small,
                                       max_weight_pct=0.65)
    problem_KS = mlrose.DiscreteOpt(length=length,
                                    fitness_fn=fitness_KS,
                                    maximize=True,
                                    max_val=2)
    problem_KS_big = mlrose.DiscreteOpt(length=1000,
                                        fitness_fn=fitness_KS_big,
                                        maximize=True,
                                        max_val=2)
    problem_KS_small = mlrose.DiscreteOpt(length=50,
                                          fitness_fn=fitness_KS_small,
                                          maximize=True,
                                          max_val=2)

    dict_of_param_dict = {}
    dict_of_param_dict['GA'] = {
        'pop_size': [100, 200],  #,1000],
        'mutation_prob': [0.5, 0.1, 0.2],
        'max_attempts': [5, 10, 30],
        'max_iters': max_iters_list,
        'random_state': rand_list
    }
    dict_of_param_dict['RHC'] = {
        'max_attempts': [30, 50, 100],  #[5,10,20,50]
        'restarts': [5, 10, 20],  #[0,1,2,5]
        'max_iters': max_iters_list,
        'random_state': rand_list
    }
    dict_of_param_dict['SA'] = {
        'max_attempts': [10, 50, 100],
        'init_temp': [1.0, 10.0, 0.5, 20, 100, 1000],
        'decay': [0.99, 0.8, 0.5],
        'max_iters': max_iters_list,
        'random_state': rand_list
    }
    dict_of_param_dict['MIMIC'] = {
        'pop_size': [100, 150],
        'keep_pct': [0.5, 0.2],
        'max_attempts': [10],
        'max_iters': [100],
        'random_state': rand_list
    }

    MIMIC_FF = {
        'pop_size': 100,
        'keep_pct': 0.5,
        'max_attempts': 30,
        'max_iters': [2, 4, 8, 16, 32, 64,
                      128],  ## put full list here before uploading
        'random_state': [0, 11, 22, 33, 44]
    }
    MIMIC_4P = {
        'pop_size': 150,
        'keep_pct': 0.2,
        'max_attempts': 30,
        'max_iters': [2, 4, 8, 16, 32, 64,
                      128],  ## put full list here before uploading
        'random_state': [0, 11, 22, 33, 44]
    }
    MIMIC_KS = {
        'pop_size': 150,
        'keep_pct': 0.5,
        'max_attempts': 30,
        'max_iters': [2, 4, 8, 16, 32, 64,
                      128],  ## put full list here before uploading
        'random_state': [0, 11, 22, 33, 44]
    }
    MIMIC_CP = {
        'pop_size': 200,
        'keep_pct': 0.2,
        'max_attempts': 30,
        'max_iters': [2, 4, 8, 16, 32, 64,
                      128],  ## put full list here before uploading
        'random_state': [0, 11, 22, 33, 44]
    }
    GA_FF = {
        'pop_size': 200,  #,1000],
        'mutation_prob': 0.5,
        'max_attempts': 30,
        'max_iters': max_iters_list_full,
        'random_state': rand_list_full
    }

    MIMIC_FF2 = {
        'pop_size': [100],
        'keep_pct': [0.5],
        'max_attempts': [30, 50],
        'max_iters': [64],
        'random_state': [55]  #,66,77,88,99]
    }

    print("starting MIMIC FF")
    # GETTING MIMIC FF RESULTS
    print("starting MIMIC FF...")
    ''' ## Started running at 3am
    results_df, curve_output_list = fitness_by_iter('MIMIC', problem_FF, MIMIC_FF['max_iters'], MIMIC_FF['random_state']\
    , pop_size=MIMIC_FF['pop_size'], max_attempts=MIMIC_FF['max_attempts'], curve=True, keep_pct=MIMIC_FF['keep_pct'])
    results_df.to_csv(output_location + 'final_MIMIC_FF_attempt_3am.csv')


    results_df, curve_output_list = fitness_by_iter('MIMIC', problem_4P, MIMIC_4P['max_iters'], MIMIC_4P['random_state']\
    , pop_size=MIMIC_4P['pop_size'], max_attempts=MIMIC_4P['max_attempts'], curve=True, keep_pct=MIMIC_4P['keep_pct'])
    results_df.to_csv(output_location + 'final_MIMIC_4P_attempt_3am.csv')


    results_df, curve_output_list = fitness_by_iter('MIMIC', problem_KS, MIMIC_KS['max_iters'], MIMIC_KS['random_state']\
    , pop_size=MIMIC_KS['pop_size'], max_attempts=MIMIC_KS['max_attempts'], curve=True, keep_pct=MIMIC_KS['keep_pct'])
    results_df.to_csv(output_location + 'final_MIMIC_KS_attempt_3am.csv')


    results_df, curve_output_list = fitness_by_iter('MIMIC', problem_cont_peaks, MIMIC_CP['max_iters'], MIMIC_CP['random_state']\
    , pop_size=MIMIC_CP['pop_size'], max_attempts=MIMIC_CP['max_attempts'], curve=True, keep_pct=MIMIC_CP['keep_pct'])
    results_df.to_csv(output_location + 'final_MIMIC_CP_attempt_3am.csv')

    '''

    ## USED FOR GRID SEARCHING PARAMETERS FOR RO ON 3 PROBLEMS
    GA_params_dict = get_params_for_grid_search('GA', max_iters_list=[200])
    print("Here are my GA params for grid search: ", GA_params_dict)
    SA_params_dict = get_params_for_grid_search('SA',
                                                max_iters_list=max_iters_list)
    print("Here are my SA params for grid search: ", SA_params_dict)
    RHC_params_dict = get_params_for_grid_search('RHC',
                                                 max_iters_list=max_iters_list)
    print("Here are my RHC params for grid search: ", RHC_params_dict)
    MIMIC_params_dict = get_params_for_grid_search(
        'MIMIC', max_iters_list=max_iters_list)
    print("Here are my MIMIC params for grid search: ", MIMIC_params_dict)
    #grid_search_MIMIC = MIMIC_best_params(problem_TPS, MIMIC_params_dict, inverse_fitness=False)
    #grid_search_MIMIC.to_csv(output_location + 'grid_search_MIMIC.csv')
    '''
    grid_search_GA = GA_best_params(problem_FF, GA_params_dict, inverse_fitness=False)
    grid_search_GA.to_csv(output_location + prefix + 'grid_search_GA_FF_really.csv')
    print("finished GA")
    grid_search_MIMIC = MIMIC_best_params(problem_FF, MIMIC_params_dict, inverse_fitness=False)
    grid_search_MIMIC.to_csv(output_location + prefix + 'grid_search_MIMIC_FF_really.csv')
    '''
    print("finished MIMIC FF")

    print("Doing GA rn")
    #results_df, curve_output_list = fitness_by_iter('GA', problem_FF, GA_FF['max_iters'], GA_FF['random_state']\
    #, pop_size=GA_FF['pop_size'], max_attempts=GA_FF['max_attempts'], mutation_prob=GA_FF['mutation_prob'],curve=True)
    #results_df.to_csv(output_location + 'final_MIMIC_FF_attempt_1am.csv')
    print("finished GA")
    ''' GRID SEARCHING

    print("Starting grid search for RHC")
    grid_search_RHC = RHC_best_params(problem_TSP, RHC_params_dict, inverse_fitness=False)
    grid_search_RHC.to_csv(output_location + prefix +'grid_search_RHC_TSP.csv')
    grid_search_RHC = RHC_best_params(problem_FF, RHC_params_dict, inverse_fitness=False)
    grid_search_RHC.to_csv(output_location + prefix + 'grid_search_RHC_FF.csv')
    grid_search_RHC = RHC_best_params(problem_cont_peaks, RHC_params_dict, inverse_fitness=False)
    grid_search_RHC.to_csv(output_location + prefix + 'grid_search_RHC_cont_peaks.csv')
    grid_search_RHC = RHC_best_params(problem_4P, RHC_params_dict, inverse_fitness=False)
    grid_search_RHC.to_csv(output_location + prefix + 'grid_search_RHC_4P.csv')

    print("Starting grid search for SA")
    grid_search_SA = SA_best_params(problem_TSP, SA_params_dict, inverse_fitness=False)
    grid_search_SA.to_csv(output_location + prefix + 'grid_search_SA_TSP.csv')
    grid_search_SA = SA_best_params(problem_FF, SA_params_dict, inverse_fitness=False)
    grid_search_SA.to_csv(output_location + prefix + 'grid_search_SA_FF.csv')
    grid_search_SA = SA_best_params(problem_cont_peaks, SA_params_dict, inverse_fitness=False)
    grid_search_SA.to_csv(output_location + prefix + 'grid_search_SA_cont_peaks.csv')
    grid_search_SA = SA_best_params(problem_4P, SA_params_dict, inverse_fitness=False)
    grid_search_SA.to_csv(output_location + prefix + 'grid_search_SA_4P.csv')

    print("Starting grid search for GA")
    grid_search_GA = GA_best_params(problem_TSP, GA_params_dict, inverse_fitness=False)
    grid_search_GA.to_csv(output_location + prefix + 'grid_search_GA_TSP.csv')
    grid_search_GA = GA_best_params(problem_FF, GA_params_dict, inverse_fitness=False)
    grid_search_GA.to_csv(output_location + prefix + 'grid_search_GA_FF.csv')
    grid_search_GA = GA_best_params(problem_cont_peaks, GA_params_dict, inverse_fitness=False)
    grid_search_GA.to_csv(output_location + prefix + 'grid_search_GA_cont_peaks.csv')
    grid_search_GA = GA_best_params(problem_4P, GA_params_dict, inverse_fitness=False)
    grid_search_GA.to_csv(output_location + prefix + 'grid_search_GA_4P.csv')
    '''
    '''
    print("Starting grid search for MIMIC")
    grid_search_MIMIC = MIMIC_best_params(problem_FF, MIMIC_params_dict, inverse_fitness=False)
    grid_search_MIMIC.to_csv(output_location + prefix + 'grid_search_MIMIC_FF.csv')
    #grid_search_MIMIC = MIMIC_best_params(problem_cont_peaks, MIMIC_params_dict, inverse_fitness=False)
    #grid_search_MIMIC.to_csv(output_location + prefix + 'grid_search_MIMIC_cont_peaks.csv')
    grid_search_MIMIC = MIMIC_best_params(problem_4P, MIMIC_params_dict, inverse_fitness=False)
    grid_search_MIMIC.to_csv(output_location + prefix + 'grid_search_MIMIC_4P.csv')
    #grid_search_MIMIC = MIMIC_best_params(problem_TSP, MIMIC_params_dict, inverse_fitness=False)
    #grid_search_MIMIC.to_csv(output_location + 'grid_search_MIMIC_TSP.csv')
    print("Finished MIMIC grid searches")

    print("Starting grid search for Knapsack")
    #grid_search_MIMIC = MIMIC_best_params(problem_KS, MIMIC_params_dict, inverse_fitness=False)
    #grid_search_MIMIC.to_csv(output_location + prefix + 'grid_search_MIMIC_KS.csv')
    #grid_search_GA = GA_best_params(problem_KS, GA_params_dict, inverse_fitness=False)
    #grid_search_GA.to_csv(output_location + prefix + 'grid_search_GA_KS.csv')
    grid_search_SA = SA_best_params(problem_KS, SA_params_dict, inverse_fitness=False)
    grid_search_SA.to_csv(output_location + prefix + 'grid_search_SA_KS.csv')
    grid_search_RHC = RHC_best_params(problem_KS, RHC_params_dict, inverse_fitness=False)
    grid_search_RHC.to_csv(output_location + prefix + 'grid_search_RHC_KS.csv')
    '''

    ## Fitting MIMIC separately and with fewer iterations for all except the FF as run time is so long for MIMIC
    max = 128
    ''' MIMIC CURVE FOR CHARTS ##### Started (again) at 8am ######

    print("Fitting for MIMIC using the 'curve=True' functionality")
    print("First for KS")
    start_time_fit = time.perf_counter()
    a,b,curve_output = mlrose.mimic(problem_KS, pop_size=100, keep_pct=0.5, max_attempts=10, max_iters=128, curve=True\
    , random_state=0)
    end_time_fit = time.perf_counter()
    time_used = end_time_fit - start_time_fit
    df1, df2 = curve_to_df(curve_output, max)
    df2['time_to_128'] = time_used
    df1.to_csv(output_location+'MIMIC_KS_full_curve.csv')
    df2.to_csv(output_location+'MIMIC_KS_short_curve.csv')
    print("Finished KS")

    print("Next for 4 Peaks")
    start_time_fit = time.perf_counter()
    a,b,curve_output = mlrose.mimic(problem_4P, pop_size=150, keep_pct=0.2, max_attempts=10, max_iters=128, curve=True\
    , random_state=0)
    end_time_fit = time.perf_counter()
    time_used = end_time_fit - start_time_fit
    df1, df2 = curve_to_df(curve_output, max)
    df2['time_to_128'] = time_used
    df1.to_csv(output_location+'MIMIC_4P_full_curve.csv')
    df2.to_csv(output_location+'MIMIC_4P_short_curve.csv')
    print("Finished 4 Peaks")

    print("Next for 4 Peaks with 100 and 0.5")
    start_time_fit = time.perf_counter()
    a,b,curve_output = mlrose.mimic(problem_4P, pop_size=100, keep_pct=0.5, max_attempts=10, max_iters=128, curve=True\
    , random_state=0)
    end_time_fit = time.perf_counter()
    time_used = end_time_fit - start_time_fit
    df1, df2 = curve_to_df(curve_output, max)
    df2['time_to_128'] = time_used
    df1.to_csv(output_location+'MIMIC_4P_pop100_keep50_full_curve.csv')
    df2.to_csv(output_location+'MIMIC_4P_pop100_keep50_short_curve.csv')
    print("Finished 4 Peaks")

    print("Next for 4 Peaks with 100 and 0.2")
    start_time_fit = time.perf_counter()
    a,b,curve_output = mlrose.mimic(problem_4P, pop_size=100, keep_pct=0.2, max_attempts=10, max_iters=128, curve=True\
    , random_state=0)
    end_time_fit = time.perf_counter()
    time_used = end_time_fit - start_time_fit
    df1, df2 = curve_to_df(curve_output, max)
    df2['time_to_128'] = time_used
    df1.to_csv(output_location+'MIMIC_4P_pop100_keep20_full_curve.csv')
    df2.to_csv(output_location+'MIMIC_4P_pop100_keep20_short_curve.csv')
    print("Finished 4 Peaks")

    print("Next for 4 Peaks with 150 and 0.5")
    start_time_fit = time.perf_counter()
    a,b,curve_output = mlrose.mimic(problem_4P, pop_size=100, keep_pct=0.2, max_attempts=10, max_iters=128, curve=True\
    , random_state=0)
    end_time_fit = time.perf_counter()
    time_used = end_time_fit - start_time_fit
    df1, df2 = curve_to_df(curve_output, max)
    df2['time_to_128'] = time_used
    df1.to_csv(output_location+'MIMIC_4P_pop150_keep50_full_curve.csv')
    df2.to_csv(output_location+'MIMIC_4P_pop150_keep50_short_curve.csv')
    print("Finished 4 Peaks")

    print("Next for 4 Peaks Big")
    start_time_fit = time.perf_counter()
    a,b,curve_output = mlrose.mimic(problem_4P_big, pop_size=150, keep_pct=0.2, max_attempts=10, max_iters=128, curve=True\
    , random_state=0)
    end_time_fit = time.perf_counter()
    time_used = end_time_fit - start_time_fit
    df1, df2 = curve_to_df(curve_output, max)
    df2['time_to_128'] = time_used
    df1.to_csv(output_location+'MIMIC_4P_big_full_curve.csv')
    df2.to_csv(output_location+'MIMIC_4P_big_short_curve.csv')
    print("Finished 4 Peaks Big")

    print("Next for KS Small")
    start_time_fit = time.perf_counter()
    a,b,curve_output = mlrose.mimic(problem_KS_small, pop_size=100, keep_pct=0.5, max_attempts=10, max_iters=128, curve=True\
    , random_state=0)
    end_time_fit = time.perf_counter()
    time_used = end_time_fit - start_time_fit
    df1, df2 = curve_to_df(curve_output, max)
    df2['time_to_128'] = time_used
    df1.to_csv(output_location+'MIMIC_KS_small_full_curve.csv')
    df2.to_csv(output_location+'MIMIC_KS_small_short_curve.csv')
    print("Finished KS small")

    print("Next FF small")
    start_time_fit = time.perf_counter()
    a,b,curve_output = mlrose.mimic(problem_FF_small, pop_size=100, keep_pct=0.5, max_attempts=10, max_iters=128, curve=True\
    , random_state=0)
    end_time_fit = time.perf_counter()
    time_used = end_time_fit - start_time_fit
    df1, df2 = curve_to_df(curve_output, max)
    df2['time_to_128'] = time_used
    df1.to_csv(output_location+'MIMIC_FF_small_full_curve.csv')
    df2.to_csv(output_location+'MIMIC_KS_small_short_curve.csv')
    print("Finished FF Small")

    print("Next for 4 Peaks Small")
    start_time_fit = time.perf_counter()
    a,b,curve_output = mlrose.mimic(problem_4P_small, pop_size=150, keep_pct=0.2, max_attempts=10, max_iters=128, curve=True\
    , random_state=0)
    end_time_fit = time.perf_counter()
    time_used = end_time_fit - start_time_fit
    df1, df2 = curve_to_df(curve_output, max)
    df2['time_to_128'] = time_used
    df1.to_csv(output_location+'MIMIC_4P_small_full_curve.csv')
    df2.to_csv(output_location+'MIMIC_4P_small_short_curve.csv')
    print("Finished 4 Peaks Small")
    '''

    ### Now GA

    GA_FF = {
        'pop_size': 100,  #,1000],
        'mutation_prob': 0.1,
        'max_attempts': 30,
        'max_iters': max_iters_list_full,
        'random_state': rand_list_full
    }
    GA_KS = {
        'pop_size': 200,  #,1000],
        'mutation_prob': 0.2,
        'max_attempts': 30,
        'max_iters': max_iters_list_full,
        'random_state': rand_list_full
    }
    GA_4P = {
        'pop_size': 200,  #,1000],
        'mutation_prob': 0.5,
        'max_attempts': 30,
        'max_iters': max_iters_list_full,
        'random_state': rand_list_full
    }
    ''' More fitness by iteration calculations
    #results_df, curve_output_list = fitness_by_iter('GA', problem_FF, GA_FF['max_iters'], GA_FF['random_state']\
    #, pop_size=GA_FF['pop_size'], max_attempts=GA_FF['max_attempts'], curve=True, mutation_prob=GA_FF['mutation_prob'])
    #results_df.to_csv(output_location + 'final_GA_FF_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('GA', problem_FF_small, GA_FF['max_iters'], GA_FF['random_state']\
    , pop_size=GA_FF['pop_size'], max_attempts=GA_FF['max_attempts'], curve=True, mutation_prob=GA_FF['mutation_prob'])
    results_df.to_csv(output_location + 'final_GA_FF_small_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('GA', problem_FF_big, GA_FF['max_iters'], GA_FF['random_state']\
    , pop_size=GA_FF['pop_size'], max_attempts=GA_FF['max_attempts'], curve=True, mutation_prob=GA_FF['mutation_prob'])
    results_df.to_csv(output_location + 'final_GA_FF_big_attempt_8am.csv')



    #results_df, curve_output_list = fitness_by_iter('GA', problem_4P, GA_4P['max_iters'], GA_4P['random_state']\
    #, pop_size=GA_4P['pop_size'], max_attempts=GA_4P['max_attempts'], curve=True, mutation_prob=GA_4P['mutation_prob'])
    #results_df.to_csv(output_location + 'final_GA_4P_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('GA', problem_4P_big, GA_4P['max_iters'], GA_4P['random_state']\
    , pop_size=GA_4P['pop_size'], max_attempts=GA_4P['max_attempts'], curve=True, mutation_prob=GA_4P['mutation_prob'])
    results_df.to_csv(output_location + 'final_GA_4P_big_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('GA', problem_4P_small, GA_4P['max_iters'], GA_4P['random_state']\
    , pop_size=GA_4P['pop_size'], max_attempts=GA_4P['max_attempts'], curve=True, mutation_prob=GA_4P['mutation_prob'])
    results_df.to_csv(output_location + 'final_GA_4P_small_attempt_8am.csv')



    #results_df, curve_output_list = fitness_by_iter('GA', problem_KS, GA_KS['max_iters'], GA_KS['random_state']\
    #, pop_size=GA_KS['pop_size'], max_attempts=GA_KS['max_attempts'], curve=True, mutation_prob=GA_KS['mutation_prob'])
    #results_df.to_csv(output_location + 'final_GA_KS_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('GA', problem_KS_big, GA_KS['max_iters'], GA_KS['random_state']\
    , pop_size=GA_KS['pop_size'], max_attempts=GA_KS['max_attempts'], curve=True, mutation_prob=GA_KS['mutation_prob'])
    results_df.to_csv(output_location + 'final_GA_KS_big_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('GA', problem_KS_small, GA_KS['max_iters'], GA_KS['random_state']\
    , pop_size=GA_KS['pop_size'], max_attempts=GA_KS['max_attempts'], curve=True, mutation_prob=GA_KS['mutation_prob'])
    results_df.to_csv(output_location + 'final_GA_KS_small_attempt_8am.csv')

    '''

    ########### SA
    print("now doing SA")
    SA_4P = {
        'max_attempts': 10,
        'schedule': mlrose.GeomDecay(init_temp=100, decay=0.8),
        'max_iters': max_iters_list_full,
        'random_state': rand_list_full
    }

    SA_FF = {
        'max_attempts': 10,
        'schedule': mlrose.GeomDecay(init_temp=100, decay=0.8),
        'max_iters': max_iters_list_full,
        'random_state': rand_list_full
    }

    results_df, curve_output_list = fitness_by_iter('SA', problem_4P, SA_4P['max_iters'], SA_4P['random_state']\
    , schedule=SA_4P['schedule'], max_attempts=SA_4P['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_SA_4P_attempt_8am.csv')

    results_df, curve_output_list = fitness_by_iter('SA', problem_4P_big, SA_4P['max_iters'], SA_4P['random_state']\
    , schedule=SA_4P['schedule'], max_attempts=SA_4P['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_SA_4P_big_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('SA', problem_4P, SA_4P['max_iters'], SA_4P['random_state']\
    , schedule=SA_4P['schedule'], max_attempts=SA_4P['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_SA_4P_small_attempt_8am.csv')
    ''' more fitness by iteration calculations
    #results_df, curve_output_list = fitness_by_iter('SA', problem_FF, SA_FF['max_iters'], SA_FF['random_state']\
    #, schedule=SA_FF['schedule'], max_attempts=SA_FF['max_attempts'], curve=True)
    #results_df.to_csv(output_location + 'final_SA_FF_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('SA', problem_FF_big, SA_FF['max_iters'], SA_FF['random_state']\
    , schedule=SA_FF['schedule'], max_attempts=SA_FF['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_SA_FF_big_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('SA', problem_FF_small, SA_FF['max_iters'], SA_FF['random_state']\
    , schedule=SA_FF['schedule'], max_attempts=SA_FF['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_SA_FF_small_attempt_8am.csv')


    SA_4P = {
    'max_attempts':10,
    'schedule':mlrose.GeomDecay(init_temp=100, decay=0.8),
    'max_iters':max_iters_list_full,
    'random_state':rand_list_full
    }

    results_df, curve_output_list = fitness_by_iter('KS', problem_4P, SA_4P['max_iters'], SA_4P['random_state']\
    , schedule=SA_4P['schedule'], max_attempts=SA_4P['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_SA_4P_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('SA', problem_4P_big, SA_4P['max_iters'], SA_4P['random_state']\
    , schedule=SA_4P['schedule'], max_attempts=SA_4P['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_SA_4P_big_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('SA', problem_4P, SA_4P['max_iters'], SA_4P['random_state']\
    , schedule=SA_4P['schedule'], max_attempts=SA_4P['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_SA_4P_small_attempt_8am.csv')
    '''
    print("picking up where I left off on making the final curves..")

    SA_KS = {
        'max_attempts': 10,
        'schedule': mlrose.GeomDecay(init_temp=1000, decay=0.99),
        'max_iters': max_iters_list_full,
        'random_state': rand_list_full
    }
    ''' more fitness by iteration calculations
    results_df, curve_output_list = fitness_by_iter('SA', problem_KS, SA_KS['max_iters'], SA_KS['random_state']\
    , schedule=SA_KS['schedule'], max_attempts=SA_KS['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_SA_KS_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('SA', problem_KS_big, SA_KS['max_iters'], SA_KS['random_state']\
    , schedule=SA_KS['schedule'], max_attempts=SA_KS['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_SA_KS_big_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('SA', problem_KS_small, SA_KS['max_iters'], SA_KS['random_state']\
    , schedule=SA_KS['schedule'], max_attempts=SA_KS['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_SA_KS_small_attempt_8am.csv')
    '''

    RHC_KS = {
        'max_attempts': 50,
        'restarts': 20,
        'max_iters': max_iters_list_full,
        'random_state': rand_list_full
    }
    '''
    results_df, curve_output_list = fitness_by_iter('RHC', problem_KS, RHC_KS['max_iters'], RHC_KS['random_state']\
    , restarts=RHC_KS['restarts'], max_attempts=RHC_KS['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_RHS_KS_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('RHC', problem_KS_big, RHC_KS['max_iters'], RHC_KS['random_state']\
    , restarts=RHC_KS['restarts'], max_attempts=RHC_KS['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_RHS_KS_big_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('RHC', problem_KS_small, RHC_KS['max_iters'], RHC_KS['random_state']\
    , restarts=RHC_KS['restarts'], max_attempts=RHC_KS['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_RHS_KS_small_attempt_8am.csv')
    '''
    RHC_FF = {
        'max_attempts': 50,
        'restarts': 20,
        'max_iters': max_iters_list_full,
        'random_state': rand_list_full
    }
    '''
    results_df, curve_output_list = fitness_by_iter('RHC', problem_FF, RHC_FF['max_iters'], RHC_FF['random_state']\
    , restarts=RHC_FF['restarts'], max_attempts=RHC_FF['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_RHS_FF_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('RHC', problem_FF_big, RHC_FF['max_iters'], RHC_FF['random_state']\
    , restarts=RHC_FF['restarts'], max_attempts=RHC_FF['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_RHS_FF_big_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('RHC', problem_FF_small, RHC_FF['max_iters'], RHC_FF['random_state']\
    , restarts=RHC_FF['restarts'], max_attempts=RHC_FF['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_RHS_FF_small_attempt_8am.csv')
    '''

    RHC_4P = {
        'max_attempts': 50,
        'restarts': 20,
        'max_iters': max_iters_list_full,
        'random_state': rand_list_full
    }
    '''
    results_df, curve_output_list = fitness_by_iter('RHC', problem_4P, RHC_4P['max_iters'], RHC_4P['random_state']\
    , restarts=RHC_4P['restarts'], max_attempts=RHC_4P['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_RHS_4P_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('RHC', problem_4P_small, RHC_4P['max_iters'], RHC_4P['random_state']\
    , restarts=RHC_4P['restarts'], max_attempts=RHC_4P['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_RHS_4P_small_attempt_8am.csv')
    results_df, curve_output_list = fitness_by_iter('RHC', problem_4P_big, RHC_4P['max_iters'], RHC_4P['random_state']\
    , restarts=RHC_4P['restarts'], max_attempts=RHC_4P['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_RHS_4P_big_attempt_8am.csv')
    '''

    ## where it stopped
    print("I will now make the complexity curves for other algos")
    SA_4P_hacked = {
        'max_attempts': 10,
        'schedule': mlrose.GeomDecay(init_temp=100, decay=0.99),
        'max_iters': max_iters_list_full,
        'random_state': rand_list_full
    }
    '''
    results_df, curve_output_list = fitness_by_iter('SA', problem_4P, SA_4P['max_iters'], SA_4P['random_state']\
    , schedule=SA_4P_hacked['schedule'], max_attempts=SA_4P['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_SA_4P_decay_99.csv')

    results_df, curve_output_list = fitness_by_iter('SA', problem_4P, SA_4P['max_iters'], SA_4P['random_state']\
    , schedule=mlrose.GeomDecay(init_temp=1, decay=0.8), max_attempts=SA_4P['max_attempts'], curve=True)
    results_df.to_csv(output_location + 'final_SA_4P_T_1_decay_80.csv')

    results_df, curve_output_list = fitness_by_iter('GA', problem_KS, GA_KS['max_iters'], GA_KS['random_state']\
    , pop_size=GA_KS['pop_size'], max_attempts=GA_KS['max_attempts'], curve=True, mutation_prob=0.1)
    results_df.to_csv(output_location + 'final_GA_KS_mutation_01.csv')
    '''
    results_df, curve_output_list = fitness_by_iter('GA', problem_KS, GA_KS['max_iters'], GA_KS['random_state']\
    , pop_size=100, max_attempts=GA_KS['max_attempts'], curve=True, mutation_prob=0.2)
    results_df.to_csv(output_location + 'final_GA_KS_mutation_02_pop_100.csv')

    ## Need a few more MIMIC chart inputs
    #print("Need a few more MIMIC chart inputs, so I will now make those")
    #print("Next FF p=100 keep=0.2")
    ''' MIMIC inputs for charts
Exemple #23
0
    plt.ylabel(y)
    plt.title(title)
    length = len(curve)
    plt.plot(range(length), curve, label=y, lw=2)
    plt.legend(loc="best")
    plt.savefig(name)


# Problem definition
length = 35
eval_count = 0

# Initialize custom fitness function object
q_fitness = mlrh.FourPeaks(t_pct=0.1)
# q_fitness = mlrose.Queens()
prob = mlrh.DiscreteOpt(length=40, fitness_fn=q_fitness, maximize=True)
experiment_name = "queen_prob"
output_directory = "queen"

# SA
sa = mlrh.SARunner(problem=prob,
                   experiment_name=experiment_name,
                   output_directory=output_directory,
                   seed=random_state,
                   max_attempts=200,
                   iteration_list=[2000],
                   temperature_list=[0.01, 0.1, 1, 10, 100, 1000],
                   decay_list=[mlrh.GeomDecay, mlrh.ExpDecay, mlrh.ArithDecay])
sa_stats, sa_curve = sa.run()

columns = ['Time', 'Fitness', 'Temperature', 'schedule_type']
Exemple #24
0
def method_compare():
    global count
    count = 0
    fitness_obj = mlrose.CustomFitness(n_peak_fit)
    opt = mlrose.DiscreteOpt(1, fitness_obj, maximize=True, max_val=10000)

    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 n-peak problem:', best_state_climb)
    print('hill climbing best fitness for n-peak problem:', best_fitness_climb)
    print('hill climbing fitting curve for n-peak 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 n-peak problem:', best_state_ann)
    print('simulated annealing best fitness for n-peak problem:',
          best_fitness_ann)
    print('simulated annealing fitting curve for n-peak 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=20, mutation_prob=0.5, curve=True)
    print('---------------------genetic alg----------------------------')
    print('genetic algorithm best state for n-peak problem:', best_state_ga)
    print('genetic algorithm best fitnees for n-peak problem:',
          best_fitness_ga)
    print('genetic algorithm fitness curve for n-peak 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=20, curve=True)
    print('------------------------mimic-------------------------------')
    print('mimic best state for n-peak problem:', best_state_mimic)
    print('mimic best fitness value for n-peak problem:', best_fitness_mimic)
    print('mimic curve for n-peak problem:', fitness_curve_mimic)
    print('number of fitness calls 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(0, 5)
    plt.title('random hill climb')
    plt.subplot(222)
    plt.plot(fitness_curve_ann)
    plt.ylabel('fitness')
    plt.xlabel('num_iter')
    plt.ylim(0, 5)
    plt.title('simulated annealing')
    plt.subplot(223)
    plt.plot(fitness_curve_ga)
    plt.ylim(0, 5)
    plt.ylabel('fitness')
    plt.xlabel('num_iter')
    plt.title('genetic algorithm')
    plt.subplot(224)
    plt.plot(fitness_curve_mimic)
    plt.ylim(0, 5)
    plt.title('mimic')
    plt.ylabel('fitness')
    plt.xlabel('num_iter')
    plt.show()
Exemple #25
0
def one_max():
    print('One Max')
    sa = []
    rhc = []
    ga = []
    mim = []

    input_sizes = [100]

    for i in input_sizes:
        state = np.array([np.random.randint(0, 2) for i in range(i)])
        # state = np.zeros(i)
        fitness = mlr.OneMax()
        problem = mlr.DiscreteOpt(length=i,
                                  fitness_fn=fitness,
                                  maximize=True,
                                  max_val=2)

        best_state, best_fitness, fitness_curve, time = randomized_hill_climb(
            problem, state, 100, 600)
        rhc.append((best_fitness, fitness_curve, time))

        best_state, best_fitness, fitness_curve, time = simulated_annealing(
            problem, state, 100, 600)
        sa.append((best_fitness, fitness_curve, time))

        best_state, best_fitness, fitness_curve, time = genetic_algorithm(
            problem, state, 100, 600)
        ga.append((best_fitness, fitness_curve, time))

        best_state, best_fitness, fitness_curve, time = mimic(
            problem, state, 100, 600)
        mim.append((best_fitness, fitness_curve, time))

    plot_data([i + 1 for i in range(len(rhc[0][1]))],
              rhc[0][1],
              title="OneMax (Input Size = " + str(len(state)) + ")",
              x_label="Iterations",
              y_label="Fitness Score",
              color="blue",
              label='RHC')

    plot_data([i + 1 for i in range(len(sa[0][1]))],
              sa[0][1],
              title="OneMax (Input Size = " + str(len(state)) + ")",
              x_label="Iterations",
              y_label="Fitness Score",
              color="orange",
              label='SA')

    plot_data([i + 1 for i in range(len(ga[0][1]))],
              ga[0][1],
              title="OneMax (Input Size = " + str(len(state)) + ")",
              x_label="Iterations",
              y_label="Fitness Score",
              color="green",
              label='GA')

    plot_data([i + 1 for i in range(len(mim[0][1]))],
              mim[0][1],
              title="OneMax (Input Size = " + str(len(state)) + ")",
              x_label="Iterations",
              y_label="Fitness Score",
              color="red",
              label='MIMIC')

    title = 'One Max'

    plt.savefig('output/' + title + '.png')
    plt.close()
Exemple #26
0
    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)

if __name__ == "__main__":
    # Initialize fitness function object using Custom function
    fitness_fn = mlrose_hiive.CustomFitness(ks_fitness_fn)
    # Define optimization problem object
    N = 10
    problem = mlrose_hiive.DiscreteOpt(length=N, fitness_fn=fitness_fn, maximize=True, max_val=2)
    max_iters = 1500
    iterations = range(0, max_iters, 50)
    random_seed = 1
    graph_file = 'ks_'
    graph_title = 'Knapsack Problem - '
    print('***************Knapsack Optimization Problem*****************')
    # Random hill climbing
    print('--------------Random Hill Climbing---------------')
    rhc(problem, iterations, random_seed, graph_file, graph_title)
    # simulate annealing
    print('--------------Simulated Annealing---------------')
    sa(problem, iterations, random_seed, graph_file, graph_title)
    # Genetic Algorithm
    print('--------------Genetic Algorithm---------------')
    ga(problem,iterations,random_seed, graph_file, graph_title)
Exemple #27
0
# -*- coding: utf-8 -*-

import mlrose_hiive as mlrose
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from time import process_time

print("Running OneMax...")

fitness = mlrose.OneMax()
problem = mlrose.DiscreteOpt(100, fitness)

RANDOM_SEED = 42
MAX_ATTEMPTS = 100

#%% tuning for SA
curve_list = []
decays = [0.999, 0.99, 0.9]
for d in decays:
    schedule = mlrose.GeomDecay(decay=d)
    _, _, curve = mlrose.simulated_annealing(
        problem,
        schedule=schedule,
        max_attempts=MAX_ATTEMPTS,
        max_iters=500,
        curve=True,
        random_state=RANDOM_SEED,
    )
    curve_list.append(curve)
Exemple #28
0
gen_times=[]
gen_scores=[]
mimic_times=[]
mimic_scores=[]
for i in range(10, 51, 10):
    if i==0:
        continue
    ex = {"weights": [random.randint(1, 20) for i in range(i)], "values": [random.randint(1, 10) for i in range(i)], "state": np.array([random.randint(0, 2) for i in range(i)])}
    input_sizes.append(i)
    weights = ex['weights']
    values = ex['values']
    state = ex['state']
    max_weight_pct = 0.6
    fitness = mlrose_hiive.Knapsack(weights, values, max_weight_pct)
    fitness.evaluate(state)
    problem = mlrose_hiive.DiscreteOpt(length = len(state), fitness_fn = fitness, maximize = True, max_val = int(max(state))+1)
    times = []
    best_scores = []

    start_time = time.time()
    best_state, best_fitness, fitness_curve = sa(problem,state, 30, 1000)
    elapsed_time = time.time() - start_time
    print(elapsed_time)
    times.append(elapsed_time*1000)
    best_scores.append(best_fitness)
    sa_times.append(elapsed_time*1000)
    sa_scores.append(best_fitness)
    plt.close()
    
    plot_data([i+1 for i in range(len(fitness_curve))], fitness_curve, title="Evaluations Required to Maximize Knapsack (Input Size = "+str(len(state))+")", x_label="Evaluations", y_label="Fitness Score", color="blue", label='Simulated annealing')
Exemple #29
0
def runPart1(savePath):
    fitness = mlrose.FourPeaks(t_pct=0.15)
    init_state = None
    fourPeaksProblem = mlrose.DiscreteOpt(length=12,
        fitness_fn=fitness, maximize=True, max_val=2)

    part1_1 = Part1(name='Four Peaks', fitness=fitness,
                    problem=fourPeaksProblem, init_state=init_state)
    part1_1.runAll(savePath)

    fitness = mlrose.Queens()
    init_state = None
    eightQueensProblem = mlrose.DiscreteOpt(length=8,
        fitness_fn=fitness, maximize=False, max_val=8)
    part1_2 = Part1(name='Eight Queens', fitness=fitness,
                    problem=eightQueensProblem, init_state=init_state)
    part1_2.runAll(savePath)

    fitness = mlrose.SixPeaks(t_pct=0.15)
    init_state = None
    sixPeaksProblem = mlrose.DiscreteOpt(length=11,
        fitness_fn=fitness, maximize=True, max_val=2)
    part1_4 = Part1(name='Six Peaks', fitness=fitness,
                    problem=sixPeaksProblem, init_state=init_state)
    part1_4.runAll(savePath)

    fitness = mlrose.FlipFlop()
    init_state = None
    flipFlopProblem = mlrose.DiscreteOpt(length=7,
        fitness_fn=fitness, maximize=True, max_val=2)
    part1_5 = Part1(name='Flip Flop - 7', fitness=fitness,
                    problem=flipFlopProblem, init_state=init_state)
    part1_5.runAll(savePath)


    fitness = mlrose.FlipFlop()
    init_state = None
    flipFlopProblem = mlrose.DiscreteOpt(length=100,
        fitness_fn=fitness, maximize=True, max_val=2)
    part1_5 = Part1(name='Flip Flop - 100', fitness=fitness,
                    problem=flipFlopProblem, init_state=init_state)
    part1_5.runAll(savePath)

    fitness = mlrose.Queens()
    init_state = None
    eightQueensProblem = mlrose.DiscreteOpt(length=80,
        fitness_fn=fitness, maximize=False, max_val=8)
    part1_2 = Part1(name='Eighty Queens', fitness=fitness,
                    problem=eightQueensProblem, init_state=init_state)
    part1_2.runAll(savePath)

    fitness = mlrose.FlipFlop()
    init_state = None
    flipFlopProblem = mlrose.DiscreteOpt(length=15,
        fitness_fn=fitness, maximize=True, max_val=2)
    part1_5 = Part1(name='Flip Flop - 15', fitness=fitness,
                    problem=flipFlopProblem, init_state=init_state)
    part1_5.runAll(savePath)


    edges = [(0, 1), (0, 2), (0, 4), (1, 3), (2, 0), (2, 3), (3, 4)]
    fitness = mlrose.MaxKColor(edges)
    init_state = None
    maxKColorsProblem = mlrose.DiscreteOpt(length=7,
        fitness_fn=fitness, maximize=False, max_val=2)
    part1_3 = Part1(name='Max-K Color', fitness=fitness,
                    problem=maxKColorsProblem, init_state=init_state)
    part1_3.runAll(savePath)

    # =============================================================
    #  Source - Tutorial from MLRose Docs
    #  https://mlrose.readthedocs.io/en/stable/source/tutorial2.html
    # 
    # =============================================================
    # Create list of city coordinates
    coords_list = [(1, 1), (4, 2), (5, 2), (6, 4), (4, 4), (3, 6), (1, 5), (2, 3)]

    # Initialize fitness function object using coords_list
    fitness_coords = mlrose.TravellingSales(coords = coords_list)

    # Create list of distances between pairs of cities
    dist_list = [(0, 1, 3.1623), (0, 2, 4.1231), (0, 3, 5.8310), (0, 4, 4.2426), \
                (0, 5, 5.3852), (0, 6, 4.0000), (0, 7, 2.2361), (1, 2, 1.0000), \
                (1, 3, 2.8284), (1, 4, 2.0000), (1, 5, 4.1231), (1, 6, 4.2426), \
                (1, 7, 2.2361), (2, 3, 2.2361), (2, 4, 2.2361), (2, 5, 4.4721), \
                (2, 6, 5.0000), (2, 7, 3.1623), (3, 4, 2.0000), (3, 5, 3.6056), \
                (3, 6, 5.0990), (3, 7, 4.1231), (4, 5, 2.2361), (4, 6, 3.1623), \
                (4, 7, 2.2361), (5, 6, 2.2361), (5, 7, 3.1623), (6, 7, 2.2361)]

    # Initialize fitness function object using dist_list
    fitness_dists = mlrose.TravellingSales(distances = dist_list)

    # Define optimization problem object
    problem_fit = mlrose.TSPOpt(length = 8, fitness_fn = fitness_coords, maximize=False)

    coords_list = [(1, 1), (4, 2), (5, 2), (6, 4), (4, 4), (3, 6), (1, 5), (2, 3)]

    # Define optimization problem object
    problem_no_fit = mlrose.TSPOpt(length = 8, coords = coords_list, maximize=False)

    part1_6 = Part1(name='TSP', fitness=coords_list,
                    problem=problem_no_fit, init_state=None)
    part1_6.runAll(savePath)

    # Knapsack
    weights = np.random.randint(2, high=20, size=50)
    values = np.random.randint(2, high=100, size=50)
    max_weight_pct = 0.8
    fitness = mlrose.Knapsack(weights, values, max_weight_pct)
    knapsackProblem = mlrose.DiscreteOpt(length=50,
        fitness_fn=fitness, maximize=False, max_val=2)

    part1_7 = Part1(name='Knapsack', fitness=fitness,
                    problem=knapsackProblem, init_state=None)
    part1_7.runAll(savePath)
#K color
#TODO make Australia map
N_K = 1
base_edges = [(0, 1), (0, 2), (1, 3), (2, 3), (2, 4), (2, 5), (3, 4),
              (4, 5)]  #Australia
edges = []
for au_idx in range(N_K):
    for edge in base_edges:
        edge = (edge[0] + 5 * au_idx, edge[1] + 5 * au_idx)
        edges.append(edge)

fitness_k = mlrose_hiive.MaxKColor(edges)
# init_state = np.array([0, 1, 0, 1, 1])
K_problem = mlrose_hiive.DiscreteOpt(length=max(max(edges)) + 1,
                                     fitness_fn=fitness_k,
                                     maximize=False,
                                     max_val=3)
fitness_k.evaluate(K_problem.state)

# Define decay schedule (sim annealing only)
schedule = mlrose_hiive.ExpDecay()

#8-Queens
fitness_Q = mlrose_hiive.Queens()
N_Q = 8
#OPti_object
# Q_problem = mlrose_hiive.DiscreteOpt(length = 8, fitness_fn = fitness_Q, maximize=False, max_val=8)

# timeit.timeit(fitness_Q.evaluate(Q_rand_state), number=100000)