Ejemplo n.º 1
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()
     }
Ejemplo n.º 2
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
Ejemplo n.º 3
0
 def __init__(self, hidden_nodes=None,
              activation='relu',
              algorithm='random_hill_climb',
              max_iters=1000,
              bias=True,
              is_classifier=True,
              learning_rate=0.1,
              early_stopping=False,
              clip_max=1e+10,
              restarts=0,
              schedule=mlrose.GeomDecay(),
              pop_size=200,
              mutation_prob=0.1,
              max_attempts=10,
              random_state=None,
              curve=False):
     super(NeuralNetwork, self).__init__(
         hidden_nodes=hidden_nodes,
         activation=activation,
         algorithm=algorithm,
         max_iters=max_iters,
         bias=bias,
         is_classifier=is_classifier,
         learning_rate=learning_rate,
         early_stopping=early_stopping,
         clip_max=clip_max,
         restarts=restarts,
         schedule=schedule,
         pop_size=pop_size,
         mutation_prob=mutation_prob,
         max_attempts=max_attempts,
         random_state=random_state,
         curve=curve)
     self.one_hot = OneHotEncoder(categories='auto')
def build_model(algorithm,
                hidden_nodes,
                activation,
                schedule=mlrose.GeomDecay(init_temp=5000),
                restarts=75,
                population=300,
                mutation=0.2,
                max_iters=20000,
                learning_rate=0.01):
    nn_model = mlrose.neural.NeuralNetwork(hidden_nodes=hidden_nodes,
                                           activation=activation,
                                           algorithm=algorithm,
                                           max_iters=max_iters,
                                           bias=True,
                                           is_classifier=True,
                                           learning_rate=learning_rate,
                                           early_stopping=False,
                                           restarts=restarts,
                                           clip_max=1,
                                           schedule=schedule,
                                           max_attempts=1000,
                                           pop_size=population,
                                           mutation_prob=mutation,
                                           random_state=17,
                                           curve=True)
    return nn_model
Ejemplo n.º 5
0
def max_iterations(n,cords,fit,temp,mint):
    best_fit1=[]
    best_fit2=[]
    best_fit3=[]
    max_iter=[]
    for i in range(100,n,10):
        best_state,best_fitness1 = mlrose.genetic_alg(problem_fit, mutation_prob = 0.2, max_attempts = 100,max_iters=i)
        best_state,best_fitness2 = mlrose.mimic(problem_fit,pop_size=200,keep_pct=0.3,max_attempts=10,max_iters=i)
        best_state,best_fitness3 = mlrose.simulated_annealing(problem_fit,schedule=mlrose.GeomDecay(init_temp=temp,decay=0.9,min_temp=mint),max_attempts=100,max_iters=i,init_state=None)
        max_iter.append(i)
        best_fit1.append(best_fitness1)
        best_fit2.append(best_fitness2)
        best_fit3.append(best_fitness3)
        print(best_fit1,best_fit2,best_fit3)
    max_iter=np.asarray(max_iter)
    best_fit1=np.asarray(best_fit1)
    best_fit2=np.asarray(best_fit2)
    best_fit3=np.asarray(best_fit3)
    line1, = plt.plot(max_iter,best_fit1,color='r',label='fitness_score')
    line2, = plt.plot(max_iter,best_fit2,color='g',label='fitness_score')
    line3, = plt.plot(max_iter,best_fit3,color='b',label='fitness_score')
    plt.ylabel('Fitness_score')
    plt.xlabel('Number of iterations')
    plt.show()
    return None
