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
def compare_multi_round_k_color():
    global count
    count = 0
    fitness_obj = mlrose.CustomFitness(k_color_fit)
    opt = mlrose.DiscreteOpt(50, fitness_obj, maximize=True, max_val=8)
    fitness_list_rhc = []
    fitness_list_ann = []
    fitness_list_genetic = []
    fitness_list_mimic = []
    num_sample_rhc = []
    num_sample_ann = []
    num_sample_genetic = []
    num_sample_mimic = []
    for i in range(20):
        best_state_climb, best_fitness_climb, fitness_curve_climb = mlrose.random_hill_climb(
            opt, curve=True)
        fitness_list_rhc.append(best_fitness_climb)
        num_sample_rhc.append(count)
        count = 0
        best_state_ann, best_fitness_ann, fitness_curve_ann = mlrose.simulated_annealing(
            opt, schedule=mlrose.ExpDecay(), curve=True)
        fitness_list_ann.append(best_fitness_ann)
        num_sample_ann.append(count)
        count = 0
        best_state_ga, best_fitness_ga, fitness_curve_ga = mlrose.genetic_alg(
            opt, pop_size=500, mutation_prob=0.5, curve=True)
        fitness_list_genetic.append(best_fitness_ga)
        num_sample_genetic.append(count)
        count = 0
        best_state_mimic, best_fitness_mimic, fitness_curve_mimic = mlrose.mimic(
            opt, pop_size=500, curve=True)
        fitness_list_mimic.append(best_fitness_mimic)
        num_sample_mimic.append(count)
        count = 0
    plt.figure(figsize=(10, 6))
    plt.subplot(121)
    plt.plot(fitness_list_rhc, label='rhc')
    plt.plot(fitness_list_ann, label='ann')
    plt.plot(fitness_list_genetic, label='ga')
    plt.plot(fitness_list_mimic, label='mimic')
    plt.xlabel('rounds')
    plt.ylabel('finess value')
    plt.title('fitness value comparision')
    plt.legend(loc='lower right')
    plt.subplot(122)
    plt.plot(num_sample_rhc, label='rhc')
    plt.plot(num_sample_ann, label='ann')
    plt.plot(num_sample_genetic, label='ga')
    plt.plot(num_sample_mimic, label='mimic')
    plt.xlabel('rounds')
    plt.ylabel('fitness calls')
    plt.title('fitness call number comparision')
    plt.legend(loc='upper right')
    plt.show()
Exemple #3
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
def scale_ann(train_features_spam_norm, train_labels_spam,
              test_features_spam_norm, test_labels_spam):
    global count
    train_acc_list = []
    test_acc_list = []
    fitness_call_list = []
    loss_list = []
    for i in range(400, 4001, 400):
        count = 0
        train_features_sub = train_features_spam_norm[:i, :]
        train_labels_sub = train_labels_spam[:i]
        fitness_obj = mlrose.CustomFitness(spam_nn_fit,
                                           train_features=train_features_sub,
                                           train_labels=train_labels_sub)
        opt = mlrose.DiscreteOpt(237, fitness_obj, maximize=True, max_val=1001)
        best_state_spam, best_fitness_spam, _ = mlrose.simulated_annealing(
            opt, schedule=mlrose.ExpDecay(exp_const=0.003), curve=True)
        loss_list.append(best_fitness_spam)
        train_predict = predict(best_state_spam, train_features_sub)
        test_predict = predict(best_state_spam, test_features_spam_norm)
        fitness_call_list.append(count)
        train_acc_list.append(accuracy_score(train_labels_sub, train_predict))
        test_acc_list.append(accuracy_score(test_labels_spam, test_predict))
    plt.figure(figsize=(10, 6))
    plt.subplot(121)
    plt.plot(np.arange(400, 4001, 400), loss_list, label='-1*loss')
    plt.xlabel('training size')
    plt.ylabel('-1*loss')
    plt.title('loss versus training size')
    plt.legend()
    plt.subplot(122)
    plt.plot(np.arange(400, 4001, 400), train_acc_list, label='train')
    plt.plot(np.arange(400, 4001, 400), test_acc_list, label='test')
    plt.xlabel('training size')
    plt.ylabel('accuracy')
    plt.title('accuracy versus training size')
    plt.legend()
    plt.show()
    # fitness calls versus training size
    plt.figure(figsize=(6, 6))
    plt.plot(np.arange(400, 4001, 400), fitness_call_list, label='#.calls')
    plt.xlabel('training size')
    plt.ylabel('fitness calls')
    plt.legend()
    plt.show()
Exemple #5
0
def generate_nn_model(alg_name, hidden_nodes=[30, 20], seed=None):
    nn_model = None

    ## RHC
    if (alg_name == 'rhc'):
        nn_model = mlrose_hiive.NeuralNetwork(hidden_nodes=hidden_nodes, activation='relu',
                                              algorithm='random_hill_climb',
                                              restarts=50,
                                              bias=True, is_classifier=True,
                                              early_stopping=True, clip_max=5,
                                              random_state=seed,
                                              max_iters=1000,
                                              learning_rate=0.001,
                                              max_attempts=100)
    ## SA
    elif (alg_name == 'sa'):
        nn_model = mlrose_hiive.NeuralNetwork(hidden_nodes=hidden_nodes, activation='relu',
                                              algorithm='simulated_annealing',
                                              schedule=mlrose_hiive.ExpDecay(),
                                              bias=True, is_classifier=True,
                                              early_stopping=True, clip_max=5,
                                              random_state=seed,
                                              max_iters=1000,
                                              learning_rate=0.0001,
                                              max_attempts=100)
    ## GA
    elif (alg_name == 'ga'):
        nn_model = mlrose_hiive.NeuralNetwork(hidden_nodes=hidden_nodes, activation='relu',
                                              algorithm='genetic_alg',
                                              pop_size = 200,
                                              mutation_prob = 0.25,
                                              bias=True, is_classifier=True,
                                              early_stopping=True, clip_max=5,
                                              random_state=seed,
                                              max_iters=1000,
                                              learning_rate=0.0001,
                                              max_attempts=100)
    else:
       print('Algorithm Name Error') 

    return nn_model
