Evolutionary_Algorithm

src.Evolutionary_Algorithm.create_first_population(population, num_classes=5)[source]

Generates the initial set of models for a genetic algorithm.

Parameters

population: int

The quantity of models to create.

num_classes: int, optional

The number of output classes in the model. Defaults to 5.

Returns

first_population_arraynp.ndarray

A 3D numpy array representing the initial population of models.

src.Evolutionary_Algorithm.create_next_population(parent_arrays, population=20, num_classes=5)[source]

Create the next generation of model arrays by performing crossover and mutation operations.

Parameters:

parent_arrayslist of np.ndarray

A list of parent arrays.

populationint, optional

The size of the population to be generated. Defaults to 20.

num_classesint, optional

The number of classes for the model. Defaults to 5.

Returns:

next_population_arraynp.ndarray

The next generation of model arrays.

src.Evolutionary_Algorithm.crossover(parent_arrays)[source]

Perform a crossover operation on a list of parent arrays to generate a child array.

Parameters:

parent_arrayslist of np.ndarray

A list of parent arrays.

Returns:

child_arraynp.ndarray

A child array that is a combination of the parent arrays.

src.Evolutionary_Algorithm.mutate(model_array, mutate_prob=0.05)[source]

Perform a mutation operation on a given model array.

Parameters:

model_arraynp.ndarray

The model array to be mutated.

mutate_probfloat, optional

The probability of mutation for each element in the array. Defaults to 0.05.

Returns:

mutated_arraynp.ndarray

The mutated model array.

src.Evolutionary_Algorithm.select_models(train_ds, val_ds, test_ds, time, population_array, generation, epochs=30, num_classes=5)[source]

Trains, evaluates, and selects the top performing models from a population based on their fitness scores.

Parameters

train_ds: tf.data.Dataset

The dataset used for training the models.

val_ds: tf.data.Dataset

The dataset used for validating the models during training.

test_ds: tf.data.Dataset

The dataset used for testing the trained models.

time: datetime or str

A timestamp utilized in directory names for saving results.

population_array: np.ndarray

A 3D numpy array representing the population of models.

generation: int

The generation number of the models, used in directory names for saving results.

epochs: int, optional

The number of epochs to train each model. Defaults to 30.

num_classes: int, optional

The number of output classes in the model. Defaults to 5.

Returns

best_models_arrays, max_fitness, average_fitnesstuple

A tuple containing a list of the best model arrays, the maximum fitness, and the average fitness of the population.

src.Evolutionary_Algorithm.start_evolution(train_ds, val_ds, test_ds, generations, population, num_classes, epochs, population_array=None, time=None)[source]

Start the evolutionary process for model optimization.

Parameters:

train_dstensorflow Dataset

The training dataset.

val_dstensorflow Dataset

The validation dataset.

test_dstensorflow Dataset

The testing dataset.

generationsint

The number of generations to evolve through.

populationint

The size of the population in each generation.

num_classesint

The number of classes in the target variable.

epochsint

The number of epochs to train each model.

population_arraynp.ndarray, optional

The initial population array.

timedatetime or str, optional

The timestamp to append to the result directory name.

Returns:

population_array, max_fitness_history, average_fitness_history, best_models_arraystuple

The final population array, the history of maximum fitness score, the history of average fitness score, and the best model arrays.