コード例 #1
0
ファイル: eightqueensdecay.py プロジェクト: zparnold/cs7641
def main():
    name_of_exp = "Eight Queens"
    fitness = mlrose.CustomFitness(queens_max)
    problem = mlrose.DiscreteOpt(length=8,
                                 fitness_fn=fitness,
                                 maximize=True,
                                 max_val=8)

    # Define initial state
    mimic = []
    init_state = np.array([0, 1, 2, 3, 4, 5, 6, 7])
    for i in [mlrose.ExpDecay(), mlrose.GeomDecay(), mlrose.ArithDecay()]:
        best_state, best_fitness, learning_curve, timing_curve = mlrose.simulated_annealing(
            problem,
            init_state=init_state,
            schedule=i,
            max_attempts=1000,
            max_iters=1000,
            curve=True,
            random_state=1)
        mimic.append(learning_curve)
        print(i)
        print(best_fitness)
    for x, z in zip(['Exp', 'Geom', 'Arith'], mimic):
        plt.plot(z, label=str(x))
    plt.legend()
    plt.title(
        'SA Randomized Optimization DecaySchedule vs Fitness Curve (8-Queens)')
    plt.xlabel('Function iteration count')
    plt.ylabel('Fitness function value')
    plt.show()
コード例 #2
0
ファイル: optima.py プロジェクト: JensenCoding/cs7641-2
    def sa(self):
        print("Creating SA curve")
        problem, init_state = self.get_prob(t_pct=0.15)
        plt.close()
        plt.figure()
        for schedule, s_str in zip(
            [mlrose.GeomDecay(),
             mlrose.ArithDecay(),
             mlrose.ExpDecay()], ['GeomDecay', 'ArithDecay', 'ExpDecay']):
            _, _, fitness_curve = mlrose.simulated_annealing(
                problem,
                schedule=schedule,
                max_attempts=100,
                max_iters=5000,
                init_state=init_state,
                curve=True)
            plt.plot(fitness_curve, label="schedule={}".format(s_str))

        plt.title("{}: Simulated Annealing".format(self.prob_name))
        plt.legend(loc="best")
        plt.xlabel('Number of Iterations')
        plt.ylabel('Fitness')
        plt.savefig(
            os.path.join(self.output_path,
                         "{}_SA Analysis.png".format(self.prob_name)))
コード例 #3
0
 def __init__(self,
              schedule=mlrose.GeomDecay(),
              max_attempts=250,
              max_iters=np.inf,
              init_state=None,
              curve=False,
              random_state=None):
     print("Initialized Simulated Annealing Optimizer")
     self.schedule = schedule
     self.max_attempts = max_attempts
     self.max_iters = max_iters
     self.init_state = init_state
     self.curve = curve
     self.random_state = random_state
     self.bestState = []
     self.bestFitness = 0
     self.parameters = {
         'max_iters':
         np.arange(1, 251),
         'schedule':
         [mlrose.GeomDecay(),
          mlrose.ArithDecay(),
          mlrose.ExpDecay()]
     }
     self.bestParameters = {
         'max_iters': int(max(np.arange(1, 251))),
         'schedule': mlrose.GeomDecay()
     }
コード例 #4
0
 def simulated_annealing_decay(problem, initial_state, decay):
     if decay == 'Geom':
         schedule = mlrose.GeomDecay()
     if decay == 'Exp':
         schedule = mlrose.ExpDecay()
     if decay == 'Arith':
         schedule = mlrose.ArithDecay()
     return mlrose.simulated_annealing(problem,
                                       init_state=initial_state,
                                       curve=False,
                                       schedule=schedule)
コード例 #5
0
 def create_nn_schedule(decay):
     if decay == 'Geom':
         schedule = mlrose.GeomDecay()
     if decay == 'Exp':
         schedule = mlrose.ExpDecay()
     if decay == 'Arith':
         schedule = mlrose.ArithDecay()
     return mlrose.NeuralNetwork(hidden_nodes=[10],
                                 algorithm='simulated_annealing',
                                 max_iters=1000,
                                 schedule=schedule,
                                 random_state=7106080)
コード例 #6
0
ファイル: optima.py プロジェクト: JensenCoding/cs7641-2
    def get_prob(self, t_pct=None, p_length=None):
        if self.prob_name == 'Four Peaks':
            fitness = mlrose.FourPeaks(t_pct)
            p_len = 100
            self.schedule = mlrose.ExpDecay()
            self.restarts = 0
            self.mutation_prob = 0.1
            self.keep_pct = 0.1
            self.pop_size = 500
        elif self.prob_name == "Continuous Peaks":
            fitness = mlrose.ContinuousPeaks(t_pct)
            p_len = 100
            self.schedule = mlrose.GeomDecay()
            self.restarts = 0
            self.mutation_prob = 0.1
            self.keep_pct = 0.2
            self.pop_size = 200
        elif self.prob_name == "Max K Color":
            fitness = mlrose.MaxKColor(self.COLOREDGE)
            p_len = 100
            self.schedule = mlrose.ExpDecay()
            self.restarts = 0
            self.mutation_prob = 0.2
            self.keep_pct = 0.2
            self.pop_size = 200
        elif self.prob_name == "Flip Flop":
            fitness = mlrose.FlipFlop()
            p_len = 100
            self.schedule = mlrose.ArithDecay()
            self.restarts = 0
            self.mutation_prob = 0.2
            self.keep_pct = 0.5
            self.pop_size = 500
        elif self.prob_name == "One Max":
            fitness = mlrose.OneMax()
            p_len = 100
            self.schedule = mlrose.GeomDecay()
            self.restarts = 0
            self.mutation_prob = 0.2
            self.keep_pct = 0.1
            self.pop_size = 100
        else:
            fitness = None
            p_len = 0

        if p_length is None:
            p_length = p_len

        problem = mlrose.DiscreteOpt(length=p_length, fitness_fn=fitness)
        init_state = np.random.randint(2, size=p_length)
        return problem, init_state