Exemple #6
0
    def runSA(self):
        default = {
            'problem': self.problem,
            'schedule': self.schedule,
            'max_attempts': 10,
            'max_iters': 1000,
            'init_state': self.init_state,
            'curve': True,
            'random_state': 1
        }

        maxAttempts = [5, 10, 20]
        schedules = [mlrose.GeomDecay(), mlrose.ExpDecay(),
                     mlrose.ArithDecay()]
        bestFitness = None
        (bestState, bestCurve, bestParams) = None, None, None
        for i in maxAttempts:
            for j in schedules:
                params = _.assign(
                    {}, default, {'max_attempts': i, 'schedule': j})

                scores = []
                for r in range(5):
                    randomSeed = np.random.randint(0, 1000)
                    params = _.assign(
                        {}, params, {'random_state': randomSeed})
                    state, fitness, curve = self._run(
                        mlrose.simulated_annealing, name='%s' % i, **params)
                    scores.append(fitness)
                avgFitness = np.mean(scores)

                if bestFitness == None or (self.isMaximize and avgFitness > bestFitness) or (not self.isMaximize and avgFitness < bestFitness):
                    bestFitness = avgFitness
                    (bestState, bestCurve, bestParams) = state, curve, params
                # if fitness == 0:
                #     break
        print('SA - Params: %s' % bestParams)
        log.info('\tSA - Best fitness found: %s\n\t\tmaxAttempts: %s \n\t\tschedule: %s' %
                 (bestFitness, bestParams['max_attempts'], type(bestParams['schedule']).__name__))

        return bestCurve
Exemple #7
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
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()
Exemple #9
0
#transform data using StandardScaler
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
hidden_layers = [12, 6, 4, 2]
models = {}

