Пример #1
0
def plot_RHCRestarts(problem, problem_fit, max_attempts, maxIter, seed, min=False):
    restarts = [0, 1, 5, 10, 50, 100]
    plt.figure()
    global start, times, evals, iter_total, rhc_curve, eval_count, algo_in

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

    for r in restarts:
        print('restarts', r)
        best_state, best_fitness, rhcfitness_curve = mlrose.random_hill_climb(problem_fit, curve=True,
                                                                              max_attempts=max_attempts,
                                                                              state_fitness_callback=callback_func,
                                                                              callback_user_info=[], restarts=r,
                                                                              random_state=seed, max_iters=maxIter)
        if min:
            rhcfitness_curve = np.array(rhcfitness_curve) * -1
        plt.plot(rhcfitness_curve, label='restarts = ' + str(r))

    plt.title(problem + " - RHC - Restarts")
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("Images\\" + problem + " - RHC - Restarts")
    plt.show()
Пример #2
0
def rhc(opt):
    global count
    count = 0
    rhc_loss_list = []
    rhc_train_acc_list = []
    rhc_test_acc_list = []
    for num_restart in range(0, 101, 5):
        best_state_spam, best_fitness_spam, _ = mlrose.random_hill_climb(
            opt, restarts=num_restart, curve=False)
        train_predict_hill = predict(best_state_spam, train_features_spam_norm)
        test_predict_hill = predict(best_state_spam, test_features_spam_norm)
        train_accuracy_hill = accuracy_score(train_labels_spam,
                                             train_predict_hill)
        test_accuracy_hill = accuracy_score(test_labels_spam,
                                            test_predict_hill)
        rhc_loss_list.append(best_fitness_spam)
        rhc_train_acc_list.append(train_accuracy_hill)
        rhc_test_acc_list.append(test_accuracy_hill)
    plt.figure(figsize=(10, 6))
    plt.subplot(121)
    plt.plot(np.arange(0, 101, 5), rhc_loss_list, label='-1*loss')
    plt.xlabel('random restart num')
    plt.ylabel('minus of loss')
    plt.title('loss versus restart num')
    plt.legend(loc='lower right')
    plt.subplot(122)
    plt.plot(np.arange(0, 101, 5), rhc_train_acc_list, label='train')
    plt.plot(np.arange(0, 101, 5), rhc_test_acc_list, label='test')
    plt.xlabel('random restart num')
    plt.ylabel('accuracy')
    plt.title('accuracy versus restart num')
    plt.legend(loc='lower right')
    plt.show()
def call_mlrose_curve(algorith_keyword, problem, pop_size=200, mutation_prob=0.1, max_attempts=10, max_iters=np.inf, curve=False\
, random_state=None, schedule=mlrose.GeomDecay(), init_state=None, restarts=0, keep_pct=0.2, fast_mimic=True):
    if algorith_keyword == 'RHC':
        best_state, best_fitness, curve_output = mlrose.random_hill_climb(problem, max_attempts=max_attempts, max_iters=max_iters\
        , restarts=restarts, init_state=init_state, curve=curve, random_state=random_state)
    elif algorith_keyword == 'GA':
        best_state, best_fitness, curve_output = mlrose.genetic_alg(problem, pop_size=pop_size, mutation_prob=mutation_prob\
        , max_attempts=max_attempts, max_iters=max_iters, curve=curve, random_state=random_state)
    elif algorith_keyword == 'SA':
        best_state, best_fitness, curve_output = mlrose.simulated_annealing(problem, schedule=schedule, max_attempts=max_attempts\
        ,max_iters=max_iters, init_state=init_state, curve=curve, random_state=random_state)
    elif algorith_keyword == 'MIMIC':
        print("problem: ", problem, "\npop_size: ", pop_size, "\n",
              "keep_pct: ", keep_pct)
        print("max_attempts: ", max_attempts, "\nmax_iters: ", max_iters,
              "\nrandom_state: ", random_state, "\nfast_mimic: ", fast_mimic)
        best_state, best_fitness, curve_output = mlrose.mimic(problem, pop_size=pop_size, keep_pct=keep_pct\
        , max_attempts=max_attempts, max_iters=max_iters, curve=curve, random_state=random_state)
        print("best_fitness: ", best_fitness)
    else:
        print(
            "\n\nIncorrect 'algorithm_keyword'. Please check the input to the 'call_mlrose' function.\n\n"
        )
        best_state, best_fitness, curve_output = 'incorrect key word', 'incorrect key word', 'incorrect key word'
    return best_state, best_fitness, curve_output
