示例#1
0
    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
示例#2
0
    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 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()
示例#4
0
文件: demo.py 项目: dfd/CS7641_hw02
def queens_problem(max_attempts, max_iters):
    fitness_cust = mlrose.CustomFitness(queens_max)

    problem = mlrose.DiscreteOpt(length=8,
                                 fitness_fn=fitness,
                                 maximize=False,
                                 max_val=8)
    #problem = mlrose.DiscreteOpt(length=8, fitness_fn=queens_max, maximize=True, max_val=8)

    # Define decay schedule
    schedule = mlrose.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 = mlrose.simulated_annealing(
        problem,
        schedule=schedule,
        max_attempts=max_attempts,
        max_iters=max_iters,
        init_state=init_state,
        random_state=1)
    print(best_state)
    print(best_fitness)
示例#5
0
def runComplexityAnnealing(pType, problem):
    if pType == 'One Max':
        neighbor = 10
        iterations = 400
    elif pType == 'Flip Flop':
        neighbor = 36
        iterations = 700
    else:
        neighbor = 4
        iterations = 1000

    s = time()
    best_state, best_fitness = mlrose.simulated_annealing(
        problem,
        schedule=mlrose.ExpDecay(),
        max_attempts=neighbor,
        max_iters=iterations,
        random_state=1)
    # best_state, best_fitness, c = mlrose.simulated_annealing(problem, schedule=mlrose.ExpDecay(),
    #                                                     max_attempts=neighbor,
    #                                                     max_iters=iterations,
    #                                                     random_state=1)
    timeTaken = time() - s

    return best_fitness, timeTaken
示例#6
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()
     }
def main():
    # Load the Wine dataset
    full_path = 'data/winequality-white.csv'
    X, y = load_dataset(full_path)

    #Split data into training and test sets
    X_train_scaled, X_test_scaled, y_train_hot, y_test_hot = train_test_split(X, y, test_size=0.2, random_state=RANDOM_STATE)

    # Initialize neural network object and fit object
    t_bef = time.time()
    nn_model1 = mlrose.NeuralNetwork(hidden_nodes = [4], activation = 'relu', \
                                     algorithm = 'simulated_annealing', max_iters = 1000, \
                                     bias = True, is_classifier = True, learning_rate = 0.9, \
                                     early_stopping = True, clip_max = 5, schedule=mlrose.ExpDecay(init_temp=20.0, exp_const=0.005, min_temp=0.001), max_attempts = 10, \
                                     random_state = RANDOM_STATE)


    nn_model1.fit(X_train_scaled, y_train_hot)
    t_aft = time.time()

    curve = nn_model1.fitness_curve

    # Predict labels for train set and assess accuracy
    y_train_pred = nn_model1.predict(X_train_scaled)

    y_train_accuracy = accuracy_score(y_train_hot, y_train_pred)

    print("Train accuracy:", y_train_accuracy)

    # Predict labels for test set and assess accuracy
    y_test_pred = nn_model1.predict(X_test_scaled)

    y_test_accuracy = accuracy_score(y_test_hot, y_test_pred)

    print("Test accuracy:", y_test_accuracy)