Ejemplo n.º 6
0
    def calcTestScore(self):
        legendCounter = 0
        learners = []
        legend = []

        nn = mlrose.NeuralNetwork(hidden_nodes=[20], activation='relu', algorithm='gradient_descent',
                                  max_iters=200, bias=True, is_classifier=True, learning_rate=0.001,
                                  early_stopping=False, clip_max=1e10, curve=False)
        learners.append(nn)
        legend.append('GD')
        nn = mlrose.NeuralNetwork(hidden_nodes=[20], activation='relu', algorithm='simulated_annealing',
                                  max_iters=800, bias=True, is_classifier=True, learning_rate=0.001,
                                  early_stopping=False, clip_max=1e10, schedule=mlrose.GeomDecay(),
                                  curve=False)
        learners.append(nn)
        legend.append('SA')
        nn = mlrose.NeuralNetwork(hidden_nodes=[20], activation='relu', algorithm='random_hill_climb',
                                  max_iters=600, bias=True, is_classifier=True, learning_rate=0.001,
                                  early_stopping=False, clip_max=1e10, curve=False)
        learners.append(nn)
        legend.append('RHC')
        nn = mlrose.NeuralNetwork(hidden_nodes=[20], activation='relu', algorithm='genetic_alg',
                                  max_iters=200, bias=True, is_classifier=True, learning_rate=0.001,
                                  early_stopping=False, clip_max=1e10, pop_size=300, mutation_prob=0.2,
                                  curve=False)
        learners.append(nn)
        legend.append('GA')

        for learner in learners:
            timeStart = time.time()
            learner.fit(self.dataset1.training_x, self.dataset1.training_y)
            testingF1 = f1_score(learner.predict(self.dataset1.testing_x), self.dataset1.testing_y, average='weighted')
            print('Testing F1-Score for' + legend[legendCounter] + ': ' + str(testingF1))
            legendCounter += 1
Ejemplo n.º 7
0
 def run(self):
     temperatures = [
         mlrose.GeomDecay(init_temp=t) for t in self.temperature_list
     ]
     return super()._run_experiment(runner_name='SA',
                                    algorithm=mlrose.simulated_annealing,
                                    schedule=('Temperature', temperatures))
Ejemplo n.º 8
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)))
Ejemplo n.º 9
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()
Ejemplo n.º 10
0
def train_nn_backdrop(__hidden_layer,
                      __learning_rate,
                      max_attempts,
                      X_train,
                      X_test,
                      y_train,
                      y_test,
                      __range=range(1000, 10000, 1000),
                      pop_size=200,
                      mutation_prob=0.2,
                      restarts=0,
                      schedule=mlrose.GeomDecay(),
                      random_state=None):
    __nn_metrics = {}
    __train_accuracy_values, __test_accuracy_values,__train_time_values, __pred_time_values = [],[],[],[]
    algorithm = 'gradient_descent'
    for i in __range:
        __train_accuracy, __test_accuracy, __train_time, __pred_time = __nn_analysis(
            algorithm, __hidden_layer, __learning_rate, i, max_attempts,
            X_train, X_test, y_train, y_test, pop_size, mutation_prob,
            restarts, schedule, random_state)
        __train_accuracy_values.append(__train_accuracy)
        __test_accuracy_values.append(__test_accuracy)
        __train_time_values.append(__train_time)
        __pred_time_values.append(__pred_time)
    __nn_metrics[algorithm] = {
        'train_accuracy': __train_accuracy_values,
        'test_accuracy': __test_accuracy_values,
        'train_time': __train_time_values,
        'pred_time': __pred_time_values
    }
    return __nn_metrics
Ejemplo n.º 11
0
def train_nn_optimization(__hidden_layer,
                          __learning_rate,
                          max_attempts,
                          X_train,
                          X_test,
                          y_train,
                          y_test,
                          __range=range(1000, 10000, 1000),
                          pop_size=200,
                          mutation_prob=0.2,
                          restarts=0,
                          schedule=mlrose.GeomDecay(),
                          random_state=None):
    __nn_metrics = {}
    for algorithm in [
            'random_hill_climb', 'simulated_annealing', 'genetic_alg'
    ]:
        __train_accuracy_values, __test_accuracy_values,__train_time_values, __pred_time_values = [],[],[],[]
        for i in __range:
            __train_accuracy, __test_accuracy, __train_time, __pred_time = __nn_analysis(
                algorithm, __hidden_layer, __learning_rate, i, max_attempts,
                X_train, X_test, y_train, y_test, pop_size, mutation_prob,
                restarts, schedule, random_state)
            __train_accuracy_values.append(__train_accuracy)
            __test_accuracy_values.append(__test_accuracy)
            __train_time_values.append(__train_time)
            __pred_time_values.append(__pred_time)
        __nn_metrics[algorithm] = {
            'train_accuracy': __train_accuracy_values,
            'test_accuracy': __test_accuracy_values,
            'train_time': __train_time_values,
            'pred_time': __pred_time_values
        }
    return __nn_metrics
