Exemplo n.º 1
0
def es(population,x_train, y_train,x_validate, y_validate, x_test, y_test):
    mut = 0.3
    cross = 0.7
    #fitness_all = ft.calculate_pop_ft(population, x_train, y_train, x_test, y_test)

    best_fit = ft.find_best(population,x_train, y_train, x_validate, y_validate)
    num_generations = 50
    decendants = []

    for i in range(num_generations):
        number_decendants = 0
        decendants = []
        while number_decendants < (len(population)+5):
            #print(population)
            father = np.random.random_integers(0,len(population)-1)
            mother = np.random.random_integers(0,len(population)-1)
            mother2 = np.random.random_integers(0,len(population)-1)


            father = population[father]
            mother = population[mother]
            mother2 = population[mother2]

            monft = ft.fitness(mother, x_train, y_train, x_validate, y_validate)
            monft2 = ft.fitness(mother2, x_train, y_train, x_validate, y_validate)

            if monft[0] < monft2[0]:
                mother = mother2

            crossed = crossover(father,mother,cross)
            mutated = mutation(crossed,mut)
            #print(crossed)
            decendants.append(mutated)

            number_decendants += 1
        ft_decendants,model_decendants = ft.calculate_pop_ft(decendants, x_train, y_train, x_validate, y_validate)

        new_population,ft_new_pop,model_new = ft.fetch_n_better(decendants,ft_decendants,model_decendants,len(population))
        ft_best = (new_population[0],(ft_new_pop[0],model_new[0]))
        population = new_population
        if ft_best[1][0] > best_fit[1][0]:
            best_fit = ft_best
    ee_accu = ft.fitness_best(best_fit[0], best_fit[1][1],x_test,y_test)

    return ee_accu
Exemplo n.º 2
0
def ga(population,x_train, y_train,x_validate, y_validate, x_test, y_test):
    mut = 0.3
    cross = 0.7
    best = ft.find_best(population, x_train, y_train, x_validate, y_validate)
    #treshold = 0.5
    new_population = []
    #parents_mated = []
    num_generations = 50
    for k in range(num_generations):
        fitness_all, models_all = ft.calculate_pop_ft(population,x_train, y_train,x_validate, y_validate)

        for j in range(int((len(population)/2))):
            parents =roulette_wheel_selection(fitness_all,2,len(population))
            parents = (population[parents[0]],population[parents[1]])
            offspring = crossover(parents[0],parents[1],cross)
            offspring2 = crossover(parents[0],parents[1],cross)
            offspring = mutation(offspring,mut)
            offspring2 = mutation(offspring2,mut)

            new_population.append(offspring)
            new_population.append(offspring2)

            #parents_mated.append(parents[0])
            #parents_mated.append(parents[1])

        #new_population = np.concatenate((new_population,parents_mated[:int((len(population)/2))]))
        population = new_population
        gen_best = ft.find_best(population, x_train, y_train, x_validate, y_validate)
        if gen_best[1][0] > best[1][0]:
            best = gen_best

        #------- Clean aux Lists------
        #parents_mated = []
        new_population = []

    #best = ft.find_best(population,x_train, y_train, x_test, y_test)
    best = ft.fitness_best(best[0],best[1][1],x_test,y_test)

    return best
Exemplo n.º 3
0
def de(population,x_train, y_train,x_validate, y_validate, x_test, y_test):
    fitness_all, models_all = ft.calculate_pop_ft(population, x_train, y_train, x_validate, y_validate)
    weight_dimention = (1200)
    mut_constant = 0.5
    limiar_crossover = 0.6
    best_idx = 0
    generations = 50
    for n in range(generations):

        for i in range(len(population)):  #Iterate over all cromossomes
            indices = [indices for indices in range(len(population)) if indices != i]
            a, b, c = population[np.random.choice(indices, 3, replace=False)]
            mutant = np.clip(a + mut_constant * (b - c), -1, 1)
            cross_points = np.random.uniform(low = 0, high= 1,size = weight_dimention) < limiar_crossover
            trial = np.where(cross_points, mutant, population[i])
            trial_ft, trial_model = ft.fitness(trial,x_train, y_train, x_test, y_test)
            if trial_ft > fitness_all[i]:
                fitness_all[i] = trial_ft
                population[i] = trial_ft
                if trial_ft > fitness_all[best_idx]:
                    best_idx = i
                    #best_weight = trial

    return fitness_all[best_idx]
Exemplo n.º 4
0
def bfo(population, x_train, y_train, x_test, y_test, x_validate, y_validate):
    #print("BFO")
    Celln = 20
    Ned = 10
    Nre = 10
    Nc = 20
    Ns = 10
    Ped = 0.25
    StepSize = 0.1

    best_fit = ft.find_best(population, x_train, y_train, x_validate,
                            y_validate)
    for l in range(Ned):
        for k in range(Nre):
            for j in range(Nc):
                population, cellHealth_all = chemotaxixAndSwim(
                    population, 1200, 20, Ns, StepSize, x_train, y_train,
                    x_validate, y_validate)
                local_best = ft.find_best(population, x_train, y_train,
                                          x_validate, y_validate)
                if local_best[1][0] > best_fit[1][0]:
                    best_fit = local_best
            #DUVIDA DEVERIA USAR O HEALTH AO INVES DO FITNESS AQUI?
            all_fitness, all_models = ft.calculate_pop_ft(
                population, x_train, y_train, x_validate, y_validate)
            new_pop, ft_new, model_new = ft.fetch_n_better(
                population, all_fitness, all_models, 10)
        #DIVIDA COMO GARANTIR QUE NO FINAL A POPULAÇÃO VAI TER 20 BACTERIAS???
        for cell in new_pop:
            if (rd.uniform(0, 1) >= Ped):
                cell = np.random.uniform(low=0, high=1.0, size=1200)
                new_pop.append(cell)
        population = new_pop
    bfo_accu = ft.fitness_best(best_fit[0], best_fit[1][1], x_test, y_test)

    return bfo_accu