Пример #4
0
def compare_methods(num_char):
    global count
    count=0
    fitness_obj=mlrose.CustomFitness(heter_string_fn)
    opt=mlrose.DiscreteOpt(num_char,fitness_obj,maximize=True,max_val=num_char)
    best_state_climb,best_fitness_climb,fitness_curve_climb=mlrose.random_hill_climb(opt,curve=True)
    print('---------------------random hill climb-------------------------')
    print('hill climbing best state for heter-string problem:',best_state_climb)
    print('hill climbing best fitness for heter-string problem:',best_fitness_climb)
    print('hill climbing fitting curve for heter-string problem:',fitness_curve_climb)
    print('number of fitness call used:',count)
    count=0
    print('-------------------simulated annealing-------------------------')
    best_state_ann,best_fitness_ann,fitness_curve_ann=mlrose.simulated_annealing(opt,schedule=mlrose.ExpDecay(),curve=True)
    print('simulated annealing best state for heter-string problem:',best_state_ann)
    print('simulated annealing best fitness for heter-string problem:',best_fitness_ann)
    print('simulated annealing fitting curve for heter-string problem:',fitness_curve_ann)
    print('number of fitness call used:',count)
    count=0
    best_state_ga,best_fitness_ga,fitness_curve_ga=mlrose.genetic_alg(opt,pop_size=200, mutation_prob=0.5,curve=True)
    print('---------------------genetic alg----------------------------')
    print('genetic algorithm best state for heter-string problem:',best_state_ga)
    print('genetic algorithm best fitnees for heter-string problem:',best_fitness_ga)
    print('genetic algorithm fitness curve for heter-string problem:',fitness_curve_ga)
    print('number of fitness call used:',count)
    count=0
    best_state_mimic,best_fitness_mimic,fitness_curve_mimic=mlrose.mimic(opt,pop_size=200,curve=True)
    print('------------------------mimic-------------------------------')
    print('mimic best state for heter-string problem:',best_state_mimic)
    print('mimic best fitness value for heter-string problem:',best_fitness_mimic)
    print('mimic curve for heter-string problem:',fitness_curve_mimic)
    print('number of fitness call used:',count)
    count=0
    plt.figure(figsize=(10,10))
    plt.subplot(221)
    plt.plot(fitness_curve_climb)
    plt.ylabel('fitness')
    plt.xlabel('num_iter')
    plt.ylim(20,50)
    plt.title('random hill climb')
    plt.subplot(222)
    plt.plot(fitness_curve_ann)
    plt.ylabel('fitness')
    plt.xlabel('num_iter')
    plt.ylim(20,50)
    plt.title('simulated annealing')
    plt.subplot(223)
    plt.plot(fitness_curve_ga)
    plt.ylim(20,50)
    plt.ylabel('fitness')
    plt.xlabel('num_iter')
    plt.title('genetic algorithm')
    plt.subplot(224)
    plt.plot(fitness_curve_mimic)
    plt.ylim(20,50)
    plt.title('mimic')
    plt.ylabel('fitness')
    plt.xlabel('num_iter')
    plt.show()
Пример #5
0
def random_hill_climb(problem_fit, vectorLength, data_dir, iterlist):
    directory = "./" + data_dir + "/curves/"
    if not os.path.exists(directory):
        os.makedirs(directory)
    path1 = './' + data_dir
    path2 = "./" + data_dir + "/curves/"

    restartl = []

    beststate = []
    bestfit = []
    curve = []
    time = []
    iterlistn = []

    for rs in (0, 5, 10, 15, 20, 25):
        for iters in iterlist:
            iterlistn.append(int(iters))
            restartl.append(rs)
            start = clock()
            best_state, best_fitness, train_curve = mlrose.random_hill_climb(problem_fit, \
                                                                restarts=rs,\
                                                                max_iters=int(iters), curve=True, random_state=randomSeed)
            end = clock()
            time.append(end - start)
            beststate.append(best_state)
            bestfit.append(best_fitness)
            curve.append(train_curve)
            if (verbose == True):
                print(rs)
                print(int(iters))
                print(best_state)
                print(best_fitness)

    ffrhc = pd.DataFrame({
        'Restarts': restartl,
        'Best Fitness': bestfit,
        'Iterations': iterlistn,
        'Time': time
    })

    rhcbeststatedf = pd.DataFrame(0.0,
                                  index=range(1, vectorLength + 1),
                                  columns=range(1,
                                                len(beststate) + 1))

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

    ffrhc.to_csv(os.path.join(path1, 'rhc.csv'))
    rhcbeststatedf.to_csv(os.path.join(path1, 'rhcstates.csv'))

    for i in range(len(curve)):
        rhccurvedf = pd.DataFrame(curve[i])
        rhccurvedf.to_csv(
            os.path.join(path2,
                         'rhccurve{}_{}.csv'.format(restartl[i],
                                                    iterlistn[i])))
