-
-
Notifications
You must be signed in to change notification settings - Fork 489
Automatic entry of the solution into the initial population #185
-
Dear all,
I want to write a code that after every execution of the genetic algorithm, every solution resulting from the execution will enter the initial population list. I also want to see the number of executions and it is possible to determine the number of executions. Does anyone have any related experience to share with me?
Regards
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 3 comments 6 replies
-
Hi @zsbeheshti,
Thanks for asking this question.
I have some questions to help you with the code:
- For the initial population, do want to insert the solutions in only the last generation of every execution? Or insert every individual solution in each generation of every execution?
- Do you have a target fitness, for example, to determine the number of executions?
For now, this is an example that runs 10 executions. For each execution x
, it uses the last population of execution x-1
as its initial population. The first execution sets the initial population to None
.
import pygad import numpy function_inputs = [4,-2,3.5,5,-11,-4.7] desired_output = 44 def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution*function_inputs) fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) return fitness number_executions = 10 initial_population = None for exec_idx in range(number_executions): print("\n**Execution {execution}**".format(execution=exec_idx)) ga_instance = pygad.GA(num_generations=15, initial_population=initial_population, num_parents_mating=10, sol_per_pop=20, num_genes=len(function_inputs), fitness_func=fitness_func, suppress_warnings=True) ga_instance.run() initial_population = ga_instance.population best_fitness = ga_instance.best_solution(ga_instance.last_generation_fitness)[1] print("Fitness: {best_fitness}".format(best_fitness=best_fitness))
Beta Was this translation helpful? Give feedback.
All reactions
-
- I want to include solutions only in the last generation of each run.
- No, I just want it to run as many times as I want.
Beta Was this translation helpful? Give feedback.
All reactions
-
So, I think the above code is what you want. Just change the value of the number_executions
variable.
Beta Was this translation helpful? Give feedback.
All reactions
-
For example, if the number of population is 10, from the second run onwards, one of the initial populations will be the solution of the last generation, and the other 9 will be randomly generated within the defined gene_space?
Beta Was this translation helpful? Give feedback.
All reactions
-
For 10 executions, then the last population of the execution x
becomes the initial population of execution x+1
. Inside each execution, the gene_space
is used to create the other solutions.
Beta Was this translation helpful? Give feedback.
All reactions
-
If I want to run the above code, I just give it an initial population at the beginning of the execution, for example, if I have 70 populations, I introduce one myself to define the initial population and it generates the other 69 randomly between 0 and 1. How can I do this?
Beta Was this translation helpful? Give feedback.
All reactions
-
This is an example. It defined only the first population and the GA generates all the other populations.
import pygad import numpy function_inputs = [4,-2,3.5,5,-11,-4.7,0.4,1.4,4.5,-5.1] desired_output = 44 def fitness_func_wrapper(ga_instanse, solution, solution_idx): return numpy.random.uniform() initial_population = numpy.random.uniform(0, 5, (5, 10)) ga_instance = pygad.GA(num_generations=70, num_parents_mating=5, fitness_func=fitness_func_wrapper, initial_population=initial_population, gene_type=int, suppress_warnings=True, gene_space=[0, 1]) ga_instance.run()
Beta Was this translation helpful? Give feedback.
All reactions
-
How can I use the combination of this code and the above code, so that I introduce a solution only for my first run?
Beta Was this translation helpful? Give feedback.
All reactions
-
This is an example. It only sets a single solution in the initial population. The GA creates all the other solutions.
The first solution is made from -1
to be obvious.
import pygad import numpy function_inputs = [4,-2,3.5,5,-11,-4.7,0.4,1.4,4.5,-5.1] desired_output = 44 def fitness_func_wrapper(ga_instanse, solution, solution_idx): return numpy.random.uniform() single_solution = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1] def on_start(ga_instance): ga_instance.initial_population[0, :] = single_solution.copy() ga_instance.population[0, :] = single_solution.copy() ga_instance = pygad.GA(num_generations=70, sol_per_pop=10, num_genes=len(function_inputs), num_parents_mating=5, fitness_func=fitness_func_wrapper, gene_type=int, on_start=on_start, suppress_warnings=True, gene_space=[0, 1]) ga_instance.run() print(ga_instance.initial_population)
Beta Was this translation helpful? Give feedback.