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: pygad/pygad.py
+30-23Lines changed: 30 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -51,6 +51,7 @@ def __init__(self,
51
51
random_mutation_max_val=1.0,
52
52
gene_space=None,
53
53
gene_constraint=None,
54
+
sample_size=100,
54
55
allow_duplicate_genes=True,
55
56
on_start=None,
56
57
on_fitness=None,
@@ -107,6 +108,7 @@ def __init__(self,
107
108
gene_space: It accepts a list of all possible values of the gene. This list is used in the mutation step. Should be used only if the gene space is a set of discrete values. No need for the 2 parameters (random_mutation_min_val and random_mutation_max_val) if the parameter gene_space exists. Added in PyGAD 2.5.0. In PyGAD 2.11.0, the gene_space can be assigned a dict.
108
109
109
110
gene_constraint: It accepts a list of constraints for the genes. Each constraint is a Python function. Added in PyGAD 3.5.0.
111
+
sample_size: To select a gene value that respects a constraint, this variable defines the size of the sample from which a value is selected. Useful if either allow_duplicate_genes or gene_constraint is used. Added in PyGAD 3.5.0.
110
112
111
113
on_start: Accepts a function/method to be called only once before the genetic algorithm starts its evolution. If functioned, then it must accept a single parameter representing the instance of the genetic algorithm. If method, then it must accept 2 parameters where the second one refers to the method's object. Added in PyGAD 2.6.0.
112
114
on_fitness: Accepts a function/method to be called after calculating the fitness values of all solutions in the population. If functioned, then it must accept 2 parameters: 1) a list of all solutions' fitness values 2) the instance of the genetic algorithm. If method, then it must accept 3 parameters where the third one refers to the method's object. Added in PyGAD 2.6.0.
raiseValueError(f"The value of the sample_size parameter must be > 0 but the value ({sample_size}) found.")
201
+
else:
202
+
self.valid_parameters=False
203
+
raiseTypeError(f"The type of the sample_size parameter must be integer but the value ({sample_size}) of type ({type(sample_size)}) found.")
204
+
205
+
self.sample_size=sample_size
206
+
192
207
# Validate allow_duplicate_genes
193
208
ifnot (type(allow_duplicate_genes) isbool):
194
209
self.valid_parameters=False
@@ -266,18 +281,6 @@ def __init__(self,
266
281
267
282
self.gene_space=gene_space
268
283
269
-
# Validate init_range_low and init_range_high
270
-
# if type(init_range_low) in GA.supported_int_float_types:
271
-
# if type(init_range_high) in GA.supported_int_float_types:
272
-
# self.init_range_low = init_range_low
273
-
# self.init_range_high = init_range_high
274
-
# else:
275
-
# self.valid_parameters = False
276
-
# raise ValueError(f"The value passed to the 'init_range_high' parameter must be either integer or floating-point number but the value ({init_range_high}) of type {type(init_range_high)} found.")
277
-
# else:
278
-
# self.valid_parameters = False
279
-
# raise ValueError(f"The value passed to the 'init_range_low' parameter must be either integer or floating-point number but the value ({init_range_low}) of type {type(init_range_low)} found.")
warnings.warn(f"K of the tournament selection ({K_tournament}) should not be greater than the number of solutions within the population ({self.sol_per_pop}).\nK will be clipped to be equal to the number of solutions in the population (sol_per_pop).\n")
894
-
elifK_tournament<=0:
893
+
iftype(K_tournament) inGA.supported_int_types:
894
+
ifK_tournament>self.sol_per_pop:
895
+
K_tournament=self.sol_per_pop
896
+
ifnotself.suppress_warnings:
897
+
warnings.warn(f"K of the tournament selection ({K_tournament}) should not be greater than the number of solutions within the population ({self.sol_per_pop}).\nK will be clipped to be equal to the number of solutions in the population (sol_per_pop).\n")
898
+
elifK_tournament<=0:
899
+
self.valid_parameters=False
900
+
raiseValueError(f"K of the tournament selection cannot be <=0 but ({K_tournament}) found.\n")
901
+
else:
895
902
self.valid_parameters=False
896
-
raiseValueError(f"K of the tournament selection cannot be <=0 but ({K_tournament}) found.\n")
903
+
raiseValueError(f"The type of K of the tournament selection must be integer but the value ({K_tournament}) of type ({type(K_tournament)}) found.")
warnings.warn(f"No value satisfied the constraint for the gene at index {gene_idx} with value {solution[gene_idx]} while creating the initial population.")
0 commit comments