Пример #6
0
def n_queens_rhc(nq_problem,
                 initial_state,
                 max_iters=np.inf,
                 num_runs=20,
                 verbose=False):
    hp_name = 'restarts'
    hp_values = [10, 20, 30]

    # run for each hp value and append results to list

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

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

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

        for run in runs:
            run_t0 = time()
            best_state, best_fitness, fitness_curve = mlrose.random_hill_climb(
                problem=nq_problem,
                restarts=restarts,
                max_attempts=10,
                max_iters=max_iters,
                init_state=initial_state,
                curve=True,
            )
            run_time = time() - run_t0
            run_times[run] = run_time

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

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

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

    # generate plots
    plot_title = "N-Queens RHC: fitness vs. iterations"
    plotting.plot_fitness_curves(
        fitness_dfs=fitness_dfs,
        hp_values=hp_values,
        hp_name=hp_name,
        title=plot_title,
    )
    plt.savefig('graphs/n_queens_rhc_fitness.png')
    plt.clf()

    return fitness_dfs
Пример #7
0
def rhc(problem, init_state, max_attempts, max_iters):
    best_state, best_fitness, fitness_curve  = mlrose_hiive.random_hill_climb(problem,
                                                          max_attempts = max_attempts, max_iters = max_iters, restarts = 100,
                                                          init_state = init_state, curve=True, random_state = 1)
    print('random hill climbing')
    print(best_state)
    print(best_fitness)
#     print(fitness_curve)
    return best_state, best_fitness, fitness_curve
Пример #8
0
def compare_multi_round_k_color():
    global count
    count = 0
    fitness_obj = mlrose.CustomFitness(k_color_fit)
    opt = mlrose.DiscreteOpt(50, fitness_obj, maximize=True, max_val=8)
    fitness_list_rhc = []
    fitness_list_ann = []
    fitness_list_genetic = []
    fitness_list_mimic = []
    num_sample_rhc = []
    num_sample_ann = []
    num_sample_genetic = []
    num_sample_mimic = []
    for i in range(20):
        best_state_climb, best_fitness_climb, fitness_curve_climb = mlrose.random_hill_climb(
            opt, curve=True)
        fitness_list_rhc.append(best_fitness_climb)
        num_sample_rhc.append(count)
        count = 0
        best_state_ann, best_fitness_ann, fitness_curve_ann = mlrose.simulated_annealing(
            opt, schedule=mlrose.ExpDecay(), curve=True)
        fitness_list_ann.append(best_fitness_ann)
        num_sample_ann.append(count)
        count = 0
        best_state_ga, best_fitness_ga, fitness_curve_ga = mlrose.genetic_alg(
            opt, pop_size=500, mutation_prob=0.5, curve=True)
        fitness_list_genetic.append(best_fitness_ga)
        num_sample_genetic.append(count)
        count = 0
        best_state_mimic, best_fitness_mimic, fitness_curve_mimic = mlrose.mimic(
            opt, pop_size=500, curve=True)
        fitness_list_mimic.append(best_fitness_mimic)
        num_sample_mimic.append(count)
        count = 0
    plt.figure(figsize=(10, 6))
    plt.subplot(121)
    plt.plot(fitness_list_rhc, label='rhc')
    plt.plot(fitness_list_ann, label='ann')
    plt.plot(fitness_list_genetic, label='ga')
    plt.plot(fitness_list_mimic, label='mimic')
    plt.xlabel('rounds')
    plt.ylabel('finess value')
    plt.title('fitness value comparision')
    plt.legend(loc='lower right')
    plt.subplot(122)
    plt.plot(num_sample_rhc, label='rhc')
    plt.plot(num_sample_ann, label='ann')
    plt.plot(num_sample_genetic, label='ga')
    plt.plot(num_sample_mimic, label='mimic')
    plt.xlabel('rounds')
    plt.ylabel('fitness calls')
    plt.title('fitness call number comparision')
    plt.legend(loc='upper right')
    plt.show()
Пример #9
0
def get_hill_climb(problem, restarts=30):

    best_state, best_fitness, fitness_curve = mlrose.random_hill_climb(
        problem,
        max_attempts=100,
        max_iters=np.inf,
        restarts=restarts,
        init_state=None,
        curve=True,
        random_state=23)

    return best_state, best_fitness, fitness_curve
