GA_fitness_curve = best_fitness_curve

# print("----------------------------------")
# print("Genetic algorithm")
# print(best_state)
# print(best_fitness)
# print(fitness_curve)
# print("----------------------------------")

# MIMIC
t_bef = time.time()
best_state, best_fitness, best_fitness_curve = mlrose.mimic(
    problem,
    pop_size=800,
    keep_pct=0.2,
    max_attempts=10,
    max_iters=1000,
    curve=True,
    fast_mimic=True,
    random_state=RANDOM_STATE)
t_aft = time.time()
clock_time[3] = t_aft - t_bef
MIMIC_fitness_curve = best_fitness_curve

# print("----------------------------------")
# print("MIMIC")
# print(best_state)
# print(best_fitness)
# print(fitness_curve)
# print("----------------------------------")
Exemplo n.º 2
0
def process_cp_with_four_algo(seed, problem):
    question = "Continue Peaks"
    iterations = [1, 500, 1000, 1250, 1500, 1750, 2000, 2250, 2500, 2750, 3000, 3500, 4000]

    fitness_list = []
    mean_fitness_list = []
    mean_time_list = []

    #===================================================================================
    # =============================  R H C==============================================
    # ==================================================================================

    fitness_score_RHC = []
    time_score_RHC = []

    for iter in iterations:
        start_time = time.time()
        best_state, best_fitness = mlrose.random_hill_climb(problem,
                                                            max_attempts=10,
                                                            max_iters=iter,
                                                            random_state=seed)
        end_time = time.time()

        time_total = end_time - start_time
        fitness_score_RHC.append(best_fitness)
        time_score_RHC.append(time_total)

    mean_fitness_list.append(np.mean(fitness_score_RHC))
    mean_time_list.append(np.mean(time_score_RHC))
    fitness_list.append(fitness_score_RHC)
    print("RHC Good")

    #===================================================================================
    # =============================  G A  ==============================================
    # ==================================================================================

    fitness_score_GA = []
    time_score_GA = []

    for iter in iterations:
        start_time = time.time()
        best_state, best_fitness = mlrose.genetic_alg(problem,
                                                      max_attempts=10,
                                                      max_iters=iter,
                                                      random_state=seed,
                                                      pop_size=80,
                                                      mutation_prob=0.1)
        end_time = time.time()

        time_total = end_time - start_time
        fitness_score_GA.append(best_fitness)
        time_score_GA.append(time_total)

    mean_fitness_list.append(np.mean(fitness_score_GA))
    mean_time_list.append(np.mean(time_score_GA))
    fitness_list.append(fitness_score_GA)

    print("GA Good")

    # ===================================================================================
    # =============================  S A  ==============================================
    # ==================================================================================

    fitness_score_SA = []
    time_score_SA = []

    for iter in iterations:
        start_time = time.time()
        schedule2 = mlrose.GeomDecay(init_temp=500, decay=0.99)
        best_state, best_fitness = mlrose.simulated_annealing(problem,
                                                              max_attempts=10,
                                                              max_iters=iter,
                                                              random_state=seed,
                                                              schedule=schedule2)
        print(iter)
        end_time = time.time()
        time_total = end_time - start_time
        fitness_score_SA.append(best_fitness)
        time_score_SA.append(time_total)

    print("SA GOOD")
    mean_fitness_list.append(np.mean(fitness_score_SA))
    mean_time_list.append(np.mean(time_score_SA))
    fitness_list.append(fitness_score_SA)

    # ===================================================================================
    # =============================  MIMIC  ==============================================
    # ==================================================================================
    print("mimic in ")
    fitness_score_MIMIC = []
    time_score_MIMIC = []

    for iter in iterations:
        start_time = time.time()
        best_state, best_fitness = mlrose.mimic(problem,
                                                max_attempts=10,
                                                max_iters=iter,
                                                random_state=seed,
                                                pop_size=100,
                                                keep_pct=0.1)
        print(iter)
        end_time = time.time()
        time_total = end_time - start_time
        fitness_score_MIMIC.append(best_fitness)
        time_score_MIMIC.append(time_total)



    mean_fitness_list.append(np.mean(fitness_score_MIMIC))
    mean_time_list.append(np.mean(time_score_MIMIC))
    fitness_list.append(fitness_score_MIMIC)
    print("mimic out ")
    # ===================================================================================
    # =============================  PLOT THE FINAL CURVE  ==============================================
    # ==================================================================================
    print("curve in")
    algorithms = ["RHC", "GA", "SA", "MIMIC"]
    algorithm_compare(fitness_list, algorithms, question, iterations)
    mean_time_compare(mean_time_list, algorithms, question)
    score_compare(mean_fitness_list, algorithms, question)
    print("curve out")
Exemplo n.º 3
0
def __discrete_bit_size_problems(problem,
                                 algorithm,
                                 length,
                                 max_iter,
                                 max_attempt,
                                 init_state,
                                 edges=None,
                                 coords=None):
    if problem == 'fourpeaks':
        __fit = mlrose.FourPeaks()
        __problem = mlrose.DiscreteOpt(length=length,
                                       fitness_fn=__fit,
                                       maximize=True,
                                       max_val=2)
    elif problem == 'kcolor':
        __fit = mlrose.MaxKColor(edges=edges)
        __problem = mlrose.DiscreteOpt(length=length,
                                       fitness_fn=__fit,
                                       maximize=True)
    elif problem == 'flipflop':
        __fit = mlrose.OneMax()
        __problem = mlrose.DiscreteOpt(length=length,
                                       fitness_fn=__fit,
                                       maximize=True,
                                       max_val=2)
    elif problem == 'continouspeaks':
        __fit = mlrose.ContinuousPeaks()
        __problem = mlrose.DiscreteOpt(length=length,
                                       fitness_fn=__fit,
                                       maximize=True,
                                       max_val=2)
    elif problem == 'travellingsales':
        __fit = mlrose.TravellingSales(coords=coords)
        __problem = mlrose.TSPOpt(length=length,
                                  fitness_fn=__fit,
                                  maximize=False)

    if algorithm == 'random_hill_climb':
        start_time = time.time()
        best_state, best_fitness, best_curve = mlrose.random_hill_climb(
            __problem,
            max_iters=max_iter,
            max_attempts=max_attempt,
            init_state=init_state,
            curve=True)
        end_time = time.time() - start_time
    elif algorithm == 'simulated_annealing':
        start_time = time.time()
        best_state, best_fitness, best_curve = mlrose.simulated_annealing(
            __problem,
            max_iters=max_iter,
            max_attempts=max_attempt,
            init_state=init_state,
            curve=True)
        end_time = time.time() - start_time
    elif algorithm == 'genetic_alg':
        start_time = time.time()
        best_state, best_fitness, best_curve = mlrose.genetic_alg(
            __problem,
            max_iters=max_iter,
            max_attempts=max_attempt,
            curve=True)
        end_time = time.time() - start_time
    elif algorithm == 'mimic':
        start_time = time.time()
        best_state, best_fitness, best_curve = mlrose.mimic(
            __problem,
            max_iters=max_iter,
            max_attempts=max_attempt,
            curve=True)
        end_time = time.time() - start_time

    return best_fitness, end_time, best_curve
Exemplo n.º 4
0
def process_tsp_with_four_algo(coor, seed, problem):

    iterations = [1, 10, 20, 30, 40, 50, 60, 70, 80, 90]
    iterations.extend([i for i in range(100, 3000, 50)])

    fitness_list = []
    mean_fitness_list = []
    mean_time_list = []

    #===================================================================================
    # =============================  R H C==============================================
    # ==================================================================================

    fitness_score_RHC = []
    time_score_RHC = []

    for iter in iterations:
        start_time = time.time()
        best_state, best_fitness = mlrose.random_hill_climb(problem,
                                                            max_attempts=10,
                                                            max_iters=iter,
                                                            random_state=seed)
        end_time = time.time()

        time_total = end_time - start_time
        fitness_score_RHC.append(best_fitness)
        time_score_RHC.append(time_total)

    mean_fitness_list.append(np.mean(fitness_score_RHC))
    mean_time_list.append(np.mean(time_score_RHC))
    fitness_list.append(fitness_score_RHC)

    #===================================================================================
    # =============================  G A  ==============================================
    # ==================================================================================

    fitness_score_GA = []
    time_score_GA = []

    for iter in iterations:
        start_time = time.time()
        best_state, best_fitness = mlrose.genetic_alg(problem,
                                                      max_attempts=10,
                                                      max_iters=iter,
                                                      random_state=seed,
                                                      pop_size=10,
                                                      mutation_prob=0.1)
        end_time = time.time()

        time_total = end_time - start_time
        fitness_score_GA.append(best_fitness)
        time_score_GA.append(time_total)

    mean_fitness_list.append(np.mean(fitness_score_GA))
    mean_time_list.append(np.mean(time_score_GA))
    fitness_list.append(fitness_score_GA)

    # ===================================================================================
    # =============================  S A  ==============================================
    # ==================================================================================

    fitness_score_SA = []
    time_score_SA = []

    for iter in iterations:
        start_time = time.time()
        schedule = mlrose.GeomDecay(init_temp=10, decay=0.5)
        best_state, best_fitness = mlrose.simulated_annealing(
            problem,
            max_attempts=10,
            max_iters=iter,
            random_state=seed,
            schedule=schedule)
        end_time = time.time()
        time_total = end_time - start_time
        fitness_score_SA.append(best_fitness)
        time_score_SA.append(time_total)

    mean_fitness_list.append(np.mean(fitness_score_SA))
    mean_time_list.append(np.mean(time_score_SA))
    fitness_list.append(fitness_score_SA)

    # ===================================================================================
    # =============================  MIMIC  ==============================================
    # ==================================================================================

    fitness_score_MIMIC = []
    time_score_MIMIC = []

    for iter in iterations:
        start_time = time.time()
        best_state, best_fitness = mlrose.mimic(problem,
                                                max_attempts=10,
                                                max_iters=iter,
                                                random_state=seed,
                                                pop_size=10)
        end_time = time.time()
        time_total = end_time - start_time
        fitness_score_MIMIC.append(best_fitness)
        time_score_MIMIC.append(time_total)

    mean_fitness_list.append(np.mean(fitness_score_MIMIC))
    mean_time_list.append(np.mean(time_score_MIMIC))
    fitness_list.append(fitness_score_MIMIC)

    # ===================================================================================
    # =============================  PLOT THE FINAL CURVE  ==============================================
    # ==================================================================================

    algorithms = ["RHC", "GA", "SA", "MIMIC"]
    algorithm_compare(fitness_list, algorithms, question, iterations)
    mean_time_compare(mean_time_list, algorithms, question)
    score_compare(mean_fitness_list, algorithms, question)
