Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Automatic entry of the solution into the initial population #185

Unanswered
zsbeheshti asked this question in Q&A
Discussion options

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

You must be logged in to vote

Replies: 3 comments 6 replies

Comment options

Hi @zsbeheshti,

Thanks for asking this question.

I have some questions to help you with the code:

  1. 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?
  2. 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))
You must be logged in to vote
4 replies
Comment options

  1. I want to include solutions only in the last generation of each run.
  2. No, I just want it to run as many times as I want.
Comment options

So, I think the above code is what you want. Just change the value of the number_executions variable.

Comment options

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?

Comment options

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.

Comment options

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?

You must be logged in to vote
0 replies
Comment options

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()
You must be logged in to vote
2 replies
Comment options

How can I use the combination of this code and the above code, so that I introduce a solution only for my first run?

Comment options

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested

AltStyle によって変換されたページ (->オリジナル) /