Example Implementations in pycellga

The pycellga.example package includes example implementations of various cellular genetic algorithms (CGAs) and their variants. These examples demonstrate how to set up and run different CGA methods using the pycellga framework, providing users with practical insights into the application of these algorithms.

Available Example Modules

### Example: Alpha-CGA Module: pycellga.example.example_alpha_cga

Demonstrates the Alpha Cellular Genetic Algorithm (Alpha-CGA), an enhanced version of CGA that incorporates an “alpha male” mechanism for improved convergence and exploration.

class ExampleProblem[source]

Bases: object

Example problem class to be minimized.

This class implements a simple sum of squares function with a global minimum value of 0, achieved when all elements of the chromosome are equal to 0.

__init__()[source]
f(x)[source]

Compute the objective function value.

This method implements the sum of squares function.

Parameters:

x (list or numpy.ndarray) – The input chromosome represented as a list or array of real values.

Returns:

The computed value of the function given the input x.

Return type:

float

run_alpha_cga_example()[source]

Run the Alpha Cellular Genetic Algorithm (alpha_cga) using the optimizer module.

The alpha_cga is configured with a 5x5 grid, 100 generations, and a chromosome size of 10. The problem being solved is an instance of the ExampleProblem class, with real-valued genes, constrained by specified mins and maxs.

Returns:

A Result instance containing the best solution’s chromosome, its fitness value, and the generation in which it was found.

Return type:

Result

### Example: Compact-CGA (CCGA) Module: pycellga.example.example_ccga

Provides an example of the Compact Cellular Genetic Algorithm (CCGA), which utilizes a compact population structure to minimize memory usage and improve computational efficiency.

class ExampleProblem[source]

Bases: object

Example problem class to be minimized.

This class implements a simple binary optimization problem, where the goal is to maximize the number of 1s.

__init__()[source]
f(x)[source]

Compute the objective function value.

This method implements a simple sum of binary values.

Parameters:

x (list or numpy.ndarray) – The input chromosome represented as a list or array of binary values (0s and 1s).

Returns:

The computed value of the function given the input x.

Return type:

int

run_ccga_example()[source]

Run the Compact Cellular Genetic Algorithm (ccga) using the optimizer module.

The ccga is configured with a 5x5 grid, 100 generations, and a chromosome size of 10. The problem being solved is an instance of the ExampleProblem class, with binary genes, constrained by specified mins and maxs.

Returns:

A Result instance containing the best solution’s chromosome, its fitness value, and the generation in which it was found.

Return type:

Result

### Example: Standard CGA Module: pycellga.example.example_cga

Illustrates the implementation of a standard Cellular Genetic Algorithm (CGA), highlighting the core principles of CGA with basic settings.

class ExampleProblem[source]

Bases: object

Example problem class to be minimized.

This class implements a simple sum of squares function with a global minimum value of 0, achieved when all elements of the chromosome are equal to 0.

__init__()[source]
f(x)[source]

Compute the objective function value.

This method implements the sum of squares function.

Parameters:

x (list or numpy.ndarray) – The input chromosome represented as a list or array of real values.

Returns:

The computed value of the function given the input x.

Return type:

float

run_cga_example()[source]

Run the Cellular Genetic Algorithm (cga) using the optimizer module.

The cga is configured with a 5x5 grid, 100 generations, and a chromosome size of 5. The problem being solved is an instance of the ExampleProblem class, with real-valued genes, constrained by specified mins and maxs.

Returns:

A Result instance containing the best solution’s chromosome, its fitness value, and the generation in which it was found.

Return type:

Result

### Example: Machine-Coded Compact CGA (MCC-CGA) Module: pycellga.example.example_mcccga

Demonstrates the Machine-Coded Compact Cellular Genetic Algorithm (MCC-CGA), which uses byte-level encoding for efficient real-valued optimization in a compact population setting.

class RealProblem[source]

Bases: object

Example problem class to be minimized.

This class implements a simple sum of squares function with a global minimum value of 0, achieved when all elements of the chromosome are equal to 0.

__init__()[source]
f(x)[source]

Compute the objective function value.

This method implements the sum of squares function.

Parameters:

x (list or numpy.ndarray) – The input chromosome represented as a list or array of real values.

Returns:

The computed value of the function given the input x.

Return type:

float

run_mcccga_example()[source]

Run the Machine-Coded Compact Cellular Genetic Algorithm (mcccga) using the optimizer module.

The mcccga is configured with a 5x5 grid, 100 generations, and a chromosome size of 10. The problem being solved is an instance of the RealProblem class, with real genes, constrained by specified mins and maxs.

Returns:

A Result instance containing the best solution’s chromosome, its fitness value, and the generation in which it was found.

Return type:

Result

### Example: Synchronous CGA (Sync-CGA) Module: pycellga.example.example_sync_cga

Illustrates the Synchronous Cellular Genetic Algorithm (Sync-CGA), where all individuals in the population are updated simultaneously, providing an alternative synchronization mechanism in CGA.

class ExampleProblem[source]

Bases: object

Example problem class to be minimized.

This class implements a simple sum of squares function with a global minimum value of 0, achieved when all elements of the chromosome are equal to 0.

__init__()[source]
f(x)[source]

Compute the objective function value.

This method implements the sum of squares function.

Parameters:

x (list or numpy.ndarray) – The input chromosome represented as a list or array of real values.

Returns:

The computed value of the function given the input x.

Return type:

float

run_sync_cga_example()[source]

Run the Synchronous Cellular Genetic Algorithm (sync_cga) using the optimizer module.

The sync_cga is configured with a 5x5 grid, 100 generations, and a chromosome size of 5. The problem being solved is an instance of the ExampleProblem class, with real-valued genes, constrained by specified mins and maxs.

Returns:

A Result instance containing the best solution’s chromosome, its fitness value, and the generation in which it was found.

Return type:

Result

## Full Module Overview

For a comprehensive overview of the example implementations available in the example package, refer to the module documentation below: