### PyGAD Regression Example Setup Source: https://pygad.readthedocs.io/en/latest/_sources/gann Sets up a complete example for training a neural network for regression using PyGAD. It includes importing necessary libraries, defining the fitness function, and a callback for generation updates. ```python import numpy import pygad import pygad.nn import pygad.gann def fitness_func(ga_instance, solution, sol_idx): global GANN_instance, data_inputs, data_outputs predictions = pygad.nn.predict(last_layer=GANN_instance.population_networks[sol_idx], data_inputs=data_inputs, problem_type="regression") solution_fitness = 1.0/numpy.mean(numpy.abs(predictions - data_outputs)) return solution_fitness def callback_generation(ga_instance): global GANN_instance, last_fitness ``` -------------------------------- ### XOR Classification Example Setup Source: https://pygad.readthedocs.io/en/latest/_sources/gann Sets up the XOR classification problem using PyGAD, including defining the fitness function, callback for generation updates, and preparing input/output data. This serves as a complete example for building and training a neural network with PyGAD. ```python import numpy import pygad import pygad.nn import pygad.gann def fitness_func(ga_instance, solution, sol_idx): global GANN_instance, data_inputs, data_outputs # If adaptive mutation is used, sometimes sol_idx is None. if sol_idx == None: sol_idx = 1 predictions = pygad.nn.predict(last_layer=GANN_instance.population_networks[sol_idx], data_inputs=data_inputs) correct_predictions = numpy.where(predictions == data_outputs)[0].size solution_fitness = (correct_predictions/data_outputs.size)*100 return solution_fitness def callback_generation(ga_instance): global GANN_instance, last_fitness population_matrices = pygad.gann.population_as_matrices(population_networks=GANN_instance.population_networks, population_vectors=ga_instance.population) GANN_instance.update_population_trained_weights(population_trained_weights=population_matrices) print(f"Generation = {ga_instance.generations_completed}") print(f"Fitness = {ga_instance.best_solution()[1]}") print(f"Change = {ga_instance.best_solution()[1] - last_fitness}") last_fitness = ga_instance.best_solution()[1].copy() # Holds the fitness value of the previous generation. last_fitness = 0 # Preparing the NumPy array of the inputs. data_inputs = numpy.array([[1, 1], [1, 0], [0, 1], [0, 0]]) # Preparing the NumPy array of the outputs. data_outputs = numpy.array([0, 1, ``` -------------------------------- ### PyGAD: Initializing with Integer Genes and Example Inputs Source: https://pygad.readthedocs.io/en/latest/_sources/pygad_more Shows the initialization of a PyGAD model with integer gene types and provides example input values for the equation. This setup is used for genetic algorithm optimization. ```python import pygad import numpy equation_inputs = [4, -2, 3.5, 8, -2] desired_output = 2671.1234 ``` -------------------------------- ### Complete Regression Neural Network Example Source: https://pygad.readthedocs.io/en/latest/gann A full Python script demonstrating the setup of a neural network for regression using PyGAD. It includes defining the fitness function, a callback for generation updates, preparing data, initializing the GANN instance, and converting the population to vectors. ```Python importnumpy importpygad importpygad.nn importpygad.gann deffitness_func(ga_instance, solution, sol_idx): global GANN_instance, data_inputs, data_outputs predictions = pygad.nn.predict(last_layer=GANN_instance.population_networks[sol_idx], data_inputs=data_inputs, problem_type="regression") solution_fitness = 1.0/numpy.mean(numpy.abs(predictions - data_outputs)) return solution_fitness defcallback_generation(ga_instance): global GANN_instance, last_fitness population_matrices = pygad.gann.population_as_matrices(population_networks=GANN_instance.population_networks, population_vectors=ga_instance.population) GANN_instance.update_population_trained_weights(population_trained_weights=population_matrices) print(f"Generation = {ga_instance.generations_completed}") print(f"Fitness = {ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]}") print(f"Change = {ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]-last_fitness}") last_fitness = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1].copy() # Holds the fitness value of the previous generation. last_fitness = 0 # Preparing the NumPy array of the inputs. data_inputs = numpy.array([[2, 5, -3, 0.1], [8, 15, 20, 13]]) # Preparing the NumPy array of the outputs. data_outputs = numpy.array([[0.1, 0.2], [1.8, 1.5]]) # The length of the input vector for each sample (i.e. number of neurons in the input layer). num_inputs = data_inputs.shape[1] # Creating an initial population of neural networks. The return of the initial_population() function holds references to the networks, not their weights. Using such references, the weights of all networks can be fetched. num_solutions = 6 # A solution or a network can be used interchangeably. GANN_instance = pygad.gann.GANN(num_solutions=num_solutions, num_neurons_input=num_inputs, num_neurons_hidden_layers=[2], num_neurons_output=2, hidden_activations=["relu"], output_activation="None") # population does not hold the numerical weights of the network instead it holds a list of references to each last layer of each network (i.e. solution) in the population. A solution or a network can be used interchangeably. # If there is a population with 3 solutions (i.e. networks), then the population is a list with 3 elements. Each element is a reference to the last layer of each network. Using such a reference, all details of the network can be accessed. population_vectors = pygad.gann.population_as_vectors(population_networks=GANN_instance.population_networks) # To prepare the initial population, there are 2 ways: # 1) Prepare it yourself and pass it to the initial_population parameter. This way is useful when the user wants to start the genetic algorithm with a custom initial population. ``` -------------------------------- ### PyGAD Example: Reproducing Images Source: https://pygad.readthedocs.io/en/latest/index Details a practical example of using PyGAD to reproduce images, outlining the steps from reading an image to preparing the fitness function and running the genetic algorithm. ```Python # Conceptual steps for image reproduction example # 1. Read an image file # img = Image.open('target_image.png') # 2. Prepare the fitness function to compare generated image with target # def image_fitness(solution, solution_idx): # # Generate image based on solution (genes) # # Calculate difference (e.g., MSE) between generated and target image # return difference # 3. Create and run GA instance # ga_instance = pygad.GA(..., fitness_func=image_fitness) # ga_instance.run() ``` -------------------------------- ### PyGAD Adaptive Mutation Example with GA Class Source: https://pygad.readthedocs.io/en/latest/_sources/utils A basic example of initializing the pygad.GA class with adaptive mutation enabled and providing sample function inputs and desired output. ```python import pygad import numpy function_inputs = [4,-2,3.5,5,-11,-4.7] # Function inputs. desired_output = 44 # Function output. ``` -------------------------------- ### Image Classification CNN Example Setup Source: https://pygad.readthedocs.io/en/latest/_sources/gacnn Provides the complete Python code for an image classification example using PyGAD and a Convolutional Neural Network (CNN). It includes necessary imports, fitness function definition, callback for generation updates, and data loading. ```python import numpy import pygad.cnn import pygad.gacnn import pygad """ Convolutional neural network implementation using NumPy A tutorial that helps to get started (Building Convolutional Neural Network using NumPy from Scratch) available in these links: https://www.linkedin.com/pulse/building-convolutional-neural-network-using-numpy-from-ahmed-gad https://towardsdatascience.com/building-convolutional-neural-network-using-numpy-from-scratch-b30aac50e50a https://www.kdnuggets.com/2018/04/building-convolutional-neural-network-numpy-scratch.html It is also translated into Chinese: http://m.aliyun.com/yunqi/articles/585741 """ def fitness_func(ga_instance, solution, sol_idx): global GACNN_instance, data_inputs, data_outputs predictions = GACNN_instance.population_networks[sol_idx].predict(data_inputs=data_inputs) correct_predictions = numpy.where(predictions == data_outputs)[0].size solution_fitness = (correct_predictions/data_outputs.size)*100 return solution_fitness def callback_generation(ga_instance): global GACNN_instance, last_fitness population_matrices = pygad.gacnn.population_as_matrices(population_networks=GACNN_instance.population_networks, population_vectors=ga_instance.population) GACNN_instance.update_population_trained_weights(population_trained_weights=population_matrices) print(f"Generation = {ga_instance.generations_completed}") print(f"Fitness = {ga_instance.best_solutions_fitness}") data_inputs = numpy.load("dataset_inputs.npy") data_outputs = numpy.load("dataset_outputs.npy") sample_shape = data_inputs.shape[1:] ``` -------------------------------- ### PyGAD Clustering Examples Source: https://pygad.readthedocs.io/en/latest/pygad Demonstrates the use of PyGAD for clustering problems. Includes examples for 2-cluster and 3-cluster scenarios using artificial samples. A tutorial is planned for Paperspace. ```Python # Code for 2-cluster problem available at: /websites/pygad_readthedocs_io_en/clustering_2_cluster.py # Code for 3-cluster problem available at: /websites/pygad_readthedocs_io_en/clustering_3_cluster.py ``` -------------------------------- ### PyGAD: Genetic Learning Algorithms Guide Source: https://pygad.readthedocs.io/en/latest/releases This tutorial provides a guide to genetic learning algorithms for optimization, utilizing the PyGAD library. It offers insights into the principles and applications of genetic algorithms in optimization. ```Python # A Guide to Genetic ‘Learning’ Algorithms for Optimization ``` -------------------------------- ### XOR Binary Classification Setup with PyTorch and PyGAD Source: https://pygad.readthedocs.io/en/latest/_sources/torchga Sets up a PyTorch model for XOR binary classification and defines the fitness function. This example highlights the necessary imports and the structure for a classification task. ```python import torch import torchga import pygad def fitness_func(ga_instance, solution, sol_idx): global data_inputs, data_outputs, torch_ga, model, loss_function predictions = pygad.torchga.predict(model=model, solution=solution, data=data_inputs) solution_fitness = 1.0 / (loss_function(predictions, data_outputs).detach().numpy() + 0.00000001) return solution_fitness def on_generation(ga_instance): print(f"Generation = {ga_instance.generations_completed}") print(f"Fitness = {ga_instance.best_solution()[1]}") # Create the PyTorch model. input_layer = torch.nn.Linear(2, 4) ``` -------------------------------- ### Example Output of PyGAD Optimization Source: https://pygad.readthedocs.io/en/latest/index Provides an example of the output generated after running the PyGAD optimization. It shows the parameters of the best solution found, its corresponding fitness value, and the predicted output based on these parameters. ```text Parameters of the best solution : [3.92692328 -0.11554946 2.39873381 3.29579039 -0.74091476 1.05468517] Fitness value of the best solution = 157.37320042925006 Predicted output based on the best solution : 44.00635432206546 ``` -------------------------------- ### PyGAD Basic GA Example Source: https://pygad.readthedocs.io/en/latest/_sources/utils This Python code demonstrates a basic PyGAD Genetic Algorithm setup. It defines a fitness function to calculate the difference between a weighted sum of inputs and a desired output, then initializes and runs the GA with specific parameters like generations, population size, and gene count, concluding with a fitness plot. ```python import pygad import numpy equation_inputs = [4,-2,3.5] desired_output = 44 def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution * equation_inputs) fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) return fitness ga_instance = pygad.GA(num_generations=10, sol_per_pop=5, num_parents_mating=2, num_genes=len(equation_inputs), fitness_func=fitness_func) ga_instance.run() ga_instance.plot_fitness() ``` -------------------------------- ### PyGAD Genetic Algorithm Example Source: https://pygad.readthedocs.io/en/latest/_sources/pygad_more A complete example of using PyGAD to run a genetic algorithm. It includes defining a fitness function, initializing the GA with specific parameters, running the algorithm, and printing the initial and final populations. ```python import pygad import numpy equation_inputs = [4, -2, 3.5, 8, -2] desired_output = 2671.1234 def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution * equation_inputs) fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) return fitness ga_instance = pygad.GA(num_generations=10, sol_per_pop=5, num_parents_mating=2, num_genes=len(equation_inputs), fitness_func=fitness_func, gene_type=[int, [float, 2], numpy.float16, numpy.int8, [float, 1]]) print("Initial Population") print(ga_instance.initial_population) ga_instance.run() print("Final Population") print(ga_instance.population) ``` -------------------------------- ### PyGAD Multi-Objective Optimization Example Source: https://pygad.readthedocs.io/en/latest/_sources/pygad_more An example demonstrating multi-objective optimization in PyGAD to find optimal weights for two linear functions. It includes setting up the GA instance, running the optimization, and printing the results. ```python import pygad import numpy """ Given these 2 functions: y1 = f(w1:w6) = w1x1 + w2x2 + w3x3 + w4x4 + w5x5 + 6wx6 y2 = f(w1:w6) = w1x7 + w2x8 + w3x9 + w4x10 + w5x11 + 6wx12 where (x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-11,-4.7) and y=50 and (x7,x8,x9,x10,x11,x12)=(-2,0.7,-9,1.4,3,5) and y=30 What are the best values for the 6 weights (w1 to w6)? We are going to use the genetic algorithm to optimize these 2 functions. This is a multi-objective optimization problem. PyGAD considers the problem as multi-objective if the fitness function returns: 1) List. 2) Or tuple. 3) Or numpy.ndarray. """ function_inputs1 = [4,-2,3.5,5,-11,-4.7] # Function 1 inputs. function_inputs2 = [-2,0.7,-9,1.4,3,5] # Function 2 inputs. desired_output1 = 50 # Function 1 output. desired_output2 = 30 # Function 2 output. def fitness_func(ga_instance, solution, solution_idx): output1 = numpy.sum(solution*function_inputs1) output2 = numpy.sum(solution*function_inputs2) fitness1 = 1.0 / (numpy.abs(output1 - desired_output1) + 0.000001) fitness2 = 1.0 / (numpy.abs(output2 - desired_output2) + 0.000001) return [fitness1, fitness2] num_generations = 100 num_parents_mating = 10 sol_per_pop = 20 num_genes = len(function_inputs1) ga_instance = pygad.GA(num_generations=num_generations, num_parents_mating=num_parents_mating, sol_per_pop=sol_per_pop, num_genes=num_genes, fitness_func=fitness_func, parent_selection_type='nsga2') ga_instance.run() ga_instance.plot_fitness(label=['Obj 1', 'Obj 2']) solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness) print(f"Parameters of the best solution : {solution}") print(f"Fitness value of the best solution = {solution_fitness}") prediction = numpy.sum(numpy.array(function_inputs1)*solution) print(f"Predicted output 1 based on the best solution : {prediction}") prediction = numpy.sum(numpy.array(function_inputs2)*solution) print(f"Predicted output 2 based on the best solution : {prediction}") ``` -------------------------------- ### PyGAD Usage: Importing and Instantiating GA Source: https://pygad.readthedocs.io/en/latest/index Shows the basic steps to import the PyGAD library and create an instance of the `pygad.GA` class, which is the starting point for any genetic algorithm implementation. ```Python import pygad # Define fitness function and other parameters # ... # Create an instance of the GA class # ga_instance = pygad.GA(num_generations=50, # sol_per_pop=100, # num_genes=7, # fitness_func=fitness_func) ``` -------------------------------- ### Complete Image Classification CNN Example with PyGAD Source: https://pygad.readthedocs.io/en/latest/gacnn A comprehensive example demonstrating the setup and training of a Convolutional Neural Network (CNN) for image classification using PyGAD's genetic algorithm capabilities. It includes defining the network architecture, a fitness function, a callback for generation updates, and loading dataset inputs/outputs. ```Python import numpy import pygad.cnn import pygad.gacnn import pygad """ Convolutional neural network implementation using NumPy A tutorial that helps to get started (Building Convolutional Neural Network using NumPy from Scratch) available in these links: https://www.linkedin.com/pulse/building-convolutional-neural-network-using-numpy-from-ahmed-gad https://towardsdatascience.com/building-convolutional-neural-network-using-numpy-from-scratch-b30aac50e50a https://www.kdnuggets.com/2018/04/building-convolutional-neural-network-numpy-scratch.html It is also translated into Chinese: http://m.aliyun.com/yunqi/articles/585741 """ defitness_func(ga_instance, solution, sol_idx): global GACNN_instance, data_inputs, data_outputs predictions = GACNN_instance.population_networks[sol_idx].predict(data_inputs=data_inputs) correct_predictions = numpy.where(predictions == data_outputs)[0].size solution_fitness = (correct_predictions/data_outputs.size)*100 return solution_fitness defcallback_generation(ga_instance): global GACNN_instance, last_fitness population_matrices = pygad.gacnn.population_as_matrices(population_networks=GACNN_instance.population_networks, population_vectors=ga_instance.population) GACNN_instance.update_population_trained_weights(population_trained_weights=population_matrices) print(f"Generation = {ga_instance.generations_completed}") print(f"Fitness = {ga_instance.best_solutions_fitness}") data_inputs = numpy.load("dataset_inputs.npy") data_outputs = numpy.load("dataset_outputs.npy") sample_shape = data_inputs.shape[1:] num_classes = 4 data_inputs = data_inputs data_outputs = data_outputs input_layer = pygad.cnn.Input2D(input_shape=sample_shape) conv_layer1 = pygad.cnn.Conv2D(num_filters=2, kernel_size=3, previous_layer=input_layer, activation_function="relu") average_pooling_layer = pygad.cnn.AveragePooling2D(pool_size=5, previous_layer=conv_layer1, stride=3) flatten_layer = pygad.cnn.Flatten(previous_layer=average_pooling_layer) dense_layer2 = pygad.cnn.Dense(num_neurons=num_classes, previous_layer=flatten_layer, activation_function="softmax") model = pygad.cnn.Model(last_layer=dense_layer2, epochs=1, learning_rate=0.01) model.summary() GACNN_instance = pygad.gacnn.GACNN(model=model, num_solutions=4) # GACNN_instance.update_population_trained_weights(population_trained_weights=population_matrices) ``` -------------------------------- ### PyGAD PyTorch Regression Example Source: https://pygad.readthedocs.io/en/latest/torchga This comprehensive example demonstrates a full regression task using PyGAD and PyTorch. It includes defining a fitness function, setting up the PyTorch model, initializing TorchGA, preparing data, running the GA, and analyzing the results. ```Python import torch import torchga import pygad def fitness_func(ga_instance, solution, sol_idx): global data_inputs, data_outputs, torch_ga, model, loss_function predictions = pygad.torchga.predict(model=model, solution=solution, data=data_inputs) abs_error = loss_function(predictions, data_outputs).detach().numpy() + 0.00000001 solution_fitness = 1.0 / abs_error return solution_fitness def on_generation(ga_instance): print(f"Generation = {ga_instance.generations_completed}") print(f"Fitness = {ga_instance.best_solution()[1]}") # Create the PyTorch model. input_layer = torch.nn.Linear(3, 5) relu_layer = torch.nn.ReLU() output_layer = torch.nn.Linear(5, 1) model = torch.nn.Sequential(input_layer, relu_layer, output_layer) # print(model) # Create an instance of the pygad.torchga.TorchGA class to build the initial population. torch_ga = torchga.TorchGA(model=model, num_solutions=10) loss_function = torch.nn.L1Loss() # Data inputs data_inputs = torch.tensor([[0.02, 0.1, 0.15], [0.7, 0.6, 0.8], [1.5, 1.2, 1.7], [3.2, 2.9, 3.1]]) # Data outputs data_outputs = torch.tensor([[0.1], [0.6], [1.3], [2.5]]) # Prepare the PyGAD parameters. Check the documentation for more information: https://pygad.readthedocs.io/en/latest/pygad.html#pygad-ga-class num_generations = 250 # Number of generations. num_parents_mating = 5 # Number of solutions to be selected as parents in the mating pool. initial_population = torch_ga.population_weights # Initial population of network weights ga_instance = pygad.GA(num_generations=num_generations, num_parents_mating=num_parents_mating, initial_population=initial_population, fitness_func=fitness_func, on_generation=on_generation) ga_instance.run() # After the generations complete, some plots are showed that summarize how the outputs/fitness values evolve over generations. ga_instance.plot_fitness(title="PyGAD & PyTorch - Iteration vs. Fitness", linewidth=4) # Returning the details of the best solution. solution, solution_fitness, solution_idx = ga_instance.best_solution() print(f"Fitness value of the best solution = {solution_fitness}") print(f"Index of the best solution : {solution_idx}") # Make predictions based on the best solution. predictions = pygad.torchga.predict(model=model, solution=solution, data=data_inputs) print("Predictions : \n", predictions.detach().numpy()) abs_error = loss_function(predictions, data_outputs) print("Absolute Error : ", abs_error.detach().numpy()) ``` -------------------------------- ### Complete PyGAD Regression Example Code Source: https://pygad.readthedocs.io/en/latest/_sources/gann Provides the complete Python code for the fish weight prediction regression example using PyGAD. This includes imports, fitness function, callback function, data loading, preparation, and GANN instance creation. ```python import numpy import pygad import pygad.nn import pygad.gann import pandas def fitness_func(ga_instance, solution, sol_idx): global GANN_instance, data_inputs, data_outputs predictions = pygad.nn.predict(last_layer=GANN_instance.population_networks[sol_idx], data_inputs=data_inputs, problem_type="regression") solution_fitness = 1.0/numpy.mean(numpy.abs(predictions - data_outputs)) return solution_fitness def callback_generation(ga_instance): global GANN_instance, last_fitness population_matrices = pygad.gann.population_as_matrices(population_networks=GANN_instance.population_networks, population_vectors=ga_instance.population) GANN_instance.update_population_trained_weights(population_trained_weights=population_matrices) print(f"Generation = {ga_instance.generations_completed}") print(f"Fitness = {ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]}") print(f"Change = {ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1] - last_fitness}") last_fitness = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1].copy() # Holds the fitness value of the previous generation. last_fitness = 0 data = numpy.array(pandas.read_csv("../data/Fish.csv")) # Preparing the NumPy array of the inputs. data_inputs = numpy.asarray(data[:, 2:], dtype=numpy.float32) # Preparing the NumPy array of the outputs. data_outputs = numpy.asarray(data[:, 1], dtype=numpy.float32) # The length of the input vector for each sample (i.e. number of neurons in the input layer). num_inputs = data_inputs.shape[1] # Creating an initial population of neural networks. The return of the initial_population() function holds references to the networks, not their weights. Using such references, the weights of all networks can be fetched. num_solutions = 6 # A solution or a network can be used interchangeably. GANN_instance = pygad.gann.GANN(num_solutions=num_solutions, num_neurons_input=num_inputs, num_neurons_hidden_layers=[2], num_neurons_output=1, hidden_activations=["relu"], output_activation="None") ``` -------------------------------- ### Complete Regression Example Code Source: https://pygad.readthedocs.io/en/latest/nn Provides the complete Python code for the fish weight prediction regression example using PyGAD, including data loading, network definition, training, prediction, and error calculation. ```Python import numpy import pygad.nn import pandas data = numpy.array(pandas.read_csv("Fish.csv")) # Preparing the NumPy array of the inputs. data_inputs = numpy.asarray(data[:, 2:], dtype=numpy.float32) # Preparing the NumPy array of the outputs. data_outputs = numpy.asarray(data[:, 1], dtype=numpy.float32) # Fish Weight # The number of inputs (i.e. feature vector length) per sample num_inputs = data_inputs.shape[1] # Number of outputs per sample num_outputs = 1 HL1_neurons = 2 # Building the network architecture. input_layer = pygad.nn.InputLayer(num_inputs) hidden_layer1 = pygad.nn.DenseLayer(num_neurons=HL1_neurons, previous_layer=input_layer, activation_function="relu") output_layer = pygad.nn.DenseLayer(num_neurons=num_outputs, previous_layer=hidden_layer1, activation_function="None") # Training the network. pygad.nn.train(num_epochs=100, last_layer=output_layer, data_inputs=data_inputs, data_outputs=data_outputs, learning_rate=0.01, problem_type="regression") # Using the trained network for predictions. predictions = pygad.nn.predict(last_layer=output_layer, data_inputs=data_inputs, problem_type="regression") # Calculating some statistics abs_error = numpy.mean(numpy.abs(predictions - data_outputs)) print(f"Absolute error : {abs_error}.") ``` -------------------------------- ### PyGAD Example: Adaptive Mutation with Fitness Function Source: https://pygad.readthedocs.io/en/latest/utils An example of using adaptive mutation in PyGAD. It includes setting up the GA instance with adaptive mutation and defining a fitness function that calculates the difference between the output of a solution and a desired output. ```Python importpygad importnumpy function_inputs = [4,-2,3.5,5,-11,-4.7] # Function inputs. desired_output = 44 # Function output. def fitness_func(ga_instance, solution, solution_idx): # The fitness function calulates the sum of products between each input and its corresponding weight. output = numpy.sum(solution*function_inputs) # The value 0.000001 is used to avoid the Inf value when the denominator numpy.abs(output - desired_output) is 0.0. fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) return fitness # Example of how to use adaptive mutation (assuming GA instance is created elsewhere) # ga_instance = pygad.GA(num_generations=50, # sol_per_pop=50, # num_parents_mating=10, # fitness_func=fitness_func, # init_range_low=-2, # init_range_high=5, # mutation_type="adaptive", # mutation_probability=[0.25, 0.1]) ``` -------------------------------- ### PyGAD KerasGA Regression Example Source: https://pygad.readthedocs.io/en/latest/kerasga This code demonstrates a complete regression example using PyGAD and Keras. It defines a fitness function, sets up a Keras model, initializes PyGAD's KerasGA, prepares data, configures and runs the GA, and finally makes predictions with the best solution. ```Python importtensorflow.keras importpygad.kerasga importnumpy importpygad deffitness_func(ga_instance, solution, sol_idx): global data_inputs, data_outputs, keras_ga, model predictions = pygad.kerasga.predict(model=model, solution=solution, data=data_inputs) mae = tensorflow.keras.losses.MeanAbsoluteError() abs_error = mae(data_outputs, predictions).numpy() + 0.00000001 solution_fitness = 1.0/abs_error return solution_fitness defon_generation(ga_instance): print(f"Generation = {ga_instance.generations_completed}") print(f"Fitness = {ga_instance.best_solution()[1]}") input_layer = tensorflow.keras.layers.Input(3) dense_layer1 = tensorflow.keras.layers.Dense(5, activation="relu")(input_layer) output_layer = tensorflow.keras.layers.Dense(1, activation="linear")(dense_layer1) model = tensorflow.keras.Model(inputs=input_layer, outputs=output_layer) keras_ga = pygad.kerasga.KerasGA(model=model, num_solutions=10) # Data inputs data_inputs = numpy.array([[0.02, 0.1, 0.15], [0.7, 0.6, 0.8], [1.5, 1.2, 1.7], [3.2, 2.9, 3.1]]) # Data outputs data_outputs = numpy.array([[0.1], [0.6], [1.3], [2.5]]) # Prepare the PyGAD parameters. Check the documentation for more information: https://pygad.readthedocs.io/en/latest/pygad.html#pygad-ga-class num_generations = 250 # Number of generations. num_parents_mating = 5 # Number of solutions to be selected as parents in the mating pool. initial_population = keras_ga.population_weights # Initial population of network weights ga_instance = pygad.GA(num_generations=num_generations, num_parents_mating=num_parents_mating, initial_population=initial_population, fitness_func=fitness_func, on_generation=on_generation) ga_instance.run() # After the generations complete, some plots are showed that summarize how the outputs/fitness values evolve over generations. ga_instance.plot_fitness(title="PyGAD & Keras - Iteration vs. Fitness", linewidth=4) # Returning the details of the best solution. solution, solution_fitness, solution_idx = ga_instance.best_solution() print(f"Fitness value of the best solution = {solution_fitness}") print(f"Index of the best solution : {solution_idx}") # Make prediction based on the best solution. predictions = pygad.kerasga.predict(model=model, solution=solution, data=data_inputs) print(f"Predictions : \n{predictions}") mae = tensorflow.keras.losses.MeanAbsoluteError() abs_error = mae(data_outputs, predictions).numpy() print(f"Absolute Error : {abs_error}") ``` -------------------------------- ### PyGAD NN: Data Preparation Example Source: https://pygad.readthedocs.io/en/latest/nn Example code snippet demonstrating data preparation for a neural network using NumPy and scikit-image. It involves reading image data and extracting features. ```Python import numpy import skimage.io,skimage.color,skimage.feature import os fruits = ["apple", "raspberry", "mango", "lemon"] # Number of samples in the datset used = 492+490+490+490=1,962 ``` -------------------------------- ### Adaptive Mutation in Genetic Algorithm with Python Examples Source: https://pygad.readthedocs.io/en/latest/releases This tutorial explores the concept of adaptive mutation in genetic algorithms, contrasting it with fixed mutation rates. It demonstrates how to implement and utilize adaptive mutation using the PyGAD Python library with practical examples. ```Python # Example demonstrating adaptive mutation in PyGAD # (Specific code not provided in the text, but the concept is explained) import pygad # Placeholder for adaptive mutation logic def adaptive_mutation_callback(ga_instance): # Logic to adjust mutation rate based on GA progress pass # Assuming a PyGAD instance is created # ga_instance = pygad.GA(...) # ga_instance.on_mutation = adaptive_mutation_callback ``` -------------------------------- ### PyGAD Example: Clustering Source: https://pygad.readthedocs.io/en/latest/index Illustrates how PyGAD can be applied to clustering problems, showcasing its utility in finding optimal cluster assignments or parameters. ```Python # Conceptual example for clustering # Define a fitness function for clustering # def clustering_fitness(solution, solution_idx): # # Evaluate clustering quality (e.g., silhouette score) # return score # ga_instance = pygad.GA(..., fitness_func=clustering_fitness) # ga_instance.run() ``` -------------------------------- ### PyGAD Logging Outputs Source: https://pygad.readthedocs.io/en/latest/index Details the logging capabilities in PyGAD, allowing users to direct output to the console, a file, or both, and provides an example of configuring logging. ```Python # Example of configuring logging # ga_instance = pygad.GA(..., save_best_solutions=True, save_solutions_to_csv=True) # ga_instance.log_dir = 'logs/' # ga_instance.log_file_name = 'pygad_log.txt' ``` -------------------------------- ### PyGAD Best Solution Output Source: https://pygad.readthedocs.io/en/latest/_sources/pygad_more Example output showing the best solution found and its fitness value after running a PyGAD genetic algorithm. ```text [ 2.77249188 -4.06570662 0.04196872 -3.47770796 -0.57502138 -3.22775267] ``` -------------------------------- ### XOR Classification Network Example Source: https://pygad.readthedocs.io/en/latest/nn This example demonstrates building a simple neural network to simulate the XOR logic gate. It includes preparing the input data for XOR, defining a network architecture with one hidden layer, and implies the subsequent steps of training and prediction. ```Python import numpy import pygad.nn # Preparing the NumPy array of the inputs for XOR. data_inputs = numpy.array([[1, 1], [1, 0], [0, 1], [0, 0]]) # The XOR problem has 2 classes (0 and 1), so the output layer has 2 neurons. # The network architecture (hidden layers, activation functions) would be defined here, # followed by training and prediction steps using pygad.nn. ``` -------------------------------- ### PyGAD GA with Adaptive Mutation and Plotting Source: https://pygad.readthedocs.io/en/latest/utils Shows an example of initializing a PyGAD GA instance with adaptive mutation and then running the optimization process. The fitness plot is generated with a specified title and line width. ```python ga_instance = pygad.GA(num_generations=200, fitness_func=fitness_func, num_parents_mating=10, sol_per_pop=20, num_genes=len(function_inputs), mutation_type="adaptive", mutation_num_genes=(3, 1)) # Running the GA to optimize the parameters of the function. ga_instance.run() ga_instance.plot_fitness(title="PyGAD with Adaptive Mutation", linewidth=5) ``` -------------------------------- ### Create a PyTorch Model Source: https://pygad.readthedocs.io/en/latest/torchga Example code for creating a simple sequential PyTorch model with linear and ReLU layers. This serves as a prerequisite for using the pygad.torchga module to train PyTorch models. ```Python import torch input_layer = torch.nn.Linear(3, 5) relu_layer = torch.nn.ReLU() output_layer = torch.nn.Linear(5, 1) model = torch.nn.Sequential(input_layer, relu_layer, output_layer) ``` -------------------------------- ### Run and Save/Load GA Instance with PyGAD Source: https://pygad.readthedocs.io/en/latest/_sources/pygad This snippet demonstrates the core workflow of using PyGAD: initializing the GA, running it, plotting fitness, retrieving the best solution, and saving/loading the GA instance. It covers parameter optimization and persistence. ```python last_fitness = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1] ga_instance = pygad.GA(num_generations=num_generations, num_parents_mating=num_parents_mating, sol_per_pop=sol_per_pop, num_genes=num_genes, fitness_func=fitness_func, on_generation=on_generation) # Running the GA to optimize the parameters of the function. ga_instance.run() ga_instance.plot_fitness() # Returning the details of the best solution. solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness) print(f"Parameters of the best solution : {solution}") print(f"Fitness value of the best solution = {solution_fitness}") print(f"Index of the best solution : {solution_idx}") prediction = numpy.sum(numpy.array(function_inputs)*solution) print(f"Predicted output based on the best solution : {prediction}") if ga_instance.best_solution_generation != -1: print(f"Best fitness value reached after {ga_instance.best_solution_generation} generations.") # Saving the GA instance. filename = 'genetic' # The filename to which the instance is saved. The name is without extension. ga_instance.save(filename=filename) # Loading the saved GA instance. loaded_ga_instance = pygad.load(filename=filename) loaded_ga_instance.plot_fitness() ``` -------------------------------- ### PyGAD Example: CoinTex Game Playing Source: https://pygad.readthedocs.io/en/latest/index Demonstrates the use of PyGAD in game playing scenarios, specifically for the CoinTex game, highlighting its application in reinforcement learning or strategy optimization. ```Python # Conceptual example for CoinTex game playing # Define a fitness function based on game performance # def game_fitness(solution, solution_idx): # # Play the game using the strategy defined by the solution # # Return score or win/loss status # return score # ga_instance = pygad.GA(..., fitness_func=game_fitness) # ga_instance.run() ``` -------------------------------- ### Initialize and Run Genetic Algorithm with PyGAD Source: https://pygad.readthedocs.io/en/latest/_sources/gann This snippet demonstrates the core process of setting up and running a genetic algorithm using PyGAD. It involves preparing the initial population, defining GA parameters such as the number of generations, parent selection, crossover, and mutation types, and then executing the algorithm. Finally, it shows how to retrieve and display the best solution found. ```Python import pygad import numpy # Assuming GANN_instance, fitness_func, callback_generation, data_inputs, data_outputs are defined elsewhere # Convert population networks to vectors for GA initialization population_vectors = pygad.gann.population_as_vectors(population_networks=GANN_instance.population_networks) # Prepare the initial population for the GA initial_population = population_vectors.copy() # Define GA parameters num_parents_mating = 4 num_generations = 500 mutation_percent_genes = 5 parent_selection_type = "sss" crossover_type = "single_point" mutation_type = "random" keep_parents = 1 init_range_low = -1 init_range_high = 1 # Initialize the PyGAD GA instance ga_instance = pygad.GA(num_generations=num_generations, num_parents_mating=num_parents_mating, initial_population=initial_population, fitness_func=fitness_func, mutation_percent_genes=mutation_percent_genes, init_range_low=init_range_low, init_range_high=init_range_high, parent_selection_type=parent_selection_type, crossover_type=crossover_type, mutation_type=mutation_type, keep_parents=keep_parents, on_generation=callback_generation) # Run the genetic algorithm ga_instance.run() # Plot the fitness over generations ga_instance.plot_fitness() # Get the best solution found solution, solution_fitness, solution_idx = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness) # Print details of the best solution print(f"Parameters of the best solution : {solution}") print(f"Fitness value of the best solution = {solution_fitness}") print(f"Index of the best solution : {solution_idx}") if ga_instance.best_solution_generation != -1: print(f"Best fitness value reached after {ga_instance.best_solution_generation} generations.") # Predict outputs using the best solution predictions = pygad.nn.predict(last_layer=GANN_instance.population_networks[solution_idx], data_inputs=data_inputs, problem_type="regression") print(f"Predictions of the trained network : {predictions}") # Calculate and print the absolute error abs_error = numpy.mean(numpy.abs(predictions - data_outputs)) print(f"Absolute error : {abs_error}.") ``` -------------------------------- ### PyGAD GA Initialization and Run Source: https://pygad.readthedocs.io/en/latest/_sources/utils Demonstrates the initialization of a PyGAD Genetic Algorithm instance with specified parameters such as generations, population size, and selection/crossover/mutation types. It then runs the GA and plots the fitness over generations. ```Python ga_instance = pygad.GA(num_generations=10, sol_per_pop=5, num_parents_mating=2, num_genes=len(equation_inputs), fitness_func=Test().fitness_func, parent_selection_type=Test().parent_selection_func, crossover_type=Test().crossover_func, mutation_type=Test().mutation_func) ga_instance.run() ga_instance.plot_fitness() ``` -------------------------------- ### PyGAD GA Initialization and Run Source: https://pygad.readthedocs.io/en/latest/_sources/visualize Initializes and runs a PyGAD genetic algorithm instance. This example sets up the parameters for a genetic algorithm, including generations, population size, mating parents, gene space, and fitness function, then executes the algorithm. ```python import pygad import numpy equation_inputs = [4, -2, 3.5, 8, -2, 3.5, 8] desired_output = 2671.1234 def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution * equation_inputs) fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) return fitness ga_instance = pygad.GA(num_generations=10, sol_per_pop=10, num_parents_mating=5, num_genes=len(equation_inputs), fitness_func=fitness_func, gene_space=[range(1, 10), range(10, 20), range(15, 30), range(20, 40), range(25, 50), range(10, 30), range(20, 50)], gene_type=int, save_solutions=True) ga_instance.run() ``` -------------------------------- ### PyGAD Fitness Function Example Source: https://pygad.readthedocs.io/en/latest/gann This Python function calculates the fitness of a solution for a genetic algorithm. It takes the GA instance, the solution, and its index as input. It uses `pygad.nn.predict` to get predictions and calculates the classification accuracy, returning it as the fitness value. This is useful for training neural networks with PyGAD. ```Python def fitness_func(ga_instance, solution, sol_idx): global GANN_instance, data_inputs, data_outputs predictions = pygad.nn.predict(last_layer=GANN_instance.population_networks[sol_idx], data_inputs=data_inputs) correct_predictions = numpy.where(predictions == data_outputs)[0].size solution_fitness = (correct_predictions/data_outputs.size)*100 return solution_fitness ```