Exemplo n.º 5
0
	ideal_pop_size = mmc_df_run_stats[['Population Size']].iloc[mmc_df_run_stats[['Fitness']].idxmax()]  # from the output of the experiment above
	return mmc_df_run_stats, mmc_df_run_curves, ideal_keep_prcnt, ideal_pop_size

# Done on a complex example then  hard coded optimized parameter value(s).
# mmc_df_run_stats, mmc_df_run_curves, ideal_keep_prcnt, ideal_pop_size = opt_mimic_params()

ideal_keep_prcnt = 0.5 # this came from the results of the experiment commented out, above.
ideal_pop_size_mimic = 100 # this came from the results of the experiment commented out, above.

mimic_best_state = []
mimic_best_fitness = []
mimic_convergence_time = []
for iter in iterations_range:
	start_time = timeit.default_timer()
	best_state, best_fitness, curve = mlrose.mimic(problem=problem, keep_pct=ideal_keep_prcnt,
											max_attempts=5000, max_iters=iter,
											pop_size=ideal_pop_size_mimic, curve=True)
	end_time = timeit.default_timer()
	convergence_time = (end_time - start_time)  # seconds
	mimic_best_state.append(best_state)
	mimic_best_fitness.append(best_fitness)
	mimic_convergence_time.append(convergence_time)

print('The fitness at the best state found using MIMIC is: ', max(mimic_best_fitness))

#======= Plots for all individual learners==========#
# Random Hill Climbing
fig1, ax1 = plt.subplots()
ax1.title.set_text("%s Flipflop, Tuned Random Hill Climbing" %(num_bits))
ax2 = ax1.twinx()
ax1.plot(iterations_range, rhc_best_fitness, 'r-')
    run_result = (complexity, "GA", run_time, best_fitness, len(curve),
                  func_calls)
    print(run_result)
    results_list.append(run_result)

    # MIMIC
    mimic_max_attempts = 500
    mimic_max_iters = np.inf
    mimic_pop_size = 2000
    mimic_keep_pct = 0.2
    start_time = time.perf_counter()
    _, best_fitness, curve = mlrose.mimic(problem,
                                          pop_size=mimic_pop_size,
                                          keep_pct=mimic_keep_pct,
                                          max_attempts=mimic_max_attempts,
                                          max_iters=mimic_max_iters,
                                          curve=True,
                                          random_state=SEED,
                                          fast_mimic=True)
    run_time = time.perf_counter() - start_time
    func_calls = problem.get_function_calls()
    problem.reset_function_calls()  # don't forget to reset before the next run

    run_result = (complexity, "MIMIC", run_time, best_fitness, len(curve),
                  func_calls)
    print(run_result)
    results_list.append(run_result)

# compile & save results
df_results = pd.DataFrame.from_records(results_list, columns=labels)
df_results.to_excel(os.path.join(OUTPUT_DIRECTORY, EXPERIMENT_NAME + '.xlsx'))
Exemplo n.º 7
0
def run_knapsack():

    if not os.path.exists('./output/Knapsack/'):
        os.mkdir('./output/Knapsack/')
    # TODO Write state regeneration functions as lamdas


    logger = logging.getLogger(__name__)
    problem_size = 25
    prob_size_int = int(problem_size)
    weights = [int(np.random.randint(1, prob_size_int/2)) for _ in range(prob_size_int)]
    values = [int(np.random.randint(1, prob_size_int/2)) for _ in range(prob_size_int)]
    flip_fit = mlrose.Knapsack(weights, values)
    flop_state_gen = lambda: np.random.randint(0, 1, size=prob_size_int)
    init_state = flop_state_gen()
    problem = mlrose.DiscreteOpt(length=prob_size_int, fitness_fn=flip_fit, maximize=True, max_val=2)

    all_results = {}

    print("Running simulated annealing montecarlos")
    sa_results, sa_timing = sim_annealing_runner(problem, init_state, state_regenerator=flop_state_gen)
    plot_montecarlo_sensitivity('Knapsack', 'sim_anneal', sa_results)
    plot_montecarlo_sensitivity('Knapsack', 'sim_anneal_timing', sa_timing)
    all_results['SA'] = [sa_results, sa_timing]


    print("Running random hill montecarlos")
    rhc_results, rhc_timing = rhc_runner(problem, init_state, state_regenerator=flop_state_gen)
    plot_montecarlo_sensitivity('Knapsack', 'rhc', rhc_results)
    plot_montecarlo_sensitivity('Knapsack', 'rhc_timing', sa_timing)
    all_results['RHC'] = [rhc_results, rhc_timing]

    print("Running genetic algorithm montecarlos")
    ga_results, ga_timing = ga_runner(problem, init_state, state_regenerator=flop_state_gen)
    plot_montecarlo_sensitivity('Knapsack', 'ga', ga_results)
    plot_montecarlo_sensitivity('Knapsack', 'ga_timing', ga_timing)
    all_results['GA'] = [ga_results, ga_timing]

    print("Running MIMIC montecarlos")
    mimic_results, mimic_timing = mimic_runner(problem, init_state, state_regenerator=flop_state_gen)
    plot_montecarlo_sensitivity('Knapsack', 'mimic', mimic_results)
    plot_montecarlo_sensitivity('Knapsack', 'mimic_timing', mimic_timing)
    all_results['MIMIC'] = [mimic_results, mimic_timing]


    with open('./output/Knapsack/flipflip_data.pickle', 'wb') as handle:
        pickle.dump(all_results, handle, protocol=pickle.HIGHEST_PROTOCOL)

    problem_size_space = np.linspace(5, 80, 20, dtype=int)

    best_fit_dict = {}
    best_fit_dict['Problem Size'] = problem_size_space
    best_fit_dict['Random Hill Climbing'] = []
    best_fit_dict['Simulated Annealing'] = []
    best_fit_dict['Genetic Algorithm'] = []
    best_fit_dict['MIMIC'] = []


    times = {}
    times['Problem Size'] = problem_size_space
    times['Random Hill Climbing'] = []
    times['Simulated Annealing'] = []
    times['Genetic Algorithm'] = []
    times['MIMIC'] = []


    fits_per_iteration = {}
    fits_per_iteration['Random Hill Climbing'] = []
    fits_per_iteration['Simulated Annealing'] = []
    fits_per_iteration['Genetic Algorithm'] = []
    fits_per_iteration['MIMIC'] = []


    for prob_size in problem_size_space:
        logger.info("---- Problem size: " + str(prob_size) + " ----")
        prob_size_int = int(prob_size)
        weights = [int(np.random.randint(1, prob_size/2)) for _ in range(prob_size_int)]
        values = [int(np.random.randint(1, prob_size/2)) for _ in range(prob_size_int)]
        flip_fit = mlrose.Knapsack(weights, values, max_weight_pct=0.5)
        flop_state_gen = lambda: np.random.randint(0, 1, size=prob_size_int)
        init_state = flop_state_gen()
        problem = mlrose.DiscreteOpt(length=prob_size_int, fitness_fn=flip_fit, maximize=True, max_val=2)

        start = datetime.now()
        _, best_fitness_sa, fit_array_sa = mlrose.simulated_annealing(problem,
                                                                   schedule=mlrose.ExpDecay(exp_const=.0001, init_temp=5.),
                                                                   max_attempts=100, 
                                                                   max_iters=10000, init_state=init_state, track_fits=True)
        best_fit_dict['Simulated Annealing'].append(best_fitness_sa)
        end = datetime.now()
        times['Simulated Annealing'].append((end-start).total_seconds())


        start = datetime.now()
        _, best_fitness_rhc, fit_array_rhc = mlrose.random_hill_climb(problem, max_attempts=100, max_iters=10000,
                                                                  restarts=50, track_fits=True)
        best_fit_dict['Random Hill Climbing'].append(best_fitness_rhc)
        end = datetime.now()
        times['Random Hill Climbing'].append((end-start).total_seconds())


        start = datetime.now()
        _, best_fitness_ga, fit_array_ga =  mlrose.genetic_alg(problem, pop_size=prob_size_int*5,
                                                            mutation_prob=.2, max_attempts=200, track_fits=True)
        best_fit_dict['Genetic Algorithm'].append(best_fitness_ga)
        end = datetime.now()
        times['Genetic Algorithm'].append((end-start).total_seconds())


        start = datetime.now()
        _, best_fitness_mimic, fit_array_mimic = mlrose.mimic(problem, pop_size=prob_size_int*5,
                                                        keep_pct=.1, max_attempts=200, track_fits=True)
        best_fit_dict['MIMIC'].append(best_fitness_mimic)
        end = datetime.now()
        times['MIMIC'].append((end-start).total_seconds())


    # For the last fit that occurs, save off the fit arrays that are generated. We will plot fitness/iteration.
    fits_per_iteration['Random Hill Climbing'] = fit_array_rhc
    fits_per_iteration['Simulated Annealing'] = fit_array_sa
    fits_per_iteration['Genetic Algorithm'] = fit_array_ga
    fits_per_iteration['MIMIC'] = fit_array_mimic

    fit_frame = pd.DataFrame.from_dict(best_fit_dict, orient='index').transpose()
    # fit_frame.pop('Unnamed: 0') # idk why this shows up.
    time_frame = pd.DataFrame.from_dict(times, orient='index').transpose()
    # time_frame.pop('Unnamed: 0') # idk why this shows up.
    fit_iteration_frame = pd.DataFrame.from_dict(fits_per_iteration, orient='index').transpose()

    fit_frame.to_csv('./output/Knapsack/problem_size_fit.csv')
    time_frame.to_csv('./output/Knapsack/problem_size_time.csv')
    fit_iteration_frame.to_csv('./output/Knapsack/fit_per_iteration.csv')