Ejemplo n.º 12
0
def find_best_param_sa_decay(seed, problem):
    init_temp_String = []
    init_temp = [0.1, 0.2, 0.3, 0.5, 0.66, 0.99]
    for int in init_temp:
        init_temp_String.append(str(int))

    fitness_list = []
    iterations = [
        1, 250, 500, 1000, 1250, 1500, 1750, 2000, 2250, 2500, 2750, 3000
    ]
    score = []

    for init in init_temp:
        for iter in iterations:
            schedule2 = mlrose.GeomDecay(init_temp=500, decay=init)
            best_state, best_fitness = mlrose.simulated_annealing(
                problem,
                max_attempts=10,
                max_iters=iter,
                random_state=seed,
                schedule=schedule2)
            score.append(best_fitness)

        fitness_list.append(np.mean(score))
        print("when init = ", init, "  the mean score is ", np.mean(score))

    sa_parameter_curve(init_temp_String, fitness_list, "sa_dacay", 0.4, "TSP")
Ejemplo n.º 13
0
def flipflop(max_iter=500,
             early_stop=None,
             mimic_early_stop=100,
             n_runs=10,
             savedir=None):
    print('\n\n|========= Flip Flop =========|\n')
    fitness = mlrose.FlipFlop()
    problem_size = [500]
    max_attempts = max_iter * 2 if early_stop is None else early_stop
    mimic_early_stop = max_attempts if mimic_early_stop is None else mimic_early_stop
    hyperparams = {
        'rhc': {
            'restarts': 0,
            'max_attempts': max_attempts
        },
        'mimic': {
            'pop_size': 500,
            'keep_pct': 0.2,
            'max_attempts': mimic_early_stop,
            'fast_mimic': True
        },
        'sa': {
            'schedule': mlrose.GeomDecay(),
            'init_state': None,
            'max_attempts': max_attempts
        },
        'ga': {
            'pop_size': 1000,
            'mutation_prob': 0.2,
            'pop_breed_percent': 0.75,
            'elite_dreg_ratio': 0.95,
            'max_attempts': mimic_early_stop
        }
    }
    print('Hyperparameters: ', hyperparams)

    results = []
    runtimes = []
    timings = {}
    for ps in problem_size:
        problem = mlrose.DiscreteOpt(ps, fitness, max_val=2, maximize=True)
        print('Running with input size', ps)
        print('-----------------------------')

        r, t, wt = util.optimize_iters(problem, max_iter, hyperparams, n_runs)
        results.append(r)
        runtimes.append(t)
        timings['ps{}'.format(ps)] = wt

    print('final runtimes')
    t = pd.DataFrame(runtimes, index=problem_size)
    print(t)

    if savedir:
        util.save_output('flipflop', savedir, t, results, timings,
                         problem_size)

    return t, results, timings
Ejemplo n.º 14
0
def predict(gradient_descent, cv, iter, restart, X_train, X_test, y_train, y_test):
    y_train_accuracy = 0
    y_test_accuracy = 0
    for i in range(restart):
        # Standardlization
        scaler = MinMaxScaler()  
        scaler.fit(X_train)
        X_train = scaler.transform(X_train)  
        X_test = scaler.transform(X_test)

        # Train Model
        if algorithm == 'gradient_descent':
            nn_model = mlrose.NeuralNetwork(hidden_nodes = [10,10], activation = 'relu',
                                        algorithm = algorithm, max_iters = iter,
                                        bias = True, is_classifier = True, learning_rate = 0.0001,
                                        early_stopping = True, max_attempts = 10)#, clip_max = 5)
        if algorithm == 'random_hill_climb':
            nn_model = mlrose.NeuralNetwork(hidden_nodes = [10,10], activation = 'relu',
                                        algorithm = algorithm, max_iters = iter,
                                        bias = True, is_classifier = True,
                                        early_stopping = True, max_attempts = 100)#, clip_max = 5)
        if algorithm == 'simulated_annealing':
            schedule = mlrose.GeomDecay(init_temp=100, decay=0.5, min_temp=0.1)
            nn_model = mlrose.NeuralNetwork(hidden_nodes = [10,10], activation = 'relu',
                                        algorithm = algorithm, max_iters = iter,
                                        bias = True, is_classifier = True,
                                        early_stopping = True, max_attempts = 100)#, schedule = schedule)#, clip_max = 5)
        if algorithm == 'genetic_alg':
            nn_model = mlrose.NeuralNetwork(hidden_nodes = [10,10], activation = 'relu',
                                        algorithm = algorithm, max_iters = iter,
                                        bias = True, is_classifier = True,
                                        pop_size = 200, mutation_prob = 0.1,
                                        early_stopping = True, max_attempts = 1000)#, clip_max = 5)
        nn_model_cv = nn_model

        nn_model.fit(X_train, y_train)

        # Cross Validation
        if cv == True:
            pipeline = Pipeline([('transformer', scaler), ('estimator', nn_model_cv)])
            cv_scores = cross_val_score(pipeline, X, y, cv=5, scoring='accuracy')

        # Scores
        y_train_pred = nn_model.predict(X_train)
        y_train_accuracy = max(y_train_accuracy, accuracy_score(y_train, y_train_pred))

        y_test_pred = nn_model.predict(X_test)
        y_test_accuracy = max(y_test_accuracy, accuracy_score(y_test, y_test_pred))
        
    print('Training accuracy: ', y_train_accuracy)
    print('Test accuracy: ', y_test_accuracy)

    if cv == True:
        print('Cross validation accuracy: ', cv_scores.mean())
        return y_train_accuracy, y_test_accuracy, cv_scores.mean()
        
    return y_train_accuracy, y_test_accuracy, y_test_accuracy