示例#8
0
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
示例#9
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)
示例#10
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)
def optimization(problem):
    df_optim = pd.DataFrame(columns=algos)
    df_iter = pd.DataFrame(columns=algos)
    df_time = pd.DataFrame(columns=algos)

    for rs in random_states:
        # RHC
        tic = time.process_time()
        best_state_rhc, best_fitness_rhc, curve_rhc = \
            mlrose.random_hill_climb(problem, max_attempts=20, max_iters=100000,
                                     restarts=0, init_state=None, curve=True, random_state=rs)
        toc = time.process_time()
        time_rhc = toc - tic

        # SA
        tic = time.process_time()
        best_state_sa, best_fitness_sa, curve_sa = \
            mlrose.simulated_annealing(problem, schedule=mlrose.ExpDecay(init_temp=1.0, exp_const=0.005, min_temp=0.001),
                                       max_attempts = 20, max_iters = 100000,
                                       init_state = None, curve = True, random_state = rs)
        toc = time.process_time()
        time_sa = toc - tic

        # GA
        tic = time.process_time()
        best_state_ga, best_fitness_ga, curve_ga = \
            mlrose.genetic_alg(problem, pop_size=200, mutation_prob=0.1, max_attempts=20, max_iters=100000,
                               curve=True, random_state=rs)
        toc = time.process_time()
        time_ga = toc - tic

        # MIMIC
        tic = time.process_time()
        best_state_m, best_fitness_m, curve_m = \
            mlrose.mimic(problem, pop_size=20, keep_pct=0.2, max_attempts=20, max_iters=100000,
                         curve=True, random_state=rs, fast_mimic=False)
        toc = time.process_time()
        time_m = toc - tic

        # df
        df_optim.loc[len(df_optim)] = [
            best_fitness_rhc, best_fitness_sa, best_fitness_ga, best_fitness_m
        ]
        df_iter.loc[len(df_iter)] = [
            len(curve_rhc),
            len(curve_sa),
            len(curve_ga),
            len(curve_m)
        ]
        df_time.loc[len(df_time)] = [time_rhc, time_sa, time_ga, time_m]
        print(rs)

    return (df_optim.mean(axis=0), df_iter.mean(axis=0), df_time.mean(axis=0))
示例#12
0
def simulatedAnnealing(problem, init_state, max_attempts, iterations):
    #Define decay schedule
    schedule = mlrose.ExpDecay()
    #Solve problem using Simulated Annealing
    sm_best_state, sm_best_fitness = mlrose.simulated_annealing(
        problem,
        schedule=schedule,
        max_attempts=max_attempts,
        max_iters=iterations,
        curve=False,
        random_state=1)
    return sm_best_fitness
示例#13
0
 def sa(self, max_iter=10000, schedule=None):
     if schedule is None:
         schedule = mlrose.ExpDecay()
     learner = mlrose.NeuralNetwork(hidden_nodes=[2],
                                    activation='relu',
                                    algorithm='simulated_annealing',
                                    max_iters=max_iter,
                                    bias=True,
                                    learning_rate=3e-03,
                                    early_stopping=True,
                                    max_attempts=1000,
                                    schedule=schedule)
     return learner
示例#14
0
def pruebas():
    # read data
    print(
        '\n============== Problema del agente viajero ======================')
    #### get data
    times_list, lips, prod_lips = times_array('datos_macbelle_dummie.csv',
                                              'tiempos_produccion.csv')

    #### metaheuristics
    n = len(lips)

    #genetic alg
    # simple
    args = {'random_state': 41}
    bs, bf = metaheuristic(times_list, meta=mr.genetic_alg, nnodes=n, **args)

    # modified hypérparameters
    args2 = {'random_state': 41, 'mutation_prob': 0.3, 'max_attempts': 20}
    bs2, bf2 = metaheuristic(times_list,
                             meta=mr.genetic_alg,
                             nnodes=n,
                             **args2)

    # simulated anealing
    # simple
    args_sa = {'random_state': 41}
    bs_sa, bf_sa = metaheuristic(times_list,
                                 meta=mr.simulated_annealing,
                                 nnodes=n,
                                 **args_sa)

    # change temperature decay
    # look documentation mlrose of decays
    args_sa2 = {'random_state': 41, 'schedule': mr.ExpDecay(exp_const=0.05)}
    bs_sa2, bf_sa2 = metaheuristic(times_list,
                                   meta=mr.simulated_annealing,
                                   nnodes=n,
                                   **args_sa2)

    # combined with genetic alg solutions
    args_sa3 = {
        'random_state': 41,
        'schedule': mr.GeomDecay(decay=0.05),
        'init_state': bs
    }  #use GA solution as initial state
    bs_sa3, bf_sa3 = metaheuristic(times_list,
                                   meta=mr.simulated_annealing,
                                   nnodes=n,
                                   **args_sa3)
    return ()