Пример #10
0
def rhc_optimization(size):
    algo = 'rhc'
    problem = get_problem(size)

    # Gridsearch params
    max_iters = 500
    restarts_values = [10, 30, 50]
    max_attempts_values = [10, 50, 100, 200]
    n_runs = len(restarts_values) * len(max_attempts_values)

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

    # Gridsearch
    global eval_count
    run_counter = 0
    for run_restarts in restarts_values:
        for run_max_attempts in max_attempts_values:
            # Print status
            run_counter += 1
            print(
                f'RUN {run_counter} of {n_runs} [restarts: {run_restarts}] [max_attempts: {run_max_attempts}]')

            # Run problem
            eval_count = 0
            start = timer()
            run_state, run_fitness, run_curves = mlrose_hiive.random_hill_climb(problem,
                                                                                restarts=run_restarts,
                                                                                max_attempts=run_max_attempts,
                                                                                max_iters=max_iters,
                                                                                random_state=42,
                                                                                curve=True)
            end = timer()

            # Save curves and params
            if run_fitness > fitness:
                restarts = run_restarts
                max_attempts = run_max_attempts
                fitness = run_fitness
                curves = run_curves
                n_invocations = eval_count
                time = end - start

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

    print(f'{algo}_{size} run.')
Пример #11
0
    def test_random_hill_climb_continuous_min():
        """Test random_hill_climb function for a continuous minimization
        problem"""

        problem = ContinuousOpt(5, OneMax(), maximize=False)
        best_state, best_fitness, _ = random_hill_climb(problem,
                                                        max_attempts=10,
                                                        restarts=20)

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

        assert (np.array_equal(best_state, x) and best_fitness == 0)
Пример #12
0
    def test_random_hill_climb_discrete_max():
        """Test random_hill_climb function for a discrete maximization
        problem"""

        problem = DiscreteOpt(5, OneMax(), maximize=True)
        best_state, best_fitness, _ = random_hill_climb(problem,
                                                        max_attempts=10,
                                                        restarts=20)

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

        assert (np.array_equal(best_state, x) and best_fitness == 5)
Пример #13
0
def plot_optimization_problem_fitness(fitness_function, iterations, random_state, title):

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

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

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

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

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

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

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

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

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

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

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

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

    plt.ylabel("Fitness Function")

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

    plt.savefig('Charts/OptimizationProblems/' + title + '.png')
    plt.clf()
Пример #14
0
    def test_random_hill_climb_max_iters():
        """Test random_hill_climb function with max_iters less than infinite"""

        problem = DiscreteOpt(5, OneMax(), maximize=True)
        x = np.array([0, 0, 0, 0, 0])

        best_state, best_fitness, _ = random_hill_climb(problem,
                                                        max_attempts=1,
                                                        max_iters=1,
                                                        restarts=0,
                                                        init_state=x)

        assert best_fitness == 1
Пример #15
0
def randomized_hill_climb(problem, init_state, max_attempts, max_iters):
    start_time = time.time()
    best_state, best_fitness, fitness_curve = mlr.random_hill_climb(
        problem,
        max_attempts,
        max_iters,
        restarts=10,
        init_state=init_state,
        curve=True,
        random_state=RANDOM_STATE)
    end_time = time.time()
    total_time = end_time - start_time

    print('Random Hill Climb')
    print("Elapsed Time", total_time)
    # print(best_state)
    print(best_fitness)
    #     print(fitness_curve)
    return best_state, best_fitness, fitness_curve, total_time
Пример #16
0
def run_RHC_3(problem, init_state, **kwargs):
    fit_vals = []
    fit_curves = []
    times = []
    fevals = []

    # run multiple times to get average
    for random_state in random_states:
        start = time.time()
        _, best_fit, fit_curve, evals = random_hill_climb(
            problem,
            random_state=random_state,
            restarts=39,
            **kwargs,
            curve=True,
            init_state=init_state,
            fevals=True)

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

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

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

    avg_fit = np.average(fit_vals)
    avg_time = round(np.mean(times), 2)
    avg_evals = np.mean(fevals)
    print(f"RHC {problem_name}: {avg_fit}: {avg_time}: {avg_evals}")
    return avg_fit, avg_time, avg_evals