def fit(length, fitness):
    problem = mlrose.DiscreteOpt(length = length, fitness_fn = fitness, maximize = True, max_val = 2)

    iterations = [10,50,100,200,400,800,1600,3200]
    RHC, SA, GA, MM = ([],[],[],[])
    time_RHC, time_SA, time_GA, time_MM = ([],[],[],[])

    for iter in iterations:
        print ("max iterations = " + str(iter))
        start_time = time.time()
        best_fitness = 0
        for times in range(10):
          best_state, best_fitness = mlrose.random_hill_climb(problem, max_attempts = 10, max_iters = iter, restarts = 0, init_state = np.random.randint(2, size=(length,)))
          best_fitness = max(best_fitness, best_fitness)
          #print(best_state)
        RHC.append(best_fitness)
        print(best_fitness)
        time_RHC.append((time.time() - start_time)/10)
        
        start_time = time.time()
        best_fitness = 0
        for times in range(10):
          best_state, best_fitness = mlrose.simulated_annealing(problem, schedule = mlrose.GeomDecay(), max_attempts = 10, max_iters = iter, init_state = np.random.randint(2, size=(length,)))
          best_fitness = max(best_fitness, best_fitness)
          #print(best_state)
        SA.append(best_fitness)
        print(best_fitness)
        time_SA.append((time.time() - start_time)/10)

        start_time = time.time()
        best_fitness = 0
        best_state, best_fitness = mlrose.genetic_alg(problem, pop_size = 200, mutation_prob = 0.1, max_attempts = 10, max_iters = iter)
        #print(best_state)
        GA.append(best_fitness)
        print(best_fitness)
        time_GA.append((time.time() - start_time))

        start_time = time.time()
        best_fitness = 0
        best_state, best_fitness = mlrose.mimic(problem, pop_size = 200, keep_pct = 0.2, max_attempts = 10, max_iters = iter)
        #print(best_state)
        MM.append(best_fitness)
        print(best_fitness)
        time_MM.append((time.time() - start_time))
    
    plot(RHC, SA, GA, MM, time_RHC, time_SA, time_GA, time_MM, iterations)
    filewrite_array("iterations:", iterations)
    filewrite_array("Fitness(RHC):", RHC)
    filewrite_array("Fitness(SA):", SA)
    filewrite_array("Fitness(GA):", GA)
    filewrite_array("Fitness(MM):", MM)
    filewrite_array("Fitness(time_RHC):", time_RHC)
    filewrite_array("Fitness(time_SA):", time_SA)
    filewrite_array("Fitness(time_GA):", time_GA)
    filewrite_array("Fitness(time_MM):", time_MM)
Ejemplo n.º 16
0
def tune_sa(problem, max_attempts=10, max_iters=np.inf, n=10, filedir=""):
    decays = [0.8, 0.85, 0.9, 0.95, 0.99, 0.999]
    for d in decays:
        schedule = mlrose.GeomDecay(init_temp=1, decay=d, min_temp=0.001)
        run_sa(problem,
               schedule,
               d,
               max_attempts,
               max_iters,
               n,
               filedir=filedir)
