pyerualjetwork 4.2.0b0__py3-none-any.whl → 4.2.0b2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,8 @@
1
1
  """
2
2
  MAIN MODULE FOR PLANEAT
3
3
 
4
+ Examples: https://github.com/HCB06/PyerualJetwork/tree/main/Welcome_to_PyerualJetwork/ExampleCodes
5
+
4
6
  ANAPLAN document: https://github.com/HCB06/Anaplan/blob/main/Welcome_to_Anaplan/ANAPLAN_USER_MANUEL_AND_LEGAL_INFORMATION(EN).pdf
5
7
 
6
8
  @author: Hasan Can Beydili
@@ -13,6 +15,7 @@ ANAPLAN document: https://github.com/HCB06/Anaplan/blob/main/Welcome_to_Anaplan/
13
15
  import cupy as cp
14
16
  import numpy as np
15
17
  import random
18
+ import math
16
19
 
17
20
 
18
21
  ### LIBRARY IMPORTS ###
@@ -70,155 +73,198 @@ def define_genomes(input_shape, output_shape, population_size, dtype=cp.float32)
70
73
  return cp.array(population_weights, dtype=dtype), population_activations
71
74
 
72
75
 
73
- def evolve(weights, activation_potentiations, what_gen, fitness, show_info=False, strategy='cross_over', bar_status=True, policy='normal_selective', target_fitness='max', mutations=True, bad_genoms_mutation_prob=None, activation_mutate_prob=0.5, save_best_genom=True, cross_over_mode='tpm', activation_add_prob=0.5, activation_delete_prob=0.5, activation_change_prob=0.5, weight_mutate_prob=1, weight_mutate_rate=32, activation_selection_add_prob=0.7, activation_selection_change_prob=0.5, activation_selection_rate=2, dtype=cp.float32):
76
+ def evolver(weights,
77
+ activation_potentiations,
78
+ what_gen,
79
+ fitness,
80
+ show_info=False,
81
+ policy='aggresive',
82
+ bad_genomes_selection_prob=None,
83
+ bar_status=True,
84
+ strategy='normal_selective',
85
+ target_fitness='max',
86
+ mutations=True,
87
+ bad_genomes_mutation_prob=None,
88
+ activation_mutate_prob=0.5,
89
+ save_best_genom=True,
90
+ fitness_bias=None,
91
+ cross_over_mode='tpm',
92
+ activation_mutate_add_prob=0.5,
93
+ activation_mutate_delete_prob=0.5,
94
+ activation_mutate_change_prob=0.5,
95
+ weight_mutate_prob=1,
96
+ weight_mutate_rate=32,
97
+ activation_selection_add_prob=0.6,
98
+ activation_selection_change_prob=0.4,
99
+ activation_selection_rate=2,
100
+ dtype=cp.float32):
74
101
  """
75
- Applies the evolving process of a population of genomes using selection, crossover, mutation, and activation function potentiation.
76
- The function modifies the population's weights and activation functions based on a specified policy, mutation probabilities, and strategy.
102
+ Applies the evolving process of a population of genomes using selection, crossover, mutation, and activation function potentiation.
103
+ The function modifies the population's weights and activation functions based on a specified policy, mutation probabilities, and strategy.
77
104
 
78
- Args:
79
- weights (cupy.ndarray): Array of weights for each genome.
80
- (first returned value of define_genomes function)
81
-
82
- activation_potentiations (list): A list of activation functions for each genome.
83
- (second returned value of define_genomes function)
84
-
85
- what_gen (int): The current generation number, used for informational purposes or logging.
86
-
87
- fitness (cupy.ndarray): A 1D array containing the fitness or reward values of each genome.
88
- The array is used to rank the genomes based on their performance. PLANEAT maximizes the reward.
89
-
90
- show_info (bool, optional): If True, prints information about the current generation and the
91
- maximum reward obtained. Also shows the current configuration. Default is False.
92
-
93
- strategy (str, optional): The strategy for combining the best and bad genomes. Options:
94
- - 'cross_over': Perform crossover between the best genomes and replace bad genomes.
95
- (Classic NEAT crossover)
96
- - 'potentiate': Cumulate the weight of the best genomes and replace bad genomes.
97
- (PLAN feature, similar to arithmetic crossover but different.)
98
- Default is 'cross_over'.
99
-
100
- bar_status (bool, optional): Loading bar status during evolving process of genomes. True or False. Default: True
105
+ 'selection' args effects cross-over.
106
+ 'mutate' args effects mutation.
101
107
 
102
- policy (str, optional): The selection policy that governs how genomes are selected for reproduction. Options:
103
- - 'normal_selective': Normal selection based on reward, where a portion of the bad genes are discarded.
104
- - 'more_selective': A more selective policy, where fewer bad genes survive.
105
- - 'less_selective': A less selective policy, where more bad genes survive.
106
- Default is 'normal_selective'.
107
-
108
- target_fitness (str, optional): Target fitness strategy for PLANEAT optimization. ('max' for machine learning, 'min' for machine unlearning.) Default: 'max'
108
+ Args:
109
+ weights (cupy.ndarray): Array of weights for each genome.
110
+ (first returned value of define_genomes function)
111
+
112
+ activation_potentiations (list): A list of activation functions for each genome.
113
+ (second returned value of define_genomes function)
114
+
115
+ what_gen (int): The current generation number, used for informational purposes or logging.
116
+
117
+ fitness (cupy.ndarray): A 1D array containing the fitness values of each genome.
118
+ The array is used to rank the genomes based on their performance. PLANEAT maximizes or minimizes this fitness based on the `target_fitness` parameter.
119
+
120
+ show_info (bool, optional): If True, prints information about the current generation and the
121
+ maximum reward obtained. Also shows the current configuration. Default is False.
122
+
123
+ strategy (str, optional): The strategy for combining the best and bad genomes. Options:
124
+ - 'normal_selective': Normal selection based on reward, where a portion of the bad genes are discarded.
125
+ - 'more_selective': A more selective strategy, where fewer bad genes survive.
126
+ - 'less_selective': A less selective strategy, where more bad genes survive.
127
+ Default is 'normal_selective'.
128
+
129
+ bar_status (bool, optional): Loading bar status during evolving process of genomes. True or False. Default: True
109
130
 