Пример #17
0
def RHC_tests(problem_fit, ma, maxIter=1000, seed=1, input_size=-1, restarts=0):
    global eval_count, iter_total, algo_in, start, times, evals

    start = time.time()
    eval_count, iter_total, times, evals = 0, 0, [], []
    algo_in = 'RHC'
    best_state, best_fitness, rhcfitness_curve = mlrose.random_hill_climb(problem_fit, curve=True, max_attempts=ma,
                                                                          state_fitness_callback=callback_func,
                                                                          callback_user_info=[], restarts=restarts,
                                                                          random_state=seed, max_iters=maxIter)
    rhc_times = copy.deepcopy(times)
    rhc_evals = copy.deepcopy(evals)
    print('RHC', input_size,
          restarts,
          seed,
          best_fitness,
          np.where(rhcfitness_curve == best_fitness)[0][0],  # iter of best fitness
          iter_total,  # total iters
          np.where(rhcfitness_curve == best_fitness)[0][0],  # num evals for best fitness
          eval_count,  # total evals
          # round(rhc_times[np.where(rhcfitness_curve == best_fitness)[0][0]], 4),  # time of best fitness
          round(rhc_times[-1], 4)  # total time
          )
Пример #18
0
def rhc(problem, iterations, random_seed, graph_file, graph_title):
    fitness = []
    fit_time = []
    fn_evals = []
    global eval_count
    for i in iterations:
        eval_count = 0
        start = datetime.datetime.now()
        best_state, best_fitness, _ = mlrose_hiive.random_hill_climb(problem,
                                   max_iters=i, random_state=random_seed)
        finish = datetime.datetime.now()
        fitness.append(best_fitness)
        fit_time.append((finish - start).total_seconds())
        fn_evals.append(eval_count)

    plt.plot(iterations, fitness, label="Fitness score")
    plt.legend(loc="best")
    plt.grid()
    generate_graph(graph_file + "rhc", graph_title + "Random Hill Climbing",
                   "Iterations", "Fitness")
    print('Best score achieved: ', max(fitness))
    index = fitness.index(max(fitness))
    print('Time taken to achieve that: ', fit_time[index])
    print('Function evaluations taken to achieve that: ', fn_evals[index])
Пример #19
0
def method_compare(opt, train_features_spam_norm, train_labels_spam,
                   test_features_spam_norm, test_labels_spam):
    #best_state_spam,best_fitness_spam,fitness_curve=mlrose.simulated_annealing(opt,schedule=mlrose.ExpDecay(),curve=True)
    #best_state_spam,best_fitness_spam,fitness_curve=mlrose.genetic_alg(opt,pop_size=2000,curve=True)

    global count
    count = 0
    loss_list_rhc = []
    loss_list_ann = []
    loss_list_ga = []

    train_acc_list_rhc = []
    train_acc_list_ann = []
    train_acc_list_ga = []
    test_acc_list_rhc = []
    test_acc_list_ann = []
    test_acc_list_ga = []

    fitness_call_list_rhc = []
    fitness_call_list_ann = []
    fitness_call_list_ga = []
    # ten rounds of rhc
    for i in range(10):
        count = 0
        best_state_spam, best_fitness_spam, fitness_curve = mlrose.random_hill_climb(
            opt, restarts=70, curve=True)
        loss_list_rhc.append(best_fitness_spam)
        train_predict_rhc = predict(best_state_spam, train_features_spam_norm)
        test_predict_rhc = predict(best_state_spam, test_features_spam_norm)
        train_acc_list_rhc.append(
            accuracy_score(train_labels_spam, train_predict_rhc))
        test_acc_list_rhc.append(
            accuracy_score(test_labels_spam, test_predict_rhc))
        fitness_call_list_rhc.append(count)
    #ten rounds of simulated annealing
    for i in range(10):
        count = 0
        best_state_spam, best_fitness_spam, _ = mlrose.simulated_annealing(
            opt, schedule=mlrose.ExpDecay(exp_const=0.003), curve=True)
        loss_list_ann.append(best_fitness_spam)
        train_predict_ann = predict(best_state_spam, train_features_spam_norm)
        test_predict_ann = predict(best_state_spam, test_features_spam_norm)
        train_acc_list_ann.append(
            accuracy_score(train_labels_spam, train_predict_ann))
        test_acc_list_ann.append(
            accuracy_score(test_labels_spam, test_predict_ann))
        fitness_call_list_ann.append(count)
    #ten rounds of genetic algorithm
    for i in range(10):
        count = 0
        best_state_spam, best_fitness_spam, _ = mlrose.genetic_alg(
            opt, pop_size=1000, curve=True)
        loss_list_ga.append(best_fitness_spam)
        train_predict_ga = predict(best_state_spam, train_features_spam_norm)
        test_predict_ga = predict(best_state_spam, test_features_spam_norm)
        train_acc_list_ga.append(
            accuracy_score(train_labels_spam, train_predict_ga))
        test_acc_list_ga.append(
            accuracy_score(test_labels_spam, test_predict_ga))
        fitness_call_list_ga.append(count)

    #plot loss curve
    plt.figure(figsize=(6, 6))
    plt.plot(np.arange(1, 11), loss_list_rhc, label='rhc')
    plt.plot(np.arange(1, 11), loss_list_ann, label='s_ann')
    plt.plot(np.arange(1, 11), loss_list_ga, label='ga')
    plt.xlabel('rounds')
    plt.ylabel('-1*losss')
    plt.title('loss versus different algorithm')
    plt.legend()
    plt.show()

    #plot acc curve
    plt.figure(figsize=(15, 6))
    plt.subplot(131)
    plt.plot(np.arange(1, 11), train_acc_list_rhc, label='train')
    plt.plot(np.arange(1, 11), test_acc_list_rhc, label='test')
    plt.xlabel('rounds')
    plt.ylabel('accuracy')
    plt.title('rhc')
    plt.legend()
    plt.subplot(132)
    plt.plot(np.arange(1, 11), train_acc_list_ann, label='train')
    plt.plot(np.arange(1, 11), test_acc_list_ann, label='test')
    plt.xlabel('rounds')
    plt.ylabel('accuracy')
    plt.title('simulated annealing')
    plt.legend()
    plt.subplot(133)
    plt.plot(np.arange(1, 11), train_acc_list_ga, label='train')
    plt.plot(np.arange(1, 11), test_acc_list_ga, label='test')
    plt.xlabel('rounds')
    plt.ylabel('accuracy')
    plt.title('genetic algorithm')
    plt.legend()

    #plot fitness call
    plt.figure(figsize=(6, 6))
    plt.plot(np.arange(1, 11), fitness_call_list_rhc, label='rhc')
    plt.plot(np.arange(1, 11), fitness_call_list_ann, label='s_ann')
    plt.plot(np.arange(1, 11), fitness_call_list_ga, label='ga')
    plt.xlabel('rounds')
    plt.ylabel('fitness call number')
    plt.title('fitness call num versus different algorithm')
    plt.legend()
    plt.show()