Exemplo n.º 8
0
def run_cpeaks():

    # If the output/Cpeaks directory doesn't exist, create it.
    #if not os.path.exists('./output/CPeaks/'):
    #    os.mkdir('./output/CPeaks/')

    problem_size = 50
    peaks_fit = mlrose.ContinuousPeaks(t_pct=.1)
    cpeaks_state_gen = lambda: np.random.randint(2, size=problem_size)
    init_state = cpeaks_state_gen()
    problem = mlrose.DiscreteOpt(length=problem_size,
                                 fitness_fn=peaks_fit,
                                 maximize=True,
                                 max_val=2)

    all_results = {}
    """
    print("Running simulated annealing montecarlos")
    sa_results, sa_timing = sim_annealing_runner(problem)
    sa_best_params = plot_montecarlo_sensitivity('CPeaks', 'sim_anneal', sa_results)
    plot_montecarlo_sensitivity('CPeaks', 'sim_anneal_timing', sa_timing)
    all_results['SA'] = [sa_results, sa_timing]


    print("Running random hill montecarlos")
    rhc_results, rhc_timing = rhc_runner(problem)
    rhc_best_params = plot_montecarlo_sensitivity('CPeaks', 'rhc', rhc_results)
    plot_montecarlo_sensitivity('CPeaks', 'rhc_timing', sa_timing)
    all_results['RHC'] = [rhc_results, rhc_timing]

    print("Running genetic algorithm montecarlos")
    ga_results, ga_timing = ga_runner(problem, init_state)
    ga_best_params = plot_montecarlo_sensitivity('CPeaks', 'ga', ga_results)
    plot_montecarlo_sensitivity('CPeaks', 'ga_timing', ga_timing)
    all_results['GA'] = [ga_results, ga_timing]

    print("Running MIMIC montecarlos")
    mimic_results, mimic_timing = mimic_runner(problem, init_state)
    MIMC_best_params = plot_montecarlo_sensitivity('CPeaks', 'mimic', mimic_results)
    plot_montecarlo_sensitivity('CPeaks', 'mimic_timing', mimic_timing)
    all_results['MIMIC'] = [mimic_results, mimic_timing]
    """

    with open('./output/CPeaks/cpeaks_data.pickle', 'wb') as handle:
        pickle.dump(all_results, handle, protocol=pickle.HIGHEST_PROTOCOL)

    problem_size_space = np.linspace(10, 100, 20, dtype=int)

    best_fit_dict = {}
    best_fit_dict['Problem Size'] = problem_size_space
    best_fit_dict['Random Hill Climbing'] = []
    best_fit_dict['Simulated Annealing'] = []
    best_fit_dict['Genetic Algorithm'] = []
    best_fit_dict['MIMIC'] = []

    times = {}
    times['Problem Size'] = problem_size_space
    times['Random Hill Climbing'] = []
    times['Simulated Annealing'] = []
    times['Genetic Algorithm'] = []
    times['MIMIC'] = []

    for prob_size in problem_size_space:
        logger.info("---- Problem size: " + str(prob_size) + " ----")
        prob_size_int = int(prob_size)
        peaks_fit = mlrose.ContinuousPeaks(t_pct=.2)
        problem = mlrose.DiscreteOpt(length=prob_size_int,
                                     fitness_fn=peaks_fit,
                                     maximize=True,
                                     max_val=2)
        cpeaks_state_gen = lambda: np.random.randint(2, size=prob_size_int)
        init_state = cpeaks_state_gen()

        start = datetime.now()
        best_stats_sa, best_fitness_sa, fitness_curve_sa = mlrose.simulated_annealing(
            problem,
            schedule=mlrose.ExpDecay(exp_const=.201,
                                     init_temp=3.6,
                                     min_temp=.101),
            max_attempts=310,
            max_iters=1100,
            curve=True)
        best_fit_dict['Simulated Annealing'].append(best_fitness_sa)
        end = datetime.now()
        times['Simulated Annealing'].append((end - start).total_seconds())

        start = datetime.now()
        best_state_rhc, best_fitness_rhc, fitness_curve_rhc = mlrose.random_hill_climb(
            problem, max_attempts=110, max_iters=1100, restarts=80, curve=True)
        best_fit_dict['Random Hill Climbing'].append(best_fitness_rhc)
        end = datetime.now()
        times['Random Hill Climbing'].append((end - start).total_seconds())

        start = datetime.now()
        best_state_ga, best_fitness_ga, fitness_curve_ga = mlrose.genetic_alg(
            problem,
            pop_size=132,
            mutation_prob=.001,
            max_attempts=210,
            max_iters=1100,
            curve=True)
        best_fit_dict['Genetic Algorithm'].append(best_fitness_ga)
        end = datetime.now()
        times['Genetic Algorithm'].append((end - start).total_seconds())

        start = datetime.now()
        best_state_mimc, best_fitness_mimic, fitness_curve_mimic = mlrose.mimic(
            problem,
            pop_size=187,
            keep_pct=.21,
            max_attempts=10,
            max_iters=1100,
            curve=True)
        best_fit_dict['MIMIC'].append(best_fitness_mimic)
        end = datetime.now()
        times['MIMIC'].append((end - start).total_seconds())

    fits_per_iteration = {}
    fits_per_iteration['Random Hill Climbing'] = fitness_curve_rhc
    fits_per_iteration['Simulated Annealing'] = fitness_curve_sa
    fits_per_iteration['Genetic Algorithm'] = fitness_curve_ga
    fits_per_iteration['MIMIC'] = fitness_curve_mimic

    fit_frame = pd.DataFrame.from_dict(best_fit_dict,
                                       orient='index').transpose()
    # fit_frame.pop('Unnamed: 0') # idk why this shows up.
    time_frame = pd.DataFrame.from_dict(times, orient='index').transpose()
    # time_frame.pop('Unnamed: 0') # idk why this shows up.
    fit_iteration_frame = pd.DataFrame.from_dict(fits_per_iteration,
                                                 orient='index').transpose()

    fit_frame.to_csv('./output/CPeaks/problem_size_fit.csv')
    time_frame.to_csv('./output/CPeaks/problem_size_time.csv')
    fit_iteration_frame.to_csv('./output/CPeaks/fit_per_iteration.csv')
def fit(K, length, fitness):
    problem = mlrose.DiscreteOpt(length=length,
                                 fitness_fn=fitness,
                                 maximize=False,
                                 max_val=K)

    iterations = [10, 50, 100, 200, 400, 800, 1600, 3200]
    RHC, SA, GA, MM = ([], [], [], [])
    time_RHC, time_SA, time_GA, time_MM = ([], [], [], [])

    for iter in iterations:
        print("max iterations = " + str(iter))
        start_time = time.time()
        best_fitness = 0
        for times in range(10):
            best_state, best_fitness = mlrose.random_hill_climb(
                problem,
                max_attempts=10,
                max_iters=iter,
                restarts=0,
                init_state=np.zeros((length, ), dtype=np.int))
            best_fitness = min(best_fitness, best_fitness)
            #print(best_state)
        RHC.append(best_fitness)
        print(best_fitness)
        time_RHC.append((time.time() - start_time) / 10)

        start_time = time.time()
        best_fitness = 0
        for times in range(10):
            best_state, best_fitness = mlrose.simulated_annealing(
                problem,
                schedule=mlrose.GeomDecay(),
                max_attempts=10,
                max_iters=iter,
                init_state=np.zeros((length, ), dtype=np.int))
            best_fitness = min(best_fitness, best_fitness)
            #print(best_state)
        SA.append(best_fitness)
        print(best_fitness)
        time_SA.append((time.time() - start_time) / 10)

        start_time = time.time()
        best_fitness = 0
        best_state, best_fitness = mlrose.genetic_alg(problem,
                                                      pop_size=200,
                                                      mutation_prob=0.1,
                                                      max_attempts=10,
                                                      max_iters=iter)
        #print(best_state)
        print(best_fitness)
        GA.append(best_fitness)
        time_GA.append((time.time() - start_time))

        start_time = time.time()
        best_fitness = 0
        best_state, best_fitness = mlrose.mimic(problem,
                                                pop_size=200,
                                                keep_pct=0.2,
                                                max_attempts=10,
                                                max_iters=iter)
        #print(best_state)
        print(best_fitness)
        MM.append(best_fitness)
        time_MM.append((time.time() - start_time))

    plot(RHC, SA, GA, MM, time_RHC, time_SA, time_GA, time_MM, iterations, K)
    filewrite_array("iterations:", iterations)
    filewrite_array("Fitness(RHC):", RHC)
    filewrite_array("Fitness(SA):", SA)
    filewrite_array("Fitness(GA):", GA)
    filewrite_array("Fitness(MM):", MM)
    filewrite_array("Fitness(time_RHC):", time_RHC)
    filewrite_array("Fitness(time_SA):", time_SA)
    filewrite_array("Fitness(time_GA):", time_GA)
    filewrite_array("Fitness(time_MM):", time_MM)
