Problem Definitions

The pycellga.problems package provides a collection of problem definitions for optimization tasks. Each problem is structured as a class inheriting from a common abstract base, ensuring consistency and flexibility across various types of optimization tasks. This module includes single-objective problems, designed to serve as benchmarks or examples for testing and applying genetic algorithms.

AbstractProblem Class

The AbstractProblem class is an abstract base class designed to standardize the definition of optimization problems within the pycellga framework. It integrates seamlessly with the pymoo optimization library and provides a consistent structure for implementing custom problem definitions.

Key Features

  • Gene Type Specification: The gen_type attribute allows users to define the type of genes (REAL, BINARY, etc.), ensuring flexibility for various problem domains.

  • Design Variable Management: Users can define the number of variables (n_var) and their bounds (xl and xu), accurately capturing the problem’s decision space.

  • Abstract Fitness Function: The f method must be implemented in subclasses to compute the fitness of a solution, serving as the core of any optimization problem.

  • Compatibility with Optimizers: The evaluate method ensures smooth integration with pymoo optimizers, handling batch evaluations and storing results efficiently.

Attributes

  • `gen_type`: Specifies the gene type for the problem (e.g., REAL, BINARY).

  • `n_var`: Number of design variables in the problem.

  • `xl` and `xu`: Lower and upper bounds for each design variable, provided as lists or arrays.

Methods

  • `f(x: List[Any]) -> float`: Abstract method for computing the fitness of a solution. This must be implemented in derived classes.

  • `evaluate(x, out, *args, **kwargs)`: A method compatible with pymoo optimizers that wraps the f method and stores computed fitness values in an output dictionary.

Example

Below is an example of how to create a custom optimization problem by inheriting from AbstractProblem:

from pycellga.problems.abstract_problem import AbstractProblem
from pycellga.common import GeneType

from typing import List
import numpy as np

class MyProblem(AbstractProblem):
    def __init__(self):
        super().__init__(gen_type=GeneType.REAL, n_var=10, xl=10, xu=10)

    def f(self, x: List[float]) -> float:
        # Example fitness function: Sphere function
        return np.sum(np.square(x))

This structure simplifies the process of defining optimization problems, enabling experimentation with diverse formulations while ensuring compatibility with modern optimization libraries.

API References

class AbstractProblem(gen_type: GeneType, n_var, xl, xu)[source]

Bases: Problem, ABC

Abstract base class for defining optimization problems compatible with pymoo.

This class provides a structure for defining optimization problems, where the user specifies the gene type, the number of variables, and their bounds. It includes an abstract method f for evaluating the fitness of a solution, which must be implemented by subclasses.

gen_type

The type of genes used in the problem (e.g., REAL, BINARY).

Type:

GeneType

n_var

The number of design variables.

Type:

int

xl

The lower bounds for the design variables.

Type:

List[float] or numpy.ndarray

xu

The upper bounds for the design variables.

Type:

List[float] or numpy.ndarray

f(x: List[Any]) float[source]

Abstract method to compute the fitness value for a given solution. Must be implemented by subclasses.

evaluate(x, out, \*args, \*\*kwargs)[source]

Computes the objective value(s) for pymoo’s optimization framework.

__init__(gen_type: GeneType, n_var, xl, xu)[source]

Initialize the AbstractProblem with gene type, variable count, and bounds.

Parameters:
  • gen_type (Any) – The type of genes used in the problem (e.g., REAL, BINARY).

  • n_var (int) – The number of design variables.

  • xl (float) – The lower bound for the design variables.

  • xu (float) – The upper bound for the design variables.

evaluate(x, out, *args, **kwargs)[source]

Evaluate function for compatibility with pymoo’s optimizer.

This method wraps the f method and allows pymoo to handle batch evaluations by storing the computed fitness values in the output dictionary.

Parameters:
  • x (numpy.ndarray) – Array of input variables.

  • out (dict) – Dictionary to store the output fitness values.

abstract f(x: List[Any]) float[source]

Abstract method for evaluating the fitness of a solution.

This method must be implemented by subclasses to define the objective function of the optimization problem.

Parameters:

x (list) – List of design variable values.

Returns:

The computed fitness value for the given solution.

Return type:

float

Single-Objective Problems

The single_objective subpackage includes a set of benchmark functions commonly used to evaluate optimization algorithms in terms of convergence speed, accuracy, and robustness. These problems are designed for scenarios where only one objective needs to be optimized.