110
- mutations (bool, optional): If True, mutations are applied to the bad genomes and potentially
111
- to the best genomes as well. Default is True.
112
-
113
- bad_genoms_mutation_prob (float, optional): The probability of applying mutation to the bad genomes.
114
- Must be in the range [0, 1]. Also affects the mutation probability of the best genomes inversely.
115
- For example, a value of 0.7 for bad genomes implies 0.3 for best genomes. Default is None,
116
- which means it is determined by the `policy` argument.
117
-
118
- activation_mutate_prob (float, optional): The probability of applying mutation to the activation functions.
119
- Must be in the range [0, 1]. Default is 0.5 (50%).
120
-
121
- save_best_genom (bool, optional): If True, ensures that the best genomes are saved and not mutated
122
- or altered during reproduction. Default is True.
123
-
124
- cross_over_mode (str, optional): Specifies the crossover method to use. Options:
125
- - 'tpm': Two-Point Matrix Crossover
126
- - 'plantic': plantic Crossover
127
- Default is 'tpm'.
128
-
129
- activation_add_prob (float, optional): The probability of adding a new activation function to the genome for mutation.
130
- Must be in the range [0, 1]. Default is 0.5.
131
-
132
- activation_delete_prob (float, optional): The probability of deleting an existing activation function
133
- from the genome for mutation. Must be in the range [0, 1]. Default is 0.5.
134
-
135
- activation_change_prob (float, optional): The probability of changing an activation function in the genome for mutation.
136
- Must be in the range [0, 1]. Default is 0.5.
137
-
138
- weight_mutate_prob (float, optional): The probability of mutating a weight in the genome.
139
- Must be in the range [0, 1]. Default is 1.
140
-
141
- weight_mutate_rate (int, optional): If the value you enter here is equal to the result of input layer * output layer,
142
- only a single weight will be mutated during each mutation process. If the value you enter here is half
143
- of the result of input layer * output layer, two weights in the weight matrix will be mutated.
144
- WARNING: if you don't understand do NOT change this value. Default is 32.
131
+ policy (str, optional): The selection policy that governs how genomes are selected for reproduction. Options:
132
+
133
+ - 'aggresive': Aggressive policy using very aggressive selection policy.
134
+ Advantages: fast training.
135
+ Disadvantages: may lead to fitness stuck in a local maximum or minimum.
145
136
 
146
- activation_selection_add_prob (float, optional): The probability of adding an existing activation function for cross over.
147
- from the genome. Must be in the range [0, 1]. Default is 0.7. (WARNING! More higher values make models more complex. For fast training rise this value.)
148
-
149
- activation_selection_change_prob (float, optional): The probability of changing an activation function in the genome for cross over.
150
- Must be in the range [0, 1]. Default is 0.5.
151
-
152
- activation_selection_rate (int, optional): If the activation list of a good genome is smaller than the value entered here, only one activation will undergo a crossover operation. In other words, this parameter controls the model complexity. Default is 2.
153
-
154
- dtype (cupy.dtype): Data type for the arrays. np.float32 by default. Example: cp.float64 or cp.float16. [fp32 for balanced devices, fp64 for strong devices, fp16 for weak devices: not reccomended!] (optional)
155
-
156
- Raises:
157
- ValueError:
158
- - If `policy` is not one of the specified values ('normal_selective', 'more_selective', 'less_selective').
159
- - If `cross_over_mode` is not one of the specified values ('tpm', 'plantic').
160
- - If `bad_genoms_mutation_prob`, `activation_mutate_prob`, or other probability parameters are not in the range [0, 1].
161
- - If the population size is odd (ensuring an even number of genomes is required for proper selection).
162
-
163
- Returns:
164
- tuple: A tuple containing:
165
- - weights (numpy.ndarray): The updated weights for the population after selection, crossover, and mutation.
166
- The shape is (population_size, output_shape, input_shape).
167
- - activation_potentiations (list): The updated list of activation functions for the population.
168
-
169
- Notes:
170
- - **Selection Process**:
171
- - The genomes are sorted by their fitness (based on `fitness`), and then split into "best" and "bad" halves.
172
- - The best genomes are retained, and the bad genomes are modified based on the selected strategy.
173
-
174
- - **Crossover and Potentiation Strategies**:
175
- - The **'cross_over'** strategy performs crossover, where parts of the best genomes' weights are combined with the other good genomes to create new weight matrices.
176
- - The **'potentiate'** strategy strengthens the best genomes by potentiating their weights towards the other good genomes.
177
-
178
- - **Mutation**:
179
- - Mutation is applied to both the best and bad genomes, depending on the mutation probability and the `policy`.
180
- - `bad_genoms_mutation_prob` determines the probability of applying mutations to the bad genomes.
181
- - If `activation_mutate_prob` is provided, activation function mutations are applied to the genomes based on this probability.
182
-
183
- - **Population Size**: The population size must be an even number to properly split the best and bad genomes. If `fitness` has an odd length, an error is raised.
184
-
185
- - **Logging**: If `show_info=True`, the current generation and the maximum reward from the population are printed for tracking the learning progress.
137
+ - 'explorer': Explorer policy increases population diversity.
138
+ Advantages: fitness does not get stuck at local maximum or minimum.
139
+ Disadvantages: slow training.
140
+
141
+ Suggestions: Use hybrid and dynamic policy. When fitness appears stuck, switch to the 'explorer' policy.
186
142
 
187
- Example:
188
- ```python
189
- weights, activation_potentiations = planeat.evolve(weights, activation_potentiations, 1, fitness, show_info=True, strategy='cross_over', policy='normal_selective')
190
- ```
143
+ Default: 'aggresive'.
144
+
145
+ target_fitness (str, optional): Target fitness strategy for PLANEAT optimization. ('max' maximizes fitness, 'min' minimizes fitness.) Default: 'max'.
146
+
147
+ fitness_bias (float, optional): Fitness bias must be a probability value between 0 and 1 that determines the effect of fitness on the crossover process. Default: Determined by the `strategy`.
191
148
 