Exemplo n.º 10
0
def run_flipflop():

    # If the output/FlipFlop directory doesn't exist, create it.
    if not os.path.exists('./output/FlipFlop/'):
        os.mkdir('./output/FlipFlop/')

    problem_size = 50
    logger = logging.getLogger(__name__)
    flip_fit = mlrose.FlipFlop()
    flop_state_gen = lambda: np.random.randint(2, size=problem_size)
    init_state = flop_state_gen()
    problem = mlrose.DiscreteOpt(length=problem_size, fitness_fn=flip_fit)
    problem2 = mlrose.DiscreteOpt(length=problem_size, fitness_fn=flip_fit)
    problem3 = mlrose.DiscreteOpt(length=problem_size, fitness_fn=flip_fit)
    problem4 = mlrose.DiscreteOpt(length=problem_size, fitness_fn=flip_fit)
    all_results = {}
    """
    print("Running random hill montecarlos")
    rhc_results, rhc_timing = rhc_runner(problem)
    rhc_best_params =plot_montecarlo_sensitivity('FlipFlop', 'rhc', rhc_results)
    plot_montecarlo_sensitivity('FlipFlop', 'rhc_timing', rhc_timing)
    all_results['RHC'] = [rhc_results, rhc_timing]

    print("Running simulated annealing montecarlos")
    sa_results, sa_timing = sim_annealing_runner(problem2)
    sa_best_param = plot_montecarlo_sensitivity('FlipFlop', 'sim_anneal', sa_results)
    plot_montecarlo_sensitivity('FlipFlop', 'sim_anneal_timing', sa_timing)
    all_results['SA'] = [sa_results, sa_timing]

    print("Running genetic algorithm montecarlos")
    ga_results, ga_timing = ga_runner(problem3, init_state)
    ga_best_param = plot_montecarlo_sensitivity('FlipFlop', 'ga', ga_results)
    plot_montecarlo_sensitivity('FlipFlop', 'ga_timing', ga_timing)
    all_results['GA'] = [ga_results, ga_timing]

    print("Running MIMIC montecarlos")
    mimic_results, mimic_timing = mimic_runner(problem4, init_state)
    MIMIC_best_param = plot_montecarlo_sensitivity('FlipFlop', 'mimic', mimic_results)
    plot_montecarlo_sensitivity('FlipFlop', 'mimic_timing', mimic_timing)
    all_results['MIMIC'] = [mimic_results, mimic_timing]
    """
    with open('./output/FlipFlop/flipflip_data.pickle', 'wb') as handle:
        pickle.dump(all_results, handle, protocol=pickle.HIGHEST_PROTOCOL)

    problem_size_space = np.linspace(10, 125, 10, dtype=int)

    best_fit_dict = {}
    best_fit_dict['Problem Size'] = problem_size_space
    best_fit_dict['Random Hill Climbing'] = []
    best_fit_dict['Simulated Annealing'] = []
    best_fit_dict['Genetic Algorithm'] = []
    best_fit_dict['MIMIC'] = []

    times = {}
    times['Problem Size'] = problem_size_space
    times['Random Hill Climbing'] = []
    times['Simulated Annealing'] = []
    times['Genetic Algorithm'] = []
    times['MIMIC'] = []

    fits_per_iteration = {}
    fits_per_iteration['Random Hill Climbing'] = []
    fits_per_iteration['Simulated Annealing'] = []
    fits_per_iteration['Genetic Algorithm'] = []
    fits_per_iteration['MIMIC'] = []

    for prob_size in problem_size_space:
        logger.info("---- Problem size: " + str(prob_size) + " ----")
        prob_size_int = int(prob_size)
        flip_fit = mlrose.FlipFlop()
        flop_state_gen = lambda: np.random.randint(2, size=prob_size_int)
        init_state = flop_state_gen()
        problem = mlrose.DiscreteOpt(length=prob_size_int,
                                     fitness_fn=flip_fit,
                                     maximize=True,
                                     max_val=2)

        start = datetime.now()
        best_state_sa, best_fitness_sa, fitness_curve_sa = mlrose.simulated_annealing(
            problem,
            schedule=mlrose.ExpDecay(exp_const=.401,
                                     init_temp=0.6,
                                     min_temp=0.101),
            max_attempts=110,
            max_iters=1100,
            curve=True)
        best_fit_dict['Simulated Annealing'].append(best_fitness_sa)
        end = datetime.now()
        times['Simulated Annealing'].append((end - start).total_seconds())

        start = datetime.now()
        best_state_rhc, best_fitness_rhc, fitness_curve_rhc = mlrose.random_hill_climb(
            problem, max_attempts=410, max_iters=1100, restarts=40, curve=True)
        best_fit_dict['Random Hill Climbing'].append(best_fitness_rhc)
        end = datetime.now()
        times['Random Hill Climbing'].append((end - start).total_seconds())

        start = datetime.now()
        best_state_ga, best_fitness_ga, fitness_curve_ga = mlrose.genetic_alg(
            problem,
            pop_size=174,
            mutation_prob=.001,
            max_attempts=410,
            max_iters=1000,
            curve=True)
        best_fit_dict['Genetic Algorithm'].append(best_fitness_ga)
        end = datetime.now()
        times['Genetic Algorithm'].append((end - start).total_seconds())

        start = datetime.now()
        best_state_mimic, best_fitness_mimic, fitness_curve_mimic = mlrose.mimic(
            problem,
            pop_size=252,
            keep_pct=.21,
            max_attempts=30,
            max_iters=1100,
            curve=True)
        best_fit_dict['MIMIC'].append(best_fitness_mimic)
        end = datetime.now()
        times['MIMIC'].append((end - start).total_seconds())

    # For the last fit that occurs, save off the fit arrays that are generated. We will plot fitness/iteration.
    fits_per_iteration['Random Hill Climbing'] = fitness_curve_rhc
    fits_per_iteration['Simulated Annealing'] = fitness_curve_sa
    fits_per_iteration['Genetic Algorithm'] = fitness_curve_ga
    fits_per_iteration['MIMIC'] = fitness_curve_mimic

    fit_frame = pd.DataFrame.from_dict(best_fit_dict,
                                       orient='index').transpose()
    # fit_frame.pop('Unnamed: 0') # idk why this shows up.
    time_frame = pd.DataFrame.from_dict(times, orient='index').transpose()
    # time_frame.pop('Unnamed: 0') # idk why this shows up.
    fit_iteration_frame = pd.DataFrame.from_dict(fits_per_iteration,
                                                 orient='index').transpose()

    fit_frame.to_csv('./output/FlipFlop/problem_size_fit.csv')
    time_frame.to_csv('./output/FlipFlop/problem_size_time.csv')
    fit_iteration_frame.to_csv('./output/FlipFlop/fit_per_iteration.csv')
Exemplo n.º 11
0
Arquivo: a2.py Projeto: rkaufholz3/a2
def solve_problem(alg, prob, rs, b, attempts, verbose):

    if verbose:
        print('\n***', alg, '-', b, 'bits ***\n')

    t0 = time.time()

    if alg == 'Simulated Annealing':
        # Define decay schedule. https://mlrose.readthedocs.io/en/stable/source/decay.html
        # schedule = mlrose.GeomDecay(init_temp=1.0, decay=0.99, min_temp=0.001)
        # schedule = mlrose.ArithDecay(init_temp=1.0, decay=0.0001, min_temp=0.001)
        # schedule = mlrose.ExpDecay(init_temp=1.0, exp_const=0.005, min_temp=0.001)
        schedule = mlrose.ExpDecay()
        # init_state = np.array([0, 0, 0, 0, 1, 1, 1, 1])  # Define initial state
        state, fit, curve = mlrose.simulated_annealing(prob,
                                                       schedule=schedule,
                                                       max_attempts=attempts,
                                                       max_iters=np.inf,
                                                       init_state=None,
                                                       curve=True,
                                                       random_state=rs)

    elif alg == 'Genetic Algorithm':
        state, fit, curve = mlrose.genetic_alg(prob,
                                               pop_size=1000,
                                               mutation_prob=0.1,
                                               max_attempts=attempts,
                                               max_iters=np.inf,
                                               curve=True,
                                               random_state=rs)

    elif alg == 'Random Hill Climb':
        state, fit, curve = mlrose.random_hill_climb(prob,
                                                     max_attempts=attempts,
                                                     max_iters=np.inf,
                                                     restarts=0,
                                                     init_state=None,
                                                     curve=True,
                                                     random_state=rs)

    elif alg == 'MIMIC':
        state, fit, curve = mlrose.mimic(prob,
                                         pop_size=500,
                                         keep_pct=0.2,
                                         max_attempts=attempts,
                                         max_iters=np.inf,
                                         curve=True,
                                         random_state=rs)

    else:
        state, fit, curve = 0, 0, 0

    t1 = time.time()
    runtime = t1 - t0

    if verbose:
        # print("Best State:", state)
        print("Best Fitness:", fit)
        print("Number of fitness evaluations:", len(curve))
        np.savetxt("fitness_curve.csv", curve, delimiter=",")
        print('Solve problem run time: %0.3fs' % runtime)

    return state, fit, curve, runtime
