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
def n_queens_sa(nq_problem,
                initial_state,
                max_iters=np.inf,
                num_runs=20,
                verbose=False):
    hp_name = 'schedule'
    hp_values = [mlrose.ArithDecay(), mlrose.GeomDecay(), mlrose.ExpDecay()]
    hp_values_strings = [
        val.get_info__()['schedule_type'] for val in hp_values
    ]

    # run for each hp value and append results to list

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

    for hp_value, hp_value_string in zip(hp_values, hp_values_strings):
        schedule = 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.simulated_annealing(
                problem=nq_problem,
                schedule=schedule,
                max_attempts=10,
                max_iters=max_iters,
                curve=True,
            )
            run_time = time() - run_t0
            run_times[run] = run_time

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

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

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

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

    return fitness_dfs
示例#3
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()
示例#4
0
    def run_sa_hyper_params(self, fitness_fn):
        fitness_name = fitness_fn.__class__.__name__
        print("Running %s" % fitness_name)
        init_states = {}
        knap_fitnesses = {}
        tsp_fitnesses = {}
        tries = 1
        for x in 2**np.arange(6, 7):
            n = int(x)
            fitness_dists = mlrose.TravellingSales(distances=get_coords(n))
            tsp_fitnesses[n] = fitness_dists
            edges = []
            for x in range(int(n * 0.75)):
                a = r.randint(0, n - 1)
                b = r.randint(0, n - 1)
                while b == a:
                    b = r.randint(0, n - 1)
                edges.append((a, b))

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

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

            for init_state in init_states_list:
                problem = mlrose.DiscreteOpt(length=len(init_state),
                                             fitness_fn=fitness_fn,
                                             maximize=True)
                if fitness_name == 'TravellingSales':
                    problem = mlrose.TSPOpt(length=n, fitness_fn=fitness_fn)
                for max_attempts in range(10, 110, 10):
                    total_score = 0
                    total_iter = 0
                    best_state, best_fitness, curve = mlrose.simulated_annealing(
                        problem,
                        max_attempts=max_attempts,
                        max_iters=10000,
                        random_state=1,
                        curve=True)
                    total_score += np.max(curve)
                    total_iter += len(curve)
                    print('The fitness at the best state is: ',
                          total_score / tries, '. Max Attempts: ',
                          max_attempts)
                    self.track_best_params(problem=fitness_name,
                                           algo='simulated_annealing',
                                           param='max_attempts',
                                           score=total_score,
                                           value=max_attempts)
示例#5
0
    def test_simulated_annealing_continuous_min():
        """Test simulated_annealing function for a continuous minimization
        problem"""

        problem = ContinuousOpt(5, OneMax(), maximize=False)
        best_state, best_fitness, _ = simulated_annealing(problem,
                                                          max_attempts=50)

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

        assert (np.array_equal(best_state, x) and best_fitness == 0)
