pycellga package
Subpackages
- pycellga.example package
- pycellga.mutation package
- Submodules
- pycellga.mutation.bit_flip_mutation module
- pycellga.mutation.byte_mutation module
- pycellga.mutation.byte_mutation_random module
- pycellga.mutation.float_uniform_mutation module
- pycellga.mutation.insertion_mutation module
- pycellga.mutation.mutation_operator module
- pycellga.mutation.shuffle_mutation module
- pycellga.mutation.swap_mutation module
- pycellga.mutation.two_opt_mutation module
- Module contents
- pycellga.neighborhoods package
- pycellga.problems package
- pycellga.recombination package
- Submodules
- pycellga.recombination.arithmetic_crossover module
- pycellga.recombination.blxalpha_crossover module
- pycellga.recombination.byte_one_point_crossover module
- pycellga.recombination.byte_uniform_crossover module
- pycellga.recombination.flat_crossover module
- pycellga.recombination.linear_crossover module
- pycellga.recombination.one_point_crossover module
- pycellga.recombination.pmx_crossover module
- pycellga.recombination.recombination_operator module
- pycellga.recombination.two_point_crossover module
- pycellga.recombination.unfair_avarage_crossover module
- pycellga.recombination.uniform_crossover module
- Module contents
- pycellga.selection package
- pycellga.tests package
- Submodules
- pycellga.tests.conftest module
- pycellga.tests.test_ackley module
- pycellga.tests.test_arithmetic_crossover module
- pycellga.tests.test_bentcigar_function module
- pycellga.tests.test_bit_flip_mutation module
- pycellga.tests.test_blxalpha_crossover module
- pycellga.tests.test_bohachevsky module
- pycellga.tests.test_byte_mutation module
- pycellga.tests.test_byte_mutation_random module
- pycellga.tests.test_byte_one_point_crossover module
- pycellga.tests.test_byte_operators module
- pycellga.tests.test_byte_uniform_crossover module
- pycellga.tests.test_chichinadze_function module
- pycellga.tests.test_compact_13 module
- pycellga.tests.test_compact_21 module
- pycellga.tests.test_compact_25 module
- pycellga.tests.test_compact_9 module
- pycellga.tests.test_count_sat module
- pycellga.tests.test_dropwave_function module
- pycellga.tests.test_ecc module
- pycellga.tests.test_flat_crossover module
- pycellga.tests.test_float_uniform_mutation module
- pycellga.tests.test_fms module
- pycellga.tests.test_grid module
- pycellga.tests.test_griewank_function module
- pycellga.tests.test_holzman_function module
- pycellga.tests.test_individual module
- pycellga.tests.test_insertion_mutation module
- pycellga.tests.test_levy_function module
- pycellga.tests.test_linear_5 module
- pycellga.tests.test_linear_9 module
- pycellga.tests.test_linear_crossover module
- pycellga.tests.test_matyas_function module
- pycellga.tests.test_maxcut100 module
- pycellga.tests.test_maxcut20_01 module
- pycellga.tests.test_maxcut20_09 module
- pycellga.tests.test_mmdp module
- pycellga.tests.test_one_max module
- pycellga.tests.test_one_point_crossover module
- pycellga.tests.test_optimizer_alpha_cga module
- pycellga.tests.test_optimizer_ccga module
- pycellga.tests.test_optimizer_cga module
- pycellga.tests.test_optimizer_mccga module
- pycellga.tests.test_optimizer_sync_cga module
- pycellga.tests.test_peak module
- pycellga.tests.test_pmx_crossover module
- pycellga.tests.test_population module
- pycellga.tests.test_pow_function module
- pycellga.tests.test_powell_function module
- pycellga.tests.test_rastrigin module
- pycellga.tests.test_rosenbrock module
- pycellga.tests.test_rothellipsoid_function module
- pycellga.tests.test_roulette_wheel_selection module
- pycellga.tests.test_schaffer2_function module
- pycellga.tests.test_schaffer_function module
- pycellga.tests.test_schwefel module
- pycellga.tests.test_shuffle_mutation module
- pycellga.tests.test_sphere module
- pycellga.tests.test_styblinskitang_function module
- pycellga.tests.test_sumofdifferentpowers_function module
- pycellga.tests.test_swap_mutation module
- pycellga.tests.test_threehumps_function module
- pycellga.tests.test_tournament_selection module
- pycellga.tests.test_tsp module
- pycellga.tests.test_two_opt_mutation module
- pycellga.tests.test_two_point_crossover module
- pycellga.tests.test_unfair_average_crossover module
- pycellga.tests.test_uniform_crossover module
- pycellga.tests.test_zakharov_function module
- pycellga.tests.test_zettle_function module
- Module contents
Submodules
pycellga.byte_operators module
- pycellga.byte_operators.bits_to_float(bit_list: list[int]) float [source]
Convert a bit representation to its float value.
- Parameters:
bit_list (list of int) – A list of 32 integers (0 or 1) representing the bit pattern of the float.
- Returns:
The float value represented by the bit pattern.
- Return type:
float
- pycellga.byte_operators.bits_to_floats(bit_list: list[int]) list[float] [source]
Convert a combined bit representation back to a list of floats.
- Parameters:
bit_list (list of int) – A list of integers (0 or 1) representing the combined bit patterns of the floats.
- Returns:
The list of float values represented by the bit pattern.
- Return type:
list of float
- pycellga.byte_operators.float_to_bits(float_number: float) list[int] [source]
Convert a float to its bit representation.
- Parameters:
float_number (float) – The float number to be converted.
- Returns:
A list of 32 integers (0 or 1) representing the bit pattern of the float.
- Return type:
list of int
- pycellga.byte_operators.floats_to_bits(float_list: list[float]) list[int] [source]
Convert a list of floats to their combined bit representation.
- Parameters:
float_list (list of float) – The list of float numbers to be converted.
- Returns:
A list of integers (0 or 1) representing the combined bit patterns of the floats.
- Return type:
list of int
pycellga.grid module
- class pycellga.grid.Grid(n_rows: int, n_cols: int)[source]
Bases:
object
A class to represent a 2D grid.
- n_rows
Number of rows in the grid.
- Type:
int
- n_cols
Number of columns in the grid.
- Type:
int
pycellga.individual module
- class pycellga.individual.GeneType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
Enum
GeneType is an enumeration class that represents the type of genome representation for an individual in an evolutionary algorithm. The three types of genome representation are BINARY, PERMUTATION, and REAL.
- BINARY = 1
- PERMUTATION = 2
- REAL = 3
- class pycellga.individual.Individual(gen_type: GeneType = GeneType.BINARY, ch_size: int = 0, mins: list[float] = [], maxs: list[float] = [])[source]
Bases:
object
A class to represent an individual in an evolutionary algorithm.
- chromosome
The chromosome representing the individual.
- Type:
list
- fitness_value
The fitness value of the individual.
- Type:
float
- position
The position of the individual, represented as a tuple (x, y).
- Type:
tuple
- neighbors_positions
The positions of the individual’s neighbors.
- Type:
list or None
- neighbors
The list of neighbors for the individual.
- Type:
list or None
- gen_type
The enum type of genome representation (GeneType.BINARY, GeneType.PERMUTATION, GeneType.REAL).
- Type:
- ch_size
The size of the chromosome.
- Type:
int
- __init__(gen_type: GeneType = GeneType.BINARY, ch_size: int = 0, mins: list[float] = [], maxs: list[float] = [])[source]
Initialize an Individual with a specific genome type and chromosome size.
- Parameters:
gen_type (str, optional) – The type of genome representation. Must be one of GeneType.BINARY, GeneType.PERMUTATION, or GeneType.REAL. (default is GeneType.BINARY)
ch_size (int) – The size of the chromosome.
mins (list[float]) – The minimum values for each gene in the chromosome.
maxs (list[float]) – The maximum values for each gene in the chromosome.
Description
------------
algorithm. (The Individual class represents an individual in an evolutionary)
BINARY (If the genome type is)
1s. (the chromosome is a list of 0s and)
PERMUTATION (If the genome type is)
permutation. (the chromosome is a list of integers representing a)
cases (In both the binary and permutation)
chromosome. (the mins and maxs lists are used to define the range of each gene in the)
REAL (If the genome type is)
numbers. (the chromosome is a list of real)
case (In this)
chromosome.
- generate_candidate(probvector: list) list [source]
Generate a candidate chromosome based on the given probability vector.
- Parameters:
vector (list of float) – The probability vector used to generate the candidate chromosome.
- Returns:
The generated candidate chromosome as a list of 0s and 1s.
- Return type:
list
- getneighbors() list [source]
Get the list of neighbors for the individual.
- Returns:
The list of neighbors for the individual.
- Return type:
list or None
- getneighbors_positions() list [source]
Get the positions of the individual’s neighbors.
- Returns:
The positions of the individual’s neighbors.
- Return type:
list or None
- randomize()[source]
Randomly initialize the chromosome based on the genome type.
- Returns:
The randomly generated chromosome.
- Return type:
list
- Raises:
NotImplementedError – If the genome type is not implemented.
pycellga.optimizer module
- pycellga.optimizer.alpha_cga(n_cols: int, n_rows: int, n_gen: int, ch_size: int, gen_type: str, p_crossover: float, p_mutation: float, problem: AbstractProblem, selection: SelectionOperator, recombination: RecombinationOperator, mutation: MutationOperator, mins: List[float] = [], maxs: List[float] = []) List [source]
Optimize a problem using an evolutionary algorithm with an alpha-male exchange mechanism.
- Parameters:
n_cols (int) – Number of columns in the grid for the population.
n_rows (int) – Number of rows in the grid for the population.
n_gen (int) – Number of generations to run the optimization.
ch_size (int) – Size of the chromosome.
gen_type (GeneType) – Type of genome representation (GeneType.BINARY, GeneType.PERMUTATION, or GeneType.REAL).
p_crossover (float) – Probability of crossover, should be between 0 and 1.
p_mutation (float) – Probability of mutation, should be between 0 and 1.
known_best (float) – Known best solution value for gap calculation.
k_tournament (int) – Tournament size for selection.
problem (AbstractProblem) – The problem instance used to evaluate fitness.
selection (SelectionOperator) – Function used for selection in the evolutionary algorithm.
recombination (RecombinationOperator) – Function used for recombination (crossover) in the evolutionary algorithm.
mutation (MutationOperator) – Function used for mutation in the evolutionary algorithm.
mins (List[float]) – List of minimum values for each gene in the chromosome (for real value optimization).
maxs (List[float]) – List of maximum values for each gene in the chromosome (for real value optimization).
- Returns:
A list containing the best solution found during the optimization process, where the first element is the chromosome, the second is the fitness value, and the third is the generation at which it was found.
- Return type:
List
- pycellga.optimizer.ccga(n_cols: int, n_rows: int, n_gen: int, ch_size: int, gen_type: str, problem: AbstractProblem, selection: SelectionOperator, mins: List[float] = [], maxs: List[float] = []) List [source]
Perform optimization using a (CCGA).
- Parameters:
n_cols (int) – Number of columns in the grid for the population.
n_rows (int) – Number of rows in the grid for the population.
n_gen (int) – Number of generations to run the optimization.
ch_size (int) – Size of the chromosome.
gen_type (GeneType) – Type of genome representation (GeneType.BINARY, Genetype.PERMUTATION, GeneType.REAL).
problem (AbstractProblem) – The problem instance used to evaluate fitness.
selection (SelectionOperator) – Function used for selection in the evolutionary algorithm.
mins (List[float]) – List of minimum values for each gene in the chromosome (for real value optimization).
maxs (List[float]) – List of maximum values for each gene in the chromosome (for real value optimization
- Returns:
A list containing the best solution found during the optimization process, where the first element is the chromosome, the second is the fitness value, and the third is the generation at which it was found.
- Return type:
List
- pycellga.optimizer.cga(n_cols: int, n_rows: int, n_gen: int, ch_size: int, gen_type: str, p_crossover: float, p_mutation: float, problem: AbstractProblem, selection: SelectionOperator, recombination: RecombinationOperator, mutation: MutationOperator, mins: list[float] = [], maxs: list[float] = []) List [source]
Optimize the given problem using a genetic algorithm.
- Parameters:
n_cols (int) – Number of columns in the population grid.
n_rows (int) – Number of rows in the population grid.
n_gen (int) – Number of generations to evolve.
ch_size (int) – Size of the chromosome.
gen_type (str) – Type of the genome representation (e.g., ‘Binary’, ‘Permutation’, ‘Real’).
p_crossover (float) – Probability of crossover (between 0 and 1).
p_mutation (float) – Probability of mutation (between 0 and 1).
known_best (float) – Known best solution for gap calculation.
k_tournament (int) – Size of the tournament for selection.
problem (AbstractProblem) – The problem instance used for fitness evaluation.
selection (SelectionOperator) – Function or class used for selecting parents.
recombination (RecombinationOperator) – Function or class used for recombination (crossover).
mutation (MutationOperator) – Function or class used for mutation.
mins (list[float]) – List of minimum values for each gene in the chromosome (for real value optimization).
maxs (list[float]) – List of maximum values for each gene in the chromosome (for real value optimization).
- Returns:
A list containing the best solution found during the optimization process, where the first element is the chromosome, the second is the fitness value, and the third is the generation at which it was found.
- Return type:
List
- pycellga.optimizer.compete(p1: Individual, p2: Individual) Tuple[Individual, Individual] [source]
Compete between two individuals to determine the better one.
- Parameters:
p1 (Individual) – First individual.
p2 (Individual) – Second individual.
- Returns:
The better individual and the loser.
- Return type:
Tuple[Individual, Individual]
- pycellga.optimizer.generate_probability_vector(mins: List[float], maxs: List[float], ntries: int) List[float] [source]
Generate a probability vector based on the given minimum and maximum values.
- Parameters:
mins (List[float]) – List of minimum values.
maxs (List[float]) – List of maximum values.
ntries (int) – Number of trials for generating the probability vector.
- Returns:
Probability vector.
- Return type:
List[float]
- pycellga.optimizer.mcccga(n_cols: int, n_rows: int, n_gen: int, ch_size: int, gen_type: str, problem: Callable[[List[float]], float], selection: SelectionOperator, mins: list[float], maxs: list[float]) List [source]
Optimize the given problem using a multi-population machine-coded compact genetic algorithm (MCCGA).
- Parameters:
n_cols (int) – Number of columns in the population grid.
n_rows (int) – Number of rows in the population grid.
n_gen (int) – Number of generations to evolve.
ch_size (int) – Size of the chromosome.
gen_type (str) – Type of the genome representation (e.g., ‘Binary’, ‘Permutation’, ‘Real’).
problem (Callable[[List[float]], float]) – Function to evaluate the fitness of a solution. Takes a list of floats and returns a float.
selection (Callable) – Function or class used for selecting parents.
mins (List[float]) – List of minimum values for the probability vector generation.
maxs (List[float]) – List of maximum values for the probability vector generation.
- Returns:
A list containing the best solution found during the optimization process, where the first element is the chromosome, the second is the fitness value, and the third is the generation at which it was found.
- Return type:
List
- pycellga.optimizer.random_vector_between(mins: List[float], maxs: List[float]) List[float] [source]
Generate a random vector of floats between the given minimum and maximum values.
- Parameters:
mins (List[float]) – List of minimum values.
maxs (List[float]) – List of maximum values.
- Returns:
Randomly generated vector.
- Return type:
List[float]
- pycellga.optimizer.sample(probvector: List[float]) List[int] [source]
Sample a vector based on the provided probability vector.
- Parameters:
probvector (List[float]) – Probability vector for sampling.
- Returns:
Sampled binary vector.
- Return type:
List[int]
- pycellga.optimizer.sync_cga(n_cols: int, n_rows: int, n_gen: int, ch_size: int, gen_type: str, p_crossover: float, p_mutation: float, problem: Callable[[List[float]], float], selection: SelectionOperator, recombination: RecombinationOperator, mutation: MutationOperator, mins: List[float] = [], maxs: List[float] = []) List [source]
Optimize the given problem using a synchronous cellular genetic algorithm (Sync-CGA).
- Parameters:
n_cols (int) – Number of columns in the population grid.
n_rows (int) – Number of rows in the population grid.
n_gen (int) – Number of generations to evolve.
ch_size (int) – Size of the chromosome.
gen_type (str) – Type of the genome representation (e.g., ‘Binary’, ‘Permutation’, ‘Real’).
p_crossover (float) – Probability of crossover between parents.
p_mutation (float) – Probability of mutation in offspring.
problem (Callable[[List[float]], float]) – Function to evaluate the fitness of a solution. Takes a list of floats and returns a float.
selection (SelectionOperator) – Function or class used for selecting parents.
recombination (RecombinationOperator) – Function or class used for recombination (crossover).
mutation (MutationOperator) – Function or class used for mutation.
mins (List[float]) – List of minimum values for each gene in the chromosome (for real value optimization).
maxs (List[float]) – List of maximum values for each gene in the chromosome (for real value optimization
- Returns:
A list containing the best solution found during the optimization process, where the first element is the chromosome, the second is the fitness value, and the third is the generation at which it was found.
- Return type:
List
- pycellga.optimizer.update_vector(vector: List[float], winner: Individual, loser: Individual, pop_size: int)[source]
Update the probability vector based on the winner and loser individuals.
- Parameters:
vector (List[float]) – Probability vector to be updated.
winner (Individual) – The winning individual.
loser (Individual) – The losing individual.
pop_size (int) – Size of the population.
pycellga.population module
- class pycellga.population.OptimizationMethod(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
Enum
OptimizationMethod is an enumeration class that represents the optimization methods used in an evolutionary algorithm. The five optimization methods are CGA, SYNCGA, ALPHA_CGA, CCGA, and MCCCGA. “cga”, “sync_cga”, “alpha_cga”, “ccga”, “mcccga”
- ALPHA_CGA = 3
- CCGA = 4
- CGA = 1
- MCCCGA = 5
- SYNCGA = 2
- class pycellga.population.Population(method_name: OptimizationMethod = OptimizationMethod.CGA, ch_size: int = 0, n_rows: int = 0, n_cols: int = 0, gen_type: str = '', problem: AbstractProblem = None, vector: list = [], mins: list[float] = [], maxs: list[float] = [])[source]
Bases:
object
A class to represent a population in an evolutionary algorithm.
- method_name
The name of the optimization method. Must be one of OptimizationMethod.CGA, OptimizationMethod.SYNCGA, OptimizationMethod.ALPHA_CGA, OptimizationMethod.CCGA, or OptimizationMethod.MCCCGA.
- Type:
- ch_size
The size of the chromosome.
- Type:
int
- n_rows
The number of rows in the grid.
- Type:
int
- n_cols
The number of columns in the grid.
- Type:
int
- gen_type
The type of genome representation (GeneType.BINARY, Genetype.PERMUTATION, GeneType.REAL).
- Type:
str
- problem
The problem instance used to evaluate fitness.
- Type:
- vector
A list used to generate candidates for the population (relevant for MCCCGA).
- Type:
list
- __init__(method_name: OptimizationMethod = OptimizationMethod.CGA, ch_size: int = 0, n_rows: int = 0, n_cols: int = 0, gen_type: str = '', problem: AbstractProblem = None, vector: list = [], mins: list[float] = [], maxs: list[float] = [])[source]
Initialize the Population with the specified parameters.
- Parameters:
method_name (OptimizationMethod.) – The name of the optimization method. Must be one of OptimizationMethod.CGA, OptimizationMethod.SYNCGA, OptimizationMethod.ALPHA_CGA, OptimizationMethod.CCGA, or OptimizationMethod.MCCCGA. Default is OptimizationMethod.CGA.
ch_size (int, optional) – The size of the chromosome (default is 0).
n_rows (int, optional) – The number of rows in the grid (default is 0).
n_cols (int, optional) – The number of columns in the grid (default is 0).
gen_type (str, optional) – The type of genome representation (default is an empty string).
problem (AbstractProblem, optional) – The problem instance used to evaluate fitness (default is None).
vector (list, optional) – A list used to generate candidates (default is an empty list).
mins (list[float]) – The minimum values for each gene in the chromosome (for real value optimization).
maxs (list[float]) – The maximum values for each gene in the chromosome (for real value optimization).
- initial_population() List[Individual] [source]
Generate the initial population of individuals.
- Returns:
A list of initialized Individual objects with their respective chromosomes, fitness values, positions, and neighbors.
- Return type:
List[Individual]