Genetic Algorithm (NSGA-III)¶
CONFETTI’s own NSGA-III genetic algorithm.
- class confetti.algorithm.BinaryRandomSampling(seed: int | None = None)¶
Bases:
SamplingGenerate an initial population of binary decision vectors.
Each variable in each individual is drawn independently from a Bernoulli(0.5) distribution.
- Parameters:
seed (int or None, default=None) – Seed for the internal
numpy.random.Generator.
- class confetti.algorithm.BitflipMutation(prob: float = 1.0, prob_var: float | None = None)¶
Bases:
MutationFlip individual bits of binary decision vectors.
Each bit in every selected individual is independently flipped with probability
prob_var.There are two probability knobs:
prob— population-level probability that an individual is mutated at all. Handled by the inheritedMutation.do()method.prob_var— per-variable probability that each bit is flipped inside_do(). When None, defaults tomin(0.5, 1 / problem.n_var).
- class confetti.algorithm.Crossover(prob: float = 0.9, n_parents: int = 2, n_offsprings: int = 2)¶
Bases:
ABCBase class for crossover operators with per-mating probability gating.
- Parameters:
- do(problem: Any, X: ndarray, parents: ndarray, rng: Generator) ndarray¶
Perform crossover on selected parent pairs.
- Parameters:
problem (Problem) – The optimization problem.
X (np.ndarray) – Decision-variable matrix of shape
(n, n_var).parents (np.ndarray) – Index array of shape
(n_matings, 2)selecting parent rows.rng (numpy.random.Generator) – Random number generator for probability sampling.
- Returns:
Offspring matrix of shape
(n_offsprings * n_matings, n_var).- Return type:
np.ndarray
- class confetti.algorithm.Mutation(prob: float = 1.0, prob_var: float | None = None)¶
Bases:
ABCBase class for mutation operators with per-individual probability gating.
The
domethod decides which individuals are mutated (controlled byprob). The subclass_doapplies the actual mutation to the selected rows.- Parameters:
- do(problem: Any, X: ndarray, rng: Generator) ndarray¶
Mutate a population with per-individual probability gating.
- Parameters:
problem (Problem) – The optimization problem.
X (np.ndarray) – Decision-variable matrix of shape
(n, n_var).rng (numpy.random.Generator) – Random number generator for probability sampling.
- Returns:
Mutated population, same shape as X.
- Return type:
np.ndarray
- class confetti.algorithm.NSGA3(pop_size: int, ref_dirs: ndarray, sampling: Sampling, crossover: Crossover, mutation: Mutation)¶
Bases:
objectNSGA-III: Non-dominated Sorting Genetic Algorithm III.
- Parameters:
- class confetti.algorithm.Problem(n_var: int, n_obj: int, n_ieq_constr: int = 0)¶
Bases:
ABCBase class for multi-objective optimization problems.
Subclasses must implement
_evaluatewhich populates an output dictionary with objective values ("F") and, optionally, inequality-constraint values ("G").- Parameters:
- evaluate(X: ndarray) tuple[ndarray, ndarray]¶
Evaluate a batch of solutions.
Calls the user-defined
_evaluateand normalises the output shapes so downstream code can rely on consistent array layouts.- Parameters:
X (np.ndarray) – Decision-variable matrix of shape
(n, n_var).- Returns:
(F, G)whereFhas shape(n, n_obj)andGhas shape(n, n_ieq_constr).- Return type:
tuple[np.ndarray, np.ndarray]
- class confetti.algorithm.Result(X: ndarray | None, F: ndarray | None, G: ndarray | None, algorithm: NSGA3)¶
Bases:
objectOutcome of a multi-objective optimisation run.
- ``X``
Decision variables of the feasible non-dominated front, shape
(n_solutions, n_var). None when no feasible solution was found.- Type:
np.ndarray or None
- ``F``
Objective values, shape
(n_solutions, n_obj).- Type:
np.ndarray or None
- ``G``
Inequality-constraint values, shape
(n_solutions, n_ieq_constr).- Type:
np.ndarray or None
- ``algorithm``
The algorithm instance (carries
n_genand other state).- Type:
- class confetti.algorithm.Sampling¶
Bases:
ABCBase class for population initialization operators.
Subclasses must implement
_dowhich returns a raw ndarray of shape(n_samples, problem.n_var).
- class confetti.algorithm.TwoPointCrossover(**kwargs)¶
Bases:
CrossoverBinary two-point crossover.
For each mating, two split points
iandj(0 < i <= j < n) are chosen uniformly at random. Offspring are constructed by swapping the middle segment[i, j)between the two parents:Offspring 0:
parent_0[:i] | parent_1[i:j] | parent_0[j:]Offspring 1:
parent_1[:i] | parent_0[i:j] | parent_1[j:]
- Parameters:
prob (float, default=0.9) – Per-mating probability that crossover is applied. Matings that do not undergo crossover copy parents unchanged. Handled by the inherited
Crossover.do()method.
- confetti.algorithm.das_dennis(n_dim: int, n_partitions: int) ndarray¶
Generate Das-Dennis reference directions on an (
n_dim- 1)-simplex.Produces a set of uniformly spaced weight vectors whose components sum to one and are all non-negative. The number of points equals
C(n_dim + n_partitions - 1, n_partitions)(stars-and-bars).- Parameters:
- Returns:
Array of shape
(n_points, n_dim)withdtype=float64, lexicographically sorted by row.- Return type:
np.ndarray
- Raises:
ValueError – If
n_dim < 1orn_partitions < 0.
References
Das, I. & Dennis, J.E. (1998). Normal-Boundary Intersection: A New Method for Generating the Pareto Surface in Nonlinear Multicriteria Optimization Problems. SIAM Journal on Optimization, 8(3), 631-657.
- confetti.algorithm.minimize(problem: Any, algorithm: NSGA3, termination: Any, seed: int | None = None, verbose: bool = False) Result¶
Run an NSGA-III optimisation.
- Parameters:
- Returns:
The optimisation result.
- Return type:
NSGA-III multi-objective evolutionary algorithm.
- class confetti.algorithm._nsga3.NSGA3(pop_size: int, ref_dirs: ndarray, sampling: Sampling, crossover: Crossover, mutation: Mutation)¶
Bases:
objectNSGA-III: Non-dominated Sorting Genetic Algorithm III.
- Parameters:
- class confetti.algorithm._nsga3.Result(X: ndarray | None, F: ndarray | None, G: ndarray | None, algorithm: NSGA3)¶
Bases:
objectOutcome of a multi-objective optimisation run.
- ``X``
Decision variables of the feasible non-dominated front, shape
(n_solutions, n_var). None when no feasible solution was found.- Type:
np.ndarray or None
- ``F``
Objective values, shape
(n_solutions, n_obj).- Type:
np.ndarray or None
- ``G``
Inequality-constraint values, shape
(n_solutions, n_ieq_constr).- Type:
np.ndarray or None
- ``algorithm``
The algorithm instance (carries
n_genand other state).- Type:
- confetti.algorithm._nsga3.minimize(problem: Any, algorithm: NSGA3, termination: Any, seed: int | None = None, verbose: bool = False) Result¶
Run an NSGA-III optimisation.
- Parameters:
- Returns:
The optimisation result.
- Return type:
Abstract base class for optimization problems.
- class confetti.algorithm._problem.Problem(n_var: int, n_obj: int, n_ieq_constr: int = 0)¶
Bases:
ABCBase class for multi-objective optimization problems.
Subclasses must implement
_evaluatewhich populates an output dictionary with objective values ("F") and, optionally, inequality-constraint values ("G").- Parameters:
- evaluate(X: ndarray) tuple[ndarray, ndarray]¶
Evaluate a batch of solutions.
Calls the user-defined
_evaluateand normalises the output shapes so downstream code can rely on consistent array layouts.- Parameters:
X (np.ndarray) – Decision-variable matrix of shape
(n, n_var).- Returns:
(F, G)whereFhas shape(n, n_obj)andGhas shape(n, n_ieq_constr).- Return type:
tuple[np.ndarray, np.ndarray]
Abstract base classes for genetic algorithm operators.
- class confetti.algorithm._operators.Crossover(prob: float = 0.9, n_parents: int = 2, n_offsprings: int = 2)¶
Bases:
ABCBase class for crossover operators with per-mating probability gating.
- Parameters:
- do(problem: Any, X: ndarray, parents: ndarray, rng: Generator) ndarray¶
Perform crossover on selected parent pairs.
- Parameters:
problem (Problem) – The optimization problem.
X (np.ndarray) – Decision-variable matrix of shape
(n, n_var).parents (np.ndarray) – Index array of shape
(n_matings, 2)selecting parent rows.rng (numpy.random.Generator) – Random number generator for probability sampling.
- Returns:
Offspring matrix of shape
(n_offsprings * n_matings, n_var).- Return type:
np.ndarray
- class confetti.algorithm._operators.Mutation(prob: float = 1.0, prob_var: float | None = None)¶
Bases:
ABCBase class for mutation operators with per-individual probability gating.
The
domethod decides which individuals are mutated (controlled byprob). The subclass_doapplies the actual mutation to the selected rows.- Parameters:
- do(problem: Any, X: ndarray, rng: Generator) ndarray¶
Mutate a population with per-individual probability gating.
- Parameters:
problem (Problem) – The optimization problem.
X (np.ndarray) – Decision-variable matrix of shape
(n, n_var).rng (numpy.random.Generator) – Random number generator for probability sampling.
- Returns:
Mutated population, same shape as X.
- Return type:
np.ndarray
- class confetti.algorithm._operators.Sampling¶
Bases:
ABCBase class for population initialization operators.
Subclasses must implement
_dowhich returns a raw ndarray of shape(n_samples, problem.n_var).
Binary random sampling operator for CONFETTI’s genetic algorithm.
- class confetti.algorithm.sampling.BinaryRandomSampling(seed: int | None = None)¶
Bases:
SamplingGenerate an initial population of binary decision vectors.
Each variable in each individual is drawn independently from a Bernoulli(0.5) distribution.
- Parameters:
seed (int or None, default=None) – Seed for the internal
numpy.random.Generator.
Bitflip mutation operator for CONFETTI’s genetic algorithm.
- class confetti.algorithm.mutation.BitflipMutation(prob: float = 1.0, prob_var: float | None = None)¶
Bases:
MutationFlip individual bits of binary decision vectors.
Each bit in every selected individual is independently flipped with probability
prob_var.There are two probability knobs:
prob— population-level probability that an individual is mutated at all. Handled by the inheritedMutation.do()method.prob_var— per-variable probability that each bit is flipped inside_do(). When None, defaults tomin(0.5, 1 / problem.n_var).
Two-point crossover operator for CONFETTI’s genetic algorithm.
- class confetti.algorithm.crossover.TwoPointCrossover(**kwargs)¶
Bases:
CrossoverBinary two-point crossover.
For each mating, two split points
iandj(0 < i <= j < n) are chosen uniformly at random. Offspring are constructed by swapping the middle segment[i, j)between the two parents:Offspring 0:
parent_0[:i] | parent_1[i:j] | parent_0[j:]Offspring 1:
parent_1[:i] | parent_0[i:j] | parent_1[j:]
- Parameters:
prob (float, default=0.9) – Per-mating probability that crossover is applied. Matings that do not undergo crossover copy parents unchanged. Handled by the inherited
Crossover.do()method.
Das-Dennis reference directions for NSGA-III.
- confetti.algorithm.reference_directions.das_dennis(n_dim: int, n_partitions: int) ndarray¶
Generate Das-Dennis reference directions on an (
n_dim- 1)-simplex.Produces a set of uniformly spaced weight vectors whose components sum to one and are all non-negative. The number of points equals
C(n_dim + n_partitions - 1, n_partitions)(stars-and-bars).- Parameters:
- Returns:
Array of shape
(n_points, n_dim)withdtype=float64, lexicographically sorted by row.- Return type:
np.ndarray
- Raises:
ValueError – If
n_dim < 1orn_partitions < 0.
References
Das, I. & Dennis, J.E. (1998). Normal-Boundary Intersection: A New Method for Generating the Pareto Surface in Nonlinear Multicriteria Optimization Problems. SIAM Journal on Optimization, 8(3), 631-657.