Ejemplo n.º 17
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)
Ejemplo n.º 18
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 simulatedAnnealing(fitness, x):
    # This code was originally taken and modified from https://mlrose.readthedocs.io/en/stable/source/intro.html
    start = time.time()

    # Initialize fitness function object using pre-defined class
    #fitness = mlrose.Queens()

    # Define optimization problem object
    if (x == 0):
        problem = mlrose.DiscreteOpt(length=12,
                                     fitness_fn=fitness,
                                     maximize=False,
                                     max_val=12)
    elif (x == 1):
        problem = mlrose.DiscreteOpt(length=9,
                                     fitness_fn=fitness,
                                     maximize=False,
                                     max_val=3)
    else:
        problem = mlrose.DiscreteOpt(length=8,
                                     fitness_fn=fitness,
                                     maximize=True,
                                     max_val=8)

    # Define decay schedule
    schedule = mlrose.GeomDecay()

    # Solve using random hill climb
    if (x == 0):
        init_state = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
    elif (x == 1):
        init_state = np.array([0, 1, 2, 0, 1, 2, 0, 1, 1])
    else:
        init_state = np.array([0, 1, 2, 3, 4, 5, 6, 7])

    best_state, best_fitness, fitness_curve = mlrose.simulated_annealing(
        problem,
        schedule=schedule,
        max_attempts=10,
        max_iters=1000,
        init_state=init_state,
        random_state=1,
        curve=True)

    end = time.time()

    print("Simulated Annealing:")
    print('The best state found is: ', best_state)
    print('The fitness at the best state is: ', best_fitness)
    print("Time: " + str(end - start))
    return best_fitness, end - start
Ejemplo n.º 20
0
def sa_tuning(X, y, lr=0.0001, max_iters=1000):
    decays = [0.8, 0.85, 0.9, 0.95, 0.99, 0.999]
    for d in decays:
        d_str = str(d).split(".")[1]
        lr_str = str(lr).split(".")[1]
        schedule = mlrose.GeomDecay(init_temp=1, decay=d, min_temp=0.001)
        sa(X,
           y,
           lr=lr,
           max_iters=max_iters,
           decay=d,
           schedule=schedule,
           title="NN SA decay=0.{d} lr=0.{lr}".format(d=d_str, lr=lr_str),
           filename="nn/sa/lr{lr}/d{d}lr{lr}".format(d=d_str, lr=lr_str))
Ejemplo n.º 21
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 ()
Ejemplo n.º 22
0
 def __init__(self,
              X,
              y,
              test_size=0.1,
              hidden_nodes=[2],
              activation='relu',
              algorithm='random_hill_climb',
              max_iters=100,
              is_classifier=True,
              learning_rate=0.1,
              early_stopping=True,
              clip_max=1e+10,
              restarts=0,
              schedule=mlrose.GeomDecay(),
              pop_size=200,
              mutation_prob=0.1,
              max_attempts=10):
     self.X = X
     self.y = y
     self.test_size = test_size
     self.hidden_nodes = hidden_nodes
     self.activation = activation
     self.algorithm = algorithm
     self.max_iters = max_iters
     self.bias = True
     self.is_classifier = is_classifier
     self.learning_rate = learning_rate
     self.early_stopping = early_stopping
     self.clip_max = clip_max
     self.restarts = restarts
     self.schedule = schedule if schedule else mlrose.GeomDecay()
     self.pop_size = pop_size
     self.mutation_prob = mutation_prob
     self.max_attempts = max_attempts
     self.random_state = None
     self.curve = True
Ejemplo n.º 23
0
def temp_SA(cords,fit,temp):
    best_fit=[]
    max_iter=[]
    for i in range(10,temp,1):
        best_state,best_fitness3 = mlrose.simulated_annealing(problem_fit,schedule=mlrose.GeomDecay(init_temp=i,decay=0.9,min_temp=0.1),max_attempts=100,max_iters=100,init_state=None)
        max_iter.append(i)
        best_fit.append(best_fitness3)
        print(best_fit)
    max_iter=np.asarray(max_iter)
    best_fit=np.asarray(best_fit)
    line1, = plt.plot(max_iter,best_fit,color='r',label='fitness_score')
    plt.ylabel('Fitness_score')
    plt.xlabel('Number of attempts')
    plt.show()
    return None
Ejemplo n.º 24
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]
Ejemplo n.º 25
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()
Ejemplo n.º 26
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
Ejemplo n.º 27
0
# move to 50000 iterations

