Example #1
0
def pso(population,x_train, y_train, x_test, y_test,x_validate,y_validate):
    # max_velocity = 0

    c1 = 1

    w, h = 20, 1200;
    velocity = [[0 for x in range(h)] for y in range(w)]
    present_positions = [[0 for x in range(h)] for y in range(w)]

    best_positions = [[0 for x in range(h)] for y in range(w)]

    best_lfitness = [0]*20
    atual_fitness = [0]*20

    gbest = ft.find_best(population, x_train, y_train, x_validate, y_validate)
    num_generations = 50

    for k in range(num_generations):

        lbest = ft.find_best(population,x_train, y_train, x_validate, y_validate)
        if lbest[1][0] > gbest[1][0]:
            gbest = lbest
        for particleIdx in range(len(population)):
            velocity[particleIdx] = a(velocity[particleIdx], lbest, c1,present_positions[particleIdx], gbest)
            present_positions[particleIdx] = b(present_positions[particleIdx],velocity[particleIdx])
            """

            for dimentionIdx in range(1200):
                print(velocity[particleIdx][dimentionIdx])
                velocity[particleIdx][dimentionIdx] = a(velocity[particleIdx][dimentionIdx], gbest,c1,present_positions[particleIdx][dimentionIdx])
                present_positions[particleIdx][dimentionIdx] = b(present_positions[particleIdx][dimentionIdx], velocity[particleIdx][dimentionIdx])
            """
    best_accu = ft.fitness_best(gbest[0], gbest[1], x_test, y_test)
    # print(best_accu)
    return best_accu
Example #2
0
def ga(population, x_train, y_train, x_test, y_test):

    new_population = []
    parents_mated = []
    num_generations = 5
    for i in range(num_generations):
        fitness_all = ft.calculate_pop_ft(population, x_train, y_train, x_test,
                                          y_test)
        for j in range(int((len(population) / 2))):
            parents = mating_pool(population, fitness_all, 2)
            offspring = crossover(parents, 3)
            offspring = mutation(offspring)

            new_population.append(offspring)

            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

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

    best = ft.find_best(population, x_train, y_train, x_test, y_test)

    return best
Example #3
0
def es(population, x_train, y_train, x_validate, y_validate, x_test, y_test):
    mut = 0.3
    #cross = 0.2

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

    for i in range(num_generations):
        fitness_all, models_all = ft.calculate_pop_ft(population, x_train,
                                                      y_train, x_validate,
                                                      y_validate)
        number_decendants = 0
        decendants = []
        while number_decendants < (len(population) + 5):
            #print(population)
            check = 1
            while check:
                father_idx = np.random.random_integers(0, len(population) - 1)

                father = population[father_idx]

                father_ft = fitness_all[father_idx]
                radom_gauss = random.gauss(0, 1)
                father_prob = probability_sum(father_ft, fitness_all)
                if radom_gauss < father_prob:
                    check = 0
            check2 = 1
            while check2:
                mother_idx = np.random.random_integers(0, len(population) - 1)

                mother = population[mother_idx]

                mother_ft = fitness_all[mother_idx]
                radom_gauss = random.gauss(0, 1)
                mother_prob = probability_sum(mother_ft, fitness_all)
                if radom_gauss < mother_prob:
                    check2 = 0

            crossed = crossover(father, mother)
            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
Example #4
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
Example #5
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
Example #6
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