Automatic entry of the solution into the initial population #185
Replies: 3 comments 6 replies
-
Hi @zsbeheshti, Thanks for asking this question. I have some questions to help you with the code:
For now, this is an example that runs 10 executions. For each execution 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.
-
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.
-
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.
-
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