192
- - The function returns the updated weights and activations after processing based on the chosen strategy, policy, and mutation parameters.
149
+ mutations (bool, optional): If True, mutations are applied to the bad genomes and potentially
150
+ to the best genomes as well. Default is True.
151
+
152
+ bad_genomes_mutation_prob (float, optional): The probability of applying mutation to the bad genomes.
153
+ Must be in the range [0, 1]. Also affects the mutation probability of the best genomes inversely.
154
+ For example, a value of 0.7 for bad genomes implies 0.3 for best genomes. Default: Determined by `policy`.
155
+
156
+ activation_mutate_prob (float, optional): The probability of applying mutation to the activation functions.
157
+ Must be in the range [0, 1]. Default is 0.5 (50%).
158
+
159
+ save_best_genom (bool, optional): If True, ensures that the best genomes are saved and not mutated
160
+ or altered during reproduction. Default is True.
161
+
162
+ cross_over_mode (str, optional): Specifies the crossover method to use. Options:
163
+ - 'tpm': Two-Point Matrix Crossover.
164
+ Default is 'tpm'.
165
+
166
+ activation_mutate_add_prob (float, optional): The probability of adding a new activation function to the genome for mutation.
167
+ Must be in the range [0, 1]. Default is 0.5.
168
+
169
+ activation_mutate_delete_prob (float, optional): The probability of deleting an existing activation function
170
+ from the genome for mutation. Must be in the range [0, 1]. Default is 0.5.
171
+
172
+ activation_mutate_change_prob (float, optional): The probability of changing an activation function in the genome for mutation.
173
+ Must be in the range [0, 1]. Default is 0.5.
174
+
175
+ weight_mutate_prob (float, optional): The probability of mutating a weight in the genome.
176
+ Must be in the range [0, 1]. Default is 1.
177
+
178
+ weight_mutate_rate (int, optional): If the value entered here equals the result of input_layer * output_layer,
179
+ only a single weight will be mutated during each mutation process. If the value is half of the result,
180
+ two weights will be mutated. WARNING: If you don't understand, do NOT change this value. Default is 32.
181
+
182
+ activation_selection_add_prob (float, optional): The probability of adding an existing activation function for crossover.
183
+ Must be in the range [0, 1]. Default is 0.6. (WARNING! Higher values increase complexity. For faster training, increase this value.)
184
+
185
+ activation_selection_change_prob (float, optional): The probability of changing an activation function in the genome for crossover.
186
+ Must be in the range [0, 1]. Default is 0.4.
187
+
188
+ activation_selection_rate (int, optional): If the activation list of a good genome is smaller than this value, only one activation will undergo crossover. This parameter controls model complexity. Default is 2.
189
+
190
+ dtype (cupy.dtype): Data type for the arrays. Default: cp.float32.
191
+ Example: cp.float64 or cp.float16 [fp32 for balanced devices, fp64 for strong devices, fp16 for weak devices: not recommended!].
192
+
193
+ Raises:
194
+ ValueError:
195
+ - If `policy` is not one of the specified values ('aggresive', 'explorer').
196
+ - If 'strategy' is not one of the specified values ('less_selective', 'normal_selective', 'more_selective')
197
+ - If `cross_over_mode` is not one of the specified values ('tpm').
198
+ - If `bad_genomes_mutation_prob`, `activation_mutate_prob`, or other probability parameters are not in the range 0 and 1.
199
+ - If the population size is odd (ensuring an even number of genomes is required for proper selection).
200
+ - If 'fitness_bias' value is not in range 0 and 1.
201
+
202
+ Returns:
203
+ tuple: A tuple containing:
204
+ - weights (numpy.ndarray): The updated weights for the population after selection, crossover, and mutation.
205
+ The shape is (population_size, output_shape, input_shape).
206
+ - activation_potentiations (list): The updated list of activation functions for the population.
207
+
208
+ Notes:
209
+ - **Selection Process**:
210
+ - The genomes are sorted by their fitness (based on `fitness`), and then split into "best" and "bad" halves.
211
+ - The best genomes are retained, and the bad genomes are modified based on the selected strategy.
212
+
213
+ - **Crossover Strategies**:
214
+ - The **'cross_over'** strategy performs crossover, where parts of the best genomes' weights are combined with other good genomes to create new weight matrices.
215
+
216
+ - **Mutation**:
217
+ - Mutation is applied to both the best and bad genomes, depending on the mutation probability and the `policy`.
218
+ - `bad_genomes_mutation_prob` determines the probability of applying mutations to the bad genomes.
219
+ - If `activation_mutate_prob` is provided, activation function mutations are applied to the genomes based on this probability.
220
+
221
+ - **Population Size**: The population size must be an even number to properly split the best and bad genomes. If `fitness` has an odd length, an error is raised.
222
+
223
+ - **Logging**: If `show_info=True`, the current generation and the maximum reward from the population are printed for tracking the learning progress.
224
+
225
+ Example:
226
+ ```python
227
+ weights, activation_potentiations = planeat.evolver(weights, activation_potentiations, 1, fitness, show_info=True, strategy='normal_selective', policy='aggresive')
228
+ ```
229
+
230
+ - The function returns the updated weights and activations after processing based on the chosen strategy, policy, and mutation parameters.
193
231
  """
194
232
 
195
233
  ### ERROR AND CONFIGURATION CHECKS:
196
-
197
- if policy == 'normal_selective':
198
- if bad_genoms_mutation_prob == None:
199
- bad_genoms_mutation_prob = 0.7
234
+ if strategy == 'normal_selective':
235
+ if bad_genomes_mutation_prob is None: bad_genomes_mutation_prob = 0.7 # EFFECTS MUTATION
236
+ if bad_genomes_selection_prob is None: bad_genomes_selection_prob = 0.25 # EFFECTS CROSS-OVER
237
+ if fitness_bias is None: fitness_bias = 0.5 # The pressure applied by FITNESS to the CROSS-OVER
200
238
 
201
- elif policy == 'more_selective':
202
- if bad_genoms_mutation_prob == None:
203
- bad_genoms_mutation_prob = 0.85
239
+ elif strategy == 'more_selective':
240
+ if bad_genomes_mutation_prob is None: bad_genomes_mutation_prob = 0.85 # EFFECTS MUTATION
241
+ if bad_genomes_selection_prob is None: bad_genomes_selection_prob = 0.1 # EFFECTS CROSS-OVER
242
+ if fitness_bias is None: fitness_bias = 0.7 # The pressure applied by FITNESS to the CROSS-OVER
204
243
 
205
- elif policy == 'less_selective':
206
- if bad_genoms_mutation_prob == None:
207
- bad_genoms_mutation_prob = 0.6
244
+ elif strategy == 'less_selective':
245
+ if bad_genomes_mutation_prob is None: bad_genomes_mutation_prob = 0.6 # EFFECTS MUTATION
246
+ if bad_genomes_selection_prob is None: bad_genomes_selection_prob = 0.5 # EFFECTS CROSS-OVER
247
+ if fitness_bias is None: fitness_bias = 0.3 # The pressure applied by FITNESS to the CROSS-OVER
208
248
 
209
249
  else:
210
- raise ValueError("policy parameter must be: 'normal_selective' or 'more_selective' or 'less_selective'")
211
-
212
-
213
- if (activation_add_prob < 0 or activation_add_prob > 1) or (activation_change_prob < 0 or activation_change_prob > 1) or (activation_delete_prob < 0 or activation_delete_prob > 1) or (weight_mutate_prob < 0 or weight_mutate_prob > 1) or (activation_selection_add_prob < 0 or activation_selection_add_prob > 1) or (activation_selection_change_prob < 0 or activation_selection_change_prob > 1):
250
+ raise ValueError("strategy parameter must be: 'normal_selective' or 'more_selective' or 'less_selective'")
251
+
252
+ if policy =='explorer': fitness_bias = 0
253
+
254
+ if ((activation_mutate_add_prob < 0 or activation_mutate_add_prob > 1) or
255
+ (activation_mutate_change_prob < 0 or activation_mutate_change_prob > 1) or
256
+ (activation_mutate_delete_prob < 0 or activation_mutate_delete_prob > 1) or
257
+ (weight_mutate_prob < 0 or weight_mutate_prob > 1) or
258
+ (activation_selection_add_prob < 0 or activation_selection_add_prob > 1) or (
259
+ activation_selection_change_prob < 0 or activation_selection_change_prob > 1)):
260
+
214
261
  raise ValueError("All hyperparameters ending with 'prob' must be a number between 0 and 1.")
215
262
 
216
- if cross_over_mode != 'tpm' and cross_over_mode != 'plantic':
217
- raise ValueError("cross_over_mode parameter must be 'tpm' or 'plantic'")
263
+ if fitness_bias < 0 or fitness_bias > 1: raise ValueError("fitness_bias value must be a number between 0 and 1.")
218
264
 
219
- if bad_genoms_mutation_prob is not None:
220
- if not isinstance(bad_genoms_mutation_prob, float) or bad_genoms_mutation_prob < 0 or bad_genoms_mutation_prob > 1:
221
- raise ValueError("bad_genoms_mutation_prob parameter must be float and 0-1 range")
265
+ if bad_genomes_mutation_prob is not None:
266
+ if not isinstance(bad_genomes_mutation_prob, float) or bad_genomes_mutation_prob < 0 or bad_genomes_mutation_prob > 1:
267
+ raise ValueError("bad_genomes_mutation_prob parameter must be float and 0-1 range")
222
268
 
223
269
  if activation_mutate_prob is not None:
224
270
  if not isinstance(activation_mutate_prob, float) or activation_mutate_prob < 0 or activation_mutate_prob > 1:
@@ -242,90 +288,90 @@ Example:
242
288
 
243
289
  ### GENOMES ARE DIVIDED INTO TWO GROUPS: GOOD GENOMES AND BAD GENOMES:
244
290
 
245
- best_weights = weights[slice_center:]
291
+ good_weights = weights[slice_center:]
246
292
  bad_weights = weights[:slice_center]
247
- best_weight = best_weights[len(best_weights)-1]
293
+ best_weights = good_weights[-1]
248
294
 
249
- best_activations = list(activation_potentiations[slice_center:])
295
+ good_activations = list(activation_potentiations[slice_center:])
250
296
  bad_activations = list(activation_potentiations[:slice_center])
251
- best_activation = best_activations[len(best_activations) - 1]
297
+ best_activations = good_activations[-1]
252
298
 
253
299
 
254
- ### NEAT IS APPLIED ACCORDING TO THE SPECIFIED POLICY, STRATEGY, AND PROBABILITY CONFIGURATION:
300
+ ### PLANEAT IS APPLIED ACCORDING TO THE SPECIFIED POLICY, STRATEGY, AND PROBABILITY CONFIGURATION:
255
301
 
256
302
  bar_format = loading_bars()[0]
257
303
 
258
304
  if bar_status: progress = initialize_loading_bar(len(bad_weights), desc="GENERATION: " + str(what_gen), bar_format=bar_format, ncols=50)
305
+ normalized_fitness = abs(normalization(fitness, dtype=dtype))
259
306
 
260
- for i in range(len(bad_weights)):
261
-
262
- if policy == 'normal_selective':
263
-
264
- if strategy == 'cross_over':
265
- bad_weights[i], bad_activations[i] = cross_over(best_weight, best_weights[i], best_activations=best_activation, good_activations=best_activations[i], cross_over_mode=cross_over_mode, activation_selection_add_prob=activation_selection_add_prob, activation_selection_change_prob=activation_selection_change_prob, activation_selection_rate=activation_selection_rate)
266
-
267
-
268
- elif strategy == 'potentiate':
269
- bad_weights[i], bad_activations[i] = potentiate(best_weight, best_weights[i], best_activations=best_activation, good_activations=best_activations[i], dtype=dtype)
270
-
271
-
272
- if mutations is True:
273
-
274
- mutation_prob = random.uniform(0, 1)
275
-
276
- if mutation_prob > bad_genoms_mutation_prob:
277
- if (save_best_genom == True and not cp.array_equal(best_weights[i], best_weight)) or save_best_genom == False:
278
- best_weights[i], best_activations[i] = mutation(best_weights[i], best_activations[i], activation_mutate_prob=activation_mutate_prob, activation_add_prob=activation_add_prob, activation_delete_prob=activation_delete_prob, activation_change_prob=activation_change_prob, weight_mutate_prob=weight_mutate_prob, threshold=weight_mutate_rate, dtype=dtype)
279
-
280
- elif mutation_prob < bad_genoms_mutation_prob:
281
- bad_weights[i], bad_activations[i] = mutation(bad_weights[i], bad_activations[i], activation_mutate_prob=activation_mutate_prob, activation_add_prob=activation_add_prob, activation_delete_prob=activation_delete_prob, activation_change_prob=activation_change_prob, weight_mutate_prob=weight_mutate_prob, threshold=weight_mutate_rate, dtype=dtype)
282
-
283
- if policy == 'more_selective':
284
-
285
- if strategy == 'cross_over':
286
- bad_weights[i], bad_activations[i] = cross_over(best_weight, best_weights[i], best_activations=best_activation, good_activations=best_activations[i], cross_over_mode=cross_over_mode, activation_selection_add_prob=activation_selection_add_prob, activation_selection_change_prob=activation_selection_change_prob, activation_selection_rate=activation_selection_rate)
287
-
288
- elif strategy == 'potentiate':
289
- bad_weights[i], bad_activations[i] = potentiate(best_weight, best_weights[i], best_activations=best_activation, good_activations=best_activations[i], dtype=dtype)
290
-
291
- if mutations is True:
292
-
293
- mutation_prob = random.uniform(0, 1)
294
-
295
- if mutation_prob > bad_genoms_mutation_prob:
296
- if (save_best_genom == True and not cp.array_equal(best_weights[i], best_weight)) or save_best_genom == False:
297
- best_weights[i], best_activations[i] = mutation(best_weights[i], best_activations[i], activation_mutate_prob=activation_mutate_prob, activation_add_prob=activation_add_prob, activation_delete_prob=activation_delete_prob, activation_change_prob=activation_change_prob, weight_mutate_prob=weight_mutate_prob, threshold=weight_mutate_rate, dtype=dtype)
298
-
299
- elif mutation_prob < bad_genoms_mutation_prob:
300
- bad_weights[i], bad_activations[i] = mutation(bad_weights[i], bad_activations[i], activation_mutate_prob=activation_mutate_prob, activation_add_prob=activation_add_prob, activation_delete_prob=activation_delete_prob, activation_change_prob=activation_change_prob, weight_mutate_prob=weight_mutate_prob, threshold=weight_mutate_rate, dtype=dtype)
301
-
302
-
307
+ best_fitness = normalized_fitness[-1]
303
308
 
304
- if policy == 'less_selective':
305
-
306
- random_index = int(random.uniform(0, len(best_weights) - 1))
307
-
308
- if strategy == 'cross_over':
309
- bad_weights[i], bad_activations[i] = cross_over(best_weights[random_index], best_weights[i], best_activations=best_activations[random_index], good_activations=best_activations[i], cross_over_mode=cross_over_mode, activation_selection_add_prob=activation_selection_add_prob, activation_selection_change_prob=activation_selection_change_prob, activation_selection_rate=activation_selection_rate)
309
+ for i in range(len(bad_weights)):
310
+ second_parent_W, second_parent_act, s_i = second_parent_selection(good_weights, bad_weights, good_activations, bad_activations, bad_genomes_selection_prob)
311
+
312
+ if policy == 'aggresive':
313
+ bad_weights[i], bad_activations[i] = cross_over(best_weights,
314
+ second_parent_W,
315
+ best_activations,
316
+ second_parent_act,
317
+ cross_over_mode=cross_over_mode,
318
+ activation_selection_add_prob=activation_selection_add_prob,
319
+ activation_selection_change_prob=activation_selection_change_prob,
320
+ activation_selection_rate=activation_selection_rate,
321
+ bad_genomes_selection_prob=bad_genomes_selection_prob,
322
+ first_parent_fitness=best_fitness,
323
+ fitness_bias=fitness_bias,
324
+ second_parent_fitness=normalized_fitness[s_i]
325
+ )
326
+ elif policy == 'explorer':
327
+ bad_weights[i], bad_activations[i] = cross_over(good_weights[i],
328
+ second_parent_W,
329
+ good_activations[i],
330
+ second_parent_act,
331
+ cross_over_mode=cross_over_mode,
332
+ activation_selection_add_prob=activation_selection_add_prob,
333
+ activation_selection_change_prob=activation_selection_change_prob,
334
+ activation_selection_rate=activation_selection_rate,
335
+ bad_genomes_selection_prob=bad_genomes_selection_prob,
336
+ first_parent_fitness=normalized_fitness[i],
337
+ fitness_bias=fitness_bias,
338
+ second_parent_fitness=normalized_fitness[s_i]
339
+ )
340
+
341
+ else: raise ValueError("policy parameter must be: 'aggresive' or 'explorer'")
342
+
343
+ if mutations is True:
344
+ mutation_prob = random.uniform(0, 1)
345
+
346
+ if mutation_prob > bad_genomes_mutation_prob:
347
+ if (save_best_genom == True and not np.array_equal(good_weights[i], best_weights)) or save_best_genom == False:
348
+ good_weights[i], good_activations[i] = mutation(good_weights[i],
349
+ good_activations[i],
350
+ activation_mutate_prob=activation_mutate_prob,
351
+ activation_add_prob=activation_mutate_add_prob,
352
+ activation_delete_prob=activation_mutate_delete_prob,
353
+ activation_change_prob=activation_mutate_change_prob,
354
+ weight_mutate_prob=weight_mutate_prob,
355
+ threshold=weight_mutate_rate,
356
+ genome_fitness=normalized_fitness[i]
357
+ )
310
358
 
311
- elif strategy == 'potentiate':
312
- bad_weights[i], bad_activations[i] = potentiate(best_weights[random_index], best_weights[i], best_activations=best_activations[random_index], good_activations=best_activations[i], dtype=dtype)
359
+ elif mutation_prob < bad_genomes_mutation_prob:
360
+ bad_weights[i], bad_activations[i] = mutation(bad_weights[i],
361
+ bad_activations[i],
362
+ activation_mutate_prob=activation_mutate_prob,
363
+ activation_add_prob=activation_mutate_add_prob,
364
+ activation_delete_prob=activation_mutate_delete_prob,
365
+ activation_change_prob=activation_mutate_change_prob,
366
+ weight_mutate_prob=weight_mutate_prob,
367
+ threshold=weight_mutate_rate,
368
+ genome_fitness=normalized_fitness[i]
369
+ )
313
370
 
314
- if mutations is True:
315
-
316
- mutation_prob = random.uniform(0, 1)
317
-
318
- if mutation_prob > bad_genoms_mutation_prob:
319
- if (save_best_genom == True and not cp.array_equal(best_weights[i], best_weight)) or save_best_genom == False:
320
- best_weights[i], best_activations[i] = mutation(best_weights[i], best_activations[i], activation_mutate_prob=activation_mutate_prob, activation_add_prob=activation_add_prob, activation_delete_prob=activation_delete_prob, activation_change_prob=activation_change_prob, weight_mutate_prob=weight_mutate_prob, threshold=weight_mutate_rate, dtype=dtype)
321
-
322
- elif mutation_prob < bad_genoms_mutation_prob:
323
- bad_weights[i], bad_activations[i] = mutation(bad_weights[i], bad_activations[i], activation_mutate_prob=activation_mutate_prob, activation_add_prob=activation_add_prob, activation_delete_prob=activation_delete_prob, activation_change_prob=activation_change_prob, weight_mutate_prob=weight_mutate_prob, threshold=weight_mutate_rate, dtype=dtype)
324
-
325
371
  if bar_status: progress.update(1)
326
372
 
327
- weights = cp.vstack((bad_weights, best_weights))
328
- activation_potentiations = bad_activations + best_activations
373
+ weights = cp.vstack((bad_weights, good_weights))
374
+ activation_potentiations = bad_activations + good_activations
329
375
 
330
376
  ### INFO PRINTING CONSOLE
331
377
 
@@ -334,26 +380,24 @@ Example:
334
380
  print("*** Configuration Settings ***")
335
381
  print(" POPULATION SIZE: ", str(len(weights)))
336
382
  print(" STRATEGY: ", strategy)
337
-
338
- if strategy == 'cross_over':
339
- print(" CROSS OVER MODE: ", cross_over_mode)
340
-
383
+ print(" CROSS OVER MODE: ", cross_over_mode)
341
384
  print(" POLICY: ", policy)
342
385
  print(" MUTATIONS: ", str(mutations))
343
- print(" BAD GENOMES MUTATION PROB: ", str(bad_genoms_mutation_prob))
344
- print(" GOOD GENOMES MUTATION PROB: ", str(round(1 - bad_genoms_mutation_prob, 2)))
386
+ print(" BAD GENOMES MUTATION PROB: ", str(bad_genomes_mutation_prob))
387
+ print(" GOOD GENOMES MUTATION PROB: ", str(round(1 - bad_genomes_mutation_prob, 2)))
345
388
  print(" WEIGHT MUTATE PROB: ", str(weight_mutate_prob))
346
389
  print(" WEIGHT MUTATE RATE (THRESHOLD VALUE FOR SINGLE MUTATION): ", str(weight_mutate_rate))
347
390
  print(" ACTIVATION MUTATE PROB: ", str(activation_mutate_prob))
348
- print(" ACTIVATION ADD PROB: ", str(activation_add_prob))
349
- print(" ACTIVATION DELETE PROB: ", str(activation_delete_prob))
350
- print(" ACTIVATION CHANGE PROB: ", str(activation_change_prob))
391
+ print(" ACTIVATION MUTATE ADD PROB: ", str(activation_mutate_add_prob))
392
+ print(" ACTIVATION MUTATE DELETE PROB: ", str(activation_mutate_delete_prob))
393
+ print(" ACTIVATION MUTATE CHANGE PROB: ", str(activation_mutate_change_prob))
351
394
  print(" ACTIVATION SELECTION ADD PROB: ", str(activation_selection_add_prob))
352
395
  print(" ACTIVATION SELECTION CHANGE PROB: ", str(activation_selection_change_prob))
396
+ print(" FITNESS BIAS: ", str(fitness_bias))
353
397
  print(" ACTIVATION SELECTION RATE (THRESHOLD VALUE FOR SINGLE CROSS OVER):", str(activation_selection_rate) + '\n')
354
-
398
+
355
399
  print("*** Performance ***")
356
- print(" MAX REWARD: ", str(cp.round(max(fitness), 2)))
400
+ print(" MAX REWARD: ", str(cp.round(max(fitness), 2)))
357
401
  print(" MEAN REWARD: ", str(cp.round(cp.mean(fitness), 2)))
358
402
  print(" MIN REWARD: ", str(cp.round(min(fitness), 2)) + '\n')
359
403
 
@@ -361,7 +405,7 @@ Example:
361
405
  print(" NOTE: Genomes are always sorted from the least successful to the most successful according to their performance ranking. Therefore, the genome at the last index is the king of the previous generation. " + '\n')
362
406
 
363
407
 
364
- return cp.array(weights), activation_potentiations
408
+ return weights, activation_potentiations
365
409
 
366
410
 
367
411
  def evaluate(x_population, weights, activation_potentiations, rl_mode=False, dtype=cp.float32):
@@ -370,14 +414,17 @@ def evaluate(x_population, weights, activation_potentiations, rl_mode=False, dty
370
414
  and weights depending on whether reinforcement learning mode is enabled or not.
371
415
 
372
416
  Args:
373
- x_population (list or numpy.ndarray): A list or 2D numpy array where each element represents
417
+ x_population (list or cupy.ndarray): A list or 2D numpy or cupy array where each element represents
374
418
  a genome (A list of input features for each genome, or a single set of input features for one genome (only in rl_mode)).
375
- weights (list or numpy.ndarray): A list or 2D numpy array of weights corresponding to each genome
419
+
420
+ weights (list or cupy.ndarray): A list or 2D numpy array of weights corresponding to each genome
376
421
  in `x_population`. This determines the strength of connections.
422
+
377
423
  activation_potentiations (list or str): A list where each entry represents an activation function
378
424
  or a potentiation strategy applied to each genome. If only one
379
425
  activation function is used, this can be a single string.
380
- rl_mode (bool, optional): If True, reinforcement learning mode is activated, this accepts x_population is a single genom. (Also weights and activation_potentations a single genomes part.)
426
+
427
+ rl_mode (bool, optional): If True, reinforcement learning mode is activated, this accepts x_population is a single genome. (Also weights and activation_potentations a single genomes part.)
381
428
  Default is False.
382
429
 
383
430
 
@@ -436,48 +483,108 @@ def evaluate(x_population, weights, activation_potentiations, rl_mode=False, dty
436
483
  return outputs
437
484
 
438
485
 
439
- def cross_over(best_weight, good_weight, best_activations, good_activations, cross_over_mode, activation_selection_add_prob, activation_selection_change_prob, activation_selection_rate):
486
+ def cross_over(first_parent_W,
487
+ second_parent_W,
488
+ first_parent_act,
489
+ second_parent_act,
490
+ cross_over_mode,
491
+ activation_selection_add_prob,
492
+ activation_selection_change_prob,
493
+ activation_selection_rate,
494
+ bad_genomes_selection_prob,
495
+ first_parent_fitness,
496
+ second_parent_fitness,
497
+ fitness_bias):
440
498
  """
