Automatic entry of the solution into the initial population #185
Unanswered
zsbeheshti
asked this question in
Q&A
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 importpygadimportnumpyfunction_inputs= [4,-2,3.5,5,-11,-4.7]
desired_output=44deffitness_func(ga_instance, solution, solution_idx):
output=numpy.sum(solution*function_inputs)
fitness=1.0/ (numpy.abs(output-desired_output) +0.000001)
returnfitnessnumber_executions=10initial_population=Noneforexec_idxinrange(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.populationbest_fitness=ga_instance.best_solution(ga_instance.last_generation_fitness)[1]
print("Fitness: {best_fitness}".format(best_fitness=best_fitness)) |
4 replies
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? |
0 replies
This is an example. It defined only the first population and the GA generates all the other populations. importpygadimportnumpyfunction_inputs= [4,-2,3.5,5,-11,-4.7,0.4,1.4,4.5,-5.1]
desired_output=44deffitness_func_wrapper(ga_instanse, solution, solution_idx):
returnnumpy.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() |
2 replies
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
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
All reactions