algos = {
    'SA': {
        'algorithm': ['simulated_annealing'],
        'schedule': [
            mlrose.GeomDecay(init_temp=1, decay=0.99),
            mlrose.GeomDecay(init_temp=100, decay=0.99),
            mlrose.GeomDecay(init_temp=1000, decay=0.99),
            mlrose.ExpDecay(init_temp=1, exp_const=0.05),
            mlrose.ExpDecay(init_temp=100, exp_const=0.05),
            mlrose.ExpDecay(init_temp=1000, exp_const=0.05),
        ],
        'max_iters': [3000],
        'learning_rate': [0.5],
        'activation': ['relu', 'sigmoid', 'tanh'],
        'clip_max': [10.0]
    },
    'GA': {
        'algorithm': ['genetic_alg'],
        'pop_size': [100, 500],
        'mutation_prob': [0.3, 0.5],
        'max_iters': [3000],
        'learning_rate': [0.5],
        'activation': ['relu', 'sigmoid', 'tanh'],
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()
Exemple #11
0
def method_compare():
    global count
    count = 0
    fitness_obj = mlrose.CustomFitness(n_peak_fit)
    opt = mlrose.DiscreteOpt(1, fitness_obj, maximize=True, max_val=10000)

    best_state_climb, best_fitness_climb, fitness_curve_climb = mlrose.random_hill_climb(
        opt, curve=True)
    print('---------------------random hill climb-------------------------')
    print('hill climbing best state for n-peak problem:', best_state_climb)
    print('hill climbing best fitness for n-peak problem:', best_fitness_climb)
    print('hill climbing fitting curve for n-peak problem:',
          fitness_curve_climb)
    print('number of fitness call used:', count)
    count = 0
    print('-------------------simulated annealing-------------------------')
    best_state_ann, best_fitness_ann, fitness_curve_ann = mlrose.simulated_annealing(
        opt, schedule=mlrose.ExpDecay(), curve=True)
    print('simulated annealing best state for n-peak problem:', best_state_ann)
    print('simulated annealing best fitness for n-peak problem:',
          best_fitness_ann)
    print('simulated annealing fitting curve for n-peak problem:',
          fitness_curve_ann)
    print('number of fitness call used:', count)
    count = 0
    best_state_ga, best_fitness_ga, fitness_curve_ga = mlrose.genetic_alg(
        opt, pop_size=20, mutation_prob=0.5, curve=True)
    print('---------------------genetic alg----------------------------')
    print('genetic algorithm best state for n-peak problem:', best_state_ga)
    print('genetic algorithm best fitnees for n-peak problem:',
          best_fitness_ga)
    print('genetic algorithm fitness curve for n-peak problem:',
          fitness_curve_ga)
    print('number of fitness call used:', count)
    count = 0
    best_state_mimic, best_fitness_mimic, fitness_curve_mimic = mlrose.mimic(
        opt, pop_size=20, curve=True)
    print('------------------------mimic-------------------------------')
    print('mimic best state for n-peak problem:', best_state_mimic)
    print('mimic best fitness value for n-peak problem:', best_fitness_mimic)
    print('mimic curve for n-peak problem:', fitness_curve_mimic)
    print('number of fitness calls used:', count)
    count = 0
    plt.figure(figsize=(10, 10))
    plt.subplot(221)
    plt.plot(fitness_curve_climb)
    plt.ylabel('fitness')
    plt.xlabel('num_iter')
    plt.ylim(0, 5)
    plt.title('random hill climb')
    plt.subplot(222)
    plt.plot(fitness_curve_ann)
    plt.ylabel('fitness')
    plt.xlabel('num_iter')
    plt.ylim(0, 5)
    plt.title('simulated annealing')
    plt.subplot(223)
    plt.plot(fitness_curve_ga)
    plt.ylim(0, 5)
    plt.ylabel('fitness')
    plt.xlabel('num_iter')
    plt.title('genetic algorithm')
    plt.subplot(224)
    plt.plot(fitness_curve_mimic)
    plt.ylim(0, 5)
    plt.title('mimic')
    plt.ylabel('fitness')
    plt.xlabel('num_iter')
    plt.show()
Exemple #12
0
def nn_impl():

    #iris_data = fetch_openml('iris')
    #X_whole, y_whole = iris_data['data'], iris_data['target']

    sklearn_data = datasets.load_breast_cancer()
    x, y = sklearn_data.data, sklearn_data.target
    #x = preprocessing.scale(x)

    # Split the initial data
    xtrain, xtest, ytrain, ytest = train_test_split(x,
                                                    y,
                                                    test_size=0.4,
                                                    random_state=42)

    ### Analysis for RHC ###
    train_accuracy_scores = []
    test_accuracy_scores = []
    time_per_iteration_rhc = []

    for i in range(1, 3000, 50):
        print(i)
        rhc_nn = mlrose_hiive.NeuralNetwork(hidden_nodes=[2],
                                            activation='identity',
                                            algorithm='random_hill_climb',
                                            bias=False,
                                            is_classifier=True,
                                            learning_rate=0.6,
                                            clip_max=1,
                                            max_attempts=1000,
                                            max_iters=i)

        start = time.time()
        rhc_nn.fit(xtrain, ytrain)

        # Train set analysis
        predictions_train = rhc_nn.predict(xtrain)
        accuracy_score_train = accuracy_score(ytrain, predictions_train)
        train_accuracy_scores.append(accuracy_score_train)

        # Test set analysis
        predictions_test = rhc_nn.predict(xtest)
        accuracy_score_test = accuracy_score(ytest, predictions_test)
        test_accuracy_scores.append(accuracy_score_test)

        time_per_iteration_rhc.append(time.time() - start)

    plt.figure()
    plt.plot(np.arange(1, 3000, 50),
             np.array(train_accuracy_scores),
             label='Train Accuracy')
    plt.plot(np.arange(1, 3000, 50),
             np.array(test_accuracy_scores),
             label='Test Accuracy')
    plt.xlabel('Iterations')
    plt.ylabel('Accuracy')
    plt.title('Accuracy vs. Iterations (RHC)')
    plt.legend()
    plt.savefig('testacc_iter_rhc.png')

    print("Finished RHC")

    ### Analysis for Simulated Annealing ###
    train_accuracy_scores = []
    test_accuracy_scores = []
    time_per_iteration_sa = []

    for i in range(1, 3000, 50):
        print(i)
        sa_nn = mlrose_hiive.NeuralNetwork(hidden_nodes=[2],
                                           activation='identity',
                                           algorithm='simulated_annealing',
                                           bias=False,
                                           is_classifier=True,
                                           learning_rate=0.6,
                                           clip_max=1,
                                           max_attempts=1000,
                                           max_iters=i)

        start = time.time()
        sa_nn.fit(xtrain, ytrain)

        # Train set analysis
        predictions_train = sa_nn.predict(xtrain)
        accuracy_score_train = accuracy_score(ytrain, predictions_train)
        train_accuracy_scores.append(accuracy_score_train)

        # Test set analysis
        predictions_test = sa_nn.predict(xtest)
        accuracy_score_test = accuracy_score(ytest, predictions_test)
        test_accuracy_scores.append(accuracy_score_test)

        time_per_iteration_sa.append(time.time() - start)

    plt.figure()
    plt.plot(np.arange(1, 3000, 50),
             np.array(train_accuracy_scores),
             label='Train Accuracy')
    plt.plot(np.arange(1, 3000, 50),
             np.array(test_accuracy_scores),
             label='Test Accuracy')
    plt.xlabel('Iterations')
    plt.ylabel('Accuracy')
    plt.title('Accuracy vs. Iterations (SA)')
    plt.legend()
    plt.savefig('testacc_iter_SA.png')

    print("Finished SA")

    ### Analysis for Genetic Algorithms ###
    train_accuracy_scores = []
    test_accuracy_scores = []
    time_per_iteration_ga = []

    for i in range(1, 3000, 50):
        print(i)
        ga_nn = mlrose_hiive.NeuralNetwork(hidden_nodes=[2],
                                           activation='identity',
                                           algorithm='genetic_alg',
                                           bias=False,
                                           is_classifier=True,
                                           learning_rate=0.6,
                                           clip_max=1,
                                           max_attempts=1000,
                                           max_iters=i)

        start = time.time()
        ga_nn.fit(xtrain, ytrain)

        # Train set analysis
        predictions_train = ga_nn.predict(xtrain)
        accuracy_score_train = accuracy_score(ytrain, predictions_train)
        train_accuracy_scores.append(accuracy_score_train)

        # Test set analysis
        predictions_test = ga_nn.predict(xtest)
        accuracy_score_test = accuracy_score(ytest, predictions_test)
        test_accuracy_scores.append(accuracy_score_test)

        time_per_iteration_ga.append(time.time() - start)

    plt.figure()
    plt.plot(np.arange(1, 3000, 50),
             np.array(train_accuracy_scores),
             label='Train Accuracy')
    plt.plot(np.arange(1, 3000, 50),
             np.array(test_accuracy_scores),
             label='Test Accuracy')
    plt.xlabel('Iterations')
    plt.ylabel('Accuracy')
    plt.title('Accuracy vs. Iterations (GA)')
    plt.legend()
    plt.savefig('testacc_iter_GA.png')

    print("Finished GA")

    ### Backpropogation (for comparison) ###
    train_accuracy_scores = []
    test_accuracy_scores = []
    time_per_iteration_bp = []
    print("backprop start")
    for i in range(1, 3000, 50):
        print(i)
        bp_nn = MLPClassifier(hidden_layer_sizes=(50, ),
                              activation='logistic',
                              max_iter=i)
        # bp_nn = mlrose_hiive.NeuralNetwork(hidden_nodes=[2], activation='identity',
        #                         algorithm='gradient_descent',
        #                         bias=False, is_classifier=True,
        #                         learning_rate = 0.6, clip_max=1,
        #                         max_attempts=1000, max_iters = i)

        start = time.time()
        bp_nn.fit(xtrain, ytrain)

        # Train set analysis
        predictions_train = bp_nn.predict(xtrain)
        accuracy_score_train = accuracy_score(ytrain, predictions_train)
        train_accuracy_scores.append(accuracy_score_train)

        # Test set analysis
        predictions_test = bp_nn.predict(xtest)
        accuracy_score_test = accuracy_score(ytest, predictions_test)
        test_accuracy_scores.append(accuracy_score_test)

        time_per_iteration_bp.append(time.time() - start)

    plt.figure()
    plt.plot(np.arange(1, 3000, 50),
             np.array(train_accuracy_scores),
             label='Train Accuracy')
    plt.plot(np.arange(1, 3000, 50),
             np.array(test_accuracy_scores),
             label='Test Accuracy')
    plt.xlabel('Iterations')
    plt.ylabel('Accuracy')
    plt.title('Accuracy vs. Iterations (Backpropogation)')
    plt.legend()
    plt.savefig('testacc_iter_bp.png')

    print("Finished Backprop")

    ### Plot runtimes for above ###
    plt.figure()
    plt.plot(np.arange(1, 3000, 50),
             np.array(time_per_iteration_rhc),
             label='RHC')
    plt.plot(np.arange(1, 3000, 50),
             np.array(time_per_iteration_sa),
             label='SA')
    plt.plot(np.arange(1, 3000, 50),
             np.array(time_per_iteration_ga),
             label='GA')
    plt.plot(np.arange(1, 3000, 50),
             np.array(time_per_iteration_ga),
             label='BP')
    plt.xlabel('Iterations')
    plt.ylabel('Training Time')
    plt.title('Training Time vs Iterations')
    plt.legend()
    plt.savefig('time_vs_iter.png')

    #### Hyperparameter Tuning - RHC ####
    ## Adjusting the number of random restarts ##
    train_accuracy_scores = []
    test_accuracy_scores = []

    for i in range(0, 500, 25):
        print(i)
        rhc_nn = mlrose_hiive.NeuralNetwork(hidden_nodes=[2],
                                            activation='identity',
                                            algorithm='random_hill_climb',
                                            bias=False,
                                            is_classifier=True,
                                            learning_rate=0.6,
                                            clip_max=1,
                                            max_attempts=1000,
                                            restarts=i)

        rhc_nn.fit(xtrain, ytrain)

        # Train set analysis
        predictions_train = rhc_nn.predict(xtrain)
        accuracy_score_train = accuracy_score(ytrain, predictions_train)
        train_accuracy_scores.append(accuracy_score_train)

        # Test set analysis
        predictions_test = rhc_nn.predict(xtest)
        accuracy_score_test = accuracy_score(ytest, predictions_test)
        test_accuracy_scores.append(accuracy_score_test)

    plt.figure()
    plt.plot(np.arange(0, 500, 25),
             np.array(train_accuracy_scores),
             label='Train Accuracy')
    plt.plot(np.arange(0, 500, 25),
             np.array(test_accuracy_scores),
             label='Test Accuracy')
    plt.xlabel('Restarts')
    plt.ylabel('Accuracy')
    plt.title('Accuracy vs. Number of Restarts (RHC)')
    plt.legend()
    plt.savefig('rhc_restarts.png')

    print("Finished RHC HP Tuning")

    #### Hyperparameter Tuning - SA ####
    ## Adjusting the type of scheduling ##
    train_accuracy_scores = []
    test_accuracy_scores = []

    # Referending sectiion 2.2 'Decay Schedules' here:
    # https://readthedocs.org/projects/mlrose/downloads/pdf/stable/

    schedule_types = [
        mlrose_hiive.ExpDecay(),
        mlrose_hiive.ArithDecay(),
        mlrose_hiive.GeomDecay()
    ]

    for st in schedule_types:
        print(st)
        sa_nn = mlrose_hiive.NeuralNetwork(hidden_nodes=[2],
                                           activation='identity',
                                           algorithm='simulated_annealing',
                                           bias=False,
                                           is_classifier=True,
                                           learning_rate=0.6,
                                           clip_max=1,
                                           max_attempts=1000,
                                           schedule=st)

        sa_nn.fit(xtrain, ytrain)

        # Train set analysis
        predictions_train = sa_nn.predict(xtrain)
        accuracy_score_train = accuracy_score(ytrain, predictions_train)
        train_accuracy_scores.append(accuracy_score_train)

        # Test set analysis
        predictions_test = sa_nn.predict(xtest)
        accuracy_score_test = accuracy_score(ytest, predictions_test)
        test_accuracy_scores.append(accuracy_score_test)

    plt.figure()
    plt.plot(['ExpDecay', 'ArithDecay', 'GeomDecay'],
             np.array(train_accuracy_scores),
             label='Train Accuracy')
    plt.plot(['ExpDecay', 'ArithDecay', 'GeomDecay'],
             np.array(test_accuracy_scores),
             label='Test Accuracy')
    plt.xlabel('Schedule Type')
    plt.ylabel('Accuracy')
    plt.title('Accuracy vs. Schedule Type (SA)')
    plt.legend()
    plt.savefig('sa_schedule_type.png')

    print("Finished SA HP Tuning")

    #### Hyperparameter Tuning - GA ####

    ## Adjusting the amount of mutation
    ## Used api as referenced in https://readthedocs.org/projects/mlrose/downloads/pdf/stable/
    train_accuracy_scores = []
    test_accuracy_scores = []

    mutation_prob_array = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
    for i in mutation_prob_array:
        ga_nn = mlrose_hiive.NeuralNetwork(hidden_nodes=[2],
                                           activation='relu',
                                           algorithm='genetic_alg',
                                           bias=False,
                                           is_classifier=True,
                                           learning_rate=0.6,
                                           clip_max=1,
                                           max_attempts=1000,
                                           mutation_prob=i)

        ga_nn.fit(xtrain, ytrain)

        # Train set analysis
        predictions_train = ga_nn.predict(xtrain)
        accuracy_score_train = accuracy_score(ytrain, predictions_train)
        train_accuracy_scores.append(accuracy_score_train)

        # Test set analysis
        predictions_test = ga_nn.predict(xtest)
        accuracy_score_test = accuracy_score(ytest, predictions_test)
        test_accuracy_scores.append(accuracy_score_test)

    plt.figure()
    plt.plot(mutation_prob_array,
             np.array(train_accuracy_scores),
             label='Train Accuracy')
    plt.plot(mutation_prob_array,
             np.array(test_accuracy_scores),
             label='Test Accuracy')
    plt.xlabel('mutation_prob')
    plt.ylabel('Accuracy')
    plt.title('Accuracy vs. Iterations (GA - mutation_prob experimentation)')
    plt.legend()
    plt.savefig('ga_mutation.png')

    print("Finished GA mutation experimentation")

    ## Adjusting the population size
    ## Used api as referenced in https://readthedocs.org/projects/mlrose/downloads/pdf/stable/
    train_accuracy_scores = []
    test_accuracy_scores = []

    pop_size_array = [100, 200, 300, 400, 500]
    for i in pop_size_array:
        ga_nn = mlrose_hiive.NeuralNetwork(hidden_nodes=[2],
                                           activation='relu',
                                           algorithm='genetic_alg',
                                           bias=False,
                                           is_classifier=True,
                                           learning_rate=0.6,
                                           clip_max=1,
                                           max_attempts=1000,
                                           pop_size=i)

        ga_nn.fit(xtrain, ytrain)

        # Train set analysis
        predictions_train = ga_nn.predict(xtrain)
        accuracy_score_train = accuracy_score(ytrain, predictions_train)
        train_accuracy_scores.append(accuracy_score_train)

        # Test set analysis
        predictions_test = ga_nn.predict(xtest)
        accuracy_score_test = accuracy_score(ytest, predictions_test)
        test_accuracy_scores.append(accuracy_score_test)

    plt.figure()
    plt.plot(pop_size_array,
             np.array(train_accuracy_scores),
             label='Train Accuracy')
    plt.plot(pop_size_array,
             np.array(test_accuracy_scores),
             label='Test Accuracy')
    plt.xlabel('pop_size')
    plt.ylabel('Accuracy')
    plt.title('Accuracy vs. Iterations (GA - pop_size experimentation)')
    plt.legend()
    plt.savefig('ga_popsize.png')

    print("Finished GA pop_size experimentation")
edges = []
for au_idx in range(N_K):
    for edge in base_edges:
        edge = (edge[0] + 5 * au_idx, edge[1] + 5 * au_idx)
        edges.append(edge)

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

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

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

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


class var_input_size():
    def __init__(self, N_Q=8, N_P=7, N_K=1):
        self.N_Q = N_Q
        self.N_K = N_K
        self.N_P = N_P
Exemple #14
0
                   max_attempts=200,
                   iteration_list=[2000],
                   temperature_list=[0.01, 0.1, 1, 10, 100, 1000],
                   decay_list=[mlrh.GeomDecay, mlrh.ExpDecay, mlrh.ArithDecay])
sa_stats, sa_curve = sa.run()


columns = ['Time', 'Fitness', 'Temperature', 'schedule_type']
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,
Exemple #15
0
def sa_optimization(size):
    algo = 'sa'
    problem = get_problem(size)

    # Gridsearch params

    init_temp = 1
    decay = 0.9
    min_temp = 0.1
    schedule_values = [
        mlrose_hiive.GeomDecay(init_temp=init_temp,
                               decay=decay,
                               min_temp=min_temp),
        mlrose_hiive.ArithDecay(init_temp=init_temp,
                                decay=decay,
                                min_temp=min_temp),
        mlrose_hiive.ExpDecay(init_temp=init_temp, min_temp=min_temp)
    ]
    max_attempts_values = [10, 50, 100, 200, 1000, 5000]
    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, max_iters=MAX_ITERS, random_state=SEED, 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.')
Exemple #16
0
def main():
    # ds1 = pd.read_csv('../assignment1/data/adult.data',
    #                   names=['age', 'workclass', 'fnlwgt', 'education', 'education-num', 'marital-status', 'occupation',
    #                          'relationship', 'race', 'sex', 'capital-gain', 'capital-loss', 'hours-per-week',
    #                          'native-country', '<=50k'])
    # ds1.dropna()
    # ds1.drop_duplicates()
    # ds1 = ds1[ds1['workclass'] != '?']
    # ds1 = ds1[ds1['occupation'] != '?']
    # ds1 = ds1[ds1['education'] != '?']
    # ds1 = ds1[ds1['marital-status'] != '?']
    # ds1 = ds1[ds1['relationship'] != '?']
    # ds1 = ds1[ds1['race'] != '?']
    # ds1 = ds1[ds1['sex'] != '?']
    # ds1 = ds1[ds1['native-country'] != '?']
    # ds1_dummies = pd.get_dummies(ds1, columns=['workclass', 'education', 'marital-status', 'occupation', 'relationship',
    #                                            'race', 'sex', 'native-country'])
    # ds1_dummies.dropna()
    # ds1_dummies['<=50k'].value_counts()
    # ds1_dummies['<=50k'] = ds1_dummies['<=50k'].map({'<=50K': 1, '>50K': 0})
    # ds1_labels = ds1_dummies['<=50k']
    # ds1_dummies = ds1_dummies.drop(['<=50k'], axis=1)
    ds2 = pd.read_csv('../assignment1/data/bank-additional-full.csv',
                      delimiter=';')
    ds2.dropna()
    ds2.drop_duplicates()
    ds2_dummies = pd.get_dummies(ds2,
                                 columns=[
                                     'job', 'marital', 'education', 'default',
                                     'housing', 'loan', 'contact', 'month',
                                     'day_of_week', 'poutcome'
                                 ])
    ds2_dummies.dropna()
    ds2_dummies['y'].value_counts()
    ds2_dummies['y'] = ds2_dummies['y'].map({'yes': 1, 'no': 0})
    ds2_labels = ds2_dummies['y']
    ds2_dummies = ds2_dummies.drop(['y'], axis=1)
    scaler = StandardScaler()

    scaled_ds1_dummies = scaler.fit_transform(ds2_dummies, y=ds2_labels)

    X_train, X_test, y_train, y_test = train_test_split(scaled_ds1_dummies,
                                                        ds2_labels,
                                                        test_size=0.20,
                                                        stratify=ds2_labels)

    v = {
        "X_train": X_train,
        "X_test": X_test,
        "y_train": y_train,
        "y_test": y_test
    }

    print("Random Hill Climbing")
    v['algo'] = 'rhc'
    rhc, rhc_t = run_something(v, 0)
    print(rhc)
    print("SA")

    v['algo'] = 'sa'
    sa, sa_t = run_something(v, 0, mlrose.ExpDecay(), 0.1)
    print(sa)
    print("GA")

    v['algo'] = 'ga'
    ga, ga_t = run_something(v, 0, mlrose.ExpDecay(), 0.1, 100)
    print(ga)
    for x, y in zip([rhc, sa, ga], ['RHC', 'SA', 'GA']):
        plt.plot(x, label=y)
    plt.legend()
    plt.title(
        'Randomized Optimization Fitness Curve for NN Weight Optimization')
    plt.xlabel('Function iteration count')
    plt.ylabel('Fitness function value')
    plt.show()
    plt.clf()
    for x, y in zip([rhc_t, sa_t, ga_t], ['RHC', 'SA', 'GA']):
        plt.plot(x, label=y)
    plt.legend()
    plt.title(
        'Randomized Optimization Timing Curve for NN Weight Optimization')
    plt.xlabel('Function iteration count')
    plt.ylabel('Time in Seconds')
    plt.show()
Exemple #17
0
def main():
    # ds1 = pd.read_csv('../assignment1/data/adult.data',
    #                   names=['age', 'workclass', 'fnlwgt', 'education', 'education-num', 'marital-status', 'occupation',
    #                          'relationship', 'race', 'sex', 'capital-gain', 'capital-loss', 'hours-per-week',
    #                          'native-country', '<=50k'])
    # ds1.dropna()
    # ds1.drop_duplicates()
    # ds1 = ds1[ds1['workclass'] != '?']
    # ds1 = ds1[ds1['occupation'] != '?']
    # ds1 = ds1[ds1['education'] != '?']
    # ds1 = ds1[ds1['marital-status'] != '?']
    # ds1 = ds1[ds1['relationship'] != '?']
    # ds1 = ds1[ds1['race'] != '?']
    # ds1 = ds1[ds1['sex'] != '?']
    # ds1 = ds1[ds1['native-country'] != '?']
    # ds1_dummies = pd.get_dummies(ds1, columns=['workclass', 'education', 'marital-status', 'occupation', 'relationship',
    #                                            'race', 'sex', 'native-country'])
    # ds1_dummies.dropna()
    # ds1_dummies['<=50k'].value_counts()
    # ds1_dummies['<=50k'] = ds1_dummies['<=50k'].map({'<=50K': 1, '>50K': 0})
    # ds1_labels = ds1_dummies['<=50k']
    # ds1_dummies = ds1_dummies.drop(['<=50k'], axis=1)
    ds2 = pd.read_csv('../assignment1/data/bank-additional-full.csv',
                      delimiter=';')
    ds2.dropna()
    ds2.drop_duplicates()
    ds2_dummies = pd.get_dummies(ds2,
                                 columns=[
                                     'job', 'marital', 'education', 'default',
                                     'housing', 'loan', 'contact', 'month',
                                     'day_of_week', 'poutcome'
                                 ])
    ds2_dummies.dropna()
    ds2_dummies['y'].value_counts()
    ds2_dummies['y'] = ds2_dummies['y'].map({'yes': 1, 'no': 0})
    ds2_labels = ds2_dummies['y']
    ds2_dummies = ds2_dummies.drop(['y'], axis=1)
    scaler = StandardScaler()

    scaled_ds1_dummies = scaler.fit_transform(ds2_dummies, y=ds2_labels)

    X_train, X_test, y_train, y_test = train_test_split(scaled_ds1_dummies,
                                                        ds2_labels,
                                                        test_size=0.20,
                                                        stratify=ds2_labels)

    v = {
        "X_train": X_train,
        "X_test": X_test,
        "y_train": y_train,
        "y_test": y_test
    }

    print("Random Hill Climbing")
    v['algo'] = 'rhc'
    rhc = run_something(v, 0)
    print(rhc)
    print("SA")

    v['algo'] = 'sa'
    sa = run_something(v, 0, mlrose.ExpDecay(), 0.1)
    print(sa)
    print("GA")

    v['algo'] = 'ga'
    ga = run_something(v, 0, mlrose.ExpDecay(), 0.1, 100)

    f, axarr = plt.subplots(1, 3)
    f.set_figheight(3)
    f.set_figwidth(9)
    for y, i in zip(rhc, ['r', 'b', 'g', 'y']):
        axarr[0].plot(y, color=i)
    axarr[0].set_title('RHC vs Activation Function')
    for y, i in zip(sa, ['r', 'b', 'g', 'y']):
        axarr[1].plot(y, color=i)
    axarr[1].set_title('SA vs Activation Function')
    for y, i in zip(ga, ['r', 'b', 'g', 'y']):
        axarr[2].plot(y, color=i)
    axarr[2].set_title('GA vs Activation Function')
    # Fine-tune figure; hide x ticks for top plots and y ticks for right plots
    # plt.setp([a.get_xticklabels() for a in axarr[0]], visible=False)
    # plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False)
    plt.legend(handles=[
        Line2D([0], [0], color='r', lw=4, label='identity'),
        Line2D([0], [0], color='b', lw=4, label='relu'),
        Line2D([0], [0], color='g', lw=4, label='sigmoid'),
        Line2D([0], [0], color='y', lw=4, label='tanh')
    ])
    # plt.title('Input size vs fitness curve One Max')
    # plt.xlabel('Function iteration count')
    # plt.ylabel('Fitness function value')

    plt.show()
Exemple #18
0
 def run(self):
     temperatures = [mlrose_hiive.ExpDecay(init_temp=t, exp_const=decay) for t in self.temperature_list for decay in self.decay_list]
     return super().run_experiment_(algorithm=mlrose_hiive.simulated_annealing,
                                    schedule=('Temperature', temperatures))
Exemple #19
0
def get_random_optimisation(problem,
                            optimisation,
                            problem_type,
                            seed=10,
                            state_fitness_callback=None):
    best_state, best_fitness, fitness_curve = None, None, None
    process_time = []

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

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

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

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

    return best_state, best_fitness, np.array(fitness_curve), process_time
Exemple #20
0
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")
Exemple #21
0
 fit_is_recalls = []
 fit_os_recalls = []
 fit_iters = []
 fit_losses = []
 fit_curves = []
 candidates = [mlr.NeuralNetwork(hidden_nodes=[13,10,7,5,1], activation='relu', algorithm = 'random_hill_climb', max_iters=1000, bias=True, is_classifier=True, learning_rate=0.001, schedule=mlr.GeomDecay(), early_stopping=False, curve=True, max_attempts = 100, random_state = seed),\
               mlr.NeuralNetwork(hidden_nodes=[13,10,7,5,1], activation='relu', algorithm = 'random_hill_climb', max_iters=1000, bias=True, is_classifier=True, learning_rate=0.0001, schedule=mlr.GeomDecay(), early_stopping=False, curve=True, max_attempts = 100, random_state = seed),\
               mlr.NeuralNetwork(hidden_nodes=[13,10,7,5,1], activation='relu', algorithm = 'random_hill_climb', max_iters=1000, bias=True, is_classifier=True, learning_rate=0.00001, schedule=mlr.GeomDecay(), early_stopping=False, curve=True, max_attempts = 100, random_state = seed),\
               mlr.NeuralNetwork(hidden_nodes=[13,10,7,5,1], activation='relu', algorithm = 'random_hill_climb', max_iters=1000, bias=True, is_classifier=True, learning_rate=0.000001, schedule=mlr.GeomDecay(), early_stopping=False, curve=True, max_attempts = 100, random_state = seed),\
               mlr.NeuralNetwork(hidden_nodes=[13,10,7,5,1], activation='relu', algorithm = 'random_hill_climb', max_iters=1000, bias=True, is_classifier=True, learning_rate=0.0000001, schedule=mlr.GeomDecay(), early_stopping=False, curve=True, max_attempts = 100, random_state = seed), \
               mlr.NeuralNetwork(hidden_nodes=[13,10,7,5,1], activation='relu', algorithm = 'random_hill_climb', max_iters=1000, bias=True, is_classifier=True, learning_rate=0.001, schedule=mlr.ArithDecay(), early_stopping=False, curve=True, max_attempts = 100, random_state = seed),\
               mlr.NeuralNetwork(hidden_nodes=[13,10,7,5,1], activation='relu', algorithm = 'random_hill_climb', max_iters=1000, bias=True, is_classifier=True, learning_rate=0.0001, schedule=mlr.ArithDecay(), early_stopping=False, curve=True, max_attempts = 100, random_state = seed),\
               mlr.NeuralNetwork(hidden_nodes=[13,10,7,5,1], activation='relu', algorithm = 'random_hill_climb', max_iters=1000, bias=True, is_classifier=True, learning_rate=0.00001, schedule=mlr.ArithDecay(), early_stopping=False, curve=True, max_attempts = 100, random_state = seed),\
               mlr.NeuralNetwork(hidden_nodes=[13,10,7,5,1], activation='relu', algorithm = 'random_hill_climb', max_iters=1000, bias=True, is_classifier=True, learning_rate=0.000001, schedule=mlr.ArithDecay(), early_stopping=False, curve=True, max_attempts = 100, random_state = seed),\
               mlr.NeuralNetwork(hidden_nodes=[13,10,7,5,1], activation='relu', algorithm = 'random_hill_climb', max_iters=1000, bias=True, is_classifier=True, learning_rate=0.0000001, schedule=mlr.ArithDecay(), early_stopping=False, curve=True, max_attempts = 100, random_state = seed),\
               mlr.NeuralNetwork(hidden_nodes=[13,10,7,5,1], activation='relu', algorithm = 'random_hill_climb', max_iters=1000, bias=True, is_classifier=True, learning_rate=0.001, schedule=mlr.ExpDecay(), early_stopping=False, curve=True, max_attempts = 100, random_state = seed),\
               mlr.NeuralNetwork(hidden_nodes=[13,10,7,5,1], activation='relu', algorithm = 'random_hill_climb', max_iters=1000, bias=True, is_classifier=True, learning_rate=0.0001, schedule=mlr.ExpDecay(), early_stopping=False, curve=True, max_attempts = 100, random_state = seed),\
               mlr.NeuralNetwork(hidden_nodes=[13,10,7,5,1], activation='relu', algorithm = 'random_hill_climb', max_iters=1000, bias=True, is_classifier=True, learning_rate=0.00001, schedule=mlr.ExpDecay(), early_stopping=False, curve=True, max_attempts = 100, random_state = seed),\
               mlr.NeuralNetwork(hidden_nodes=[13,10,7,5,1], activation='relu', algorithm = 'random_hill_climb', max_iters=1000, bias=True, is_classifier=True, learning_rate=0.000001, schedule=mlr.ExpDecay(), early_stopping=False, curve=True, max_attempts = 100, random_state = seed),\
               mlr.NeuralNetwork(hidden_nodes=[13,10,7,5,1], activation='relu', algorithm = 'random_hill_climb', max_iters=1000, bias=True, is_classifier=True, learning_rate=0.0000001, schedule=mlr.ExpDecay(), early_stopping=False, curve=True, max_attempts = 100, random_state = seed)]
 for i in range(len(candidates)):
     init_weights = np.random.uniform(-0.5,0.5,423)
     bp = candidates[i]
     start_time = time.time()
     bp.fit(X_train_sc_1000, y_train_1000, init_weights=init_weights)
     end_time = time.time()
     fit_times.append(end_time-start_time)
     y_in = pd.Series(bp.predict(X_train_sc_1000).flatten())
     y_out = pd.Series(bp.predict(X_test_sc_1000).flatten())
     model_loss = bp.loss
     fit_losses.append(model_loss)
y_test_hot = oh.transform(y_test.reshape(-1, 1)).todense().astype(int)

# lrs = [0.002]  # 6
# lrs = [0.002, 0.004, 0.02, 0.06, 0.2, 0.8]  # 6
lrs = [0.003,  0.05, 0.7]  # 6
# mil = [100]  # 4
# mil = [100, 500, 1000, 2500]  # 4
mil = [145, 435, 1300]  # 4
# acti = ['tanh']
acti = ['relu', 'sigmoid', 'tanh']
NNparams = np.array(np.meshgrid(lrs,mil,acti),dtype=object).T.reshape(-1,3)
# rsts = [10]
rsts = [7, 12]
# rsts = [5, 10, 15]
# schs = [mlr.ArithDecay(15)]
schs = [mlr.ArithDecay(25), mlr.ExpDecay(25), mlr.GeomDecay(25)]
# schs = [mlr.ArithDecay(15), mlr.ExpDecay(15), mlr.GeomDecay(15),
        # mlr.ArithDecay(175), mlr.ExpDecay(175), mlr.GeomDecay(175)]
# pops = [225]
pops = [150, 225]
# pops = [75, 150, 225]
# muts = [0.25]
muts = [0.33, 0.66]
# muts = [0.25, 0.5, 0.75]
gaParams = np.array(np.meshgrid(pops, muts)).T.reshape(-1,2)

experiments = []
for lr, mi, act in NNparams:
    for rst in rsts:
        experiments.append(mlr.NeuralNetwork(hidden_nodes=[7,5],
                                activation=act,
Exemple #23
0
input_sizes = [25, 50, 75, 100, 150, 200, 300]

opt_probs = [
    (get_one_max, 'one max'),
    (get_four_peaks, 'four peaks'),
    (get_six_peaks, 'six peaks'),
    (get_continuous_peaks, 'continuous peaks'),
    (get_flip_flop, 'flip flop'),
]

sim_ann = get_sim_ann

options = [(mlrose.GeomDecay(), 'geometric'),
           (mlrose.ArithDecay(), 'arithmetic'),
           (mlrose.ExpDecay(), 'exponential')]

metric_dict = {prob[1]: {} for prob in opt_probs}

for size in input_sizes:
    print('####################################### New input size ', size,
          ' ##############################################')
    for prob in opt_probs:
        problem_name = prob[1]
        problem = prob[0](size)
        print('----------------------------------New Problem ', problem_name,
              ' ------------------------------------------')
        for opt in options:
            option_name = opt[1]
            opt_val = opt[0]
            print('___________New Option_____________')
x_test = scaler.transform(x_test)

for algo in algos:
    print('--------------New Algo ', algo, '----------------------')

    nn_model = mlrose.NeuralNetwork(hidden_nodes=[3],
                                    activation='relu',
                                    algorithm=algo,
                                    max_iters=100,
                                    bias=True,
                                    is_classifier=True,
                                    learning_rate=0.0001,
                                    early_stopping=False,
                                    clip_max=1,
                                    restarts=30,
                                    schedule=mlrose.ExpDecay(1),
                                    pop_size=300,
                                    mutation_prob=0.4,
                                    max_attempts=10,
                                    random_state=4,
                                    curve=False)

    print('Fitting model...')
    nn_model.fit(x_train, y_train)

    y_train_pred = nn_model.predict(x_train)

    print('Training Score: ', accuracy_score(y_train, y_train_pred))

    y_test_pred = nn_model.predict(x_test)
Exemple #25
0
def simulated_annealing_experiment(optimization_problem, hparams,
                                   output_fn_base):
    metrics = {}
    logs = []
    print("----Running Simulated Annealing Experiment-----")
    print("Hyperparameters Used: ")
    print(hparams)

    schedule = None
    if (hparams["decay"] == "Geometric"):
        schedule = mlrose.GeomDecay(init_temp=hparams["initial_temp"])
    elif (hparams["decay"] == "Arithmetic"):
        schedule = mlrose.ArithDecay(init_temp=hparams["initial_temp"])
    else:
        schedule = mlrose.ExpDecay(init_temp=hparams["initial_temp"])

    best_state, best_fitness, _ = mlrose.simulated_annealing(
        optimization_problem, schedule, hparams["max_attempts"],
        hparams["max_iters"])

    # Iterations and runtime
    fitness_scores = []
    runtimes = []
    iteration_count = range(1, hparams["max_iters"])
    for iter in iteration_count:
        start_time = time.time()
        best_state, best_fitness, _ = mlrose.simulated_annealing(
            optimization_problem, schedule, hparams["max_attempts"], iter)
        end_time = time.time()
        runtimes.append(end_time - start_time)
        fitness_scores.append(-best_fitness if optimization_problem.
                              prob_type == 'tsp' else best_fitness)

    plot_single(fitness_scores,
                "%s/Annealing/annealing_fitness_iterations" % output_fn_base,
                xvals=iteration_count)
    plot_runtimes(runtimes,
                  "%s/Annealing/annealing_runtime_iterations" % output_fn_base)
    metrics["runtimes"] = runtimes
    metrics["fitness"] = fitness_scores

    # intial temp
    fitness_scores_geo = []
    fitness_scores_arith = []
    fitness_scores_exp = []
    temps = range(1, 100)

    for temp in temps:
        decays = [
            mlrose.GeomDecay(init_temp=temp),
            mlrose.ArithDecay(init_temp=temp),
            mlrose.ExpDecay(init_temp=temp)
        ]
        for i in range(0, len(decays)):
            best_state, best_fitness, _ = mlrose.simulated_annealing(
                optimization_problem, decays[i], hparams["max_attempts"], 400)
            if (i == 0):
                fitness_scores_geo.append(
                    -best_fitness if optimization_problem.prob_type ==
                    'tsp' else best_fitness)
            elif (i == 1):
                fitness_scores_arith.append(
                    -best_fitness if optimization_problem.prob_type ==
                    'tsp' else best_fitness)
            else:
                fitness_scores_exp.append(
                    -best_fitness if optimization_problem.prob_type ==
                    'tsp' else best_fitness)

    # Iterations and runtime
    decays = [
        mlrose.GeomDecay(init_temp=temp),
        mlrose.ArithDecay(init_temp=temp),
        mlrose.ExpDecay(init_temp=temp)
    ]
    fitness_scores_geo2 = []
    fitness_scores_arith2 = []
    fitness_scores_exp2 = []
    for i in range(0, len(decays)):
        if (i == 0):
            iteration_count2 = range(1, 2 * hparams["max_iters"])
            for iter in iteration_count2:
                best_state, best_fitness, _ = mlrose.simulated_annealing(
                    optimization_problem, decays[i], hparams["max_attempts"],
                    iter)
                fitness_scores_geo2.append(
                    -best_fitness if optimization_problem.prob_type ==
                    'tsp' else best_fitness)
        elif (i == 1):
            iteration_count2 = range(1, 2 * hparams["max_iters"])
            for iter in iteration_count2:
                best_state, best_fitness, _ = mlrose.simulated_annealing(
                    optimization_problem, decays[i], hparams["max_attempts"],
                    iter)
                fitness_scores_arith2.append(
                    -best_fitness if optimization_problem.prob_type ==
                    'tsp' else best_fitness)
        else:
            iteration_count2 = range(1, 2 * hparams["max_iters"])
            for iter in iteration_count2:

                best_state, best_fitness, _ = mlrose.simulated_annealing(
                    optimization_problem, decays[i], hparams["max_attempts"],
                    iter)
                fitness_scores_exp2.append(
                    -best_fitness if optimization_problem.prob_type ==
                    'tsp' else best_fitness)

    plot_single(fitness_scores,
                "%s/Annealing/annealing_fitness_iterations" % output_fn_base,
                xvals=iteration_count)
    plot_runtimes(runtimes,
                  "%s/Annealing/annealing_runtime_iterations" % output_fn_base)
    metrics["runtimes"] = runtimes
    metrics["fitness"] = fitness_scores

    plot_multiple(
        [fitness_scores_geo2, fitness_scores_arith2, fitness_scores_exp2], [
            "Geometric Decay Fitness", "Arithmetic Decay Fitness",
            "Exponential Decay Fitness"
        ],
        "%s/Annealing/sa_fitness_decay_iterations" % output_fn_base,
        title="SA Fitness Scores Over Iterations - %s" %
        get_name_of_experiment(output_fn_base),
        xlab="Iterations")

    plot_multiple(
        [fitness_scores_geo, fitness_scores_arith, fitness_scores_exp], [
            "Geometric Decay Fitness", "Arithmetic Decay Fitness",
            "Exponential Decay Fitness"
        ],
        "%s/Annealing/sa_fitness_decay" % output_fn_base,
        title="SA Fitness Scores Over Various Temps - %s" %
        get_name_of_experiment(output_fn_base),
        xlab="Initial Temperature")

    plot_multiple(
        [fitness_scores_geo], ["Geometric Decay Fitness"],
        "%s/Annealing/sa_fitness_decay_geo" % output_fn_base,
        title="SA Fitness Over Various Temps - Geometric Decay - %s" %
        get_name_of_experiment(output_fn_base),
        xlab="Initial Temperature")
    plot_multiple(
        [fitness_scores_arith], ["Arithmetic Decay Fitness"],
        "%s/Annealing/sa_fitness_decay_arith" % output_fn_base,
        title="SA Fitness Over Various Temps - Arithmetic Decay - %s" %
        get_name_of_experiment(output_fn_base),
        xlab="Initial Temperature")
    plot_multiple(
        [fitness_scores_exp], ["Exponential Decay Fitness"],
        "%s/Annealing/sa_fitness_decay_exp" % output_fn_base,
        title="SA Fitness Over Various Temps - Exponential Decay - %s" %
        get_name_of_experiment(output_fn_base),
        xlab="Initial Temperature")

    plot_multiple(
        [
            pd.Series(fitness_scores_geo).rolling(window=10).mean(),
            pd.Series(fitness_scores_arith).rolling(window=10).mean(),
            pd.Series(fitness_scores_exp).rolling(window=10).mean()
        ], [
            "Geometric Decay Fitness", "Arithmetic Decay Fitness",
            "Exponential Decay Fitness"
        ],
        "%s/Annealing/sa_fitness_decay_sma" % output_fn_base,
        title="SA Fitness Scores Over Various Temps - Rolling Mean - %s" %
        get_name_of_experiment(output_fn_base),
        xlab="Initial Temperature")

    logs.append("\tHyperparameters: \n")
    logs.append("\t%s" % str(hparams))
    logs.append("\n\n\tBest State: \n\t\t")
    logs.append(str(list(best_state)))
    logs.append("\n\tBest Fitness: \n\t\t")
    logs.append(str(best_fitness))

    return logs, metrics