441
- Performs a selected Crossover operation on two sets of weights and activation functions.
499
+ Performs a crossover operation on two sets of weights and activation functions.
442
500
  This function combines two individuals (represented by their weights and activation functions)
443
501
  to create a new individual by exchanging parts of their weight matrices and activation functions.
444
502
 
445
503
  Args:
446
- best_weight (numpy.ndarray): The weight matrix of the first individual (parent).
447
- good_weight (numpy.ndarray): The weight matrix of the second individual (parent).
448
- best_activations (str or list): The activation function(s) of the first individual.
449
- good_activations (str or list): The activation function(s) of the second individual.
504
+ first_parent_W (cupy.ndarray): The weight matrix of the first individual (parent).
505
+
506
+ second_parent_W (numpy.ndarray): The weight matrix of the second individual (parent).
507
+
508
+ first_parent_act (str or list): The activation function(s) of the first individual.
509
+
510
+ second_parent_act (str or list): The activation function(s) of the second individual.
511
+
450
512
  cross_over_mode (str): Determines the crossover method to be used. Options:
451
- - 'tpm': Two-Point Matrix Crossover, where sub-matrices of weights
452
- are swapped between parents.
453
- - 'plan': Output Connections Crossover, where specific connections
454
- in the weight matrix are crossed over. Default is 'tpm'.
513
+ - 'tpm': Two-Point Matrix Crossover, where sub-matrices of weights are swapped between parents.
514
+
515
+ activation_selection_add_prob (float): Probability of adding new activation functions
516
+ from the second parent to the child genome.
517
+
518
+ activation_selection_change_prob (float): Probability of replacing an activation function in the child genome
519
+ with one from the second parent.
520
+
521
+ activation_selection_rate (float): Determines how quickly activation functions are added or replaced
522
+ during the crossover process.
523
+
524
+ bad_genomes_selection_prob (float): Probability of selecting a "bad" genome for replacement with the offspring.
525
+
526
+ first_parent_fitness (float): Fitness score of the first parent.
527
+
528
+ second_parent_fitness (float): Fitness score of the second parent.
529
+
530
+ fitness_bias (float): A bias factor used to favor fitter parents during crossover operations.
455
531
 
