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
Randomly generate one or more values for the gene.
57
+
It accepts:
58
+
-range_min: The minimum value in the range from which a value is selected.
59
+
-range_max: The maximum value in the range from which a value is selected.
60
+
-gene_value: The original gene value before applying mutation.
61
+
-gene_idx: The index of the gene in the solution.
62
+
-mutation_by_replacement: A flag indicating whether mutation by replacement is enabled or not. The reason is to make this helper method usable while generating the initial population. In this case, mutation_by_replacement does not matter and should be considered False.
63
+
-num_values: The number of random valus to generate. It tries to generate a number of values up to a maximum of num_values. But it is not always guranteed because the total number of values might not be enough or the random generator creates duplicate random values.
64
+
If num_values=1, it returns a single numeric value. If num_values>1, it returns an array with number of values equal to num_values.
# Rounding different values could return the same value multiple times.
84
+
# For example, 2.8 and 2.7 will be 3.0.
85
+
# Use the unique() function to avoid any duplicates.
86
+
random_value=numpy.unique(random_value)
87
+
88
+
ifnum_values==1:
89
+
random_value=random_value[0]
90
+
91
+
returnrandom_value
92
+
93
+
defget_valid_gene_constraint_values(self,
94
+
range_min,
95
+
range_max,
96
+
gene_value,
97
+
gene_idx,
98
+
mutation_by_replacement,
99
+
solution,
100
+
num_values=100):
101
+
"""
102
+
Randomly generate values for the gene that satisfy the constraint.
103
+
It accepts:
104
+
-range_min: The minimum value in the range from which a value is selected.
105
+
-range_max: The maximum value in the range from which a value is selected.
106
+
-gene_value: The original gene value before applying mutation.
107
+
-gene_idx: The index of the gene in the solution.
108
+
-mutation_by_replacement: A flag indicating whether mutation by replacement is enabled or not. The reason is to make this helper method usable while generating the initial population. In this case, mutation_by_replacement does not matter and should be considered False.
109
+
-solution: The solution in which the gene exists.
110
+
-num_values: The number of random valus to generate. It tries to generate a number of values up to a maximum of num_values. But it is not always guranteed because the total number of values might not be enough or the random generator creates duplicate random values.
111
+
If num_values=1, it returns a single numeric value. If num_values>1, it returns an array with number of values equal to num_values.
Creates an initial population randomly as a NumPy array. The array is saved in the instance attribute named 'population'.
1383
1386
1384
-
low: The lower value of the random range from which the gene values in the initial population are selected. It defaults to -4. Available in PyGAD 1.0.20 and higher.
1385
-
high: The upper value of the random range from which the gene values in the initial population are selected. It defaults to -4. Available in PyGAD 1.0.20.
1387
+
It accepts:
1388
+
-low: The lower value of the random range from which the gene values in the initial population are selected. It defaults to -4. Available in PyGAD 1.0.20 and higher.
1389
+
-high: The upper value of the random range from which the gene values in the initial population are selected. It defaults to -4. Available in PyGAD 1.0.20.
1390
+
-allow_duplicate_genes: Whether duplicate genes are allowed or not.
1391
+
-mutation_by_replacement: Whether mutation by replacement is enabled or not.
1392
+
-gene_type: The data type of the genes.
1393
+
-gene_constraint: The constraints of the genes.
1386
1394
1387
1395
This method assigns the values of the following 3 instance attributes:
dtype=self.gene_type[0])# A NumPy array holding the initial population.
1412
+
dtype=self.gene_type[0])
1404
1413
else:
1405
1414
# Create an empty population of dtype=object to support storing mixed data types within the same array.
1406
1415
self.population=numpy.zeros(
1407
1416
shape=self.pop_size, dtype=object)
1408
1417
# Loop through the genes, randomly generate the values of a single gene across the entire population, and add the values of each gene to the population.
0 commit comments