示例#15
0
def nn_sa_temps(train_x, train_y, test_x, test_y):

    min_temps = [1, 5]
    init_temps = [100, 500]
    markers = ['o', 'x', '^', 's']
    colors = ['g', 'b', 'r', 'c', 'y', 'm']

    counter = 0

    for index, min_temp in enumerate(min_temps):

        for jindex, init_temp in enumerate(init_temps):

            schedule = mlrose.ExpDecay(init_temp=init_temp,
                                       exp_const=0.001,
                                       min_temp=min_temp)

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

            fitted_model = nn_model.fit(train_x, train_y)

            label = "min_temp: " + str(min_temp) + ", init_temp: " + str(
                init_temp)

            plt.plot(fitted_model.fitness_curve,
                     c=colors[counter],
                     label=label)

            counter += 1

    plt.xlabel("No. of Iterations")
    plt.ylabel("Fitness")
    plt.title("NN Training: SA")
    plt.legend(loc="lower right")
    plt.tight_layout()
    plt.savefig("nn_sa_temps")
    plt.cla()
 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]
示例#17
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()
示例#18
0
def main():
    # want to maximize this
    fitness = mlrose.CustomFitness(detected_max)
    problem = mlrose.DiscreteOpt(length=24,
                                 fitness_fn=fitness,
                                 maximize=True,
                                 max_val=scale_factor)
    schedule = mlrose.ExpDecay()
    best_state, max_faces = mlrose.simulated_annealing(
        problem,
        schedule=schedule,
        max_attempts=10,
        max_iters=1000,
        init_state=initial_state,
        random_state=1)

    print('Optimal state found: ', best_state)
    print('Max fitness found: ', max_faces)
    # save the optimal found
    get_img_from_state(best_state)
    print("Number of faces in output: ", len(detect_faces(cv2.imread(OUTPUT))))
示例#19
0
def runalgos(problem, max_attempts, iterations):
    #Define decay schedule
    schedule = mlrose.ExpDecay()
    #Solve problem using Random Hill Climbing
    rh_best_state, rh_best_fitness = mlrose.random_hill_climb(
        problem,
        max_attempts=max_attempts,
        max_iters=iterations,
        restarts=1,
        init_state=init_state,
        random_state=1)
    #Solve problem using Simulated Annealing
    sm_best_state, sm_best_fitness = mlrose.simulated_annealing(
        problem,
        schedule=schedule,
        max_attempts=max_attempts,
        max_iters=iterations,
        curve=False,
        random_state=1)
    #Solve problem using Gentic Algorithm
    genA_best_state, genA_best_fitness = mlrose.genetic_alg(
        problem,
        pop_size=200,
        mutation_prob=0.1,
        max_attempts=max_attempts,
        max_iters=iterations,
        curve=False,
        random_state=1)
    #Solve problem using MIMIC
    mimic_best_state, mimic_best_fitness = mlrose.mimic(
        problem,
        pop_size=200,
        keep_pct=0.2,
        max_attempts=max_attempts,
        max_iters=iterations,
        curve=False,
        random_state=1)
    #return rh_best_state,rh_best_fitness,sm_best_state,sm_best_fitness,genA_best_state,genA_best_fitness,mimic_best_state,mimic_best_fitness
    return rh_best_fitness, sm_best_fitness, genA_best_fitness, mimic_best_fitness
示例#20
0
def main():

    # mlrose  

    fitness_cust = mlrose.CustomFitness(simulate)

    problem = mlrose.DiscreteOpt(fitness_fn=fitness_cust, maximize=False, length=2, max_val = 30)


    schedule = mlrose.ExpDecay()
    
    init_state = np.array([28,7])

    best_time_values, best_avg_time = mlrose.simulated_annealing(problem, schedule = schedule,
                                                      max_attempts = 10, max_iters = 100,
                                                      init_state = init_state, random_state = 1)

    print("Best time values:")
    print(best_time_values)

    print("Best avg time:")
    print(best_avg_time)
示例#21
0
rsed = int(sys.argv[16])

dat = np.loadtxt(open("data/digit.csv", "rb"), delimiter=",", skiprows=1)
target = dat[:, dat.shape[1] - 1].flatten().astype(np.int32)
V = dat[:, 0:(dat.shape[1] - 1)]
Xtrain = V[0:splitN]  # 70% of data are training data.
endN = min(splitN + splitN, 5540)
Xtest = V[splitN:endN]
ytrain = target[0:splitN]
ytest = target[splitN:endN]

