Exemplo n.º 1
0
def ConvergencePlot(waveform,
                    input_num,
                    mating_parent_number,
                    max_gen=20,
                    convergence_point=100000):
    with open("GensPerPopSize.csv", mode="a") as GenCSV:
        GenPPS = csv.writer(GenCSV, delimiter=',')
        GensPerPopSize = []
        PopSize = []
        for population_size in range(5, 50):
            generation = 1
            # Defining the population size.
            pop_size = (
                population_size, input_num
            )  # The population will have sol_per_pop chromosome where each chromosome has num_weights genes.
            #Creating the initial population.
            new_population = ga.create_population(pop_size)
            fitness = ga.cal_pop_fitness(waveform, new_population)
            while (np.max(fitness) < convergence_point):
                # Measuring the fitness of each chromosome in the population.
                fitness = ga.cal_pop_fitness(waveform, new_population)
                # Selecting the best parents in the population for mating.
                parents = ga.select_mating_pool(new_population, fitness,
                                                mating_parent_number)
                # Generating next generation using crossover.
                offspring_crossover = ga.crossover(
                    parents,
                    offspring_size=(pop_size[0] - parents.shape[0], input_num))
                # Adding some variations to the offspring using mutation.
                offspring_mutation = ga.mutation(offspring_crossover,
                                                 num_mutations=2)
                # Creating the new population based on the parents and offspring.
                new_population[0:parents.shape[0], :] = parents
                new_population[parents.shape[0]:, :] = offspring_mutation

                generation += 1
                if (generation > max_gen):
                    print("{} Did Not Converge".format(population_size))
                    generation = max_gen
                    break
            GensPerPopSize.append(generation)
            PopSize.append(population_size)
            GenPPS.writerow([population_size, generation])
            print("{} Population Size Complete".format(population_size))

    return GensPerPopSize, PopSize
Exemplo n.º 2
0
def GA_filter(waveform, input_num, solutions_per_population,
              mating_parent_number, num_generations):

    # Defining the population size.
    pop_size = (
        solutions_per_population, input_num
    )  # The population will have sol_per_pop chromosome where each chromosome has num_weights genes.
    #Creating the initial population.
    new_population = ga.create_population(pop_size)

    best_outputs = []
    for generation in range(num_generations):
        # Measuring the fitness of each chromosome in the population.
        fitness = ga.cal_pop_fitness(waveform, new_population)
        # The best result in the current iteration.
        best_outputs.append(np.max(fitness))
        # Selecting the best parents in the population for mating.
        parents = ga.select_mating_pool(new_population, fitness,
                                        mating_parent_number)
        # Generating next generation using crossover.
        offspring_crossover = ga.crossover(
            parents,
            offspring_size=(pop_size[0] - parents.shape[0], input_num))
        # Adding some variations to the offspring using mutation.
        offspring_mutation = ga.mutation(offspring_crossover, num_mutations=2)
        # Creating the new population based on the parents and offspring.
        new_population[0:parents.shape[0], :] = parents
        new_population[parents.shape[0]:, :] = offspring_mutation

        #        if (generation < 20):
        #            print("{}\n {}\n\n".format(new_population, pop_fitness))
        if (generation % 10 == 0 and generation != 0):
            print("{} Generations Completed".format(generation))

    # Getting the best solution after iterating finishing all generations.
    #At first, the fitness is calculated for each solution in the final generation.
    fitness = ga.cal_pop_fitness(waveform, new_population)
    # Then return the index of that solution corresponding to the best fitness.
    best_match_idx = np.where(fitness == np.max(fitness))[0]
    return new_population[
        best_match_idx, :], fitness[best_match_idx][0][0], best_outputs