You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/example_dynamic_population_size.py
+63-24
Original file line number
Diff line number
Diff line change
@@ -3,44 +3,83 @@
3
3
4
4
"""
5
5
This is an example to dynamically change the population size (i.e. number of solutions/chromosomes per population) during runtime.
6
-
The following 2 instance attributes must be changed to meet the new desired population size:
7
-
1) population: This is a NumPy array holding the population.
8
-
2) num_offspring: This represents the number of offspring to produce during crossover.
9
-
For example, if the population initially has 20 solutions and 6 genes. To change it to have 30 solutions, then:
10
-
1)population: Create a new NumPy array with the desired size (30, 6) and assign it to the population instance attribute.
11
-
2)num_offspring: Set the num_offspring attribute accordingly (e.g. 29 assuming that keep_elitism has the default value of 1).
6
+
7
+
The user has to carefully inspect the parameters and instance attributes to select those that must be changed to be consistent with the new population size.
8
+
Check this link for more information: https://pygad.readthedocs.io/en/latest/pygad_more.html#change-population-size-during-runtime
12
9
"""
13
10
11
+
defupdate_GA(ga_i,
12
+
pop_size):
13
+
"""
14
+
Update the parameters and instance attributes to match the new population size.
15
+
16
+
Parameters
17
+
----------
18
+
ga_i : TYPE
19
+
The pygad.GA instance.
20
+
pop_size : TYPE
21
+
The new population size.
22
+
23
+
Returns
24
+
-------
25
+
None.
26
+
"""
27
+
28
+
ga_i.pop_size=pop_size
29
+
ga_i.sol_per_pop=ga_i.pop_size[0]
30
+
ga_i.num_parents_mating=int(ga_i.pop_size[0]/2)
31
+
32
+
# Calculate the new value for the num_offspring parameter.
0 commit comments