oneH = OneHotEncoder()
ytrain = oneH.fit_transform(ytrain.reshape(-1, 1)).todense()
ytest = oneH.fit_transform(ytest.reshape(-1, 1)).todense()

schedule = mlrose.ExpDecay(init_temp=iniT, exp_const=Tdcy, min_temp=minT)

timeCostAvg = 0.0
ytrainAccAvg = 0.0
ytestAccAvg = 0.0
fitnessAvg = np.zeros(mxit)
Ntry = 10

for i in range(Ntry):
    model = mlrose.NeuralNetwork(hidden_nodes=[16],
                                 activation='relu',
                                 algorithm='simulated_annealing',
                                 max_iters=mxit,
                                 bias=True,
                                 is_classifier=True,
                                 learning_rate=lrte,
示例#22
0
def main():
    name_of_exp = "One Max"
    fitness = mlrose.OneMax()
    rhc = []
    sa = []
    ga = []
    mimic = []
    z_s = ['RHC', 'SA', 'GA', 'MIMIC']
    for i in [5, 10, 15]:
        problem = mlrose.DiscreteOpt(length=i,
                                     fitness_fn=fitness,
                                     maximize=True,
                                     max_val=2)
        # Define initial state
        init_state = np.zeros(i)

        # Solve problem using simulated annealing
        best_fitness = 0
        learning_curve = []
        max_atts = 10
        print("RHC")
        while best_fitness != i:
            best_state, best_fitness, learning_curve, timing_curve = mlrose.random_hill_climb(
                problem,
                max_attempts=max_atts,
                max_iters=max_atts,
                restarts=1,
                init_state=init_state,
                curve=True,
                random_state=1)
            max_atts += 10

        rhc.append(learning_curve)
        print(i)
        print(best_fitness)
        print(max_atts)
        print("SA")
        best_fitness = 0
        learning_curve = []
        max_atts = 10
        while best_fitness != i:
            best_state, best_fitness, learning_curve, timing_curve = mlrose.simulated_annealing(
                problem,
                max_attempts=max_atts,
                max_iters=max_atts,
                schedule=mlrose.ExpDecay(),
                init_state=init_state,
                curve=True,
                random_state=1)
            max_atts += 10

        sa.append(learning_curve)
        print(i)
        print(best_fitness)
        print(max_atts)
        print("GA")
        best_fitness = 0
        learning_curve = []
        max_atts = 10
        while best_fitness != i:
            best_state, best_fitness, learning_curve, timing_curve = mlrose.genetic_alg(
                problem,
                pop_size=100,
                mutation_prob=0.1,
                max_attempts=max_atts,
                max_iters=max_atts,
                curve=True,
                random_state=1)
            max_atts += 10
        ga.append(learning_curve)
        print(i)
        print(best_fitness)
        print(max_atts)
        print("MIMC")
        best_fitness = 0
        learning_curve = []
        max_atts = 10
        while best_fitness != i:
            best_state, best_fitness, learning_curve, timing_curve = mlrose.mimic(
                problem,
                pop_size=300,
                keep_pct=0.1,
                max_attempts=max_atts,
                max_iters=max_atts,
                curve=True,
                random_state=1,
                fast_mimic=True)
            max_atts += 10
        mimic.append(learning_curve)
        print(i)
        print(best_fitness)
        print(max_atts)
    f, axarr = plt.subplots(1, 4)
    f.set_figheight(3)
    f.set_figwidth(12)
    for y in rhc:
        for i in ['5', '10', '15']:
            axarr[0].plot(y, label='{}'.format(i))
    axarr[0].set_title('RHC vs Input Size')
    for y in sa:
        for i in ['5', '10', '15']:
            axarr[1].plot(y, label='{}'.format(i))
    axarr[1].set_title('SA vs Input Size')
    for y in ga:
        for i in ['5', '10', '15']:
            axarr[2].plot(y, label='{}'.format(i))
    axarr[2].set_title('GA vs Input Size')
    for y in mimic:
        for i in ['5', '10', '15']:
            axarr[3].plot(y, label='{}'.format(i))
    axarr[3].set_title('MIMC vs Input Size')
    # 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='g', lw=4, label='5'),
        Line2D([0], [0], color='brown', lw=4, label='10'),
        Line2D([0], [0], color='y', lw=4, label='15')
    ])
    #plt.title('Input size vs fitness curve One Max')
    # plt.xlabel('Function iteration count')
    #plt.ylabel('Fitness function value')

    plt.show()
