Skip to content

Commit 2bd8bab

Browse files
Matias GonzalezMatias Gonzalez
authored andcommitted
extradata
1 parent 74d7a2b commit 2bd8bab

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

pygad.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ def __init__(self,
4848
save_best_solutions=False,
4949
save_solutions=False,
5050
suppress_warnings=False,
51-
stop_criteria=None):
51+
stop_criteria=None,
52+
extra_data=None):
5253

5354
"""
5455
The constructor of the GA class accepts all parameters required to create an instance of the GA class. It validates such parameters.
@@ -111,6 +112,8 @@ def __init__(self,
111112
stop_criteria: Added in PyGAD 2.15.0. It is assigned to some criteria to stop the evolution if at least one criterion holds.
112113
"""
113114

115+
self.extra_data = extra_data
116+
114117
# If suppress_warnings is bool and its valud is False, then print warning messages.
115118
if type(suppress_warnings) is bool:
116119
self.suppress_warnings = suppress_warnings
@@ -635,11 +638,11 @@ def __init__(self,
635638
# Check if the fitness_func is a function.
636639
if callable(fitness_func):
637640
# Check if the fitness function accepts 2 paramaters.
638-
if (fitness_func.__code__.co_argcount == 2):
641+
if (fitness_func.__code__.co_argcount >= 2 and fitness_func.__code__.co_argcount <= 3):
639642
self.fitness_func = fitness_func
640643
else:
641644
self.valid_parameters = False
642-
raise ValueError("The fitness function must accept 2 parameters:\n1) A solution to calculate its fitness value.\n2) The solution's index within the population.\n\nThe passed fitness function named '{funcname}' accepts {argcount} parameter(s).".format(funcname=fitness_func.__code__.co_name, argcount=fitness_func.__code__.co_argcount))
645+
raise ValueError("The fitness function must accept up to 3 parameters:\n1) A solution to calculate its fitness value.\n2) The solution's index within the population.\n\nThe passed fitness function named '{funcname}' accepts {argcount} parameter(s).".format(funcname=fitness_func.__code__.co_name, argcount=fitness_func.__code__.co_argcount))
643646
else:
644647
self.valid_parameters = False
645648
raise ValueError("The value assigned to the fitness_func parameter is expected to be of type function but ({fitness_func_type}) found.".format(fitness_func_type=type(fitness_func)))
@@ -1160,7 +1163,7 @@ def cal_pop_fitness(self):
11601163
# Use the parent's index to return its pre-calculated fitness value.
11611164
fitness = self.previous_generation_fitness[parent_idx]
11621165
else:
1163-
fitness = self.fitness_func(sol, sol_idx)
1166+
fitness = self.fitness_func(sol, self.extra_data, sol_idx)
11641167
if type(fitness) in GA.supported_int_float_types:
11651168
pass
11661169
else:

0 commit comments

Comments
 (0)