Repository navigation
Expand file tree
/
Copy pathpygad.py
More file actions
292 lines (253 loc) · 23.6 KB
/
Copy pathpygad.py
File metadata and controls
292 lines (253 loc) · 23.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import numpy
import cloudpickle
from pygad import utils
from pygad import helper
from pygad import visualize
# Inherit from all these classes so that their methods can be accessed through the `self` object of the `pygad.GA` class.
class GA(utils.parent_selection.ParentSelection,
utils.crossover.Crossover,
utils.mutation.Mutation,
utils.nsga.NSGA,
utils.nsga2.NSGA2,
utils.nsga3.NSGA3,
utils.report.Report,
utils.validation.Validation,
utils.engine.GAEngine,
helper.unique.Unique,
helper.misc.Helper,
visualize.plot.Plot):
supported_int_types = [int, numpy.int8, numpy.int16, numpy.int32, numpy.int64,
numpy.uint, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64,
object]
supported_float_types = [float, numpy.float16, numpy.float32, numpy.float64,
object]
supported_int_float_types = supported_int_types + supported_float_types
def __init__(self,
num_generations,
num_parents_mating,
fitness_func,
fitness_batch_size=None,
initial_population=None,
sol_per_pop=None,
num_genes=None,
init_range_low=-4,
init_range_high=4,
gene_type=float,
parent_selection_type="sss",
keep_parents=None,
keep_elitism=1,
K_tournament=3,
nsga3_num_divisions=None,
crossover_type="single_point",
crossover_probability=None,
sbx_crossover_eta=30,
mutation_type="random",
mutation_probability=None,
polynomial_mutation_eta=20,
mutation_by_replacement=False,
mutation_percent_genes='default',
mutation_num_genes=None,
random_mutation_min_val=-1.0,
random_mutation_max_val=1.0,
gene_space=None,
gene_constraint=None,
sample_size=100,
allow_duplicate_genes=True,
on_start=None,
on_fitness=None,
on_parents=None,
on_crossover=None,
on_mutation=None,
on_generation=None,
on_stop=None,
save_best_solutions=False,
save_solutions=False,
suppress_warnings=False,
stop_criteria=None,
parallel_processing=None,
random_seed=None,
logger=None,
gene_categorical=None):
"""
The constructor of the GA class accepts all parameters required to create an instance of the GA class. It validates such parameters.
num_generations: Number of generations.
num_parents_mating: Number of solutions to be selected as parents in the mating pool.
fitness_func: Accepts a function/method and returns the fitness value of the solution. In PyGAD 2.20.0, a third parameter is passed referring to the 'pygad.GA' instance.
fitness_batch_size: Added in PyGAD 2.19.0. Supports calculating the fitness in batches. If the value is 1 or None, then the fitness function is called for each individual solution. If given another value X where X is neither 1 nor None (e.g. X=3), then the fitness function is called once for each X (3) solutions.
initial_population: A user-defined initial population. It is useful when the user wants to start the generations with a custom initial population. It defaults to None which means no initial population is specified by the user. In this case, PyGAD creates an initial population using the 'sol_per_pop' and 'num_genes' parameters. An exception is raised if the 'initial_population' is None while any of the 2 parameters ('sol_per_pop' or 'num_genes') is also None. A supplied population must be a non-empty rectangular 2D numeric list, tuple, or NumPy array. Its shape determines sol_per_pop and num_genes before per-gene validation. The input is copied, converted, checked against gene constraints, and repaired for duplicates when required.
sol_per_pop: Number of solutions in the population.
num_genes: Number of genes in the solution.
init_range_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.
init_range_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.
It is OK for the 2 parameters ('init_range_low' and 'init_range_high') to be equal, or for one to be higher or lower than the other (i.e. 'init_range_low' does not need to be lower than 'init_range_high'). Generated range values remain inside the interval after conversion and rounding; an error is raised if the type and precision cannot represent any valid value.
gene_type: The type of the gene. It is assigned to any of these types (int, numpy.int8, numpy.int16, numpy.int32, numpy.int64, numpy.uint, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64, float, numpy.float16, numpy.float32, numpy.float64) and forces all the genes to be of that type.
parent_selection_type: Type of parent selection.
keep_parents: It defaults to None, which is treated as -1 (keep all selected parents). If 0, this means no parent in the current population will be used in the next population. If -1, this means all parents in the current population will be used in the next population. If set to a value > 0, then the specified value refers to the number of parents in the current population to be used in the next population. Some parent selection operators such as rank selection, favor population diversity and therefore keeping the parents in the next generation can be beneficial. However, some other parent selection operators, such as roulette wheel selection (RWS), have higher selection pressure and keeping more than one parent in the next generation can seriously harm population diversity. IMPORTANT: This parameter has an effect only when the keep_elitism parameter is 0. Because keep_elitism defaults to 1, keep_parents is ignored unless you also set keep_elitism=0. If you explicitly set keep_parents while keep_elitism is greater than 0, a warning is raised to flag that keep_parents will have no effect. Thanks to Prof. Fernando Jiménez (http://webs.um.es/fernan) for editing this sentence.
K_tournament: When the value of 'parent_selection_type' is 'tournament', the 'K_tournament' parameter specifies the number of solutions from which a parent is selected randomly.
nsga3_num_divisions: Only used when 'parent_selection_type' is 'nsga3' or 'tournament_nsga3'. It is the number of divisions per objective axis used to build the structured reference points (the 'p' parameter from Deb & Jain 2014). The total number of reference points is C(M + p - 1, p) where M is the number of objectives. Must be a positive integer. Defaults to None.
keep_elitism: Added in PyGAD 2.18.0. It can take the value 0 or a positive integer that satisfies (0 <= keep_elitism <= sol_per_pop). It defaults to 1 which means only the best solution in the current generation is kept in the next generation. If assigned 0, this means it has no effect. If assigned a positive integer K, then the best K solutions are kept in the next generation. It cannot be assigned a value greater than the value assigned to the sol_per_pop parameter. If this parameter has a value different from 0, then it takes precedence over the keep_parents parameter, which will have no effect (a warning is raised if keep_parents was explicitly set in this case). To use keep_parents instead, set keep_elitism=0.
crossover_type: Type of the crossover operator. If crossover_type=None, then the crossover step is bypassed which means no crossover is applied and thus no offspring will be created in the next generations. The next generation will use the solutions in the current population.
crossover_probability: The probability of selecting a solution for the crossover operation. If the generated value is < crossover_probability, the solution is selected. The value must be between 0 and 1 inclusive.
sbx_crossover_eta: Only used when 'crossover_type' is 'sbx'. The distribution index that controls how close the children stay to the parents (higher value = closer). Defaults to 30.
mutation_type: Type of the mutation operator. If mutation_type=None, then the mutation step is bypassed which means no mutation is applied and thus no changes are applied to the offspring created using the crossover operation. The offspring will be used unchanged in the next generation.
mutation_probability: The probability of selecting a gene for the mutation operation. If the generated value is < mutation_probability, the gene is selected. Zero leaves offspring unchanged. It accepts either a single value for fixed mutation or a list/tuple/numpy.ndarray of 2 values for adaptive mutation. The values must be between 0 and 1 inclusive. If specified, then no need for the 2 parameters mutation_percent_genes and mutation_num_genes.
polynomial_mutation_eta: Only used when 'mutation_type' is 'polynomial'. The distribution index that controls how small the mutation step is (higher value = smaller step). Defaults to 20.
mutation_by_replacement: An optional bool parameter. It applies when random or adaptive mutation draws random values. In this case, setting mutation_by_replacement=True means replace the gene by the randomly generated value. If False, then it has no effect and random mutation works by adding the random value to the gene.
mutation_percent_genes: Percentage of genes to mutate which defaults to the string 'default' which means 10%. This parameter has no action if any of the 2 parameters mutation_probability or mutation_num_genes exist.
mutation_num_genes: Number of genes to mutate which defaults to None. If the parameter mutation_num_genes exists, then no need for the parameter mutation_percent_genes. This parameter has no action if the mutation_probability parameter exists.
random_mutation_min_val: The minimum value of the range from which a random value is selected to be added to the selected gene(s) to mutate. It defaults to -1.0.
random_mutation_max_val: The maximum value of the range from which a random value is selected to be added to the selected gene(s) to mutate. It defaults to 1.0.
gene_space: It accepts a list of all possible values of the gene. This list is used in the mutation step. It 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.
gene_constraint: It accepts a list of constraints for the genes. Each constraint is a Python function. Added in PyGAD 3.5.0.
gene_categorical: Marks the genes whose values are categories with no order, such as 0 for relu, 1 for tanh, and 2 for sigmoid. It accepts None (the default, every gene is ordered), a single bool applied to all genes, or a list with one bool per gene. A categorical gene must have a finite gene_space (a list of numbers, a range, or a dict with a step). For a categorical gene, polynomial mutation picks one of the other allowed values at random and SBX crossover copies the value of one parent. The other operators are not affected. Added in PyGAD 3.9.0.
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 randomly. Useful if either allow_duplicate_genes or gene_constraint is used. Added in PyGAD 3.5.0.
on_start: Accepts a function/method to be called only once before the genetic algorithm starts its evolution. If function, 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.
on_fitness: Accepts a function/method to be called after calculating the fitness values of all solutions in the population. If function, 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.
on_parents: Accepts a function/method to be called after selecting the parents that mate. If function, then it must accept 2 parameters: the first one represents the instance of the genetic algorithm and the second one represents the selected parents. If method, then it must accept 3 parameters where the third one refers to the method's object. Added in PyGAD 2.6.0.
on_crossover: Accepts a function/method to be called each time the crossover operation is applied. If function, then it must accept 2 parameters: the first one represents the instance of the genetic algorithm and the second one represents the offspring generated using crossover. If method, then it must accept 3 parameters where the third one refers to the method's object. Added in PyGAD 2.6.0.
on_mutation: Accepts a function/method to be called each time the mutation operation is applied. If function, then it must accept 2 parameters: the first one represents the instance of the genetic algorithm and the second one represents the offspring after applying the mutation. If method, then it must accept 3 parameters where the third one refers to the method's object. Added in PyGAD 2.6.0.
on_generation: Accepts a function/method to be called after each generation. If function, then it must accept a single parameter representing the instance of the genetic algorithm. If the function returned "stop", then the run() method stops without completing the other generations. If method, then it must accept 2 parameters where the second one refers to the method's object. Added in PyGAD 2.6.0.
on_stop: Accepts a function/method to be called only once exactly before the genetic algorithm stops or when it completes all the generations. If function, then it must accept 2 parameters: the first one represents the instance of the genetic algorithm and the second one is a list of fitness values of the last population's solutions. If method, then it must accept 3 parameters where the third one refers to the method's object. Added in PyGAD 2.6.0.
save_best_solutions: Added in PyGAD 2.9.0 and its type is bool. If True, then the best solution in each generation is saved into the 'best_solutions' attribute. Use this parameter with caution as it may cause memory overflow when either the number of generations or the number of genes is large.
save_solutions: Added in PyGAD 2.15.0 and its type is bool. If True, then all solutions in each generation are saved into the 'solutions' attribute. Use this 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.
suppress_warnings: Added in PyGAD 2.10.0 and its type is bool. If True, then no warning messages will be displayed. It defaults to False.
allow_duplicate_genes: Added in PyGAD 2.13.0. If True, then a solution/chromosome may have duplicate gene values. If False, PyGAD tries to give every gene a unique numeric value after conversion and rounding, using replacement chains when needed. Duplicates may remain if the spaces, ranges, constraints, or sampling limit leave no usable alternative.
stop_criteria: Added in PyGAD 2.15.0. It is assigned to some criteria to stop the evolution if at least one criterion holds.
parallel_processing: Added in PyGAD 2.17.0. Defaults to `None` which means no parallel processing is used. If a positive integer is assigned, it specifies the number of threads to be used. If a list or a tuple of exactly 2 elements is assigned, then: 1) The first element can be either "process" or "thread" to specify whether processes or threads are used, respectively. 2) The second element can be: 1) A positive integer to select the maximum number of processes or threads to be used. 2) 0 to indicate that parallel processing is not used. This is identical to setting 'parallel_processing=None'. 3) None to use the default value as calculated by the concurrent.futures module.
random_seed: Added in PyGAD 2.18.0. It accepts None or a Python/NumPy integer from 0 through 2**32-1. Each GA owns numpy_random_generator and python_random_generator; their states continue across runs and checkpoints. Custom operators can use these generators for reproducible random draws.
logger: Added in PyGAD 2.20.0. It accepts a logger object of the 'logging.Logger' class to log the messages. If no logger is passed, then a default logger is created to log/print the messages to the console exactly like using the 'print()' function.
"""
try:
self.validate_parameters(num_generations=num_generations,
num_parents_mating=num_parents_mating,
fitness_func=fitness_func,
fitness_batch_size=fitness_batch_size,
initial_population=initial_population,
sol_per_pop=sol_per_pop,
num_genes=num_genes,
init_range_low=init_range_low,
init_range_high=init_range_high,
gene_type=gene_type,
parent_selection_type=parent_selection_type,
keep_parents=keep_parents,
keep_elitism=keep_elitism,
K_tournament=K_tournament,
nsga3_num_divisions=nsga3_num_divisions,
crossover_type=crossover_type,
crossover_probability=crossover_probability,
sbx_crossover_eta=sbx_crossover_eta,
mutation_type=mutation_type,
mutation_probability=mutation_probability,
polynomial_mutation_eta=polynomial_mutation_eta,
mutation_by_replacement=mutation_by_replacement,
mutation_percent_genes=mutation_percent_genes,
mutation_num_genes=mutation_num_genes,
random_mutation_min_val=random_mutation_min_val,
random_mutation_max_val=random_mutation_max_val,
gene_space=gene_space,
gene_constraint=gene_constraint,
sample_size=sample_size,
allow_duplicate_genes=allow_duplicate_genes,
on_start=on_start,
on_fitness=on_fitness,
on_parents=on_parents,
on_crossover=on_crossover,
on_mutation=on_mutation,
on_generation=on_generation,
on_stop=on_stop,
save_best_solutions=save_best_solutions,
save_solutions=save_solutions,
suppress_warnings=suppress_warnings,
stop_criteria=stop_criteria,
parallel_processing=parallel_processing,
random_seed=random_seed,
logger=logger,
gene_categorical=gene_categorical)
except Exception as e:
if hasattr(self, 'logger'):
self.logger.exception(e)
# sys.exit(-1)
raise e
def save(self, filename):
"""
Serialise the GA instance to disk with ``cloudpickle``. The
file extension ``.pkl`` is added automatically.
Parameters
----------
filename : str
Path (without extension) where the pickle file is written.
"""
cloudpickle_serialized_object = cloudpickle.dumps(self)
with open(filename + ".pkl", 'wb') as file:
file.write(cloudpickle_serialized_object)
def push_to_vilvik(self, *, api_key=None, **overrides):
"""
Push this GA run to Vilvik (https://vilvik.com) as an
editable, continuable cloud record. Thin convenience wrapper
over the Vilvik SDK, which must be installed separately::
pip install vilvik
Call after ``ga.run()``. Sign in first with ``vilvik login``
or set the ``VILVIK_API_KEY`` environment variable.
Parameters
----------
api_key : str, optional
Explicit API key. When None, the SDK falls back to the
CLI login or the ``VILVIK_API_KEY`` environment variable.
**overrides
Forwarded to ``vilvik.push``. Common keys include
``name``, ``fitness_source``, ``callbacks``,
``preamble``, and ``dry_run``.
Returns
-------
record : object
The created Vilvik record, or a capture report when
``dry_run=True``.
Raises
------
ImportError
If the ``vilvik`` package is not installed.
"""
try:
import vilvik
except ImportError as exc:
raise ImportError(
"push_to_vilvik requires the Vilvik SDK. Install it with: "
"pip install vilvik"
) from exc
import pygad
return vilvik.push(
self,
api_key=api_key,
origin_overrides={"client": "pygad_wrapper", "pygad_version": pygad.__version__},
**overrides,
)
def load(filename):
"""
Load a GA instance from a ``cloudpickle`` file written by
``pygad.GA.save``. The file extension ``.pkl`` is added
automatically.
Parameters
----------
filename : str
Path (without extension) where the pickle file is read from.
Returns
-------
ga_in : pygad.GA
The restored GA instance.
Raises
------
FileNotFoundError
If the file does not exist.
BaseException
If the file exists but cannot be unpickled (for example when
the original fitness function is not importable).
"""
try:
with open(filename + ".pkl", 'rb') as file:
ga_in = cloudpickle.load(file)
except FileNotFoundError:
raise FileNotFoundError(f"Error reading the file {filename}. Please check your inputs.")
except:
# raise BaseException("Error loading the file. If the file already exists, please reload all the functions previously used (e.g. fitness function).")
raise BaseException("Error loading the file.")
return ga_in