示例#23
0
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()
示例#24
0
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")
示例#25
0
def sim_annealing_runner(problem):
    initial_temps = np.arange(.1, 5, .5)  #.1)
    final_temps = np.arange(.001, 1, .1)
    decay_rates = np.arange(.001, 1, .1)
    attempts = np.arange(10, 500, 50).astype(int)
    iterations = np.arange(10, 500, 50).astype(int)

    scoring_dict = {}
    timing_dict = {}

    init_temp_scores = []
    final_temp_scores = []

    for i, temp in enumerate(initial_temps):
        anneal_schedule = mlrose.ExpDecay(init_temp=temp)
        best_fits = []
        for i in MC_RUNS:
            _, best_fitness = mlrose.simulated_annealing(
                problem,
                schedule=anneal_schedule,
                max_attempts=100,
                max_iters=800)
            best_fits.append(best_fitness)

        init_temp_scores.append(best_fits)
    scoring_dict['Initial Temperature'] = pd.DataFrame(init_temp_scores,
                                                       columns=MC_RUNS,
                                                       index=initial_temps)

    for i, temp in enumerate(final_temps):
        anneal_schedule = mlrose.ExpDecay(min_temp=temp)
        best_fits = []
        for i in MC_RUNS:
            _, best_fitness = mlrose.simulated_annealing(
                problem,
                schedule=anneal_schedule,
                max_attempts=100,
                max_iters=800)
            best_fits.append(best_fitness)

        final_temp_scores.append(best_fits)
    scoring_dict['Ending Temperature'] = pd.DataFrame(final_temp_scores,
                                                      index=final_temps,
                                                      columns=MC_RUNS)

    decay_scores = []
    for i, rate in enumerate(decay_rates):
        anneal_schedule = mlrose.ExpDecay(exp_const=rate)
        best_fits = []
        for i in MC_RUNS:
            _, best_fitness = mlrose.simulated_annealing(
                problem,
                schedule=anneal_schedule,
                max_attempts=100,
                max_iters=800)
            best_fits.append(best_fitness)
        decay_scores.append(best_fits)
    scoring_dict['Decay Rate'] = pd.DataFrame(decay_scores,
                                              columns=MC_RUNS,
                                              index=decay_rates)

    attempts_scores = []
    attempts_timing = []
    for i, att in enumerate(attempts):
        anneal_schedule = mlrose.ExpDecay()
        best_fits = []
        times = []
        for i in MC_RUNS:
            start = datetime.now()
            _, best_fitness = mlrose.simulated_annealing(
                problem,
                schedule=anneal_schedule,
                max_attempts=int(att),
                max_iters=800)
            end = datetime.now()
            best_fits.append(best_fitness)
            times.append((end - start).total_seconds())

        attempts_scores.append(best_fits)
        attempts_timing.append(times)
    scoring_dict['Number of Attempts'] = pd.DataFrame(attempts_scores,
                                                      columns=MC_RUNS,
                                                      index=attempts)
    timing_dict['Number of Attempts'] = pd.DataFrame(attempts_timing,
                                                     columns=MC_RUNS,
                                                     index=attempts)

    iteration_scores = []
    for i, iteration in enumerate(iterations):
        anneal_schedule = mlrose.ExpDecay()
        best_fits = []
        for i in MC_RUNS:
            _, best_fitness = mlrose.simulated_annealing(
                problem,
                schedule=anneal_schedule,
                max_attempts=100,
                max_iters=int(iteration))
            best_fits.append(best_fitness)
        iteration_scores.append(best_fits)
    scoring_dict['Max Iterations'] = pd.DataFrame(iteration_scores,
                                                  columns=MC_RUNS,
                                                  index=iterations)

    return scoring_dict, timing_dict
示例#26
0
    end = time.time()
    return np.array([best_score, len(curve), fit_evals, end - start])


