Skip to content

Commit 042b886

Browse files
committed
Move best_solutions validation to footer
1 parent 260be03 commit 042b886

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

pygad/utils/validation.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,15 +1307,33 @@ def _validate_footer(self,
13071307
save_best_solutions,
13081308
save_solutions):
13091309

1310-
# In case the 'gene_space' parameter is nested, then make sure the number of its elements equals to the number of genes.
1310+
# Validate num_generations
13111311
if type(num_generations) in self.supported_int_types:
1312-
if num_generations > 0:
1312+
if num_generations >= 0:
13131313
self.num_generations = num_generations
13141314
else:
1315-
raise ValueError(f"The value assigned to the 'num_generations' parameter must be a positive integer > 0. But the value {num_generations} found.")
1315+
raise ValueError(f"The value assigned to the 'num_generations' parameter must be a non-negative integer >= 0. But the value {num_generations} found.")
1316+
else:
1317+
self.valid_parameters = False
1318+
raise ValueError(f"Unexpected value ({num_generations}) of type ({type(num_generations)}) assigned to the 'num_generations' parameter. It must be assigned a non-negative integer.")
1319+
1320+
# Validate save_best_solutions
1321+
if type(save_best_solutions) is bool:
1322+
if save_best_solutions == True:
1323+
if not self.suppress_warnings:
1324+
warnings.warn("Use the 'save_best_solutions' parameter with caution as it may cause memory overflow when either the number of generations or number of genes is large.")
13161325
else:
13171326
self.valid_parameters = False
1318-
raise ValueError(f"Unexpected value ({num_generations}) of type ({type(num_generations)}) assigned to the 'num_generations' parameter. It must be assigned a positive integer.")
1327+
raise TypeError(f"The value passed to the 'save_best_solutions' parameter must be of type bool but {type(save_best_solutions)} found.")
1328+
1329+
# Validate save_solutions
1330+
if type(save_solutions) is bool:
1331+
if save_solutions == True:
1332+
if not self.suppress_warnings:
1333+
warnings.warn("Use the 'save_solutions' parameter with caution as it may cause memory overflow when either the number of generations, number of genes, or number of solutions in population is large.")
1334+
else:
1335+
self.valid_parameters = False
1336+
raise TypeError(f"The value passed to the 'save_solutions' parameter must be of type bool but {type(save_solutions)} found.")
13191337

13201338
# Set the `run_completed` property to False. It is set to `True` only after the `run()` method is complete.
13211339
self.run_completed = False
@@ -1485,24 +1503,6 @@ def validate_parameters(self,
14851503
on_generation,
14861504
on_stop)
14871505

1488-
# Validate save_best_solutions
1489-
if type(save_best_solutions) is bool:
1490-
if save_best_solutions == True:
1491-
if not self.suppress_warnings:
1492-
warnings.warn("Use the 'save_best_solutions' parameter with caution as it may cause memory overflow when either the number of generations or number of genes is large.")
1493-
else:
1494-
self.valid_parameters = False
1495-
raise TypeError(f"The value passed to the 'save_best_solutions' parameter must be of type bool but {type(save_best_solutions)} found.")
1496-
1497-
# Validate save_solutions
1498-
if type(save_solutions) is bool:
1499-
if save_solutions == True:
1500-
if not self.suppress_warnings:
1501-
warnings.warn("Use the 'save_solutions' parameter with caution as it may cause memory overflow when either the number of generations, number of genes, or number of solutions in population is large.")
1502-
else:
1503-
self.valid_parameters = False
1504-
raise TypeError(f"The value passed to the 'save_solutions' parameter must be of type bool but {type(save_solutions)} found.")
1505-
15061506
self._validate_stop_criteria(stop_criteria)
15071507

15081508
self._validate_parallel_processing(parallel_processing)

0 commit comments

Comments
 (0)