456
532
  Returns:
457
533
  tuple: A tuple containing:
458
- - new_weight (numpy.ndarray): The weight matrix of the new individual created by crossover.
459
- - new_activations (list): The list of activation functions of the new individual created by crossover.
534
+ - child_W (numpy.ndarray): The weight matrix of the new individual created by crossover.
535
+ - child_act (list): The list of activation functions of the new individual created by crossover.
460
536
 
461
537
  Notes:
462
538
  - The crossover is performed based on the selected `cross_over_mode`.
463
- - In 'tpm', random sub-matrices from the parent weight matrices are swapped.
464
- - In 'plantic', specific connections in the weight matrix are swapped between parents.
465
- - The crossover operation combines the activation functions of both parents:
466
- - If the activation functions are passed as strings, they are converted to lists for uniform handling.
467
- - The resulting activation functions depend on the crossover method and the parent's configuration.
539
+ - In 'tpm' mode, random sub-matrices from the parent weight matrices are swapped.
540
+ - Activation functions from both parents are combined using the probabilities and rates provided.
468
541
 
469
542
  Example:
470
543
  ```python
471
- new_weights, new_activations = cross_over(best_weight, good_weight, best_activations, good_activations, cross_over_mode='tpm')
544
+ new_weights, new_activations = cross_over(
545
+ first_parent_W=parent1_weights,
546
+ second_parent_W=parent2_weights,
547
+ first_parent_act=parent1_activations,
548
+ second_parent_act=parent2_activations,
549
+ cross_over_mode='tpm',
550
+ activation_selection_add_prob=0.8,
551
+ activation_selection_change_prob=0.5,
552
+ activation_selection_rate=0.1,
553
+ bad_genomes_selection_prob=0.7,
554
+ first_parent_fitness=0.9,
555
+ second_parent_fitness=0.85,
556
+ fitness_bias=0.6
557
+ )
472
558
  ```