model = mlrose.NeuralNetwork(hidden_nodes=[64],
                             activation='relu',
                             algorithm='simulated_annealing',
                             max_iters=50000,
                             clip_max=10,
                             bias=False,
                             is_classifier=True,
                             learning_rate=1.0,
                             early_stopping=False,
                             restarts=0,
                             max_attempts=1,
                             random_state=3,
                             schedule=mlrose.GeomDecay(init_temp=1.0,
                                                       decay=decay,
                                                       min_temp=10e-10),
                             curve=True)
history_sa.append(model.fit(x_train, y_train))

y_train_pred = model.predict(x_train)
train_accuracy = accuracy_score(y_train, y_train_pred)

y_test_pred = model.predict(x_test)
test_accuracy = accuracy_score(y_test, y_test_pred)

print(" SA : Full train accruacy ,test accuracy", train_accuracy,
      test_accuracy)

# move to 5000 iterations
Ejemplo n.º 28
0
# Done on a complex example then  hard coded optimized parameter value(s).
# sa_df_run_stats, sa_df_run_curves, ideal_initial_temp = opt_sa_params()

ideal_initial_temp = 100  # this came from the results of the experiment commented out, above.

sa_best_state = []
sa_best_fitness = []
sa_convergence_time = []
for iter in iterations_range:
    start_time = timeit.default_timer()
    best_state, best_fitness, curve = mlrose.simulated_annealing(
        problem=problem,
        max_attempts=1000,
        max_iters=iter,
        curve=True,
        schedule=mlrose.GeomDecay(init_temp=ideal_initial_temp))
    end_time = timeit.default_timer()
    convergence_time = (end_time - start_time)  # seconds
    sa_best_state.append(best_state)
    sa_best_fitness.append(best_fitness)
    sa_convergence_time.append(convergence_time)

print('The fitness at the best state found using Simulated Annealing is: ',
      min(sa_best_fitness))

#========== Mutual-Information-Maximizing Input Clustering (MIMIC) ==========#
time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
print("Starting MIMIC at: " + time)


# Optimize the algorithm parameters (Manual)
Ejemplo n.º 29
0
    index = index + 1

index = 0
#Simulated Annealing
for each in problem_size:
    fitness_fn = mlrose.Knapsack(weights_list[index], values_list[index],
                                 max_weight_pct)
    problem = mlrose.DiscreteOpt(length=each,
                                 fitness_fn=fitness_fn,
                                 maximize=True,
                                 max_val=2)
    best_state, best_fitness, statistics = mlrose.simulated_annealing(
        problem=problem,
        max_attempts=max_attempts,
        max_iters=max_iters,
        schedule=mlrose.GeomDecay(init_temp=2.2, decay=0.7, min_temp=1),
        return_statistics=True)
    sa_state.append(best_state)
    sa_fitness.append(best_fitness)
    sa_statistics_fn_evals.append(statistics['fitness_evals'])
    sa_statistics_time.append(statistics['time'])
    sa_statistics_fitness.append(best_fitness)
    sa_statistics.append(statistics)
    index = index + 1

index = 0
#Genetic Algorithm
for each in problem_size:
    fitness_fn = mlrose.Knapsack(weights_list[index], values_list[index],
                                 max_weight_pct)
    problem = mlrose.DiscreteOpt(length=each,
Ejemplo n.º 30
0
                                 fitness_fn=fitness,
                                 maximize=True,
                                 max_val=2)

# Set random seed
np.random.seed(2)

## Solve problem using the genetic algorithm
start_time = time.time()
best_state1, best_fitness1 = mlrose.random_hill_climb(problem_fit,
                                                      max_attempts=100,
                                                      max_iters=100)

start_time2 = time.time()
best_state2, best_fitness2 = mlrose.simulated_annealing(
    problem_fit, schedule=mlrose.GeomDecay(), max_attempts=100, max_iters=100)

start_time3 = time.time()
best_state3, best_fitness3 = mlrose.genetic_alg(problem_fit,
                                                pop_size=200,
                                                mutation_prob=0.1,
                                                max_attempts=100,
                                                max_iters=100)

start_time4 = time.time()
best_state4, best_fitness4 = mlrose.mimic(problem_fit,
                                          pop_size=200,
                                          keep_pct=0.2,
                                          max_attempts=10,
                                          max_iters=100)