Пример #20
0
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)

    ## Simulated Annealing
    sub_start = time.time()
    best_sa_state, best_sa_fitness, sa_curve = mlr.simulated_annealing(
        problem,
        schedule=schedule,
        max_attempts=attempts,
        random_state=seed,
Пример #21
0
def Knapsack():
    rs = 2  #random state
    ma = 200  #max attempts

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    plt.title("Knapsack problem using MIMIC with different parameters")
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("Knapsack MIMIC parameter pop pct")
    plt.show()
Пример #22
0
                    max_attempts=200,
                    iteration_list=[2500],
                    restart_list=[20])
rhc_stats, rhc_curve = rhc.run()

columns = ['Time', 'Fitness', 'Restarts', 'current_restart']
df=pd.read_csv("./queen/queen_prob/rhc__queen_prob__run_stats_df.csv")
print(df[columns].sort_values(by=['Fitness'], ascending=False))

max_attempts = 500
max_iters = 2500
restarts = 20
eval_count = 0
best_state, best_fitness, rhc_curve = mlrh.random_hill_climb(prob,
                                                         max_attempts=max_attempts,
                                                         max_iters=max_iters,
                                                         random_state=random_state,
                                                         restarts=restarts,
                                                         curve=True)
print("Randomized Hill Climbing - Total Function Evaluations:", eval_count)
plot_fitness_iteration('fitness_iteration_rhc_queens.png',rhc_curve,
                       "Queens - Randomized Hill Climbing: restarts: {}".format(restarts))


# Fitness vs iterations (combined)

all_curves = {"MIMIC": mimic_curve, "Genetic Alg": gen_curve, "Simulated Annealing": sa_curve, "Random Hill Climb": rhc_curve}

np.array([len(x) for x in all_curves]).max()

plt.xlabel("Iterations")
plt.ylabel("Fitness")
Пример #23
0
plt.close()

print(df.max())

#%% Putting together
RANDOM_SEED = 21

curve_list = []
time_list = []
n_eval = []
algo_list = ["RHC", "SA", "GA", "MIMIC"]

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