473
559
  """
474
560
 
475
- ### THE GIVEN GENOMES' WEIGHTS ARE RANDOMLY SELECTED AND COMBINED OVER A RANDOM RANGE. SIMILARLY, THEIR ACTIVATIONS ARE COMBINED. A NEW GENOME IS RETURNED WITH THE COMBINED WEIGHTS FIRST, FOLLOWED BY THE ACTIVATIONS:
561
+ ### THE GIVEN GENOMES' WEIGHTS ARE RANDOMLY SELECTED AND COMBINED OVER A RANDOM RANGE. SIMILARLY, THEIR ACTIVATIONS ARE COMBINED. A NEW GENOME IS RETURNED WITH THE COMBINED WEIGHTS FIRST, FOLLOWED BY THE ACTIVATIONS:
476
562
 
477
563
  start = 0
564
+
565
+ row_end = first_parent_W.shape[0]
566
+ col_end = first_parent_W.shape[1]
567
+
568
+ total_gene = row_end * col_end
569
+ half_of_gene = int(total_gene / 2)
570
+
571
+ decision = dominant_parent_selection(bad_genomes_selection_prob)
478
572
 
479
- row_end = best_weight.shape[0]
480
- col_end = best_weight.shape[1]
573
+ if decision == 'first_parent':
574
+ dominant_parent_W = cp.copy(first_parent_W)
575
+ dominant_parent_act = first_parent_act
576
+
577
+ undominant_parent_W = cp.copy(second_parent_W)
578
+ undominant_parent_act = second_parent_act
579
+ succes = second_parent_fitness
580
+
581
+ elif decision == 'second_parent':
582
+ dominant_parent_W = cp.copy(second_parent_W)
583
+ dominant_parent_act = second_parent_act
584
+
585
+ undominant_parent_W = cp.copy(first_parent_W)
586
+ undominant_parent_act = first_parent_act
587
+ succes = first_parent_fitness
481
588
 
482
589
  while True:
483
590
 
@@ -487,48 +594,47 @@ def cross_over(best_weight, good_weight, best_activations, good_activations, cro
487
594
  row_cut_end = int(random.uniform(start, row_end))
488
595
  col_cut_end = int(random.uniform(start, col_end))
489
596
 
490
- if (row_cut_end > row_cut_start) and (col_cut_end > col_cut_start):
597
+ if ((row_cut_end > row_cut_start) and
598
+ (col_cut_end > col_cut_start) and
599
+ (((row_cut_end + 1) - (row_cut_start + 1) * 2) + ((col_cut_end + 1) - (col_cut_start + 1) * 2) <= half_of_gene)):
491
600
  break
601
+
602
+ selection_bias = random.uniform(0, 1)
492
603
 
493
- new_weight = cp.copy(best_weight)
494
- best_w2 = cp.copy(good_weight)
495
-
496
- if cross_over_mode == 'tpm':
497
- new_weight[row_cut_start:row_cut_end, col_cut_start:col_cut_end] = best_w2[row_cut_start:row_cut_end, col_cut_start:col_cut_end]
498
-
499
- elif cross_over_mode == 'plantic':
500
- new_weight[row_cut_start:row_cut_end,:] = best_w2[row_cut_start:row_cut_end,:]
604
+ if fitness_bias > selection_bias:
605
+ row_cut_start = math.floor(row_cut_start * succes)
606
+ row_cut_end = math.ceil(row_cut_end * succes)
501
607
 
608
+ col_cut_start = math.floor(col_cut_start * succes)
609
+ col_cut_end = math.ceil(col_cut_end * succes)
502
610
 
503
- if isinstance(best_activations, str):
504
- best = [best_activations]
611
+ child_W = dominant_parent_W
505
612
 
506
- if isinstance(good_activations, str):
507
- good = [good_activations]
613
+ if cross_over_mode == 'tpm':
614
+ child_W[row_cut_start:row_cut_end, col_cut_start:col_cut_end] = undominant_parent_W[row_cut_start:row_cut_end, col_cut_start:col_cut_end]
508
615
 
509
- if isinstance(best_activations, list):
510
- best = best_activations
511
616
 
512
- if isinstance(good_activations, list):
513
- good = good_activations
617
+ if isinstance(dominant_parent_act, str): dominant_parent_act = [dominant_parent_act]
618
+ if isinstance(undominant_parent_act, str): undominant_parent_act = [undominant_parent_act]
514
619
 
515
- new_activations = list(np.copy(best))
620
+ child_act = list(np.copy(dominant_parent_act))
516
621
 
517
622
  activation_selection_add_prob = 1 - activation_selection_add_prob # if prob 0.8 (%80) then 1 - 0.8. Because 0-1 random number probably greater than 0.2
518
623
  potential_activation_selection_add = random.uniform(0, 1)
519
624
 
520
625
  if potential_activation_selection_add > activation_selection_add_prob:
521
626
 
627
+ activation_selection_rate = activation_selection_rate / succes
522
628
  new_threshold = activation_selection_rate
523
629
 
524
630
  while True:
525
631
 
526
- random_index_good = int(random.uniform(0, len(good)-1))
527
- random_good_activation = good[random_index_good]
632
+ random_index = int(random.uniform(0, len(undominant_parent_act)-1))
633
+ random_undominant_activation = undominant_parent_act[random_index]
528
634
 
529
- new_activations.append(random_good_activation)
635
+ child_act.append(random_undominant_activation)
530
636
 
531
- if len(best) > new_threshold:
637
+ if len(dominant_parent_act) > new_threshold:
532
638
  new_threshold += activation_selection_rate
533
639
  pass
534
640
 
@@ -539,85 +645,61 @@ def cross_over(best_weight, good_weight, best_activations, good_activations, cro
539
645
  potential_activation_selection_change_prob = random.uniform(0, 1)
540
646
 
541
647
  if potential_activation_selection_change_prob > activation_selection_change_prob:
542
-
648
+
649
+ activation_selection_rate = activation_selection_rate / succes
543
650
  new_threshold = activation_selection_rate
544
651
 
545
652
  while True:
546
653
 
547
- random_index_good = int(random.uniform(0, len(good)-1))
548
- random_index_best = int(random.uniform(0, len(best)-1))
549
- random_good_activation = good[random_index_good]
654
+ random_index_undominant = int(random.uniform(0, len(undominant_parent_act)-1))
655
+ random_index_dominant = int(random.uniform(0, len(dominant_parent_act)-1))
656
+ random_undominant_activation = undominant_parent_act[random_index_undominant]
550
657
 
551
- new_activations[random_index_best] = good[random_index_good]
658
+ child_act[random_index_dominant] = random_undominant_activation
552
659
 
553
- if len(best) > new_threshold:
660
+ if len(dominant_parent_act) > new_threshold:
554
661
  new_threshold += activation_selection_rate
555
662
  pass
556
663
 
557
664
  else:
558
665
  break
559
666
 
560
- return new_weight, new_activations
667
+ return child_W, child_act
561
668
 
562
- def potentiate(best_weight, good_weight, best_activations, good_activations, dtype=cp.float32):
563
- """
564
- Combines two sets of weights and activation functions by adding the weight matrices and
565
- concatenating the activation functions. The resulting weight matrix is normalized. (Max abs normalization.)
566
-
567
- Args:
568
- best_weight (numpy.ndarray): The weight matrix of the first individual (parent).
569
- good_weight (numpy.ndarray): The weight matrix of the second individual (parent).
570
- best_activations (str or list): The activation function(s) of the first individual.
571
- good_activations (str or list): The activation function(s) of the second individual.
572
- dtype (cupy.dtype): Data type for the arrays. np.float32 by default. Example: cp.float64 or cp.float16. [fp32 for balanced devices, fp64 for strong devices, fp16 for weak devices: not reccomended!] (optional)
573
-
574
- Returns:
575
- tuple: A tuple containing:
576
- - new_weight (numpy.ndarray): The new weight matrix after potentiation and normalization. (Max abs normalization.)
577
- - new_activations (list): The new activation functions after concatenation.
578
-
579
- Notes:
580
- - The weight matrices are element-wise added and then normalized using the `normalization` function. (Max abs normalization.)
581
- - The activation functions from both parents are concatenated to form the new activation functions list.
582
- - If the activation functions are passed as strings, they are converted to lists for uniform handling.
583
- """
584
-
585
- new_weight = best_weight + good_weight
586
- new_weight = normalization(new_weight, dtype=dtype)
587
-
588
- if isinstance(best_activations, str):
589
- best = [best_activations]
590
-
591
- if isinstance(good_activations, str):
592
- good = [good_activations]
593
-
594
- if isinstance(best_activations, list):
595
- best = best_activations
596
-
597
- if isinstance(good_activations, list):
598
- good = good_activations
599
-
600
- new_activations = best + good
601
-
602
- return new_weight, new_activations
603
669
 
604
- def mutation(weight, activations, activation_mutate_prob, activation_add_prob, activation_delete_prob, activation_change_prob, weight_mutate_prob, threshold, dtype=cp.float32):
670
+ def mutation(weight,
671
+ activations,
672
+ activation_mutate_prob,
673
+ activation_add_prob,
674
+ activation_delete_prob,
675
+ activation_change_prob,
676
+ weight_mutate_prob,
677
+ threshold,
678
+ genome_fitness):
605
679
  """