Exemplo n.º 12
0
                                                mutation_prob=0.2,
                                                max_attempts=100)
t9 = time.time() - start
start = time.time()
best_state29, best_fitness29 = mlrose.genetic_alg(problem_fit10,
                                                  mutation_prob=0.2,
                                                  max_attempts=100)
t10 = time.time() - start

time1 = [t1, t2, t3, t4, t5, t6, t7, t8, t9, t10]
#best_state10, best_fitness10 = mlrose.genetic_alg(problem_fit10, mutation_prob = 0.2, max_attempts = 100)

start = time.time()
best_state10, best_fitness10 = mlrose.mimic(problem_fit1,
                                            pop_size=200,
                                            keep_pct=0.3,
                                            max_attempts=10,
                                            max_iters=1000)
t21 = time.time() - start
start = time.time()
best_state11, best_fitness11 = mlrose.mimic(problem_fit2,
                                            pop_size=200,
                                            keep_pct=0.3,
                                            max_attempts=10,
                                            max_iters=1000)
t22 = time.time() - start
start = time.time()
best_state12, best_fitness12 = mlrose.mimic(problem_fit3,
                                            pop_size=200,
                                            keep_pct=0.3,
                                            max_attempts=10,
Exemplo n.º 13
0
  # Solve problem using simulated annealing
  start = clock()
  best_SA_state, best_SA_fitness, curve = mlrose.simulated_annealing(problem, max_attempts = 100, max_iters = iter, init_state = None,curve=True, random_state = 1)
  SA_timings.append(clock() - start)


  # genetic_alg(problem, pop_size=200, mutation_prob=0.1, max_attempts=10, max_iters=inf, curve=False, random_state=None)
  start = clock()
  best_GA_state, best_GA_fitness = mlrose.genetic_alg(problem, mutation_prob = 0.2, max_attempts = 10, max_iters = iter,
                                                random_state = 1)
  GA_timings.append(clock() - start)

  # MIMIC
  start = clock()
  best_Mimic_state, best_Mimic_fitness = mlrose.mimic(problem, pop_size=200, keep_pct=0.2, max_attempts=10, max_iters=iter, curve=False, random_state=None)
  MIMIC_timings.append(clock() - start)

  RHC_iterations.append(best_RHC_fitness)
  SA_iterations.append(best_SA_fitness)
  GA_iterations.append(best_GA_fitness)
  MIMIC_iterations.append(best_Mimic_fitness)


plt.figure()
ticks = range(len(iterations))
print(GA_iterations)
print(MIMIC_iterations)
print(best_Mimic_state)
    # Plot mean accuracy scores for training and test sets
lw = 2
Exemplo n.º 14
0
#sprint(fitness_two.evaluate(state))

best_state_two, best_fitness_two = ml.genetic_alg(opt_two, random_state = 2)

#best_state_two, best_fitness_two = ml.random_hill_climb(opt_two, random_state = 2)

best_state_two, best_fitness_two = ml.simulated_annealing(opt_two, max_iters=1, random_state = 2)

#best_state_two, best_fitness_two = ml.mimic(opt_two, max_iters=1, random_state = 2)

print(best_state_two)


print(best_fitness_two)

#plt.scatter([0,1,0,1,2,3],[1,2,2,3,3,4])
#plt.savefig("test.png")
edges = [(0, 1), (0, 2), (0, 4), (1, 3), (2, 0), (2, 3), (3, 4)]

fitness_three = ml.MaxKColor(edges)
opt_three = ml.DiscreteOpt(length=5, fitness_fn=fitness_three, maximize=False)
#best_state_three, best_fitness_three = ml.genetic_alg(opt_three, random_state = 2)
#best_state_three, best_fitness_three = ml.random_hill_climb(opt_three, random_state = 2)
best_state_three, best_fitness_three = ml.simulated_annealing(opt_three, random_state = 2)
best_state_three, best_fitness_three = ml.mimic(opt_three, max_iters=1, random_state = 2)

print(fitness_three.evaluate([1, 0, 0, 1, 0]))

print(best_state_three)

print(best_fitness_three)
Exemplo n.º 15
0
def main():
    np.random.seed(0)
    i_s = np.random.randint(2, size=50)
    fitness = mlrose.SixPeaks()
    problem = mlrose.DiscreteOpt(length=len(i_s),
                                 fitness_fn=fitness,
                                 maximize=True,
                                 max_val=2)

    # Define decay schedule
    schedule = mlrose.ExpDecay()

    # Define initial state
    init_state = i_s
    x_s = []
    y_s = []
    z_s = ['RHC', 'SA', 'GA', 'MIMIC']
    w_s = []
    print('begin hill climb')
    # Solve problem using simulated annealing
    best_state, best_fitness, learning_curve, timing_curve = mlrose.random_hill_climb(
        problem, max_attempts=10000, curve=True, random_state=1)
    print(best_state)
    print(best_fitness)
    x_s.append(np.arange(0, len(learning_curve)))
    y_s.append(learning_curve)
    w_s.append(timing_curve)
    print('begin SA')
    best_state, best_fitness, learning_curve, timing_curve = mlrose.simulated_annealing(
        problem,
        max_attempts=250,
        max_iters=250,
        schedule=schedule,
        init_state=init_state,
        curve=True,
        random_state=1)
    print(best_state)
    print(best_fitness)
    x_s.append(np.arange(0, len(learning_curve)))
    y_s.append(learning_curve)
    w_s.append(timing_curve)
    print('begin GA')
    best_state, best_fitness, learning_curve, timing_curve = mlrose.genetic_alg(
        problem,
        pop_size=200,
        mutation_prob=0.1,
        max_attempts=250,
        max_iters=250,
        curve=True,
        random_state=1)
    print(best_state)
    print(best_fitness)
    x_s.append(np.arange(0, len(learning_curve)))
    y_s.append(learning_curve)
    w_s.append(timing_curve)
    print('begin MIMIC')
    best_state, best_fitness, learning_curve, timing_curve = mlrose.mimic(
        problem,
        pop_size=250,
        keep_pct=0.2,
        max_attempts=250,
        max_iters=250,
        curve=True,
        random_state=1,
        fast_mimic=True)
    print(best_state)
    print(best_fitness)
    x_s.append(np.arange(0, len(learning_curve)))
    y_s.append(learning_curve)
    w_s.append(timing_curve)
    for x, y, z in zip(x_s, y_s, z_s):
        plt.plot(x, y, label=z)
    plt.legend()
    plt.title('Randomized Optimization Iterations vs Fitness Function Value')
    plt.xlabel('Function iteration count')
    plt.ylabel('Fitness function value')
    plt.show()
    plt.clf()
    for x, w, z in zip(x_s, w_s, z_s):
        plt.plot(x, w, label=z)
    plt.legend()
    plt.title('Randomized Optimization Time vs Fitness Function Value')
    plt.xlabel('Function iteration count')
    plt.ylabel('Time in Second')
    plt.show()
Exemplo n.º 16
0
def optimizationFunction(n,iMin, iMax, iStep, jMin, jMax, jStep):
    
    
    np.random.seed(100)
    optScenarios = ['Knap Sack', 'Four Peaks', 'K - Colors']
    for x in range(3):
        if x == 0:
            weights = np.random.randint(1,10,size=n)
            #values = np.random.randint(1,50,size=n)
            values = [i for i in range(1,n+1)]
            max_weight_pct = 0.5
            fitnessFunction = mlrose.Knapsack(weights, values, max_weight_pct)
            optModel = mlrose.DiscreteOpt(len(values), fitness_fn = fitnessFunction, maximize=True)
        elif x == 1:
            inp = [0] * int(n/2) + [1]*int(n - int(n/2))
            np.random.shuffle(inp)
            fitnessFunction = mlrose.FourPeaks(t_pct = 0.15)
            optModel = mlrose.DiscreteOpt(len(inp), fitness_fn = fitnessFunction, maximize =True)
        elif x == 2:
            edges = [(np.random.randint(0,n), np.random.randint(0,n)) for ab in range(n)]
            fitnessFunction = mlrose.MaxKColor(edges)
            optModel = mlrose.DiscreteOpt(len(edges), fitness_fn = fitnessFunction, maximize =True)
                
        decay = mlrose.ExpDecay()
    

        optResults = {'iterations':[],'attempts':[],'fitness':[],'time':[], 'optimization':[]}
        for i in range(iMin,iMax,iStep):

            for j in range(jMin,jMax,jStep):
                start_time = timer()
                best_state, best_fitness = mlrose.random_hill_climb(optModel, max_attempts = j, max_iters = i, random_state=100)
                opt_time = timer() - start_time
           
                optResults['iterations'].append(i)
                optResults['attempts'].append(j)
                optResults['fitness'].append(best_fitness)
                optResults['time'].append(opt_time)
                optResults['optimization'].append('Random Hill')
                start_time = timer()
                best_state, best_fitness = mlrose.simulated_annealing(optModel, schedule=decay, max_attempts = j,max_iters = i,random_state=1000)
                opt_time = timer() - start_time
                optResults['iterations'].append(i)
                optResults['attempts'].append(j)
                optResults['fitness'].append(best_fitness)
                optResults['time'].append(opt_time)
                optResults['optimization'].append('Simulated Annealing')
           
                start_time = timer()
                best_state, best_fitness = mlrose.genetic_alg(optModel, pop_size=200, mutation_prob = 0.25, max_attempts = j, max_iters = i, random_state=5000)
                opt_time = timer() - start_time
               
                optResults['iterations'].append(i)
                optResults['attempts'].append(j)
                optResults['fitness'].append(best_fitness)
                optResults['time'].append(opt_time)
                optResults['optimization'].append('Genetic Algorithm')
                start_time = timer()
                best_state, best_fitness = mlrose.mimic(optModel, pop_size = 200, keep_pct = 0.3, max_attempts = j, max_iters = i, random_state=150)
                opt_time = timer() - start_time
                optResults['iterations'].append(i)
                optResults['attempts'].append(j)
                optResults['fitness'].append(best_fitness)
                optResults['time'].append(opt_time)
                optResults['optimization'].append('MIMIC')
       
        optResults = pd.DataFrame(optResults)
 
        plotGraphs(optResults,optScenarios[x])
Exemplo n.º 17
0
main_total_iterations_mimic = []
main_opt_iterations_mimic = []

for y in range(10):
    best_fit_mimic = []
    execution_times_mimic = []
    total_iterations_mimic = []
    opt_iterations_mimic = []
    for x in range(2):
        start = time.time()
        opt = ml.DiscreteOpt((y * 6) + 6, fitness)
        #best_state, best_fitness, curve = ml.random_mimic_climb(opt, pop_size=(y*10)+10, max_iters=5, restarts = 0, curve=True, random_state = 2)
        best_state_mimic, best_fitness_mimic, curve_mimic = ml.mimic(
            opt,
            pop_size=(y * 6) + 6,
            max_attempts=10,
            max_iters=900,
            curve=True,
            random_state=2)

        end = time.time()
        best_fit_mimic.append(best_fitness_mimic)
        total_iterations_mimic.append(len(curve_mimic))
        opt_iterations_mimic.append(np.argmax(curve_mimic))

        if (len(sample_sizes) < 10 and x == 0):
            sample_sizes.append((y * 6) + 6)
        execution_times_mimic.append(end - start)
        #print("x = " + x)
        #print("y = " + y)
    main_best_fit_mimic.append(np.mean(best_fit_mimic))
Exemplo n.º 18
0
    elif params["algo"] == 1:  # SA
        # Define decay schedule
        schedule = mlrose.ExpDecay()
        # Solve problem using simulated annealing
        best_state, best_fitness = mlrose.simulated_annealing(
            problem,
            schedule=schedule,
            max_attempts=max_atp,
            max_iters=max_iter,
            init_state=init_state)
    elif params["algo"] == 2:  #GA
        # genetic_alg(problem, pop_size=200, mutation_prob=0.1, max_attempts=10, max_iters=inf, curve=False, random_state=None)
        best_state, best_fitness = mlrose.genetic_alg(problem,
                                                      pop_size=100,
                                                      mutation_prob=0.1,
                                                      max_attempts=max_atp,
                                                      max_iters=max_iter)
        best_state = [int(i) for i in best_state]
    else:  #MIMIC
        # mimic(problem, pop_size=200, keep_pct=0.2, max_attempts=10, max_iters=inf, curve=False, random_state=None)
        best_state, best_fitness = mlrose.mimic(problem,
                                                pop_size=200,
                                                keep_pct=0.2,
                                                max_attempts=max_atp,
                                                max_iters=max_iter)

    best_fit_value = params["number"] * (params["number"] - 1) / 2
    print('The best state found is: ', best_state)
    print('The fitness at the best state is: ',
          best_fitness / (best_fit_value))
Exemplo n.º 19
0
def main():
    name_of_exp = "K-Color"
    edges = [(0, 1), (0, 2), (0, 4), (1, 3), (2, 0), (2, 3), (3, 4)]
    fitness = mlrose.MaxKColor(edges)
    init_state = np.zeros(5)
    problem = mlrose.DiscreteOpt(length=5, fitness_fn=fitness, maximize=False, max_val=2)

    # Define decay schedule
    schedule = mlrose.ExpDecay()

    x_s = []
    y_s = []
    z_s = ['RHC', 'SA', 'GA', 'MIMIC']
    w_s = []
    max_val = 3.0
    found_flag = False
    for restarts in np.arange(0, 5):
        if found_flag:
            break
        for max_iter_atts in np.arange(10, 1000, 10):
            if found_flag:
                break
            # Solve problem using simulated annealing
            best_state, best_fitness, learning_curve, timing_curve = mlrose.random_hill_climb(problem, max_attempts=int(
                max_iter_atts), max_iters=int(max_iter_atts),
                                                                                              restarts=int(restarts),
                                                                                              init_state=init_state,
                                                                                              curve=True,
                                                                                              random_state=1)
            if best_fitness >= max_val:
                x_s.append(np.arange(0, len(learning_curve)))
                y_s.append(learning_curve)
                w_s.append(timing_curve)
                print(best_state)
                print(best_fitness)
                print(max_iter_atts)
                print(restarts)
                found_flag = True

    found_flag = False
    for sched in [mlrose.ExpDecay(), mlrose.GeomDecay(), mlrose.ArithDecay()]:
        if found_flag:
            break
        for max_iter_atts in np.arange(10, 1000, 10):
            if found_flag:
                break
            best_state, best_fitness, learning_curve, timing_curve = mlrose.simulated_annealing(problem,
                                                                                                max_attempts=int(
                                                                                                    max_iter_atts),
                                                                                                max_iters=int(
                                                                                                    max_iter_atts),
                                                                                                schedule=sched,
                                                                                                init_state=init_state,
                                                                                                curve=True,
                                                                                                random_state=1)
            if best_fitness >= max_val:
                x_s.append(np.arange(0, len(learning_curve)))
                y_s.append(learning_curve)
                w_s.append(timing_curve)
                print(best_state)
                print(best_fitness)
                print(max_iter_atts)
                print(sched)
                found_flag = True

    found_flag = False
    for prob in np.arange(0.1, 1.1, 0.1):
        if found_flag:
            break
        for pop_size in np.arange(100, 5000, 100):
            if found_flag:
                break
            for max_iter_atts in np.arange(10, 1000, 10):
                if found_flag:
                    break
                best_state, best_fitness, learning_curve, timing_curve = mlrose.genetic_alg(problem,
                                                                                            pop_size=int(pop_size),
                                                                                            mutation_prob=prob,
                                                                                            max_attempts=int(
                                                                                                max_iter_atts),
                                                                                            max_iters=int(
                                                                                                max_iter_atts),
                                                                                            curve=True,
                                                                                            random_state=1)
                if best_fitness >= max_val:
                    x_s.append(np.arange(0, len(learning_curve)))
                    y_s.append(learning_curve)
                    w_s.append(timing_curve)
                    print(best_state)
                    print(best_fitness)
                    print(max_iter_atts)
                    print(prob)
                    print(pop_size)
                    found_flag = True

    found_flag = False
    for prob in np.arange(0.1, 1.1, 0.1):
        if found_flag:
            break
        for pop_size in np.arange(100, 5000, 100):
            if found_flag:
                break
            for max_iter_atts in np.arange(10, 1000, 10):
                if found_flag:
                    break
                best_state, best_fitness, learning_curve, timing_curve = mlrose.mimic(problem, pop_size=int(pop_size),
                                                                                      keep_pct=prob,
                                                                                      max_attempts=int(max_iter_atts),
                                                                                      max_iters=int(max_iter_atts),
                                                                                      curve=True,
                                                                                      random_state=1,
                                                                                      fast_mimic=True)
                if best_fitness >= max_val:
                    x_s.append(np.arange(0, len(learning_curve)))
                    y_s.append(learning_curve)
                    w_s.append(timing_curve)
                    print(best_state)
                    print(best_fitness)
                    print(max_iter_atts)
                    print(prob)
                    print(pop_size)
                    found_flag = True

    for x, y, z in zip(x_s, y_s, z_s):
        plt.plot(x, y, label=z)
    plt.legend()
    plt.title('Randomized Optimization Iterations vs Fitness Function Value for {}'.format(name_of_exp))
    plt.xlabel('Function iteration count')
    plt.ylabel('Fitness function value')
    plt.show()
    plt.clf()
    for x, w, z in zip(x_s, w_s, z_s):
        plt.plot(x, w, label=z)
    plt.legend()
    plt.title('Randomized Optimization Time vs Fitness Function Value for {}'.format(name_of_exp))
    plt.xlabel('Function iteration count')
    plt.ylabel('Time in Seconds')
    plt.show()
Exemplo n.º 20
0
schedule = mlrose.GeomDecay(init_temp=3.0, decay=0.99, min_temp=0.001)

# Define initial state
init_state = np.array([0,1,2,3,4,5,6,7])

# Solve problem using simulated annealing
start_sa = time.time()
best_state, best_fitness, sa_curve = mlrose.simulated_annealing(problem, schedule = schedule,
                                                      max_attempts = 800, max_iters = 1000,
                                                      init_state = init_state, random_state = 1, curve=True)
end_sa = time.time()
sa_total_time = end_sa - start_sa 


start_mimic = time.time()
best_state_2, best_fitness_2, curve = mlrose.mimic(problem, pop_size=150, keep_pct=0.1, max_attempts = 800, max_iters = 1000, 
                                        curve=True, random_state = None, fast_mimic=True)

end_mimic = time.time()
mimic_total_time = end_mimic - start_mimic 


start_ga = time.time()
best_state_3, best_fitness_3, ga_curve = mlrose.genetic_alg(problem, random_state = 0,mutation_prob=0.8,curve=True, max_attempts=800, max_iters=1000)
end_ga = time.time()
ga_total_time = end_ga - start_ga

start_rhc = time.time()
best_state_4, best_fitness_4, rhc_curve = mlrose.random_hill_climb(problem, max_attempts=500, max_iters=1000, restarts=5,curve=True, init_state=init_state)
end_rhc = time.time()
rhc_total_time = end_rhc - start_rhc
Exemplo n.º 21
0
start_time = time.time()
best_state1, best_fitness1 = mlrose.random_hill_climb(problem_fit,
                                                      max_attempts=100,
                                                      max_iters=100)

start_time2 = time.time()
best_state2, best_fitness2 = mlrose.simulated_annealing(
    problem_fit, schedule=mlrose.GeomDecay(), max_attempts=100, max_iters=100)

start_time3 = time.time()
best_state3, best_fitness3 = mlrose.genetic_alg(problem_fit,
                                                pop_size=200,
                                                mutation_prob=0.1,
                                                max_attempts=100,
                                                max_iters=100)

start_time4 = time.time()
best_state4, best_fitness4 = mlrose.mimic(problem_fit,
                                          pop_size=200,
                                          keep_pct=0.2,
                                          max_attempts=10,
                                          max_iters=100)

print("Random Hill Climb OneMax Fitness Score:", best_fitness1)
print("--- %s RH training seconds ---" % (time.time() - start_time))
print("Simulated Annealing OneMax Fitness Score:", best_fitness2)
print("--- %s SA training seconds ---" % (time.time() - start_time2))
print("Genetic Algorithm OneMax Fitness Score:", best_fitness3)
print("--- %s GA training seconds ---" % (time.time() - start_time3))
print("MIMIC Algorithm OneMax Fitness Score:", best_fitness4)
print("--- %s MIMIC training seconds ---" % (time.time() - start_time4))
Exemplo n.º 22
0
def mimic_runner(problem, state, gen_plots=True, state_regenerator=None):
    """
    Runs the simulated annealing experiment on a given problem. 

    Free variables are:
    Initial temperature
    Final temperature
    Decay rate
    # of attempts
    # of iterations
    """
    problem_size = len(state)
    attempts = np.arange(25, 25, 1).astype(int)
    pop_size = np.arange(problem_size//4, problem_size * 6, problem_size//4).astype(int)
    percents = np.arange(.005, .3, .01)

    scoring_dict = {}
    timing_dict = {}


    attempts_scores = []
    attempt_timing = []
    # print("Running attempt sweep")
    for i, att in enumerate(attempts):
        best_fits = []
        times = []
        for i in MC_RUNS:
            init_state = state_regenerator()
            print("Running attempt sweep " + str(att) + ": " + str(i))
            start = datetime.now()
            _, best_fitness =  mlrose.mimic(problem, pop_size=problem_size, keep_pct=.3, max_attempts=int(att))
            end = datetime.now()
            best_fits.append(best_fitness)
            times.append((end-start).total_seconds())
        attempt_timing.append(times)
        attempts_scores.append(best_fits)
    scoring_dict['NumberofAttempts'] = pd.DataFrame(attempts_scores, columns=MC_RUNS, index=attempts)
    timing_dict['NumberofAttempts'] = pd.DataFrame(attempt_timing, columns=MC_RUNS, index=attempts)


    population_scores = []
    population_timing = []
    print("Running population size sweep")
    for i, psize in enumerate(pop_size):
        times = []
        best_fits = []
        for i in MC_RUNS:
            init_state = state_regenerator()
            print("Running population size sweep " + str(psize) + ": " + str(i))
            start = datetime.now()
            _, best_fitness = mlrose.mimic(problem, pop_size=int(psize), keep_pct=.3, max_attempts=10)
            end = datetime.now()
            best_fits.append(best_fitness)
            times.append((end-start).total_seconds())
        population_scores.append(best_fits)
        population_timing.append(times)
    scoring_dict['PopulationSize'] = pd.DataFrame(population_scores, columns=MC_RUNS, index=pop_size)
    timing_dict['PopulationSize'] = pd.DataFrame(population_timing, columns=MC_RUNS, index=pop_size)

    mutation_scores = []
    print("Running mutation rate sweep")
    for i, prcnt in enumerate(percents):

        best_fits = []
        for i in MC_RUNS:
            init_state = state_regenerator()
            print("Running percentage kept sweep " + str(prcnt) + ": " + str(i))
            _, best_fitness = mlrose.mimic(problem, pop_size=problem_size*2, keep_pct=prcnt, max_attempts=20)
            best_fits.append(best_fitness)
        mutation_scores.append(best_fits)
    scoring_dict['PercentageKept'] = pd.DataFrame(mutation_scores, columns=MC_RUNS, index=percents)

    return scoring_dict, timing_dict
Exemplo n.º 23
0
np.random.seed(123)
weights = np.random.uniform(size=probSize)
values = np.random.uniform(size=probSize)
fitnessF = mlrose.Knapsack(weights=weights, values=values, max_weight_pct=thre)
problemFit = mlrose.DiscreteOpt(length=probSize,
                                fitness_fn=fitnessF,
                                maximize=True,
                                max_val=2)
# max_val: number of unique values each element in the state vector can take.

timeStart = time.time()
best_state, best_fitness, fitness_curve = mlrose.mimic(problemFit,
                                                       pop_size=popu,
                                                       keep_pct=kepp,
                                                       max_attempts=int(1e9),
                                                       max_iters=maxIter,
                                                       curve=True,
                                                       random_state=rseed,
                                                       fast_mimic=True)
timeEnd = time.time()
timeCost = timeEnd - timeStart

timeCost = str(timeCost)
best_state = ",".join(map(str, best_state))
fitness_curve = ",".join(map(str, fitness_curve))
rst = [timeCost, best_fitness, best_state, fitness_curve]
fileName = ("output/MM-psiz-" + str(probSize) + "-thre-" + str(thre) +
            "-popu-" + str(popu) + "-kepp-" + str(kepp) + "-mxit-" +
            str(maxIter) + "-seed-" + str(rseed) + '-.txt')
with open(fileName, 'w') as f:
    for item in rst:
Exemplo n.º 24
0
    GA_best_fitness.append(GA_fitness)
    GA_time_avg.append(GA_time)

MIMIC_best_fitness = []
MIMIC_time_avg = []

for j in range(0, 5):
    print(j)
    MIMIC_fitness = []
    MIMIC_time = []
    for i in array:
        print(i)
        start = time.time()
        best_state, best_fitness, fitness_curve = mlrose.mimic(
            problem,
            pop_size=1000,
            keep_pct=0.2,
            max_attempts=200,
            max_iters=i)
        end = time.time()
        MIMIC_fitness.append(fitness.evaluate(best_state))
        MIMIC_time.append(end - start)
        print(end - start)

    MIMIC_best_fitness.append(MIMIC_fitness)
    MIMIC_time_avg.append(MIMIC_time)

GA_best_fitness = [77, 94, 93, 94, 95, 94, 96, 96]
MIMIC_best_fitness = [80, 92, 94, 95, 95, 96, 96, 96]

plt.figure(figsize=(12, 6))
plt.plot(array,
Exemplo n.º 25
0
        state, opt = mlrose.simulated_annealing(problem=popt, max_attempts=20)
        s = time.clock()
        sat.append(s - t)
        sao.append(opt)

        t = time.clock()
        state, opt = mlrose.genetic_alg(pop_size=i * 20,
                                        problem=popt,
                                        max_attempts=20)
        s = time.clock()
        gat.append(s - t)
        gao.append(opt)

        t = time.clock()
        state, opt = mlrose.mimic(pop_size=i * 20,
                                  problem=popt,
                                  max_attempts=20)
        s = time.clock()
        mmt.append(s - t)
        mmo.append(opt)

    plt.plot(bits, rhct, bits, sat, bits, gat, bits, mmt)
    plt.title("Knapsack Problem")
    plt.xlabel("Bit State Length")
    plt.ylabel("Time")
    plt.legend(["rhc", "sa", "ga", "mimic"])
    plt.show()

    plt.plot(bits, rhco, bits, sao, bits, gao, bits, mmo)
    plt.title("Knapsack Problem")
    plt.xlabel("Bit State Length")
Exemplo n.º 26
0
    def test_mimic(self,
                   title,
                   max_attempts_range=[100],
                   pop_range=[200],
                   keep_pct_range=[0.2]):
        pop_len = range(len(pop_range))
        mut_len = range(len(keep_pct_range))
        i_pop = 0
        i_mut = 0

        color = 0
        colors = ['k', 'b', 'g', 'r', 'c']
        fig = plt.figure(figsize=(8, 12))
        ax1 = fig.add_subplot(2, 1, 1, projection='3d')
        ax2 = fig.add_subplot(2, 1, 2, projection='3d')
        fig.suptitle(title + " MIMIC Algorithm")
        print(title + " MIMIC Algo")
        best = [0, 0, 0, 0]
        for m in max_attempts_range:
            fitness_arr = np.zeros((len(keep_pct_range), len(pop_range)))
            time_arr = np.zeros((len(keep_pct_range), len(pop_range)))
            i_pop = 0
            for p in pop_range:
                i_mut = 0
                for kp in keep_pct_range:
                    start = time.time()
                    best_state, best_fitness, curve = mlrose.mimic(
                        self.problem_fit,
                        pop_size=p,
                        keep_pct=kp,
                        max_attempts=math.ceil(m),
                        max_iters=np.inf,
                        curve=True)
                    fitness_arr[i_mut][i_pop] = (best_fitness)
                    time_arr[i_mut][i_pop] = (round(time.time() - start, 2))
                    if best_fitness > best[3]:
                        best[0] = m
                        best[1] = p
                        best[2] = kp
                        best[3] = best_fitness

                    i_mut += 1
                i_pop += 1

            X, Y = np.meshgrid(pop_len, mut_len)
            surf = ax1.plot_surface(X,
                                    Y,
                                    fitness_arr,
                                    lw=2,
                                    label=m,
                                    color=colors[color])
            surf._facecolors2d = surf._facecolors3d
            surf._edgecolors2d = surf._edgecolors3d
            ax2.plot_surface(X, Y, time_arr, lw=2, color=colors[color])
            color += 1

        ax1.set(xlabel="Population", ylabel="Keep %", zlabel='Fitness')
        ax2.set(xlabel="Population", ylabel="Keep %", zlabel='Time (s)')
        ax1.set(xticks=range(len(pop_range)),
                xticklabels=pop_range,
                yticks=range(len(keep_pct_range)),
                yticklabels=keep_pct_range)
        fig.legend(loc='center right', title='Attempts')
        plt.tight_layout()
        print(
            title +
            " MIMIC max_attempts={a}, # population={b}, keep %={c}, best_fitness={d}"
            .format(a=best[0], b=best[1], c=best[2], d=best[3]))
        #ax1.text(x=0.05,y=0.95, s="max_attempts={a}\n# population={b}\nkeep %={c}\nbest_fitness={d}".format(a=best[0], b=best[1], c=best[2], d=best[3]))
        self.saveToNewDir(fig, "./", title + "_MIMIC.png")
        plt.clf()
Exemplo n.º 27
0
def one_max():
    problem = mlrose.DiscreteOpt(length=20,
                                 fitness_fn=mlrose.OneMax(),
                                 maximize=True,
                                 max_val=2)
    init_state = np.array([0] * 20)
    startTime = datetime.now()
    best_state, best_fitness, fitness_curve_rhc = mlrose.random_hill_climb(
        problem,
        max_attempts=1000,
        max_iters=2500,
        restarts=0,
        init_state=init_state,
        curve=True,
        random_state=1,
        state_fitness_callback=None,
        callback_user_info=None)

    totalTime = datetime.now() - startTime
    rhcTime = totalTime.total_seconds()
    print("RHC")
    print("Time: ", rhcTime)
    print("best_fitness: ", best_fitness)
    print("Iteration: %d " % len(fitness_curve_rhc))

    ###############################
    startTime = datetime.now()
    best_statesa, best_fitnesssa, fitness_curve_sa = mlrose.simulated_annealing(
        problem,
        max_attempts=1000,
        max_iters=2500,
        init_state=init_state,
        curve=True,
        random_state=1,
        state_fitness_callback=None,
        callback_user_info=None)

    totalTime = datetime.now() - startTime
    saTime = totalTime.total_seconds()
    print("SA")
    print("Time: ", saTime)
    print("best_fitness: ", best_fitnesssa)
    print("Iteration: %d " % len(fitness_curve_sa))

    ###############################
    startTime = datetime.now()
    best_statega, best_fitnessga, fitness_curve_ga = mlrose.genetic_alg(
        problem,
        max_attempts=1000,
        max_iters=2500,
        curve=True,
        random_state=1,
        state_fitness_callback=None,
        callback_user_info=None)

    totalTime = datetime.now() - startTime
    gaTime = totalTime.total_seconds()
    print("GA")
    print("Time: ", gaTime)
    print("best_fitness: ", best_fitnessga)
    print("Iteration:  %d " % len(fitness_curve_ga))

    ###############################
    startTime = datetime.now()
    best_statemm, best_fitnessmm, fitness_curve_mm = mlrose.mimic(
        problem,
        max_attempts=1000,
        max_iters=2500,
        curve=True,
        random_state=1,
        state_fitness_callback=None,
        callback_user_info=None)

    totalTime = datetime.now() - startTime
    mmTime = totalTime.total_seconds()
    print("MIMIC")
    print("Time: ", mmTime)
    print("best_fitness: ", best_fitnessmm)
    print("Iteration: %d " % len(fitness_curve_mm))
Exemplo n.º 28
0
length_hc = list(range(1, c2.shape[0] + 1))

start_time = time.time()
a3, b3, c3 = mlrose.simulated_annealing(
    problem_fit,
    random_state=2,
    curve=True,
)
span_sa = time.time() - start_time
fitness_sa = c3
length_sa = list(range(1, c3.shape[0] + 1))

start_time = time.time()
a4, b4, c4 = mlrose.mimic(
    problem_fit,
    random_state=2,
    curve=True,
)
span_mc = time.time() - start_time
fitness_mc = c4
length_mc = list(range(1, c4.shape[0] + 1))

max = np.concatenate(
    (fitness_ga, fitness_hc, fitness_mc, fitness_sa)).max() + 5
x_max = np.concatenate((length_ga, length_hc, length_sa, length_mc)).max()
plt.figure(figsize=(12, 8))
plt.plot(length_ga, fitness_ga, linewidth=6, label="genetic_alg")
plt.plot(length_hc, fitness_hc + 2, linewidth=6, label="random_hill_climb")
plt.plot(length_sa, fitness_sa, linewidth=6, label="simulated_annealing")
plt.plot(length_mc, fitness_mc, linewidth=6, label="mimic")
plt.title("Continuous Peaks Problem Fitness, SIZE = 50", fontsize=20)
Exemplo n.º 29
0
                                                      init_state = init_state)

print('The best state found is (SA): ',opt_state)
print('The fitness at the best state is (SA): ', opt_fit)




# Hill Climb
opt_state, opt_fit = mlrose.random_hill_climb(problem,
                                                      max_attempts = 5000, max_iters = 1000000000)

print('The best state found is (RHC): ',opt_state)
print('The fitness at the best state is (RHC): ', opt_fit)'''
'''Genetic Alg'''

# GA
'''opt_state, opt_fit = mlrose.genetic_alg(problem,mutation_prob=0.005,pop_size=10000,max_attempts = 50, max_iters = 2500)


print('The best state found is (GA): ',opt_state)
print('The fitness at the best state is (GA): ', opt_fit)'''

# Mimic
opt_state, opt_fit = mlrose.mimic(problem,
                                  pop_size=1000,
                                  max_attempts=100,
                                  max_iters=300)

print('The best state found is (mimic): ', opt_state)
print('The fitness at the best state is (mimic): ', opt_fit)
Exemplo n.º 30
0
        _, best_fitness_ga, fit_array_ga = mlrose.genetic_alg(
            problem,
            pop_size=prob_size_int * 15,
            max_iters=10000,
            mutation_prob=.15,
            max_attempts=200,
            track_fits=True)
        best_fit_dict['Genetic Algorithm'].append(best_fitness_ga)
        end = datetime.now()
        times['Genetic Algorithm'].append((end - start).total_seconds())

        start = datetime.now()
        _, best_fitness_mimic, fit_array_mimic = mlrose.mimic(
            problem,
            pop_size=prob_size_int * 10,
            max_iters=10000,
            keep_pct=.1,
            max_attempts=100,
            track_fits=True)
        best_fit_dict['MIMIC'].append(best_fitness_mimic)
        end = datetime.now()
        times['MIMIC'].append((end - start).total_seconds())

    # For the last fit that occurs, save off the fit arrays that are generated. We will plot fitness/iteration.
    fits_per_iteration['Random Hill Climbing'] = fit_array_rhc
    fits_per_iteration['Simulated Annealing'] = fit_array_sa
    fits_per_iteration['Genetic Algorithm'] = fit_array_ga
    fits_per_iteration['MIMIC'] = fit_array_mimic

    fit_frame = pd.DataFrame.from_dict(best_fit_dict,
                                       orient='index').transpose()