Esempio n. 1
0
 def genetic_alg(cls, iteration_num, parent_num, fitness,
                 number_of_solutions, num_genes, crossover_probability,
                 mutation_probability, after_mutation_func):
     # initial_population is built by sol_per_pop and num_genes
     # num_genes = Number of genes in the solution / chromosome
     ga_instance = pygad.GA(num_generations=iteration_num,
                            num_parents_mating=parent_num,
                            fitness_func=fitness,
                            sol_per_pop=number_of_solutions,
                            num_genes=num_genes,
                            gene_type=float,
                            init_range_low=0.0,
                            init_range_high=1.0,
                            parent_selection_type="rws",
                            keep_parents=0,
                            crossover_type="single_point",
                            crossover_probability=crossover_probability,
                            mutation_type="random",
                            mutation_probability=mutation_probability,
                            mutation_by_replacement=False,
                            random_mutation_min_val=0.0,
                            random_mutation_max_val=1.0,
                            on_mutation=after_mutation_func)
     ga_instance.run()
     solution, solution_fitness, solution_idx = ga_instance.best_solution()
     print("Parameters of the best solution : {solution}".format(solution=solution))
     print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
     print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
     filename = 'genetic'
     ga_instance.save(filename=filename)
     loaded_ga_instance = pygad.load(filename=filename)
     return loaded_ga_instance.best_solution()
Esempio n. 2
0
 def fit(self):
     parameters = self.get_genetic_parameters()
     if os.path.exists(self.path['GA'] + '.pkl'):
         self.ga = pygad.load(self.path['GA'])
         self.ga.fitness_func = fitness_function_factory(self)
     else:
         self.ga = pygad.GA(**parameters)
     self.ga.path = self.path['GA']
     if len(self.ga.solutions) < (self.ga.num_generations +
                                  1) * self.ga.sol_per_pop:
         self.ga.run()
     return
Esempio n. 3
0
                       num_parents_mating=num_parents_mating,
                       sol_per_pop=sol_per_pop,
                       num_genes=num_genes,
                       fitness_func=fitness_func,
                       on_generation=on_generation)

# Running the GA to optimize the parameters of the function.
ga_instance.run()

ga_instance.plot_fitness()

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness)
print("Parameters of the best solution : {solution}".format(solution=solution))
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))

prediction = numpy.sum(numpy.array(function_inputs)*solution)
print("Predicted output based on the best solution : {prediction}".format(prediction=prediction))

if ga_instance.best_solution_generation != -1:
    print("Best fitness value reached after {best_solution_generation} generations.".format(best_solution_generation=ga_instance.best_solution_generation))

# Saving the GA instance.
filename = 'genetic' # The filename to which the instance is saved. The name is without extension.
ga_instance.save(filename=filename)

# Loading the saved GA instance.
loaded_ga_instance = pygad.load(filename=filename)
loaded_ga_instance.plot_fitness()
Esempio n. 4
0
def Run(testset_size, test_weight):

    desired_output = 100

    def fitness_func(solution, solution_idx):

        output = fuzzification.Run(solution, testset_size, test_weight)
        fitness = 1.0 / numpy.abs(output - desired_output)
        return fitness

    fitness_function = fitness_func

    num_generations = 100
    num_parents_mating = 7

    sol_per_pop = 50
    num_genes = 8

    init_range_low = -2
    init_range_high = 40

    parent_selection_type = "sss"
    keep_parents = 7

    crossover_type = "single_point"

    mutation_type = "random"
    mutation_percent_genes = 10

    def callback_generation(ga_instance):
        global last_fitness
        print("Generation = {generation}".format(
            generation=ga_instance.generations_completed))
        print("Fitness    = {fitness}".format(
            fitness=ga_instance.best_solution()[1]))
        print("Change     = {change}".format(
            change=ga_instance.best_solution()[1] - last_fitness))

    ga_instance = pygad.GA(num_generations=num_generations,
                           num_parents_mating=num_parents_mating,
                           fitness_func=fitness_function,
                           sol_per_pop=sol_per_pop,
                           num_genes=num_genes,
                           init_range_low=init_range_low,
                           init_range_high=init_range_high,
                           parent_selection_type=parent_selection_type,
                           keep_parents=keep_parents,
                           crossover_type=crossover_type,
                           mutation_type=mutation_type,
                           mutation_percent_genes=mutation_percent_genes,
                           callback_generation=callback_generation)

    ga_instance.run()

    ga_instance.plot_result()

    solution, solution_fitness, solution_idx = ga_instance.best_solution()
    print("Parameters of the best solution : {solution}".format(
        solution=solution))
    print("Fitness value of the best solution = {solution_fitness}".format(
        solution_fitness=solution_fitness))
    print("Index of the best solution : {solution_idx}".format(
        solution_idx=solution_idx))

    #prediction = numpy.sum(numpy.array(function_inputs)*solution)
    #print("Predicted output based on the best solution : {prediction}".format(prediction=prediction))

    if ga_instance.best_solution_generation != -1:
        print(
            "Best fitness value reached after {best_solution_generation} generations."
            .format(
                best_solution_generation=ga_instance.best_solution_generation))

    filename = 'genetic'
    ga_instance.save(filename=filename)

    loaded_ga_instance = pygad.load(filename=filename)
    loaded_ga_instance.plot_result()