606
680
  Performs mutation on the given weight matrix and activation functions.
607
681
  - The weight matrix is mutated by randomly changing its values based on the mutation probability.
608
682
  - The activation functions are mutated by adding, removing, or replacing them with predefined probabilities.
609
683
 
610
684
  Args:
611
- weight (numpy.ndarray): The weight matrix to mutate.
685
+ weight (cupy.ndarray): The weight matrix to mutate.
686
+
612
687
  activations (list): The list of activation functions to mutate.
688
+
613
689
  activation_mutate_prob (float): The overall probability of mutating activation functions.
690
+
614
691
  activation_add_prob (float): Probability of adding a new activation function.
692
+
615
693
  activation_delete_prob (float): Probability of removing an existing activation function.
694
+
616
695
  activation_change_prob (float): Probability of replacing an existing activation function with a new one.
696
+
617
697
  weight_mutate_prob (float): The probability of mutating weight matrix.
698
+
618
699
  threshold (float): If the value you enter here is equal to the result of input layer * output layer, only a single weight will be mutated during each mutation process. If the value you enter here is half of the result of input layer * output layer, two weights in the weight matrix will be mutated.
619
- dtype (cupy.dtype): Data type for the arrays. np.float32 by default. Example: cp.float64 or cp.float16. [fp32 for balanced devices, fp64 for strong devices, fp16 for weak devices: not reccomended!] (optional)
620
700
 