# SA
t1 = process_time()
_, _, curve = mlrose.simulated_annealing(
    problem, max_attempts=MAX_ATTEMPTS, curve=True, random_state=RANDOM_SEED,
)
t2 = process_time()
time_list.append((t2 - t1) / len(curve))
curve_list.append(curve)
n_eval.append(np.argmin(curve) + 1)
Пример #24
0
def get_random_optimisation(problem,
                            optimisation,
                            problem_type,
                            seed=10,
                            state_fitness_callback=None):
    best_state, best_fitness, fitness_curve = None, None, None
    process_time = []

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

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

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

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

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

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

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

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

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

            for n, init_states_list in init_states.items():
                if fitness_name == 'MaxKColor':
                    fitness_fn = knap_fitnesses[n]
                if fitness_name == 'TravellingSales':
                    fitness_fn = tsp_fitnesses[n]
                print('%s: i=%d' % ('mimic', n))
                if n > 256:
                    break
                total_score = 0
                total_iter = 0
                start = time.time()
                for init_state in init_states_list:
                    problem = mlrose.DiscreteOpt(length=len(init_state),
                                                 fitness_fn=fitness_fn,
                                                 maximize=True)
                    if fitness_name == 'TravellingSales':
                        problem = mlrose.TSPOpt(length=n,
                                                fitness_fn=fitness_fn)
                    keep_pct = self.get_best_param(problem=fitness_name,
                                                   algo='mimic',
                                                   param='keep_pct')
                    pop_size = self.get_best_param(problem=fitness_name,
                                                   algo='mimic',
                                                   param='pop_size')
                    best_state, best_fitness, curve = mlrose.mimic(
                        problem,
                        max_iters=10000,
                        random_state=1,
                        curve=True,
                        pop_size=pop_size,
                        keep_pct=keep_pct,
                        max_attempts=10)
                    total_score += np.mean(curve)
                    total_iter += len(curve)
                end = time.time()
                print('The fitness at the best state is: ',
                      total_score / tries)
                self.track(problem=fitness_name,
                           algo='mimic',
                           i=n,
                           score=total_score / tries,
                           training_time=(end - start) / tries,
                           max_iter=total_iter / tries)
Пример #26
0
    def run_rhc_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' % ('random_hill_climb', 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.random_hill_climb(
                        problem,
                        max_attempts=max_attempts,
                        max_iters=10000,
                        random_state=1,
                        curve=True,
                        restarts=10)
                    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='random_hill_climb',
                                           param='max_attempts',
                                           score=total_score,
                                           value=max_attempts)

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

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

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

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

    print('GA')

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

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

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

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

    print('MIMIC')

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

    if min:
        # To Maximize TSP, need to make everything negative
        gafitness_curve = np.array(gafitness_curve) * -1
        rhcfitness_curve = np.array(rhcfitness_curve) * -1
        safitness_curve = np.array(safitness_curve) * -1
        mimicfitness_curve = np.array(mimicfitness_curve) * -1
        return gafitness_curve, rhcfitness_curve, safitness_curve, mimicfitness_curve, ga_times, rhc_times, sa_times, mim_times, rhc_evals, sa_evals, ga_evals, mim_evals
    else:
        return gafitness_curve, rhcfitness_curve, safitness_curve, mimicfitness_curve, ga_times, rhc_times, sa_times, mim_times, rhc_evals, sa_evals, ga_evals, mim_evals
Пример #28
0
FITNESS_FUNCS = {
    'fourpeaks': mlrose.FourPeaks(),
    'onemax': mlrose.OneMax(),
    #    'path': mlrose.CustomFitness(path, problem_type='discrete'),
    'flipflop': mlrose.FlipFlop(),
    #    'cliffs': mlrose.CustomFitness(cf1, problem_type='discrete'),
    #    'cliffs': mlrose.CustomFitness(is_larger, problem_type='discrete'),
    #    'max2color': mlrose.MaxKColorGenerator.generate(seed=42, number_of_nodes=PROBLEM_LENGTH, max_colors=2),
    #    'mod': mlrose.CustomFitness(cf2, problem_type='discrete')
}

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

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

results = []

PART_1 = True
PART_2 = True

if PART_1:
    for f_name, fitness in FITNESS_FUNCS.items():
        evaluate_fitness(f_name, fitness, f_name == 'max2color')
        alg2curve = {}
        overall_best_fitness = -1
Пример #29
0
def main():

    four_peaks_fitness = four_peaks.get_four_peaks()
    four_peaks_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(100, four_peaks_fitness, maximize=True, max_val=2)
    problem_list = []
    for item in four_peaks_list:
        problem_list.append(mlrose.DiscreteOpt(100, item, maximize=True, max_val=2))
    # Random hill climb
    print('Random Hill Climb')
    print(mlrose.random_hill_climb(problem, max_attempts=100, max_iters=1000, restarts=100, init_state=None, curve=True, random_state=27))

    for item in range(len(four_peaks_list)):
        experiment_name = 'rhc_tuning_' + str(item)
        rhc = runners.RHCRunner(problem=problem,
                experiment_name=experiment_name,
                output_directory='./',
                seed=27,
                iteration_list=2 ** np.arange(10),
                max_attempts=5000,
                restart_list=[25],
                max_iters=10)
        # the two data frames will contain the results
        df_run_stats, df_run_curves = rhc.run()
        print(df_run_curves.loc[df_run_curves['Fitness'].idxmax()])
    return