# ## SA param search
runs = 15
temps = np.arange(1, 32, 2)
sa_results = {}
with concurrent.futures.ThreadPoolExecutor() as exe:
    for prob in probs:
        if prob['name'] not in sa_results: sa_results[prob['name']] = {}
        for temp in temps:
            if temp not in sa_results[prob['name']]:
                sa_results[prob['name']][temp] = []
            for run in range(runs + 1):
                schedule = mlrose.ExpDecay(init_temp=temp, min_temp=0)
                sa_results[prob['name']][temp].append(
                    exe.submit(sim_anneal,
                               prob['obj'],
                               max_iters=500,
                               schedule=schedule))

# ### SA param search plotting
sa_plot_data = {}
for prob in sa_results:
    sa_plot_data[prob] = None
    for temp in sa_results[prob]:
        data = None
        for run in sa_results[prob][temp]:
            # DATA: temp, score, iters, fit_evals, runtime
            if data is None:
示例#27
0
import time as tt
import numpy as np
import mlrose as mlrose
import pandas as pd

ps = [8,32,64,128]


for p in ps:
    fitness = mlrose.FlipFlop()
    istate = np.array(np.zeros(p), dtype=int)

    problem = mlrose.DiscreteOpt(length=p, fitness_fn=fitness,
                                 maximize=True, max_val=2)

    schedule = mlrose.ExpDecay(init_temp=0.5, exp_const=0.005, min_temp=0.001)

    start_fit_time = tt.time()
    best_state, best_fitness,fitness_curve = mlrose.random_hill_climb(problem,max_attempts = 500, max_iters = 500,restarts=0,init_state = istate, random_state = 1,curve=True)

    end_fit_time = tt.time()

    it = len(fitness_curve)

    time = end_fit_time - start_fit_time
    tpi = time/it
    print('SA')    
    print(f'sa, input size: {p},time={time}, iterations={it}, time per iteration={tpi}')


示例#28
0
# FOUR PEAKS

fourpeaks = mlrose.FourPeaks()
fourpeaks_problem = mlrose.DiscreteOpt(length=60,
                                       maximize=True,
                                       max_val=2,
                                       fitness_fn=fourpeaks)

base_test(fourpeaks,
          theoretical_best_fitness=lambda x: 2 * x - 0.1 * x - 1)  # 73s
optimize_ga(fourpeaks_problem)  # 768s
optimize_sa(fourpeaks_problem)  # 532s
optimize_rhc(fourpeaks_problem)  # 822s
optimize_mimic(fourpeaks_problem)  # 2464s
final_test(fourpeaks_problem, [700, 0.4], mlrose.ExpDecay(8, 0.00001), 5000,
           [500, 0.022])  # 1191s

# SAW
saw_fitness = mlrose.CustomFitness(saw, problem_type='discrete')
saw_problem = mlrose.DiscreteOpt(length=700,
                                 maximize=True,
                                 max_val=2,
                                 fitness_fn=saw_fitness)

base_test(saw_fitness,
          theoretical_best_fitness=lambda x: x,
          lengths=range(200, 701, 100))  # 2476s
optimize_ga(saw_problem)  # 7255s
optimize_sa(saw_problem)  # 8730s
optimize_rhc(saw_problem)  # 103s
示例#29
0
import numpy as np

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler, OneHotEncoder
from sklearn.metrics import accuracy_score

# Initialize fitness function object using pre-defined class
fitness = mlrose.FourPeaks(t_pct=0.15)
# Define optimization problem object
problem = mlrose.DiscreteOpt(length=100,
                             fitness_fn=fitness,
                             maximize=True,
                             max_val=2)
# Define decay schedule
schedule = mlrose.ExpDecay()
# Run Genetic Algorithm
best_state, best_fitness, fit_curve = mlrose.genetic_alg(problem,
                                                         max_attempts=1000,
                                                         max_iters=1000,
                                                         curve=True,
                                                         random_state=1)
# Stop Timer
elapsed_time = timer() - start  # in seconds
print('Time elapsed time in seconds is: ', elapsed_time)
print('The best state found is: ', best_state)
print('The fitness at the best state is: ', best_fitness)
# Plot Curve
import matplotlib.pyplot as plt
plt.plot(fit_curve)
plt.ylabel('Fitness')
示例#30
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)