コード例 #7
0
 def sim_annealing(self, max_attempts=10, max_iters=np.inf, decay='geom'):
     decay_lookup = {
         'geom': mlrose.GeomDecay(),
         'exp': mlrose.ExpDecay(),
         'arith': mlrose.ArithDecay()
     }
     start = time.time()
     best_state, best_fitness = simulated_annealing(
         self.problem_fit,
         max_attempts=max_attempts,
         max_iters=max_iters,
         schedule=decay_lookup[decay],
         random_state=111)
     end = time.time()
     time_elapsed = end - start
     return [best_fitness, time_elapsed]
コード例 #8
0
    def test_simulated_annealing(self,
                                 title,
                                 max_attempts_range=[100],
                                 decay_range=['geom']):
        decay_lookup = {
            'geom': mlrose.GeomDecay(),
            'exp': mlrose.ExpDecay(),
            'arith': mlrose.ArithDecay()
        }
        fig, (ax1, ax2) = plt.subplots(2, figsize=(12, 8), dpi=80)
        fig.suptitle(title + " Simmulated Annealing")
        print(title + " Simulated Annealing Algo")
        best = [0, 0, 0]
        for m in max_attempts_range:
            fitness_arr = []
            time_arr = []
            for d in decay_range:
                start = time.time()
                # solve using simulated annealing
                best_state, best_fitness, curve = mlrose.simulated_annealing(
                    self.problem_fit,
                    schedule=decay_lookup[d],
                    max_attempts=m,
                    max_iters=np.inf,
                    curve=True)
                fitness_arr.append(best_fitness)
                time_arr.append(round(time.time() - start, 2))
                if best_fitness > best[2]:
                    best[0] = m
                    best[1] = d
                    best[2] = best_fitness

            ax1.plot(decay_range, fitness_arr, label=m, lw=2)
            ax2.plot(decay_range, time_arr, lw=2)

        ax1.set(xlabel="Decay Range", ylabel="Fitness")
        ax2.set(xlabel="Decay Range", ylabel="Time(s)")
        print(title +
              " SA max_attempts={a}, # decay={b}, best_fitness={c}".format(
                  a=best[0], b=best[1], c=best[2]))
        fig.legend(loc='center right', title='Attempts')
        plt.tight_layout()
        self.saveToNewDir(fig, "./", title + "_Simulated_Annealing.png")
        plt.clf()
コード例 #9
0
ファイル: fourpeak.py プロジェクト: evilChipmunk/ML2
def runAnnealing(problem, basePath):
    iterations = 500
    iterations = 1000
    neighborhood = [10]
    neighborhood = [2, 4, 12, 36, 50, 75]
    neighborhood = [4]
    schedules = [('Exp', mlrose.ExpDecay()), ('Arith', mlrose.ArithDecay()),
                 ('Geom', mlrose.GeomDecay())]
    schedules = [('Exp', mlrose.ExpDecay())]

    times = np.zeros((len(neighborhood), iterations))
    nIndex = 0
    schedule = mlrose.ExpDecay()
    fig, ax = plt.subplots()
    plt.title('Annealing')
    for neighbor in neighborhood:
        s = time()
        x = []
        y = []
        for i in range(1, iterations + 1):
            best_state, best_fitness = mlrose.simulated_annealing(
                problem,
                schedule=schedule,
                max_attempts=neighbor,
                max_iters=i,
                random_state=1)
            x.append(i)
            y.append(best_fitness)
            e = time()
            timeTaken = e - s
            times[nIndex, i - 1] = timeTaken
            print('Itt: {0} - Time:{1}'.format(i, timeTaken))
        nIndex += 1
        plotLine(x, y, ax,
                 'Neighbors: {0} - {1}'.format(neighbor, 'Exponential Decay'))
    plotTime(x, times, ax)
    showLegend(fig, ax)

    if basePath:
        plt.savefig('{0}\\{1}.png'.format(basePath, 'Annealing'))
    else:
        plt.show()
    return
