Nonlinear Programming (NLP)#
Nonlinear programming (NLP) is a subset of optimization where the objective function or constraints are nonlinear. Unlike linear programming, where relationships between variables are linear, NLP deals with more complex systems where variables may interact in intricate ways, resulting in non-straightforward solutions. NLP is used in a variety of fields such as economics, engineering, machine learning, and operations research, where real-world problems often exhibit nonlinear behaviors. The goal of NLP is to find the best possible solution (such as maximum profit or minimum cost) subject to given constraints.
Genetic Algorithms (GA)#
Genetic algorithms (GAs) are a type of optimization algorithm inspired by the principles of natural selection and genetics. GAs work by iteratively evolving a population of potential solutions to a problem through processes like selection, crossover (recombination), and mutation. Each individual solution is represented as a “chromosome,” and better solutions are evolved over generations by selecting and breeding the fittest individuals. GAs are particularly useful for solving complex, nonlinear, or discrete optimization problems where traditional methods may struggle. They are widely applied in fields such as artificial intelligence, engineering, and economics.
For a good explanation of how Genetic Algorithms work, read this from MathWorks.
- class sigmaepsilon.math.optimize.ga.Genom(*, phenotype: list[float] = <factory>, genotype: list[int] = <factory>, fitness: float, age: int = 0, index: int = -1)[source]#
A data class for members of a population.
- model_config: ClassVar[ConfigDict] = {}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
Binary Genetic Algorithm (BGA)#
Binary genetic algorithms (BGA) are a specific type of genetic algorithm where each solution is encoded as a string of binary digits (0s and 1s). These binary strings, known as genotypes (or chromosomes), represent the decision variables in the problem. Through genetic operations like selection, crossover, and mutation, BGAs evolve a population of solutions over time to find the best possible outcome. This approach is particularly well-suited for optimization problems where variables naturally lend themselves to binary encoding, such as combinatorial optimization and certain engineering design tasks.
- class sigmaepsilon.math.optimize.bga.BinaryGeneticAlgorithm(fnc: Callable, ranges: Iterable, *, length: int = 5, p_c: float = 1, p_m: float = 0.2, nPop: int = 100, maxiter: int = 200, miniter: int = 0, elitism: int | float | None = 1, maxage: int = 5, minimize: bool = False)[source]#
An implementation of a Binary Genetic Algorithm (BGA) for finding minimums of real valued unconstrained problems of continuous variables in n-dimensional vector spaces.
The class is able to solve unconstrained optimization problems of the form:
\[\begin{eqnarray} & maximize& \quad f(\mathbf{x}) \quad in \quad \mathbf{x} \in \mathbf{R}^n. \end{eqnarray}\]Note
This class is designed for maximizing the objective function. To minimize it, either negate the objective function or pass
minimize=Truewhen instantiating the class.- Parameters:
fnc (Callable) – The function to evaluate. It is assumed, that the function expects and N number of scalar arguments as a 1d iterable.
ranges (Iterable) – Ranges for each scalar argument to the objective function.
length (int, Optional) – Chromosome length. The higher the value, the more precision. Default is 5.
p_c (float, Optional) – Probability of crossover. Default is 1.
p_m (float, Optional) – Probability of mutation. Default is 0.2.
nPop (int, Optional) – The size of the population. Default is 100.
maxiter (int, Optional) – The maximum number of iterations. Default is 200.
miniter (int, Optional) – The minimum number of iterations. Default is 100.
elitism (float or int, Optional) – Determines the portion of the population designated as elite, which automatically survives to the next generation. If less than or equal to 1, it specifies a fraction of the population. If greater than 1, it indicates the exact number of individuals to be selected as elite. The default value of 1 assures that the reigning champion is always preserved. To turn this off, det the value to None. Default is 1.
ftol (float, Optional) – Torelance for floating point operations. Default is 1e-12.
maxage (int, Optional) – The age is the maximum number of generations a candidate spends at the top (being the best candidate) before termination. Default is 5.
minimize (bool, Optional) – If True, the objective function is minimized. Default is False.
See also
Examples
Find the minimizer of the Rosenbrock function. The exact value of the solution is x = [1.0, 1.0].
>>> from sigmaepsilon.math.optimize import BinaryGeneticAlgorithm as BGA >>> >>> def rosenbrock(x): ... a, b = 1, 100 ... return (a-x[0])**2 + b*(x[1]-x[0]**2)**2 >>> >>> >>> ranges = [[-10, 10], [-10, 10]] >>> bga = BGA(rosenbrock, ranges, length=12, nPop=100, minimize=True) >>> _ = bga.solve() >>> champion = bga.champion >>> x = champion.phenotype >>> fx = champion.fitness