701
+ genome_fitness (float): Fitness value of genome
702
+
621
703
  Returns:
622
704
  tuple: A tuple containing:
623
705
  - mutated_weight (numpy.ndarray): The weight matrix after mutation.
@@ -635,8 +717,7 @@ def mutation(weight, activations, activation_mutate_prob, activation_add_prob, a
635
717
  the optimization process.
636
718
  """
637
719
 
638
- if isinstance(activations, str):
639
- activations = [activations]
720
+ if isinstance(activations, str): activations = [activations]
640
721
 
641
722
  weight_mutate_prob = 1 - weight_mutate_prob # if prob 0.8 (%80) then 1 - 0.8. Because 0-1 random number probably greater than 0.2
642
723
  potential_weight_mutation = random.uniform(0, 1)
@@ -646,6 +727,8 @@ def mutation(weight, activations, activation_mutate_prob, activation_add_prob, a
646
727
  start = 0
647
728
  row_end = weight.shape[0]
648
729
  col_end = weight.shape[1]
730
+
731
+ threshold = threshold * genome_fitness
649
732
  new_threshold = threshold
650
733
 
651
734
  while True:
@@ -662,7 +745,6 @@ def mutation(weight, activations, activation_mutate_prob, activation_add_prob, a
662
745
  else:
663
746
  break
664
747
 
665
-
666
748
  activation_mutate_prob = 1 - activation_mutate_prob
667
749
  potential_activation_mutation = random.uniform(0, 1)
668
750
 
@@ -685,13 +767,7 @@ def mutation(weight, activations, activation_mutate_prob, activation_add_prob, a
685
767
 
686
768
  random_index_all_act = int(random.uniform(0, len(all_acts)-1))
687
769
  activations.append(all_acts[random_index_all_act])
688
-
689
- for i in range(weight.shape[0]):
690
-
691
- weight[i,:] = apply_activation(weight[i,:], activations[-1])
692
770
 
693
- weight = normalization(weight, dtype=dtype)
694
-
695
771
  except:
696
772
 
697
773
  activation = activations
@@ -700,26 +776,12 @@ def mutation(weight, activations, activation_mutate_prob, activation_add_prob, a
700
776
  activations.append(activation)
701
777
  activations.append(all_acts[int(random.uniform(0, len(all_acts)-1))])
702
778
 
703
- for i in range(weight.shape[0]):
704
-
705
- weight[i,:] = apply_activation(weight[i,:], activations[-1])
706
-
707
- weight = normalization(weight, dtype=dtype)
708
-
709
779
  if potential_activation_delete_prob > activation_delete_prob and len(activations) > 1:
710
780
 
711
781
  random_index = random.randint(0, len(activations) - 1)
712
-
713
- wc = cp.copy(weight)
714
- for i in range(weight.shape[0]):
715
-
716
- wc[i,:] = apply_activation(wc[i,:], activations[random_index])
717
- weight[i,:] -= wc[i,:]
718
-
719
782
  activations.pop(random_index)
720
- weight = normalization(weight, dtype=dtype)
721
783
 
722
-
784
+
723
785
  if potential_activation_change_prob > activation_change_prob:
724
786
 
725
787
  random_index_all_act = int(random.uniform(0, len(all_acts)-1))
@@ -727,18 +789,30 @@ def mutation(weight, activations, activation_mutate_prob, activation_add_prob, a
727
789
 
728
790
  activations[random_index_genom_act] = all_acts[random_index_all_act]
729
791
 
730
- wc = cp.copy(weight)
731
- for i in range(weight.shape[0]):
792
+ return weight, activations
793
+
732
794
 
733
- wc[i,:] = apply_activation(wc[i,:], activations[random_index_genom_act])
734
- weight[i,:] -= wc[i,:]
795
+ def second_parent_selection(good_weights, bad_weights, good_activations, bad_activations, bad_genomes_selection_prob):
796
+
797
+ selection_prob = random.uniform(0, 1)
798
+ random_index = int(random.uniform(0, len(good_weights) - 1))
799
+
800
+ if selection_prob > bad_genomes_selection_prob:
801
+ second_selected_W = good_weights[random_index]
802
+ second_selected_act = good_activations[random_index]
735
803
 
736
- weight = normalization(weight, dtype=dtype)
804
+ else:
805
+ second_selected_W = bad_weights[random_index]
806
+ second_selected_act = bad_activations[random_index]
807
+
808
+ return second_selected_W, second_selected_act, random_index
809
+
737
810
 
738
- for i in range(weight.shape[0]):
811
+ def dominant_parent_selection(bad_genomes_selection_prob):
739
812
 
740
- weight[i,:] = apply_activation(weight[i,:], activations[random_index_genom_act])
813
+ selection_prob = random.uniform(0, 1)
741
814
 
742
- weight = normalization(weight, dtype=dtype)
815
+ if selection_prob > bad_genomes_selection_prob: decision = 'first_parent'
816
+ else: decision = 'second_parent'
743
817
 
744
- return weight, activations
818
+ return decision