crossovers
- thefittest.utils.crossovers.binomial(individ: ndarray[Any, dtype[float64]], mutant: ndarray[Any, dtype[float64]], CR: float64) ndarray[Any, dtype[float64]]
Perform binomial crossover operation for differential evolution.
- Parameters:
- individNDArray[np.float64]
A 1D array containing the genetic material of an individual.
- mutantNDArray[np.float64]
A 1D array containing the genetic material of a mutant individual.
- CRnp.float64
Crossover probability, determines the likelihood of crossover.
- Returns:
- NDArray[np.float64]
A 1D array representing offspring resulting from the binomial crossover operation.
Notes
Binomial crossover is used in differential evolution. It operates on real-valued chromosomes. For each gene in the chromosome, the gene from the mutant individual is selected with probability CR, and the gene from the original individual is selected with probability 1 - CR. The function returns a new chromosome representing potential offspring.
Examples
>>> import numpy as np >>> from thefittest.utils.crossovers import binomial >>> >>> # Example >>> # Define an individual, a mutant, and the crossover probability >>> individ = np.array([1.0, 2.0, 3.0], dtype=np.float64) >>> mutant = np.array([4.0, 5.0, 6.0], dtype=np.float64) >>> CR = 0.8 >>> >>> # Perform binomial crossover >>> offspring = binomial(individ, mutant, CR) >>> >>> # Display results >>> print("Original Individual:", individ) Original Individual: ... >>> print("Mutant Individual:", mutant) Mutant Individual: ... >>> print("Offspring After Binomial Crossover:", offspring) Offspring After Binomial Crossover: ...
- thefittest.utils.crossovers.binomialGA(individ: ndarray[Any, dtype[int8]], mutant: ndarray[Any, dtype[int8]], CR: float64) ndarray[Any, dtype[int8]]
Perform binomial crossover operation for the Genetic Algorithm with Success History based Parameter Adaptation (SHAGA).
- Parameters:
- individNDArray[np.int8]
A 1D array containing the genetic material of an individual in binary form.
- mutantNDArray[np.int8]
A 1D array containing the genetic material of a mutant individual in binary form.
- CRnp.float64
Crossover probability, determines the likelihood of crossover.
- Returns:
- NDArray[np.byte]
A 1D array representing offspring resulting from the binomial crossover operation in binary form.
Notes
Binomial crossover is used in the Genetic Algorithm with Success History based Parameter Adaptation (SHAGA). It operates on binary chromosomes. For each gene in the chromosome, the gene from the mutant individual is selected with probability CR, and the gene from the original individual is selected with probability 1 - CR. The function returns a new chromosome representing potential offspring in binary form.
Examples
>>> import numpy as np >>> from thefittest.utils.crossovers import binomialGA >>> >>> # Example >>> # Define a binary individual, a binary mutant, and the crossover probability >>> individ = np.array([1, 0, 1, 0, 1], dtype=np.int8) >>> mutant = np.array([0, 1, 0, 1, 0], dtype=np.int8) >>> CR = 0.8 >>> >>> # Perform binomial crossover for SHAGA >>> offspring = binomialGA(individ, mutant, CR) >>> >>> # Display results >>> print("Original Binary Individual:", individ) Original Binary Individual: ... >>> print("Mutant Binary Individual:", mutant) Mutant Binary Individual: ... >>> print("Offspring After Binomial Crossover (SHAGA):", offspring) Offspring After Binomial Crossover (SHAGA): ...
- thefittest.utils.crossovers.empty_crossover(individs: ndarray[Any, dtype[int8]], fitness: ndarray[Any, dtype[float64]], rank: ndarray[Any, dtype[float64]]) ndarray[Any, dtype[int8]]
Perform an empty crossover operation for a genetic algorithm.
- Parameters:
- individsNDArray[np.byte]
A 2D array containing individuals where each row represents an individual.
- fitnessNDArray[np.float64]
A 1D array containing the fitness values of individuals. (not used)
- rankNDArray[np.float64]
A 1D array containing the rank values of individuals. The higher the rank, the better the individual. (not used)
- Returns:
- NDArray[np.byte]
Offspring resulting from the empty crossover operation.
Notes
This empty crossover function simply returns a copy of the first individual without any crossover. It is used as part of a genetic algorithm.
Examples
>>> import numpy as np >>> from thefittest.utils.crossovers import empty_crossover >>> >>> # Example >>> # Define the parents, fitness values, and ranks >>> parents = np.array([[1, 0, 1]], dtype=np.byte) # First and second parent >>> fitness_values = np.array([0.5], dtype=np.float64) >>> ranks = np.array([1.0], dtype=np.float64) >>> >>> # Perform empty crossover >>> offspring = empty_crossover(parents, fitness_values, ranks) >>> >>> # Display results >>> print("Original Individuals:", parents) Original Individuals: ... >>> print("Offspring After Empty Crossover:", offspring) Offspring After Empty Crossover: ...
- thefittest.utils.crossovers.empty_crossoverGP(individs: ndarray[Any, dtype[_ScalarType_co]], fitness: ndarray[Any, dtype[float64]], rank: ndarray[Any, dtype[float64]], max_level: int) Tree
>>> import numpy as np >>> from thefittest.utils.crossovers import empty_crossoverGP >>> from thefittest.base import Tree >>> from thefittest.base import init_symbolic_regression_uniset >>> >>> >>> # Example >>> X = np.array([[0.3, 0.7], [0.3, 1.1], [3.5, 11.0]], dtype=np.float64) >>> functional_set_names = ("add", "mul") >>> max_tree_level = 5 >>> >>> # Initialize Universal Set for Symbolic Regression >>> universal_set = init_symbolic_regression_uniset(X, functional_set_names) >>> >>> # Define the parents, fitness values, ranks, and maximum allowed depth >>> parents = np.array([Tree.random_tree(universal_set, max_tree_level)], dtype=object) >>> fitness_values = np.array([0.8], dtype=np.float64) >>> ranks = np.array([1.0], dtype=np.float64) >>> max_depth = 7 >>> >>> # Perform empty crossover for genetic programming >>> offspring = empty_crossoverGP(parents, fitness_values, ranks, max_depth) >>> >>> print("Original Individual:", parents[0]) Original Individual: ... >>> print("Offspring After Empty Crossover (GP):", offspring) Offspring After Empty Crossover (GP): ...
- thefittest.utils.crossovers.one_point_crossover(individs: ndarray[Any, dtype[int8]], fitness: ndarray[Any, dtype[float64]], rank: ndarray[Any, dtype[float64]]) ndarray[Any, dtype[int8]]
Perform one-point crossover operation for a genetic algorithm.
- Parameters:
- individsNDArray[np.byte]
A 2D array containing individuals where each row represents an individual.
- fitnessNDArray[np.float64]
A 1D array containing the fitness values of individuals. (not used)
- rankNDArray[np.float64]
A 1D array containing the rank values of individuals. The higher the rank, the better the individual. (not used)
- Returns:
- NDArray[np.byte]
Offspring resulting from the one-point crossover operation.
Notes
One-point crossover randomly selects a crossover point along the chromosome. The genetic material beyond this point is swapped between two parents, creating two new chromosomes for potential offspring. The function returns one of the two resulting offspring randomly.
Examples
>>> import numpy as np >>> from thefittest.utils.crossovers import one_point_crossover >>> >>> # Example >>> # Define the parents, fitness values, and ranks >>> parents = np.array([[1, 0, 1], [0, 1, 1]], dtype=np.byte) # First and second parent >>> fitness_values = np.array([0.5, 0.8], dtype=np.float64) >>> ranks = np.array([2.0, 1.0], dtype=np.float64) >>> >>> # Perform one_point crossover >>> offspring = one_point_crossover(parents, fitness_values, ranks) >>> >>> # Display results >>> print("Original Individuals:", parents) Original Individuals: ... >>> print("Offspring After Empty Crossover:", offspring) Offspring After Empty Crossover: ...
- thefittest.utils.crossovers.one_point_crossoverGP(individs: ndarray[Any, dtype[_ScalarType_co]], fitness: ndarray[Any, dtype[float64]], rank: ndarray[Any, dtype[float64]], max_level: int) Tree
Perform one-point crossover operation for genetic programming.
- Parameters:
- individsNDArray
A 1D array containing two individuals represented as trees.
- fitnessNDArray[np.float64]
A 1D array containing the fitness values of individuals. (not used)
- rankNDArray[np.float64]
A 1D array containing the rank values of individuals. The higher the rank, the better the individual. (not used)
- max_levelint
Maximum allowed depth/level of the resulting offspring. (not used)
- Returns:
- Tree
Offspring resulting from the one-point crossover operation.
Notes
One-point crossover is used in genetic programming. It involves selecting a common region between two parents and exchanging genetic material at a randomly chosen point within this region. The resulting offspring represents a combination of genetic material from both parents.
Examples
>>> import numpy as np >>> from thefittest.utils.crossovers import one_point_crossoverGP >>> from thefittest.base import Tree >>> from thefittest.base import init_symbolic_regression_uniset >>> >>> # Example >>> >>> X = np.array([[0.3, 0.7], [0.3, 1.1], [3.5, 11.0]], dtype=np.float64) >>> functional_set_names = ("add", "mul") >>> max_tree_level = 5 >>> >>> # Initialize Universal Set for Symbolic Regression >>> universal_set = init_symbolic_regression_uniset(X, functional_set_names) >>> >>> parent1 = Tree.random_tree(universal_set, max_tree_level) >>> parent2 = Tree.random_tree(universal_set, max_tree_level) >>> >>> fitness_values = np.array([0.5, 0.8], dtype=np.float64) >>> ranks = np.array([2.0, 1.0], dtype=np.float64) >>> max_depth = 7 # Set the maximum allowed depth >>> >>> # Perform one-point crossover >>> offspring = one_point_crossoverGP(np.array([parent1, parent2]), fitness_values, ranks, max_depth) >>> >>> # Display results >>> print("Parent 1:", parent1) Parent 1: ... >>> print("Parent 2:", parent2) Parent 2: ... >>> print("Offspring After One-Point Crossover:", offspring) Offspring After One-Point Crossover: ...
- thefittest.utils.crossovers.standard_crossover(individs: ndarray[Any, dtype[_ScalarType_co]], fitness: ndarray[Any, dtype[float64]], rank: ndarray[Any, dtype[float64]], max_level: int) Tree
Perform standard crossover operation for genetic programming.
- Parameters:
- individsNDArray
A 1D array containing two individuals represented as trees.
- fitnessNDArray[np.float64]
A 1D array containing the fitness values of individuals. (not used)
- rankNDArray[np.float64]
A 1D array containing the rank values of individuals. The higher the rank, the better the individual. (not used)
- max_levelint
Maximum allowed depth/level of the resulting offspring.
- Returns:
- Tree
Offspring resulting from the standard crossover operation.
Notes
Standard crossover is used in genetic programming. It involves selecting a random subtree from one parent and replacing a subtree in the other parent. The resulting offspring represents a combination of genetic material from both parents. If the depth/level of the offspring exceeds the specified maximum level, a random parent is chosen as the offspring.
Examples
>>> import numpy as np >>> from thefittest.utils.crossovers import standard_crossover >>> from thefittest.base import Tree >>> from thefittest.base import init_symbolic_regression_uniset >>> >>> # Example >>> >>> X = np.array([[0.3, 0.7], [0.3, 1.1], [3.5, 11.0]], dtype=np.float64) >>> functional_set_names = ("add", "mul") >>> max_tree_level = 5 >>> >>> # Initialize Universal Set for Symbolic Regression >>> universal_set = init_symbolic_regression_uniset(X, functional_set_names) >>> >>> parent1 = Tree.random_tree(universal_set, max_tree_level) >>> parent2 = Tree.random_tree(universal_set, max_tree_level) >>> >>> fitness_values = np.array([0.5, 0.8], dtype=np.float64) >>> ranks = np.array([2.0, 1.0], dtype=np.float64) >>> max_depth = 7 # Set the maximum allowed depth >>> >>> # Perform standard crossover >>> offspring = standard_crossover(np.array([parent1, parent2]), fitness_values, ranks, max_depth) >>> >>> # Display results >>> print("Parent 1:", parent1) Parent 1: ... >>> print("Parent 2:", parent2) Parent 2: ... >>> print("Offspring After Standard Crossover:", offspring) Offspring After Standard Crossover: ...
- thefittest.utils.crossovers.two_point_crossover(individs: ndarray[Any, dtype[int8]], fitness: ndarray[Any, dtype[float64]], rank: ndarray[Any, dtype[float64]]) ndarray[Any, dtype[int8]]
Perform two-point crossover operation for a genetic algorithm.
- Parameters:
- individsNDArray[np.byte]
A 2D array containing individuals where each row represents an individual.
- fitnessNDArray[np.float64]
A 1D array containing the fitness values of individuals. (not used)
- rankNDArray[np.float64]
A 1D array containing the rank values of individuals. The higher the rank, the better the individual. (not used)
- Returns:
- NDArray[np.byte]
Offspring resulting from the two-point crossover operation.
Notes
Two-point crossover randomly selects two crossover points along the chromosome. The genetic material between these two points is swapped between two parents, creating two new chromosomes for potential offspring. The function returns one of the two resulting offspring randomly.
Examples
>>> import numpy as np >>> from thefittest.utils.crossovers import two_point_crossover >>> >>> # Example >>> # Define the parents, fitness values, and ranks >>> parents = np.array([[1, 0, 1, 0], [0, 1, 1, 1]], dtype=np.byte) # First and second parent >>> fitness_values = np.array([0.5, 0.8], dtype=np.float64) >>> ranks = np.array([2.0, 1.0], dtype=np.float64) >>> >>> # Perform two-point crossover >>> offspring = two_point_crossover(parents, fitness_values, ranks) >>> >>> # Display results >>> print("Original Individuals:", parents) Original Individuals: ... >>> print("Offspring After Two-Point Crossover:", offspring) Offspring After Two-Point Crossover: ...
- thefittest.utils.crossovers.uniform_crossover(individs: ndarray[Any, dtype[int8]], fitness: ndarray[Any, dtype[float64]], rank: ndarray[Any, dtype[float64]]) ndarray[Any, dtype[int8]]
Perform uniform crossover operation for a genetic algorithm.
- Parameters:
- individsNDArray[np.byte]
A 2D array containing individuals where each row represents an individual.
- fitnessNDArray[np.float64]
A 1D array containing the fitness values of individuals. (not used)
- rankNDArray[np.float64]
A 1D array containing the rank values of individuals. The higher the rank, the better the individual. (not used)
- Returns:
- NDArray[np.byte]
Offspring resulting from the uniform crossover operation.
Notes
Uniform crossover randomly selects genetic material from the parents for each gene in the chromosome. Each gene of the offspring is randomly chosen from one of the parents. The function returns a new chromosome representing potential offspring.
Examples
>>> import numpy as np >>> from thefittest.utils.crossovers import uniform_crossover >>> >>> # Example >>> # Define the parents, fitness values, and ranks >>> parents = np.array([[1, 0, 1, 0], [0, 1, 1, 1]], dtype=np.byte) # First and second parent >>> fitness_values = np.array([0.5, 0.8], dtype=np.float64) >>> ranks = np.array([2.0, 1.0], dtype=np.float64) >>> >>> # Perform uniform crossover >>> offspring = uniform_crossover(parents, fitness_values, ranks) >>> >>> # Display results >>> print("Original Individuals:", parents) Original Individuals: ... >>> print("Offspring After Uniform Crossover:", offspring) Offspring After Uniform Crossover: ...
- thefittest.utils.crossovers.uniform_crossoverGP(individs: ndarray[Any, dtype[_ScalarType_co]], fitness: ndarray[Any, dtype[float64]], rank: ndarray[Any, dtype[float64]], max_level: int) Tree
Perform uniform crossover operation for genetic programming.
- Parameters:
- individsNDArray
A 1D array containing two or more individuals represented as trees.
- fitnessNDArray[np.float64]
A 1D array containing the fitness values of individuals. (not used)
- rankNDArray[np.float64]
A 1D array containing the rank values of individuals. The higher the rank, the better the individual. (not used)
- max_levelint
Maximum allowed depth/level of the resulting offspring. (not used)
- Returns:
- Tree
Offspring resulting from the uniform crossover operation.
Notes
Uniform crossover is used in genetic programming. It involves selecting a common region between two or more parents and exchanging genetic material at randomly chosen points within this region. The resulting offspring represents a combination of genetic material from all parents.
Examples
>>> import numpy as np >>> from thefittest.utils.crossovers import uniform_crossoverGP >>> from thefittest.base import Tree >>> from thefittest.base import init_symbolic_regression_uniset >>> >>> # Example >>> >>> X = np.array([[0.3, 0.7], [0.3, 1.1], [3.5, 11.0]], dtype=np.float64) >>> functional_set_names = ("add", "mul") >>> max_tree_level = 5 >>> >>> # Initialize Universal Set for Symbolic Regression >>> universal_set = init_symbolic_regression_uniset(X, functional_set_names) >>> >>> parent1 = Tree.random_tree(universal_set, max_tree_level) >>> parent2 = Tree.random_tree(universal_set, max_tree_level) >>> parent3 = Tree.random_tree(universal_set, max_tree_level) >>> >>> fitness_values = np.array([0.5, 0.8, 0.6], dtype=np.float64) >>> ranks = np.array([2.0, 1.0, 3.0], dtype=np.float64) >>> max_depth = 7 # Set the maximum allowed depth >>> >>> # Perform uniform crossover >>> offspring = uniform_crossoverGP(np.array([parent1, parent2, parent3]), fitness_values, ranks, max_depth) >>> >>> # Display results >>> print ("Parent 1:", parent1) Parent 1: ... >>> print("Parent 2:", parent2) Parent 2: ... >>> print("Parent 3:", parent3) Parent 3: ... >>> print("Offspring After Uniform Crossover:", offspring) Offspring After Uniform Crossover: ...
- thefittest.utils.crossovers.uniform_proportional_crossover(individs: ndarray[Any, dtype[int8]], fitness: ndarray[Any, dtype[float64]], rank: ndarray[Any, dtype[float64]]) ndarray[Any, dtype[int8]]
Perform uniform proportional crossover operation for a genetic algorithm.
- Parameters:
- individsNDArray[np.byte]
A 2D array containing individuals where each row represents an individual.
- fitnessNDArray[np.float64]
A 1D array containing the fitness values of individuals.
- rankNDArray[np.float64]
A 1D array containing the rank values of individuals. The higher the rank, the better the individual. (not used)
- Returns:
- NDArray[np.byte]
Offspring resulting from the uniform proportional crossover operation.
Notes
Uniform proportional crossover randomly selects genetic material from the parents for each gene in the chromosome. The probability of choosing genetic material from a parent is proportional to its fitness value. The function returns a new chromosome representing potential offspring.
Examples
>>> import numpy as np >>> from thefittest.utils.crossovers import uniform_proportional_crossover >>> >>> # Example >>> # Define the parents, fitness values, and ranks >>> parents = np.array([[1, 0, 1, 0], [0, 1, 1, 1]], dtype=np.byte) # First and second parent >>> fitness_values = np.array([0.5, 0.8], dtype=np.float64) >>> ranks = np.array([2.0, 1.0], dtype=np.float64) >>> >>> # Perform uniform proportional crossover >>> offspring = uniform_proportional_crossover(parents, fitness_values, ranks) >>> >>> # Display results >>> print("Original Individuals:", parents) Original Individuals: ... >>> print("Offspring After Uniform Proportional Crossover:", offspring) Offspring After Uniform Proportional Crossover: ...
- thefittest.utils.crossovers.uniform_proportional_crossover_GP(individs: ndarray[Any, dtype[_ScalarType_co]], fitness: ndarray[Any, dtype[float64]], rank: ndarray[Any, dtype[float64]], max_level: int) Tree
Perform uniform proportional crossover operation for genetic programming.
- Parameters:
- individsNDArray
A 1D array containing two or more individuals represented as trees.
- fitnessNDArray[np.float64]
A 1D array containing the fitness values of individuals.
- rankNDArray[np.float64]
A 1D array containing the rank values of individuals. The higher the rank, the better the individual. (not used)
- max_levelint
Maximum allowed depth/level of the resulting offspring. (not used)
- Returns:
- Tree
Offspring resulting from the uniform proportional crossover operation.
Notes
Uniform proportional crossover is a genetic programming operator. It involves selecting a common region between two or more parents, and the genetic material is exchanged at randomly chosen points within this region. The probability of selecting genetic material from a parent is proportional to its fitness value. This means that individuals with higher fitness values have a higher chance of contributing genetic material to the offspring. The resulting tree represents a combination of genetic material from all parents.
Examples
>>> import numpy as np >>> from thefittest.utils.crossovers import uniform_proportional_crossover_GP >>> from thefittest.base import Tree >>> from thefittest.base import init_symbolic_regression_uniset >>> >>> # Example >>> >>> X = np.array([[0.3, 0.7], [0.3, 1.1], [3.5, 11.0]], dtype=np.float64) >>> functional_set_names = ("add", "mul") >>> max_tree_level = 5 >>> >>> # Initialize Universal Set for Symbolic Regression >>> universal_set = init_symbolic_regression_uniset(X, functional_set_names) >>> >>> parent1 = Tree.random_tree(universal_set, max_tree_level) >>> parent2 = Tree.random_tree(universal_set, max_tree_level) >>> parent3 = Tree.random_tree(universal_set, max_tree_level) >>> >>> fitness_values = np.array([0.5, 0.8, 0.6], dtype=np.float64) >>> ranks = np.array([2.0, 1.0, 3.0], dtype=np.float64) >>> max_depth = 7 # Set the maximum allowed depth >>> >>> # Perform uniform proportional crossover >>> offspring = uniform_proportional_crossover_GP(np.array([parent1, parent2, parent3]), fitness_values, ranks, max_depth) >>> >>> # Display results >>> print("Parent 1:", parent1) Parent 1: ... >>> print("Parent 2:", parent2) Parent 2: ... >>> print("Parent 3:", parent3) Parent 3: ... >>> print("Offspring After Uniform Proportional Crossover:", offspring) Offspring After Uniform Proportional Crossover: ...
- thefittest.utils.crossovers.uniform_rank_crossover(individs: ndarray[Any, dtype[int8]], fitness: ndarray[Any, dtype[float64]], rank: ndarray[Any, dtype[float64]]) ndarray[Any, dtype[int8]]
Perform uniform rank crossover operation for a genetic algorithm.
- Parameters:
- individsNDArray[np.byte]
A 2D array containing individuals where each row represents an individual.
- fitnessNDArray[np.float64]
A 1D array containing the fitness values of individuals. (not used)
- rankNDArray[np.float64]
A 1D array containing the rank values of individuals. The higher the rank, the better the individual.
- Returns:
- NDArray[np.byte]
Offspring resulting from the uniform rank crossover operation.
Notes
Uniform rank crossover randomly selects genetic material from the parents for each gene in the chromosome. The probability of choosing genetic material from a parent is proportional to its rank value. The function returns a new chromosome representing potential offspring.
Examples
>>> import numpy as np >>> from thefittest.utils.crossovers import uniform_rank_crossover >>> >>> # Example >>> # Define the parents, fitness values, and ranks >>> parents = np.array([[1, 0, 1, 0], [0, 1, 1, 1]], dtype=np.byte) # First and second parent >>> fitness_values = np.array([0.5, 0.8], dtype=np.float64) >>> ranks = np.array([2.0, 1.0], dtype=np.float64) >>> >>> # Perform uniform rank crossover >>> offspring = uniform_rank_crossover(parents, fitness_values, ranks) >>> >>> # Display results >>> print("Original Individuals:", parents) Original Individuals: ... >>> print("Offspring After Uniform Rank Crossover:", offspring) Offspring After Uniform Rank Crossover: ...
- thefittest.utils.crossovers.uniform_rank_crossover_GP(individs: ndarray[Any, dtype[_ScalarType_co]], fitness: ndarray[Any, dtype[float64]], rank: ndarray[Any, dtype[float64]], max_level: int) Tree
Perform uniform rank crossover operation for genetic programming.
- Parameters:
- individsNDArray
A 1D array containing two or more individuals represented as trees.
- fitnessNDArray[np.float64]
A 1D array containing the fitness values of individuals. (not used)
- rankNDArray[np.float64]
A 1D array containing the rank values of individuals. The higher the rank, the better the individual. The rank values are used to determine the probability of selecting genetic material from each parent.
- max_levelint
Maximum allowed depth/level of the resulting offspring. (not used)
- Returns:
- Tree
Offspring resulting from the uniform rank crossover operation.
Notes
Uniform rank crossover is a genetic programming operator. It involves selecting a common region between two or more parents, and the genetic material is exchanged at randomly chosen points within this region. The probability of selecting genetic material from a parent is proportional to its rank value. This means that individuals with higher rank values have a higher chance of contributing genetic material to the offspring. The resulting tree represents a combination of genetic material from all parents.
Examples
>>> import numpy as np >>> from thefittest.utils.crossovers import uniform_rank_crossover_GP >>> from thefittest.base import Tree >>> from thefittest.base import init_symbolic_regression_uniset >>> >>> # Example >>> >>> X = np.array([[0.3, 0.7], [0.3, 1.1], [3.5, 11.0]], dtype=np.float64) >>> functional_set_names = ("add", "mul") >>> max_tree_level = 5 >>> >>> # Initialize Universal Set for Symbolic Regression >>> universal_set = init_symbolic_regression_uniset(X, functional_set_names) >>> >>> parent1 = Tree.random_tree(universal_set, max_tree_level) >>> parent2 = Tree.random_tree(universal_set, max_tree_level) >>> parent3 = Tree.random_tree(universal_set, max_tree_level) >>> >>> fitness_values = np.array([0.5, 0.8, 0.6], dtype=np.float64) >>> ranks = np.array([2.0, 1.0, 3.0], dtype=np.float64) >>> max_depth = 7 # Set the maximum allowed depth >>> >>> # Perform uniform rank crossover >>> offspring = uniform_rank_crossover_GP(np.array([parent1, parent2, parent3]), fitness_values, ranks, max_depth) >>> >>> # Display results >>> print("Parent 1:", parent1) Parent 1: ... >>> print("Parent 2:", parent2) Parent 2: ... >>> print("Parent 3:", parent3) Parent 3: ... >>> print("Offspring After Uniform Rank Crossover:", offspring) Offspring After Uniform Rank Crossover: ...
- thefittest.utils.crossovers.uniform_tournament_crossover(individs: ndarray[Any, dtype[int8]], fitness: ndarray[Any, dtype[float64]], rank: ndarray[Any, dtype[float64]]) ndarray[Any, dtype[int8]]
Perform uniform tournament crossover operation for a genetic algorithm.
- Parameters:
- individsNDArray[np.byte]
A 2D array containing individuals where each row represents an individual.
- fitnessNDArray[np.float64]
A 1D array containing the fitness values of individuals.
- rankNDArray[np.float64]
A 1D array containing the rank values of individuals. The higher the rank, the better the individual. (not used)
- Returns:
- NDArray[np.byte]
Offspring resulting from the uniform tournament crossover operation.
Notes
Uniform tournament crossover randomly selects genetic material from the parents for each gene in the chromosome. The parents for each gene are chosen through tournament selection based on their fitness values. The function returns a new chromosome representing potential offspring.
Examples
>>> import numpy as np >>> from thefittest.utils.crossovers import uniform_tournament_crossover >>> >>> # Example >>> # Define the parents, fitness values, and ranks >>> parents = np.array([[1, 0, 1, 0], [0, 1, 1, 1]], dtype=np.byte) # First and second parent >>> fitness_values = np.array([0.5, 0.8], dtype=np.float64) >>> ranks = np.array([2.0, 1.0], dtype=np.float64) >>> >>> # Perform uniform tournament crossover >>> offspring = uniform_tournament_crossover(parents, fitness_values, ranks) >>> >>> # Display results >>> print("Original Individuals:", parents) Original Individuals: ... >>> print("Offspring After Uniform Tournament Crossover:", offspring) Offspring After Uniform Tournament Crossover: ...
- thefittest.utils.crossovers.uniform_tournament_crossover_GP(individs: ndarray[Any, dtype[_ScalarType_co]], fitness: ndarray[Any, dtype[float64]], rank: ndarray[Any, dtype[float64]], max_level: int) Tree
Perform uniform tournament crossover operation for genetic programming.
- Parameters:
- individsNDArray
A 1D array containing two or more individuals represented as trees.
- fitnessNDArray[np.float64]
A 1D array containing the fitness values of individuals.
- rankNDArray[np.float64]
A 1D array containing the rank values of individuals. The higher the rank, the better the individual. The rank values are used to determine the probability of selecting genetic material from each parent.
- max_levelint
Maximum allowed depth/level of the resulting offspring. (not used)
- Returns:
- Tree
Offspring resulting from the uniform tournament crossover operation.
Notes
Uniform tournament crossover is a genetic programming operator. It involves selecting a common region between two or more parents, and the genetic material is exchanged at randomly chosen points within this region. The probability of selecting genetic material from a parent is determined through a tournament selection process, where individuals with higher fitness or rank values have a higher chance of contributing genetic material to the offspring. The resulting tree represents a combination of genetic material from all parents.
Examples
>>> import numpy as np >>> from thefittest.utils.crossovers import uniform_tournament_crossover_GP >>> from thefittest.base import Tree >>> from thefittest.base import init_symbolic_regression_uniset >>> >>> # Example >>> >>> X = np.array([[0.3, 0.7], [0.3, 1.1], [3.5, 11.0]], dtype=np.float64) >>> functional_set_names = ("add", "mul") >>> max_tree_level = 5 >>> >>> # Initialize Universal Set for Symbolic Regression >>> universal_set = init_symbolic_regression_uniset(X, functional_set_names) >>> >>> parent1 = Tree.random_tree(universal_set, max_tree_level) >>> parent2 = Tree.random_tree(universal_set, max_tree_level) >>> parent3 = Tree.random_tree(universal_set, max_tree_level) >>> >>> fitness_values = np.array([0.5, 0.8, 0.6], dtype=np.float64) >>> ranks = np.array([2.0, 1.0, 3.0], dtype=np.float64) >>> max_depth = 7 # Set the maximum allowed depth >>> >>> # Perform uniform tournament crossover >>> offspring = uniform_tournament_crossover_GP(np.array([parent1, parent2, parent3]), fitness_values, ranks, max_depth) >>> >>> # Display results >>> print("Parent 1:", parent1) Parent 1: ... >>> print("Parent 2:", parent2) Parent 2: ... >>> print("Parent 3:", parent3) Parent 3: ... >>> print("Offspring After Uniform Tournament Crossover:", offspring) Offspring After Uniform Tournament Crossover: ...