コード例 #10
0
     mlrose.GeomDecay(init_temp=10.0, decay=0.9, min_temp=0.001)
 }, {
     "schedule":
     mlrose.GeomDecay(init_temp=10.0, decay=0.8, min_temp=0.001)
 }, {
     "schedule":
     mlrose.GeomDecay(init_temp=10.0, decay=0.7, min_temp=0.001)
 }, {
     "schedule":
     mlrose.GeomDecay(init_temp=10.0, decay=0.6, min_temp=0.001)
 }, {
     "schedule":
     mlrose.GeomDecay(init_temp=10.0, decay=0.5, min_temp=0.001)
 }, {
     "schedule":
     mlrose.ArithDecay(init_temp=10.0, decay=0.0001, min_temp=0.001)
 }, {
     "schedule":
     mlrose.ArithDecay(init_temp=10.0, decay=0.001, min_temp=0.001)
 }, {
     "schedule":
     mlrose.ArithDecay(init_temp=10.0, decay=0.01, min_temp=0.001)
 }],
 mlrose.genetic_alg: [{
     "mutation_prob": .15
 }, {
     "mutation_prob": .35
 }, {
     "mutation_prob": .55
 }, {
     "mutation_prob": .75
コード例 #11
0
ファイル: traveling_salesman.py プロジェクト: zparnold/cs7641
def main():
    name_of_exp = "TSP"

    # Create list of city coordinates
    coords_list = [(1, 1), (4, 2), (5, 2), (6, 4), (4, 4), (3, 6), (1, 5), (2, 3)]

    # Initialize fitness function object using coords_list
    fitness_coords = mlrose.TravellingSales(coords=coords_list)
    problem = mlrose.TSPOpt(length=8, fitness_fn=fitness_coords,
                            maximize=False)

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

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

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

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

    for x, y, z in zip(x_s, y_s, z_s):
        plt.plot(x, y, label=z)
    plt.legend()
    plt.title('Randomized Optimization Iterations vs Fitness Function Value for {}'.format(name_of_exp))
    plt.xlabel('Function iteration count')
    plt.ylabel('Fitness function value')
    plt.show()
    plt.clf()
    for x, w, z in zip(x_s, w_s, z_s):
        plt.plot(x, w, label=z)
    plt.legend()
    plt.title('Randomized Optimization Time vs Fitness Function Value for {}'.format(name_of_exp))
    plt.xlabel('Function iteration count')
    plt.ylabel('Time in Seconds')
    plt.show()
コード例 #12
0
ファイル: tsp_qatar.py プロジェクト: blaze225/dsti_coursework
def solve_sa(problem_fit):
    """ Solving using Simulated Annealing """

    min_fitness = np.inf
    best_params_dict = {}

    # Parameter list
    initial_temp_list = [10, 100, 1000]
    min_temp_list = [1, 10, 100]
    decay_list = [0.001, 0.01, 0.1]

    # Solve
    for t0 in initial_temp_list:
        for t in min_temp_list:
            # Initial temp should be greater than Minimum temp
            if t0 < t:
                continue
            for d in decay_list:
                for c in range(2):
                    if c == 0:
                        schedule = mlrose.ExpDecay(init_temp=t0,
                                                   exp_const=d,
                                                   min_temp=t)
                    else:
                        schedule = mlrose.ArithDecay(init_temp=t0,
                                                     decay=d,
                                                     min_temp=t)
                    # Start timer
                    t_start = time.time()
                    best_state, best_fitness, fitness_curve = mlrose.simulated_annealing(
                        problem=problem_fit,
                        schedule=schedule,
                        max_attempts=500,
                        max_iters=5000,
                        random_state=np.random.seed(7),
                        curve=True)
                    # End timer
                    t_end = time.time()

                    # Storing the best result
                    if best_fitness < min_fitness:
                        min_fitness = best_fitness
                        best_params_dict['Fitness'] = best_fitness
                        best_params_dict['Solution'] = best_state
                        best_params_dict['Initial_temp'] = t0
                        best_params_dict['Min_temp'] = t
                        best_params_dict['Decay'] = d
                        best_params_dict[
                            'Schedule'] = "Exponential" if c == 0 else "Arithmetic"
                        best_params_dict['Fitness_curve'] = fitness_curve
                        best_params_dict['Time'] = round(t_end - t_start, 2)

    # Printing the best result
    print("-- Parameters --")
    print("\tCooling Schedule: ", best_params_dict['Schedule'])
    print("\tInitial Temperature: ", best_params_dict['Initial_temp'])
    print("\tMin Temperature: ", best_params_dict['Min_temp'])
    print("\tDecay rate: ", best_params_dict['Decay'])
    print("\tMax Attempts at each step: 100")
    print("\tStopping Criterion = Max Iterations of the algorithm: 10000")
    print("Results:")
    print("\tSolution (Order of city traversal by index):  ",
          best_params_dict['Solution'])
    print("\tFitness: ", round(best_params_dict['Fitness'], 2))
    print("Computational Time: ", best_params_dict['Time'], " seconds\n\n")

    # Plotting
    plt.plot(-(best_params_dict['Fitness_curve']))
    plt.title("Convergence curve: TSP-Qatar using Simulated Annealing")
    plt.xlabel("Iterations")
    plt.ylabel("Fitness")
    plt.savefig("tsp_qatar_sa.png")
コード例 #13
0
def optimize_sa(problem, trials=50):
    init_temps = np.linspace(1.0, 10.0, 10)
    decay_rates = np.linspace(0.1, 0.99, 10)

    for i in range(len(decay_rates)):
        decay_rates[i] = int(decay_rates[i] * 100) / 100

    fitness_values = [[] for _ in range(len(decay_rates))]

    for i, decay_rate in enumerate(decay_rates):
        for init_temp in init_temps:
            samples = []
            for _ in range(trials):
                decay = mlrose.GeomDecay(init_temp=init_temp, decay=decay_rate)
                _, fitness_value, _ = mlrose.simulated_annealing(
                    problem, decay)
                samples.append(fitness_value)
            fitness_values[i].append(np.mean(samples))

    display_cm(
        np.array(fitness_values), f'Simulated annealing tuning on '
        f'{repr(problem.fitness_fn).split(".")[-1].split(" ")[0]}, length={problem.length}',
        'Initial temperature', 'Decay rate', init_temps, decay_rates)

    fitness_values = [[] for _ in range(len(decay_rates))]

    for i, decay_rate in enumerate(decay_rates):
        for init_temp in init_temps:
            samples = []
            for _ in range(trials):
                decay = mlrose.ArithDecay(init_temp=init_temp,
                                          decay=decay_rate)
                _, fitness_value, _ = mlrose.simulated_annealing(
                    problem, decay)
                samples.append(fitness_value)
            fitness_values[i].append(np.mean(samples))

    display_cm(
        np.array(fitness_values), f'Simulated annealing tuning on '
        f'{repr(problem.fitness_fn).split(".")[-1].split(" ")[0]}, length={problem.length}',
        'Initial temperature', 'Decay rate', init_temps, decay_rates)

    init_temps = np.linspace(1.0, 10.0, 10)
    exp_consts = np.linspace(0.0001, 0.003, 10)

    for i in range(len(exp_consts)):
        exp_consts[i] = int(exp_consts[i] * 10000) / 10000

    fitness_values = [[] for _ in range(len(exp_consts))]

    for i, exp_const in enumerate(exp_consts):
        for init_temp in init_temps:
            samples = []
            time_samples = []
            for _ in range(trials):
                decay = mlrose.ExpDecay(init_temp=init_temp,
                                        exp_const=exp_const)
                start = time.time()
                _, fitness_value, _ = mlrose.simulated_annealing(
                    problem, decay)
                time_samples.append(time.time() - start)
                samples.append(fitness_value)
            fitness_values[i].append(np.mean(samples))

    display_cm(
        np.array(fitness_values), f'Simulated annealing tuning on '
        f'{repr(problem.fitness_fn).split(".")[-1].split(" ")[0]}, length={problem.length}',
        'Initial temperature', 'Exp const', init_temps, exp_consts)
コード例 #14
0
for iterations in (10000, 50000):
    for attempts in (100, 300):
        for temp in (0.01, 0.1, 1, 10):
            start_time = time.perf_counter()
            nn_model1 = mlrose.NeuralNetwork(
                hidden_nodes=[5],
                activation='sigmoid',
                bias=False,
                is_classifier=True,
                learning_rate=0.1,
                early_stopping=True,
                random_state=SEED,
                algorithm='simulated_annealing',
                max_iters=iterations,
                max_attempts=attempts,
                schedule=mlrose.ArithDecay(init_temp=temp))
            nn_model1.fit(X_train, y_train)
            run_time = time.perf_counter() - start_time

            # # predict & assess on training data
            # y_train_pred = nn_model1.predict(X_train)
            # y_train_error = 1.0 - accuracy_score(y_train, y_train_pred)
            # y_train_kappa = cohen_kappa_score(y_train, y_train_pred)  # , labels=target_names)
            # y_train_confusion = confusion_matrix(y_train, y_train_pred)  # , labels=target_names)
            # print("# TRAIN DATA:\nerror=", y_train_error, "\nkappa=", y_train_kappa, "\nconfusion matrix=\n", y_train_confusion)

            # predict & assess on test data
            y_test_pred = nn_model1.predict(X_test)
            y_test_error = 1.0 - accuracy_score(y_test, y_test_pred)
            y_test_kappa = cohen_kappa_score(
                y_test, y_test_pred)  # , labels=target_names)
コード例 #15
0
ファイル: train.py プロジェクト: zachsirera/7641-assignment2
def train_sa_nn(train_x, train_y, test_x, test_y):

    activations = ['identity', 'relu', 'sigmoid', 'tanh']
    markers = ['o', 'x', '^', 's']
    colors = ['g', 'b', 'r', 'c']

    legend_items = [
        mlines.Line2D([], [],
                      color='k',
                      marker='o',
                      markersize=5,
                      linestyle='None',
                      label='identity'),
        mlines.Line2D([], [],
                      color='k',
                      marker='x',
                      markersize=5,
                      linestyle='None',
                      label='relu'),
        mlines.Line2D([], [],
                      color='k',
                      marker='^',
                      markersize=5,
                      linestyle='None',
                      label='sigmoid'),
        mlines.Line2D([], [],
                      color='k',
                      marker='s',
                      markersize=5,
                      linestyle='None',
                      label='tanh'),
        mlines.Line2D([0], [0], color='g', lw=2),
        mlines.Line2D([0], [0], color='b', lw=2),
        mlines.Line2D([0], [0], color='r', lw=2),
        mlines.Line2D([0], [0], color='c', lw=2)
    ]

    legend_labels = [
        'identity', 'relu', 'sigmoid', 'tanh', 'geom', 'arith', 'exp'
    ]

    schedules = [{
        "title":
        "Geometric",
        "schedule":
        mlrose.GeomDecay(init_temp=10, decay=0.95, min_temp=1),
        "abbrev":
        "geom"
    }, {
        "title":
        "Arithmetic",
        "schedule":
        mlrose.ArithDecay(init_temp=10, decay=0.95, min_temp=1),
        "abbrev":
        "arith"
    }, {
        "title":
        "Exponential",
        "schedule":
        mlrose.ExpDecay(init_temp=10, exp_const=0.05, min_temp=1),
        "abbrev":
        "exp"
    }]

    for index, activation in enumerate(activations):

        for jindex, schedule in enumerate(schedules):

            nn_model = mlrose.NeuralNetwork(hidden_nodes=[8, 6, 5],
                                            activation=activation,
                                            algorithm='simulated_annealing',
                                            schedule=schedule['schedule'],
                                            max_iters=1000,
                                            bias=True,
                                            is_classifier=True,
                                            learning_rate=0.0001,
                                            early_stopping=True,
                                            clip_max=5,
                                            max_attempts=100,
                                            random_state=3)

            fitted_model = nn_model.fit(train_x, train_y)

            # Assess accuracy on test set
            y_test_pred = nn_model.predict(test_x)

            plt.plot(fitted_model.loss,
                     accuracy_score(test_y, y_test_pred),
                     linestyle='-',
                     c=colors[jindex],
                     label=activation,
                     marker=markers[index])

    plt.title("NN Training: SA \n Decay Schedules")
    plt.xlabel("Loss")
    plt.ylabel("Accuracy")
    plt.legend(legend_items, legend_labels, loc="lower right")
    plt.tight_layout()
    plt.savefig("nn_sa_by_schedule")
    plt.cla()
コード例 #16
0
def NeuralNetOptimizeSA(ImportedData, Data):
    iterations = [25, 75, 225]

    # SA parameters
    decay = ['geom', 'exp', 'arith']

    print("Optimizing SA")
    decay_lookup = {
        'geom': mlrose.GeomDecay(),
        'exp': mlrose.ExpDecay(),
        'arith': mlrose.ArithDecay()
    }

    fig = plt.figure(figsize=(12, 8))
    ax = fig.gca(projection='3d')
    fig.suptitle("Neural Nets SA")
    title = "SA"
    elapsed_arr = np.zeros((len(decay), len(iterations)))
    train_arr = np.zeros((len(decay), len(iterations)))
    test_arr = np.zeros((len(decay), len(iterations)))
    cv_arr = np.zeros((len(decay), len(iterations)))
    f1_arr = np.zeros((len(decay), len(iterations)))

    itr = 0
    for i in iterations:
        rest = 0
        for r in decay:
            nn = mlrose.NeuralNetwork(hidden_nodes=[20],
                                      algorithm='simulated_annealing',
                                      max_iters=i,
                                      schedule=decay_lookup[decay[rest]],
                                      curve=False)
            t0 = time.time()
            nn.fit(Data['xTrain'], Data['yTrain'])
            elapsed_arr[rest][itr] = time.time() - t0
            yPred = nn.predict(Data["xTest"])
            train_arr[rest][itr] = accuracy_score(
                Data['yTrain'], nn.predict(Data['xTrain'])) * 100
            test_arr[rest][itr] = accuracy_score(Data['yTest'], yPred) * 100
            cv_arr[rest][itr] = cross_val_score(
                nn, Data['xTrain'], Data['yTrain'], cv=5).mean() * 100
            f1_arr[rest][itr] = f1_score(
                Data["yTest"], yPred, average='binary') * 100

            rest += 1
        itr += 1

    X, Y = np.meshgrid(range(len(iterations)), range(len(decay)))
    ax.contour(X, Y, train_arr, zdir='x', offset=0, color='b')
    ax.contour(X, Y, train_arr, zdir='y', offset=len(decay) - 1, color='b')
    surf = ax.plot_surface(X,
                           Y,
                           train_arr,
                           lw=2,
                           label='train',
                           color='b',
                           alpha=0.3)
    surf._facecolors2d = surf._facecolors3d
    surf._edgecolors2d = surf._edgecolors3d
    ax.contour(X, Y, test_arr, zdir='x', offset=0, color='g')
    ax.contour(X, Y, test_arr, zdir='y', offset=len(decay) - 1, color='g')
    surf = ax.plot_surface(X,
                           Y,
                           test_arr,
                           lw=2,
                           label='test',
                           color='g',
                           alpha=0.3)
    surf._facecolors2d = surf._facecolors3d
    surf._edgecolors2d = surf._edgecolors3d
    ax.contour(X, Y, cv_arr, zdir='x', offset=0, color='r')
    ax.contour(X, Y, cv_arr, zdir='y', offset=len(decay) - 1, color='r')
    surf = ax.plot_surface(X,
                           Y,
                           cv_arr,
                           lw=2,
                           label='CV',
                           color='r',
                           alpha=0.3)
    surf._facecolors2d = surf._facecolors3d
    surf._edgecolors2d = surf._edgecolors3d
    ax.contour(X, Y, f1_arr, zdir='x', offset=0, color='c')
    ax.contour(X, Y, f1_arr, zdir='y', offset=len(decay) - 1, color='c')
    surf = ax.plot_surface(X,
                           Y,
                           f1_arr,
                           lw=2,
                           label='F1',
                           color='c',
                           alpha=0.3)
    surf._facecolors2d = surf._facecolors3d
    surf._edgecolors2d = surf._edgecolors3d

    ax.set(xlabel="Iterations", ylabel="Decay", zlabel='Accuracy')
    ax.set(xticks=range(len(iterations)),
           xticklabels=iterations,
           yticks=range(len(decay)),
           yticklabels=decay)
    fig.legend(loc='center right')
    plt.tight_layout()
    #plt.show()
    saveFigToNewDir(fig, "./", title + "_NeuralNet.png")
    plt.clf()
コード例 #17
0
ファイル: ANN.py プロジェクト: JensenCoding/cs7641-2
    def cv_analysis(self):

        test_acc_sa_1 = []
        for i in range(1000, 50000, 2000):
            print(i)
            nn_model_sa = self.sa(i, mlrose.GeomDecay())

            nn_model_sa.fit(self.training_x, self.training_y)
            # Predict labels for test set and assess accuracy
            y_test_pred_sa = nn_model_sa.predict(self.testing_x)
            y_test_accuracy_sa = accuracy_score(self.testing_y, y_test_pred_sa)
            test_acc_sa_1.append(y_test_accuracy_sa)

        test_acc_sa_2 = []
        for i in range(1000, 50000, 2000):
            nn_model_sa = self.sa(i, mlrose.ExpDecay())

            nn_model_sa.fit(self.training_x, self.training_y)

            # Predict labels for test set and assess accuracy
            y_test_pred_sa = nn_model_sa.predict(self.testing_x)
            y_test_accuracy_sa = accuracy_score(self.testing_y, y_test_pred_sa)
            test_acc_sa_2.append(y_test_accuracy_sa)

        test_acc_sa_3 = []
        for i in range(1000, 50000, 2000):
            nn_model_sa = self.sa(i, mlrose.ArithDecay())

            nn_model_sa.fit(self.training_x, self.training_y)

            # Predict labels for test set and assess accuracy
            y_test_pred_sa = nn_model_sa.predict(self.testing_x)
            y_test_accuracy_sa = accuracy_score(self.testing_y, y_test_pred_sa)
            test_acc_sa_3.append(y_test_accuracy_sa)

        plt.close()
        plt.figure()
        plt.plot(np.arange(1000, 50000, 2000),
                 np.array(test_acc_sa_1),
                 label='Geometric Decay')
        plt.plot(np.arange(1000, 50000, 2000),
                 np.array(test_acc_sa_2),
                 label='Exponential Decay')
        plt.plot(np.arange(1000, 50000, 2000),
                 np.array(test_acc_sa_3),
                 label='Arithmetic Decay')
        plt.title('Neural Network SA Analysis')
        plt.legend(loc="best")
        plt.xlabel('Number of Iterations')
        plt.ylabel('Testing Accuracy')
        plt.savefig(os.path.join(self.output_path, 'NN_SA_analysis.png'))

        test_acc_ga_1 = []
        for i in range(1000, 50000, 2000):
            print(i)
            nn_model_ga = self.ga(i, pop_size=100, mutation_prob=0.1)

            nn_model_ga.fit(self.training_x, self.training_y)

            # Predict labels for test set and assess accuracy
            y_test_pred_ga = nn_model_ga.predict(self.testing_x)
            y_test_accuracy_ga = accuracy_score(self.testing_y, y_test_pred_ga)
            test_acc_ga_1.append(y_test_accuracy_ga)

        test_acc_ga_2 = []
        for i in range(1000, 50000, 2000):
            nn_model_ga = self.ga(i, pop_size=200, mutation_prob=0.1)

            nn_model_ga.fit(self.training_x, self.training_y)

            # Predict labels for test set and assess accuracy
            y_test_pred_ga = nn_model_ga.predict(self.testing_x)
            y_test_accuracy_ga = accuracy_score(self.testing_y, y_test_pred_ga)
            test_acc_ga_2.append(y_test_accuracy_ga)

        test_acc_ga_3 = []
        for i in range(1000, 50000, 2000):
            nn_model_ga = self.ga(i, pop_size=500, mutation_prob=0.1)

            nn_model_ga.fit(self.training_x, self.training_y)

            # Predict labels for test set and assess accuracy
            y_test_pred_ga = nn_model_ga.predict(self.testing_x)
            y_test_accuracy_ga = accuracy_score(self.testing_y, y_test_pred_ga)
            test_acc_ga_3.append(y_test_accuracy_ga)

        test_acc_ga_4 = []
        for i in range(1000, 50000, 2000):
            nn_model_ga = self.ga(i, pop_size=100, mutation_prob=0.5)

            nn_model_ga.fit(self.training_x, self.training_y)

            # Predict labels for test set and assess accuracy
            y_test_pred_ga = nn_model_ga.predict(self.testing_x)
            y_test_accuracy_ga = accuracy_score(self.testing_y, y_test_pred_ga)
            test_acc_ga_4.append(y_test_accuracy_ga)

        test_acc_ga_5 = []
        for i in range(1000, 50000, 2000):
            nn_model_ga = self.ga(i, pop_size=200, mutation_prob=0.5)

            nn_model_ga.fit(self.training_x, self.training_y)

            # Predict labels for test set and assess accuracy
            y_test_pred_ga = nn_model_ga.predict(self.testing_x)
            y_test_accuracy_ga = accuracy_score(self.testing_y, y_test_pred_ga)
            test_acc_ga_5.append(y_test_accuracy_ga)

        test_acc_ga_6 = []
        for i in range(1000, 50000, 2000):
            nn_model_ga = self.ga(i, pop_size=500, mutation_prob=0.5)

            nn_model_ga.fit(self.training_x, self.training_y)

            # Predict labels for test set and assess accuracy
            y_test_pred_ga = nn_model_ga.predict(self.testing_x)
            y_test_accuracy_ga = accuracy_score(self.testing_y, y_test_pred_ga)
            test_acc_ga_6.append(y_test_accuracy_ga)

        plt.close()
        plt.figure()
        plt.plot(np.arange(1000, 50000, 2000),
                 np.array(test_acc_ga_1),
                 label='0.1 and 100')
        plt.plot(np.arange(1000, 50000, 2000),
                 np.array(test_acc_ga_2),
                 label='0.1 and 200')
        plt.plot(np.arange(1000, 50000, 2000),
                 np.array(test_acc_ga_3),
                 label='0.1 and 500')
        plt.plot(np.arange(1000, 50000, 2000),
                 np.array(test_acc_ga_4),
                 label='0.5 and 100')
        plt.plot(np.arange(1000, 50000, 2000),
                 np.array(test_acc_ga_5),
                 label='0.5 and 200')
        plt.plot(np.arange(1000, 50000, 2000),
                 np.array(test_acc_ga_6),
                 label='0.5 and 500')
        plt.title('Neural Network GA Analysis')
        plt.legend(loc="best")
        plt.xlabel('Number of Iterations')
        plt.ylabel('Testing Accuracy')
        plt.savefig(os.path.join(self.output_path, 'NN_GA_analysis.png'))
コード例 #18
0
def main():
    name_of_exp = "K-Color"
    edges = [(0, 1), (0, 2), (0, 4), (1, 3), (2, 0), (2, 3), (3, 4)]
    fitness = mlrose.MaxKColor(edges)
    init_state = np.zeros(5)
    problem = mlrose.DiscreteOpt(length=5, fitness_fn=fitness, maximize=False, max_val=2)

    # Define decay schedule
    schedule = mlrose.ExpDecay()

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

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

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

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

    for x, y, z in zip(x_s, y_s, z_s):
        plt.plot(x, y, label=z)
    plt.legend()
    plt.title('Randomized Optimization Iterations vs Fitness Function Value for {}'.format(name_of_exp))
    plt.xlabel('Function iteration count')
    plt.ylabel('Fitness function value')
    plt.show()
    plt.clf()
    for x, w, z in zip(x_s, w_s, z_s):
        plt.plot(x, w, label=z)
    plt.legend()
    plt.title('Randomized Optimization Time vs Fitness Function Value for {}'.format(name_of_exp))
    plt.xlabel('Function iteration count')
    plt.ylabel('Time in Seconds')
    plt.show()
コード例 #19
0
import six
import sys
sys.modules['sklearn.externals.six'] = six
import mlrose
from problem import fitnessFunction, showProducts

fitness = mlrose.CustomFitness(fitnessFunction)
problem = mlrose.DiscreteOpt(length=14,
                             fitness_fn=fitness,
                             maximize=True,
                             max_val=2)
bestSoluction, bestCost = mlrose.simulated_annealing(
    problem, schedule=mlrose.ArithDecay())

print('Soluction: ', bestSoluction)
showProducts(bestSoluction)
コード例 #20
0
    test_pred = based_model.predict(X_test)
    #print(based_model.predicted_probs)
    print("testing data accuracy: "+str(accuracy_score(y_test, test_pred)*100)+"%")

    # RHC
    opti_model = mlrose.NeuralNetwork(hidden_nodes = [50, 20, 8, 2], activation = "relu", \
                                      bias = True, is_classifier = True, early_stopping = True, \
                                      algorithm=algos[1], \
                                      max_iters=options["max_iter"], max_attempts=options["max_iter"]*0.1, \
                                      learning_rate=options["lrate"])

    if params["optimization_algo"] == 2: # SA
        schedule = mlrose.GeomDecay()
        if options["sched"] == 1:
            schedule = mlrose.ArithDecay()
        elif options["sched"] == 2:
            schedule = mlrose.ExpDecay()

        opti_model = mlrose.NeuralNetwork(hidden_nodes = [50, 20, 8, 2], activation = "relu", \
                                          bias = True, is_classifier = True, early_stopping = True, \
                                          algorithm=algos[2], \
                                          max_iters=options["max_iter"], max_attempts=options["max_iter"]*0.1, \
                                          learning_rate=options["lrate"], \
                                          schedule=schedule)
    
    elif params["optimization_algo"] == 3: #GA
        opti_model = mlrose.NeuralNetwork(hidden_nodes = [50, 20, 8, 2], activation = "relu", \
                                          bias = True, is_classifier = True, early_stopping = True, \
                                          algorithm=algos[3], \
                                          max_iters=options["max_iter"], max_attempts=options["max_iter"]*0.1, \
コード例 #21
0
X = dataset[1:303, 0:12].astype(float)
Y = dataset[1:303, 13].astype(int)

X_train, X_test, y_train, y_test = train_test_split(X,
                                                    Y,
                                                    test_size=0.2,
                                                    random_state=3)
# Normalize feature data
scaler = MinMaxScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# One hot encode target values

lr = [0.001, 0.01, 0.1]
max_iters = [1000, 5000, 10000, 25000, 50000]
decays = [mlrose.ArithDecay(), mlrose.GeomDecay(), mlrose.ExpDecay()]
res = []

nn_model1 = mlrose.NeuralNetwork(hidden_nodes=[2, 7, 1],
                                 activation='relu',
                                 algorithm='simulated_annealing',
                                 schedule=decays[0],
                                 max_iters=70000,
                                 bias=True,
                                 is_classifier=True,
                                 learning_rate=0.01,
                                 early_stopping=False,
                                 clip_max=10,
                                 max_attempts=30)

# Predict labels for train set and assess accuracy
コード例 #22
0
rhc_best_state, rhc_best_fitness, rhc_curve = mlrose.random_hill_climb(
    problem,
    max_attempts=rhc_max_attempts,
    max_iters=rhc_max_iters,
    restarts=rhc_restarts,
    curve=True,
    random_state=SEED)
rhc_time = time.perf_counter() - start_time
print("RHC best fitness: {0:.0f} in {1:.4f} seconds and {2} iterations".format(
    rhc_best_fitness, rhc_time, len(rhc_curve)))
# print('RHC best state:\n', rhc_best_state)

# SA
sa_max_attempts = 500
sa_max_iters = np.inf
sa_schedule = mlrose.ArithDecay(init_temp=10)
start_time = time.perf_counter()
sa_best_state, sa_best_fitness, sa_curve = mlrose.simulated_annealing(
    problem,
    schedule=sa_schedule,
    max_attempts=sa_max_attempts,
    max_iters=sa_max_iters,
    curve=True,
    random_state=SEED)
sa_time = time.perf_counter() - start_time
print("SA best fitness: {0:.0f} in {1:.4f} seconds and {2} iterations".format(
    sa_best_fitness, sa_time, len(sa_curve)))
# print('SA best state:\n', sa_best_state)

# GA
ga_max_attempts = 1
コード例 #23
0
    def optimizeNeuralNetworWeights(self, optimizer):
        self.NNmaxIter = np.arange(5, 1006, 100)
        if (optimizer == 'genetic_alg'):
            self.NNmaxIter = np.arange(5, 1006, 250)
        kFold = 5
        kFoldGen = 1
        iteration = 0
        learner = []
        legend = []
        timeTraining = []
        learnersTrainingScore = []
        learnersTestingScore = []
        learnersTrainingScoreStd = []
        learnersTestingScoreStd = []
        kFoldTrainingScore = []
        kFoldTestingScore = []
        learnersTime = []
        learnersTimeStd = []
        kFoldTime = []

        for iter in self.NNmaxIter:
            learner.append([])
            legend.append([])
            if (optimizer == 'gradient_descent'):
                nn = mlrose.NeuralNetwork(hidden_nodes=[20], activation='relu', algorithm='gradient_descent',
                                            max_iters=int(iter), bias=True, is_classifier=True, learning_rate=0.001,
                                            early_stopping=False, clip_max=1e10, curve=False)
                learner[iteration].append(nn)
                legend[iteration].append('GA')
            elif (optimizer == 'simulated_annealing'):
                nn = mlrose.NeuralNetwork(hidden_nodes=[20], activation='relu', algorithm='simulated_annealing',
                                            max_iters=int(iter), bias=True, is_classifier=True, learning_rate=0.001,
                                            early_stopping=False, clip_max=1e10, schedule=mlrose.ExpDecay(),
                                            curve=False)
                learner[iteration].append(nn)
                legend[iteration].append('Exp')
                nn = mlrose.NeuralNetwork(hidden_nodes=[20], activation='relu', algorithm='simulated_annealing',
                                          max_iters=int(iter), bias=True, is_classifier=True, learning_rate=0.001,
                                          early_stopping=False, clip_max=1e10, schedule=mlrose.ArithDecay(),
                                          curve=False)
                learner[iteration].append(nn)
                legend[iteration].append('Arithmetic')
                nn = mlrose.NeuralNetwork(hidden_nodes=[20], activation='relu', algorithm='simulated_annealing',
                                          max_iters=int(iter), bias=True, is_classifier=True, learning_rate=0.001,
                                          early_stopping=False, clip_max=1e10, schedule=mlrose.GeomDecay(),
                                          curve=False)
                learner[iteration].append(nn)
                legend[iteration].append('Geometric')
            elif (optimizer == 'genetic_alg'):
                nn = mlrose.NeuralNetwork(hidden_nodes=[20], activation='relu', algorithm='genetic_alg',
                                            max_iters=int(iter), bias=True, is_classifier=True, learning_rate=0.001,
                                            early_stopping=False, clip_max=1e10, pop_size=300, mutation_prob=0.2,
                                            curve=False)
                learner[iteration].append(nn)
                legend[iteration].append('300')
                nn = mlrose.NeuralNetwork(hidden_nodes=[20], activation='relu', algorithm='genetic_alg',
                                          max_iters=int(iter), bias=True, is_classifier=True, learning_rate=0.001,
                                          early_stopping=False, clip_max=1e10, pop_size=200, mutation_prob=0.2,
                                          curve=False)
                learner[iteration].append(nn)
                legend[iteration].append('200')
                nn = mlrose.NeuralNetwork(hidden_nodes=[20], activation='relu', algorithm='genetic_alg',
                                          max_iters=int(iter), bias=True, is_classifier=True, learning_rate=0.001,
                                          early_stopping=False, clip_max=1e10, pop_size=100, mutation_prob=0.2,
                                          curve=False)
                learner[iteration].append(nn)
                legend[iteration].append('100')
                nn = mlrose.NeuralNetwork(hidden_nodes=[20], activation='relu', algorithm='genetic_alg',
                                          max_iters=int(iter), bias=True, is_classifier=True, learning_rate=0.001,
                                          early_stopping=False, clip_max=1e10, pop_size=200, mutation_prob=0.1,
                                          curve=False)
                learner[iteration].append(nn)
                legend[iteration].append('0.1')
                nn = mlrose.NeuralNetwork(hidden_nodes=[20], activation='relu', algorithm='genetic_alg',
                                          max_iters=int(iter), bias=True, is_classifier=True, learning_rate=0.001,
                                          early_stopping=False, clip_max=1e10, pop_size=200, mutation_prob=0.2,
                                          curve=False)
                learner[iteration].append(nn)
                legend[iteration].append('0.2')
                nn = mlrose.NeuralNetwork(hidden_nodes=[20], activation='relu', algorithm='genetic_alg',
                                          max_iters=int(iter), bias=True, is_classifier=True, learning_rate=0.001,
                                          early_stopping=False, clip_max=1e10, pop_size=200, mutation_prob=0.3,
                                          curve=False)
                learner[iteration].append(nn)
                legend[iteration].append('0.3')
                kFold = kFoldGen
            elif (optimizer == 'random_hill_climb'):
                nn = mlrose.NeuralNetwork(hidden_nodes=[20], activation='relu', algorithm='random_hill_climb',
                                             max_iters=int(iter), bias=True, is_classifier=True, learning_rate=0.001,
                                             early_stopping=False, clip_max=1e10, curve=False)
                learner[iteration].append(nn)
                legend[iteration].append('RHC')

            iteration += 1

        for l in range(len(learner[0])):
            learnersTrainingScore.append([])
            learnersTestingScore.append([])
            learnersTrainingScoreStd.append([])
            learnersTestingScoreStd.append([])
            learnersTime.append([])
            learnersTimeStd.append([])
            for iter in range(len(self.NNmaxIter)):
                learnersTrainingScore[l].append(l)
                learnersTestingScore[l].append(l)
                learnersTrainingScoreStd[l].append(l)
                learnersTestingScoreStd[l].append(l)
                learnersTime[l].append(l)
                learnersTimeStd[l].append(l)
                kFoldTrainingScore = []
                kFoldTestingScore = []
                kFoldTime = []
                for k in range(kFold):
                    training_x, testing_x, training_y, testing_y = ms.train_test_split(
                        self.dataset1.training_x, self.dataset1.training_y, test_size=0.2, random_state=self.dataset1.seed,
                        stratify=self.dataset1.training_y)

                    timeStart = time.time()
                    learner[iter][l].fit(training_x, training_y)
                    timeTraining = (time.time() - timeStart)

                    trainingF1 = f1_score(learner[iter][l].predict(training_x), training_y, average='weighted')
                    testingF1 = f1_score(learner[iter][l].predict(testing_x), testing_y, average='weighted')
                    kFoldTrainingScore.append(trainingF1)
                    kFoldTestingScore.append(testingF1)
                    kFoldTime.append(timeTraining)

                learnersTrainingScore[l][iter] = (np.mean(kFoldTrainingScore))
                learnersTestingScore[l][iter] = (np.mean(kFoldTestingScore))
                learnersTrainingScoreStd[l][iter] = (np.std(kFoldTrainingScore))
                learnersTestingScoreStd[l][iter] = (np.std(kFoldTestingScore))
                learnersTime[l][iter] = (np.mean(kFoldTime))
                learnersTimeStd[l][iter] = (np.std(kFoldTime))

        learnersTrainingScore = np.array(learnersTrainingScore)
        learnersTestingScore = np.array(learnersTestingScore)
        learnersTrainingScoreStd = np.array(learnersTrainingScoreStd)
        learnersTestingScoreStd = np.array(learnersTestingScoreStd)
        learnersTime = np.array(learnersTime)
        learnersTimeStd = np.array(learnersTimeStd)

        learnerCount = 0
        if (optimizer != 'genetic_alg'):
            plt.style.use('seaborn-whitegrid')
            for l in range(len(learnersTrainingScore)):
                plt.plot(self.NNmaxIter, learnersTrainingScore[l], label='Train for ' + legend[0][l])
                plt.plot(self.NNmaxIter, learnersTestingScore[l], label='Valid. for ' + legend[0][l])
            plt.ylabel('Score', fontsize=12)
            plt.xlabel('Number of Iterations', fontsize=12)
            plt.title('F1 Score for NN trained using ' + optimizer, fontsize=12, y=1.03)
            plt.legend()
            plt.savefig('Figures/' + optimizer + '-F1-Score.png')
            plt.close()
        else:
            plt.style.use('seaborn-whitegrid')
            splitParams = int(len(learnersTrainingScore) / 2)
            for l in range(splitParams):
                plt.plot(self.NNmaxIter, learnersTrainingScore[l], label='Train for ' + legend[0][l])
                plt.plot(self.NNmaxIter, learnersTestingScore[l], label='Valid. for ' + legend[0][l])
            plt.ylabel('Score', fontsize=12)
            plt.xlabel('Number of Iterations', fontsize=12)
            plt.title('F1 Score for NN trained using ' + optimizer, fontsize=12, y=1.03)
            plt.legend()
            plt.savefig('Figures/' + optimizer + '-F1-Score, 1.png')
            plt.close()

            plt.style.use('seaborn-whitegrid')
            for l in range(splitParams):
                plt.plot(self.NNmaxIter, learnersTrainingScore[l+splitParams], label='Train for ' + legend[0][l+splitParams])
                plt.plot(self.NNmaxIter, learnersTestingScore[l+splitParams], label='Valid. for ' + legend[0][l+splitParams])
            plt.ylabel('Score', fontsize=12)
            plt.xlabel('Number of Iterations', fontsize=12)
            plt.title('F1 Score for NN trained using ' + optimizer, fontsize=12, y=1.03)
            plt.legend()
            plt.savefig('Figures/' + optimizer + '-F1-Score, 2.png')
            plt.close()

        return learnersTrainingScore, learnersTrainingScoreStd, learnersTestingScore, learnersTestingScoreStd, learnersTime, learnersTimeStd