# the two data frames will contain the results
#df_run_stats, df_run_curves = sa.run()
# four peaks, knapsack and k-colors
#four_peaks_fitness = mlrose.FourPeaks(t_pct=0.15)
#state = np.array([1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0])
#fitness.evaluate(state)

# Knapsack
#weights = [10, 5, 2, 8, 15]
#weights = np.ones(100)
#values = [1, 2, 3, 4, 5]
# values = np.arange(1, 101)
# max_weight_pct = 0.6
# knapsack_fitness = mlrose.Knapsack(weights, values, max_weight_pct)
# state = np.array([1, 0, 2, 1, 0])
#fitness.evaluate(state)

# K-colors
# edges = [(0, 1), (0, 2), (0, 4), (1, 3), (2, 0), (2, 3), (3, 4)]
# k_color_fitness = mlrose.MaxKColor(edges)
# state = np.array([0, 1, 0, 1, 1])
#fitness.evaluate(state)

# Two aspects of optimization:
#  - Performance
#  - Runtime
#  Make sure to have numbers and analysis for both these numbers for all parts

# For part 1 (requires lots of tuning again, yay)
#  - grab some problems: Salesman, knapsack
#  - Make sure to highlight (one algorithm outperforms all the others significantly) each of the 3
#  - Lose major points if one is missing. Important part is highlighting here
#  - Vary problem size/difficulties, some different stuff. Should change the behavior
#  - If there is a change, see when one starts beating the others and explain why
#  - People are having problems getting MIMIC to outperforms (K-coloring should work)
#  - Play with hyper parameters if it doesnt outperform when should. Also TIMING >= Accuracy here
#  - mlrose is bad for MIMIC, takes a lot of tuning, previous semesters people have re-written internal mlrose code
#  - max iteration should be tuning independantly for each separate algorithm, some algorithms take way more iterations to converge
#  - Dont retune for every different problem size, tune once for one size, find the results you want, use the same HPs for other sizes and show how it changes
#  - For varying problem size, vary in smaller increments, some 10 -> 50 to see a difference, some 20 -> 30
#  - Some are quadratic/exponential, watch out for enormous times for problem size varying.

# ALL THESE PLOTS HAVE TO BE IN THIS: (called this summary of whole assignment)
# 1. F-evals vs fitness on single plot with Iterations vs fitness
# 2. Hyper parameter tuning for both problem and algorithms
# 3. Complexity vs Fitness on single plot with Complexity vs fevals

# max iteration should be tuning independantly for each separate algorithm, some algorithms take way more iterations to converge

# For varying problem size, vary length (LEAVE MAX_VAL AS 2 SO THEY ARE BIT STRINGS)
#problem = mlrose.DiscreteOpt(100, four_peaks_fitness, maximize=True, max_val=2)

# Random hill climb
# print('Random Hill Climb')
# print(mlrose.random_hill_climb(problem, max_attempts=100, max_iters=1000, restarts=100, init_state=None, curve=True, random_state=27))

# # Simulated_annealing
# print('Simulated annealing')
# #print(mlrose.simulated_annealing(problem, schedule=mlrose.GeomDecay(), max_attempts=100, max_iters=1000, init_state=None, curve=True, random_state=27))
# experiment_name = 'example_experiment'

# sa = runners.SARunner(problem=problem,
#                 experiment_name=experiment_name,
#                 output_directory='./',
#                 seed=27,
#                 iteration_list=2 ** np.arange(14),
#                 max_attempts=5000,
#                 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()

# # Genetic algorithm (best with four peaks)
# print('Genetic Algorithm')
# print(mlrose.genetic_alg(problem, pop_size=200, mutation_prob=0.1, max_attempts=100, max_iters=1000, curve=True, random_state=27))

# # MIMIC
# print('MIMIC')
# print(mlrose.mimic(problem, pop_size=200, keep_pct=0.2, max_attempts=10, max_iters=10, curve=True, random_state=27))
Пример #30
0
def ContinuousPeaks():
    rs = 2  # random_state
    ma = 100  # max_attempts

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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