示例#6
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()
def simulated_annealing(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/"

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

    for iters in iterlist:
        for CE in [0.20, 0.40, 0.60, 0.80, 1.0]:
            CEl.append(CE)
            start = clock()
            best_state, best_fitness, train_curve = mlrose.simulated_annealing(problem_fit,\
                                                                        max_iters=int(iters),\
                                                                        curve=True, \
                                                                        schedule=mlrose.GeomDecay(init_temp=CE),
                                                                        random_state=randomSeed)

            end = clock()
            time.append(end - start)
            beststate.append(best_state)
            bestfit.append(best_fitness)
            curve.append(train_curve)
            iterlistn.append(int(iters))
            if (verbose == True):
                print(CE)
                print(int(iters))
                print(best_state)
                print(best_fitness)

    ffsa = pd.DataFrame({
        'Best Fitness': bestfit,
        'Iterations': iterlistn,
        'Time': time,
        'CE': CEl
    })
    beststatedf = pd.DataFrame(0.0,
                               index=range(1, vectorLength + 1),
                               columns=range(len(beststate)))
    for i in range(len(curve)):
        pd.DataFrame(curve[i]).to_csv(
            os.path.join(path2,
                         'sacurve_{}_{}.csv'.format(iterlistn[i], CEl[i])))

    for i in range(1, len(beststate) + 1):
        beststatedf.loc[:, i] = beststate[i - 1]
    ffsa.to_csv(os.path.join(path1, 'sa.csv'))
    beststatedf.to_csv(os.path.join(path1, 'sastates.csv'))
示例#8
0
    def test_simulated_annealing_discrete_max():
        """Test simulated_annealing function for a discrete maximization
        problem"""

        problem = DiscreteOpt(5, OneMax(), maximize=True)
        best_state, best_fitness, _ = simulated_annealing(problem,
                                                          max_attempts=50)

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

        assert (np.array_equal(best_state, x) and best_fitness == 5)
示例#9
0
def get_sim_ann(problem, schedule=mlrose.GeomDecay()):

    best_state, best_fitness, fitness_curve = mlrose.simulated_annealing(
        problem,
        schedule=schedule,
        max_attempts=100,
        max_iters=np.inf,
        init_state=None,
        curve=True,
        random_state=2)

    return best_state, best_fitness, fitness_curve
示例#10
0
def sa_optimization(size):
    algo = 'sa'
    problem = get_problem(size)

    # Gridsearch params
    max_iters = 500
    schedule_values = [mlrose_hiive.GeomDecay(), mlrose_hiive.ArithDecay(), mlrose_hiive.algorithms.decay.ExpDecay()]
    max_attempts_values = [10, 50, 100, 200]
    n_runs = len(schedule_values) * len(max_attempts_values)

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

    # Gridsearch
    global eval_count
    run_counter = 0
    for run_schedule in schedule_values:
        for run_max_attempts in max_attempts_values:
            # Print status
            run_counter += 1
            print(f'RUN {run_counter} of {n_runs} [schedule: {run_schedule.__class__.__name__}] [max_attempts: {run_max_attempts}]')

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

            # Save curves and params
            if run_fitness > fitness:
                schedule = run_schedule.__class__.__name__
                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['schedule'] = schedule
    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 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()
示例#12
0
    def test_simulated_annealing_max_iters():
        """Test simulated_annealing 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, _ = simulated_annealing(problem,
                                                          max_attempts=1,
                                                          max_iters=1,
                                                          init_state=x)

        assert best_fitness == 1
示例#13
0
def sa(problem, init_state, max_attempts, max_iters):
    # Define decay schedule
    schedule = mlrose_hiive.ExpDecay()
    # Define initial state
#     init_state = np.array([0, 1, 2, 3, 4, 5, 6, 7])
    # Solve problem using simulated annealing
    best_state, best_fitness, fitness_curve  = mlrose_hiive.simulated_annealing(problem, schedule = schedule,
                                                          max_attempts = max_attempts, max_iters = max_iters,init_state = init_state, curve=True, random_state = 1)
    print('simulated annealing')                                                     
    print(best_state)
    print(best_fitness)
#     print(fitness_curve)
    return best_state, best_fitness, fitness_curve
示例#14
0
def scale_ann(train_features_spam_norm, train_labels_spam,
              test_features_spam_norm, test_labels_spam):
    global count
    train_acc_list = []
    test_acc_list = []
    fitness_call_list = []
    loss_list = []
    for i in range(400, 4001, 400):
        count = 0
        train_features_sub = train_features_spam_norm[:i, :]
        train_labels_sub = train_labels_spam[:i]
        fitness_obj = mlrose.CustomFitness(spam_nn_fit,
                                           train_features=train_features_sub,
                                           train_labels=train_labels_sub)
        opt = mlrose.DiscreteOpt(237, fitness_obj, maximize=True, max_val=1001)
        best_state_spam, best_fitness_spam, _ = mlrose.simulated_annealing(
            opt, schedule=mlrose.ExpDecay(exp_const=0.003), curve=True)
        loss_list.append(best_fitness_spam)
        train_predict = predict(best_state_spam, train_features_sub)
        test_predict = predict(best_state_spam, test_features_spam_norm)
        fitness_call_list.append(count)
        train_acc_list.append(accuracy_score(train_labels_sub, train_predict))
        test_acc_list.append(accuracy_score(test_labels_spam, test_predict))
    plt.figure(figsize=(10, 6))
    plt.subplot(121)
    plt.plot(np.arange(400, 4001, 400), loss_list, label='-1*loss')
    plt.xlabel('training size')
    plt.ylabel('-1*loss')
    plt.title('loss versus training size')
    plt.legend()
    plt.subplot(122)
    plt.plot(np.arange(400, 4001, 400), train_acc_list, label='train')
    plt.plot(np.arange(400, 4001, 400), test_acc_list, label='test')
    plt.xlabel('training size')
    plt.ylabel('accuracy')
    plt.title('accuracy versus training size')
    plt.legend()
    plt.show()
    # fitness calls versus training size
    plt.figure(figsize=(6, 6))
    plt.plot(np.arange(400, 4001, 400), fitness_call_list, label='#.calls')
    plt.xlabel('training size')
    plt.ylabel('fitness calls')
    plt.legend()
    plt.show()
示例#15
0
def run_SA_3(problem, init_state, **kwargs):
    start = time.time()
    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 = simulated_annealing(
            problem,
            random_state=random_state,
            schedule=GeomDecay(decay=0.94),
            **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/sa_{problem_name}_{len(init_state)}_{dt_string}"

    # plt.plot(average_curves(fit_curves), label="sa")
    # plt.title(f"SA {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"SA {problem_name}: {avg_fit}: {avg_time}: {avg_evals}")
    return avg_fit, avg_time, avg_evals
示例#16
0
def plot_SAdecay(problem, problem_fit, max_attempts, init_temp, maxIter, seed, min=False):
    decay_r = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95]
    plt.figure()
    for d in decay_r:
        SAschedule = mlrose.GeomDecay(init_temp=init_temp, decay=d, min_temp=0.01)
        best_state, best_fitness, safitness_curve = mlrose.simulated_annealing(problem_fit, schedule=SAschedule,
                                                                               curve=True, max_attempts=max_attempts,
                                                                               random_state=seed, max_iters=maxIter)
        if min:
            safitness_curve = np.array(safitness_curve) * -1
        plt.plot(safitness_curve, label='decay rate = ' + str(d))

    plt.title(problem + " - SA - Decay Rates")
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("Images\\" + problem + " - SA - Decay Rates")
    plt.show()
示例#17
0
def plot_SATemps(problem, problem_fit, max_attempts, decay, maxIter, seed, min=False):
    temps = [10000000, 1000000, 100000, 10000, 1000, 100, 10, 1, 0.1]
    plt.figure()
    for t in temps:
        SAschedule = mlrose.GeomDecay(init_temp=t, decay=decay, min_temp=0.01)
        best_state, best_fitness, safitness_curve = mlrose.simulated_annealing(problem_fit, schedule=SAschedule,
                                                                               curve=True, max_attempts=max_attempts,
                                                                               random_state=seed, max_iters=maxIter)
        if min:
            safitness_curve = np.array(safitness_curve) * -1
        plt.plot(safitness_curve, label='Temperature = ' + str(t))

    plt.title(problem + " - SA - Initial Temperature")
    plt.xlabel('Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)
    plt.legend()
    plt.savefig("Images\\" + problem + " - SA - Initial Temperature")
    plt.show()
示例#18
0
def simulated_annealing(problem, init_state, max_attempts, max_iters):
    schedule = mlr.ExpDecay()  # Tune this?

    start_time = time.time()
    best_state, best_fitness, fitness_curve = mlr.simulated_annealing(
        problem,
        schedule=schedule,
        max_attempts=max_attempts,
        max_iters=max_iters,
        init_state=init_state,
        curve=True,
        random_state=RANDOM_STATE)
    end_time = time.time()
    total_time = end_time - start_time

    print('Simulated Annealing')
    print("Elapsed Time", total_time)
    #print(best_state)
    print(best_fitness)
    #     print(fitness_curve)
    return best_state, best_fitness, fitness_curve, total_time
示例#19
0
def s_ann(opt):
    global count
    count = 0
    ann_loss_list = []
    ann_train_acc_list = []
    ann_test_acc_list = []
    for exp_value in np.arange(0.0005, 0.0101, 0.0005):
        best_state_spam, best_fitness_spam, _ = mlrose.simulated_annealing(
            opt, schedule=mlrose.ExpDecay(exp_const=exp_value), curve=True)
        train_predict_gen = predict(best_state_spam, train_features_spam_norm)
        test_predict_gen = predict(best_state_spam, test_features_spam_norm)
        train_accuracy_hill = accuracy_score(train_labels_spam,
                                             train_predict_gen)
        test_accuracy_hill = accuracy_score(test_labels_spam, test_predict_gen)
        ann_loss_list.append(best_fitness_spam)
        ann_train_acc_list.append(train_accuracy_hill)
        ann_test_acc_list.append(test_accuracy_hill)
    plt.figure(figsize=(10, 6))
    plt.subplot(121)
    plt.plot(np.arange(0.0005, 0.0101, 0.0005), ann_loss_list, label='-1*loss')
    plt.xlabel('exp_const value')
    plt.ylabel('minus of loss')
    plt.title('loss versus exp_const value')
    plt.legend(loc='lower right')
    plt.subplot(122)
    plt.plot(np.arange(0.0005, 0.0101, 0.0005),
             ann_train_acc_list,
             label='train')
    plt.plot(np.arange(0.0005, 0.0101, 0.0005),
             ann_test_acc_list,
             label='test')
    plt.xlabel('exp_const value')
    plt.ylabel('accuracy')
    plt.title('accuracy versus exp_const value')
    plt.legend(loc='lower right')
    plt.show()
示例#20
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()
示例#21
0
        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,
        curve=True,
        init_state=init)
    print(f"        best SA State: {best_sa_state}")
    sub_end = time.time()
    sa_fitnesses.append(best_sa_fitness)
    sa_times.append(sub_end - sub_start)

    ## Genetic Algorithm
    sub_start = time.time()
    best_ga_state, best_ga_fitness, ga_curve = mlr.genetic_alg(
        problem,
        pop_size=200,
        max_attempts=attempts,
        random_state=seed,
示例#22
0
# Define optimization problem object
problem = mlrose.DiscreteOpt(length=8,
                             fitness_fn=fitness,
                             maximize=False,
                             max_val=8)

# Define decay schedule
schedule = mlrose.ExpDecay()

# Solve using simulated annealing - attempt 1
print('Example 1 - Attempt 1')
init_state = np.array([0, 1, 2, 3, 4, 5, 6, 7])
best_state, best_fitness, fitness_curve = mlrose.simulated_annealing(
    problem,
    schedule=schedule,
    max_attempts=10,
    max_iters=1000,
    init_state=init_state,
    random_state=1)
print(best_state)
print(best_fitness)

# Solve using simulated annealing - attempt 2
best_state, best_fitness, fitness_curve = mlrose.simulated_annealing(
    problem,
    schedule=schedule,
    max_attempts=100,
    max_iters=1000,
    init_state=init_state,
    random_state=1)
print('Example 1 - Attempt 2')
示例#23
0
def sa(problem, iterations, random_seed, graph_file, graph_title):
    decays = [0.001, 0.002, 0.003, 0.004, 0.005]
    best_score = []
    time_taken = []
    fn_evals_taken = []
    # fig1, ax1 = plt.subplots()
    # fig2, ax2 = plt.subplots()
    global eval_count
    for decay in decays:
        schedule = mlrose_hiive.ArithDecay(init_temp=1.0, decay=decay)
        fitness = []
        fit_time = []
        fn_evals = []
        for i in iterations:
            eval_count = 0
            start = datetime.datetime.now()
            # Solve using simulated annealing - attempt 1
            best_state, best_fitness, _ = mlrose_hiive.simulated_annealing(problem, schedule=schedule,
                                                                max_iters=i, random_state=random_seed)
            finish = datetime.datetime.now()
            fn_evals.append(eval_count)
            fitness.append(best_fitness)
            fit_time.append((finish - start).total_seconds())
            # print('iteration: ',i)
            # print('best_state:', best_state)
            # print('best_fitness: ', best_fitness)
        best_score.append(max(fitness))
        index = fitness.index(max(fitness))
        time_taken.append(fit_time[index])
        fn_evals_taken.append(fn_evals[index])
        # print('index: ', index)
        # print('time for that: ', fit_time[index])
        plt.plot(iterations, fitness, label="Cooling = " + str(decay))
        # ax2.plot(fn_evals, fitness, label="Cooling = " + str(decay))

    plt.legend(loc="best")
    plt.grid()
    generate_graph(graph_file + "sa_iter", graph_title + "Simulated Annealing", "Iterations", "Fitness")

    """
    ax2.legend(loc="best")
    ax2.grid()
    generate_graph("cp_sa_evals", "Continuous Peaks - Simulated Annealing", "Function evaluations", "Fitness")
    """
    # Decays best_score and time_taken
    plt.plot(decays, best_score)
    plt.grid()
    generate_graph(graph_file + "sa_decays", graph_title + "Simulated Annealing",
                   "Cooling Component", "Best Score Achieved")

    plt.plot(decays, time_taken)
    plt.grid()
    generate_graph(graph_file + "sa_decay_time", graph_title + "Simulated Annealing",
                   "Cooling Component", "Time taken to achieve that")

    plt.scatter(time_taken, best_score)
    for i, txt in enumerate(decays):
        plt.annotate(s=str(txt), xy=(time_taken[i], best_score[i]))
    plt.legend(loc='best', title='Cooling Component')
    plt.grid()
    generate_graph(graph_file + "sa_scatter", graph_title + "Simulated Annealing",
                   "Time Taken", "Best Score achieved")

    print('decays: ', decays)
    print('Best scores reached: ', best_score)
    print('Time taken to do that: ', time_taken)
    print('Function evaluations taken: ', fn_evals_taken)
示例#24
0
文件: tsp.py 项目: xiangxa/CS7641
    coords_list.append(np.random.rand(2))
fitness = mlrose.TravellingSales(coords=coords_list)
problem = mlrose.TSPOpt(prob_length, fitness)

RANDOM_SEED = 42
MAX_ATTEMPTS = 200

#%% tuning for SA
curve_list = []
decays = [0.999, 0.99, 0.9]
for d in decays:
    schedule = mlrose.GeomDecay(decay=d)
    _, _, curve = mlrose.simulated_annealing(
        problem,
        schedule=schedule,
        max_attempts=MAX_ATTEMPTS,
        max_iters=3000,
        curve=True,
        random_state=RANDOM_SEED,
    )
    curve_list.append(curve)

df = 1 / pd.DataFrame(curve_list).transpose()
df.columns = decays
df.plot()
plt.xlabel("Iteration")
plt.ylabel("Fitness")
plt.title("TSP: Fitness curve vs decay rate in SA")
plt.savefig("output/tsp_sa_decay.png")
plt.close()

print(df.max())
示例#25
0
文件: queens.py 项目: abagde93/CS7641
sa_stats, sa_curve = sa.run()


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


max_attempts = 200
max_iters = 2000
init_temp = 0.01
schedule = mlrh.ExpDecay(init_temp)
eval_count = 0
best_state, best_fitness, sa_curve = mlrh.simulated_annealing(prob,
                                                             max_attempts=max_attempts,
                                                             max_iters=max_iters,
                                                             random_state=random_state,
                                                             schedule=schedule,
                                                             curve=True)
print("Simulated Annealing - Total Function Evaluations:", eval_count)
plot_fitness_iteration('fitness_iteration_sa_queens.png', sa_curve,
                       "Queens - Simulated Annealing: schedule: {}, init_temp: {}".format(schedule.__class__.__name__, init_temp))

# GA
ga = mlrh.GARunner(problem=prob,
                   experiment_name=experiment_name,
                   output_directory=output_directory,
                   seed=random_state,
                   max_attempts=20,
                   iteration_list=[100],
                   population_sizes=[10, 100, 200, 300],
                   mutation_rates=[0.1, 0.25, 0.5, 0.75, 1.0])
示例#26
0
文件: main.py 项目: aarushig14/OMSCS
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
示例#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
文件: main.py 项目: aarushig14/OMSCS
def plot_sa_graph(prob_type):
    try:
        os.mkdir("./SA")
    except FileExistsError:
        pass
    except OSError as error:
        print("Error creating directory results.")

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

    fitness, problem = get_fitness_function(prob_type)
    process_time = []
    fitness_score = []
    for j in range(3):
        process_time.append([])
        fitness_score.append([])
        if j != 2:
            decay_const = [
                0.005, 0.01, 0.05, 0.1, 0.15, 0.25, 0.5, 0.75, 0.9, 0.99
            ]
        else:
            decay_const = [
                0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 0.9, 0.99
            ]
        for i in decay_const:
            print('.', end="")
            if j == 0:
                decay = mlrose.GeomDecay(decay=i)
            elif j == 1:
                decay = mlrose.ExpDecay(exp_const=i)
            else:
                decay = mlrose.ArithDecay(decay=i)
            start = time.time()
            best_state, best_fitness, fitness_curve = mlrose.simulated_annealing(
                problem,
                schedule=decay,
                max_attempts=1000,
                max_iters=100,
                random_state=10)
            fitness_score[j].append(best_fitness)
            process_time[j].append(time.time() - start)

    plt.figure(100)
    plt.plot([0.005, 0.01, 0.05, 0.1, 0.15, 0.25, 0.5, 0.75, 0.9, 0.99],
             fitness_score[0],
             'r',
             label='GeomDecay')
    plt.plot([0.005, 0.01, 0.05, 0.1, 0.15, 0.25, 0.5, 0.75, 0.9, 0.99],
             fitness_score[1],
             'b',
             label='ExpDecay')
    plt.plot([0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 0.9, 0.99],
             fitness_score[2],
             'g',
             label='ArithDecay')
    plt.xlim(0.0001, 0.2)
    plt.legend()
    plt.xlabel("decay constant")
    plt.ylabel("fitness score")
    plt.title('Simulated Annealing')
    plt.savefig("SA/" + "Fitness.png")

    plt.figure(101)
    plt.plot([0.005, 0.01, 0.05, 0.1, 0.15, 0.25, 0.5, 0.75, 0.9, 0.99],
             process_time[0],
             'r',
             label='GeomDecay')
    plt.plot([0.005, 0.01, 0.05, 0.1, 0.15, 0.25, 0.5, 0.75, 0.9, 0.99],
             process_time[1],
             'b',
             label='ExpDecay')
    plt.plot([0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 0.9, 0.99],
             process_time[2],
             'g',
             label='ArithDecay')
    plt.xlim(0.0001, 0.2)
    plt.legend()
    plt.xlabel("decay constant")
    plt.ylabel("process time")
    plt.title('Simulated Annealing')
    plt.savefig("SA/" + "Time.png")
示例#29
0
    '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
        for alg_name, alg in ALGORITHMS.items():
示例#30
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)