metaheuristic_designer.encodings package

Subpackages

Submodules

metaheuristic_designer.encodings.composite_encoding module

Encoding that chains a sequence of encodings into a single composite operation.

class CompositeEncoding(encodings: Iterable[Encoding], **kwargs)[source]

Bases: ParameterExtendingEncoding

Encoding that applies a sequence of encodings in order.

Encodings are applied from first to last for decoding, and in reverse order for encoding. This allows stacking transformations such as type casting followed by reshaping or sigmoid mapping.

Parameters:
  • encodings (iterable of Encoding) – The encodings to apply in sequence.

  • **kwargs – Forwarded to ParameterExtendingEncoding.

gather_params()[source]

Overridable thin wrapper around get_params

encode_func(solution: Iterable, params: dict | None = None) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]
decode_func(solutions: Iterable) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]
encode(solutions: Iterable, params: dict | None = None) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Encodes a list of solutions to our problem to an population matrix.

Parameters:

solutions (Iterable) – Solutions that should be encoded.

Returns:

population – Population array.

Return type:

ndarray

decode(population: Iterable) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Decodes a population matrix into a list/array of solutions.

Parameters:

population (ndarray) – Population that should be decoded.

Returns:

solutions – List/array of solutions.

Return type:

Iterable

extract_solution(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Return only the solution part of the genotype matrix.

Parameters:

population_matrix (MatrixLike) – The full genotype matrix.

Returns:

The first dimension columns containing the solution.

Return type:

MatrixLike

extract_params(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Return only the auxiliary-parameter part of the genotype matrix.

Parameters:

population_matrix (MatrixLike) – The full genotype matrix.

Returns:

The columns beyond dimension that contain the extra parameters.

Return type:

MatrixLike

metaheuristic_designer.encodings.image_encoding module

Encoding for image-based optimization tasks.

class ImageEncoding(shape: Tuple[int, int], color: bool = True, **kwargs)[source]

Bases: Encoding

Encoding that maps between flat genotype vectors and image tensors.

Each individual is reshaped to (height, width, channels). When color is False the channel dimension is omitted (grayscale).

Parameters:
  • shape (tuple of int) – (height, width) of the image.

  • color (bool, optional) – If True (default), the image has 3 colour channels (RGB). If False, it has 1 channel (grayscale).

  • **kwargs – Forwarded to Encoding.

encode(solution: Iterable) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Encodes a list of solutions to our problem to an population matrix.

Parameters:

solutions (Iterable) – Solutions that should be encoded.

Returns:

population – Population array.

Return type:

ndarray

decode(population: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) Iterable[source]

Decodes a population matrix into a list/array of solutions.

Parameters:

population (ndarray) – Population that should be decoded.

Returns:

solutions – List/array of solutions.

Return type:

Iterable

metaheuristic_designer.encodings.matrix_encoding module

Encoding that reshapes vectors into matrices.

class MatrixEncoding(shape: Tuple[int, int], **kwargs)[source]

Bases: Encoding

Encoding that reshapes flat genotype vectors into 2-D matrices.

Each individual is reshaped according to the given shape.

Parameters:
  • shape (tuple of int) – (rows, cols) of the resulting matrix.

  • **kwargs – Forwarded to Encoding.

encode(solutions: Iterable) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Encodes a list of solutions to our problem to an population matrix.

Parameters:

solutions (Iterable) – Solutions that should be encoded.

Returns:

population – Population array.

Return type:

ndarray

decode(population: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) Iterable[source]

Decodes a population matrix into a list/array of solutions.

Parameters:

population (ndarray) – Population that should be decoded.

Returns:

solutions – List/array of solutions.

Return type:

Iterable

metaheuristic_designer.encodings.parameter_extending_encoding module

Encoding that splits a genotype into a solution part and auxiliary parameters.

class ParameterExtendingEncoding(dimension: int, param_sizes: Iterable[Tuple[str, int]], base_encoding: Encoding | None = None, verify: bool = False, **kwargs)[source]

Bases: Encoding, ABC

Encoding that appends extra parameters to the solution genotype.

The genotype vector is split into two parts: the first dimension elements hold the actual solution, and the remaining elements store one or more named parameter blocks (e.g., velocity for PSO, mutation strengths for self-adaptation). A base encoding is applied to the solution part; the parameters are stored raw.

Parameters:
  • dimension (int) – Number of decision variables in the solution.

  • param_sizes (iterable of (name, length)) – Named blocks of extra parameters appended to the genotype.

  • base_encoding (Encoding, optional) – Encoding applied to the solution part. Defaults to DefaultEncoding.

  • verify (bool, optional) – If True, additional shape and key checks are performed in encode_params().

  • **kwargs – Forwarded to Encoding.

encode(solution: Iterable, params: dict | None = None) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Encodes a list of solutions to our problem to an population matrix.

Parameters:

solutions (Iterable) – Solutions that should be encoded.

Returns:

population – Population array.

Return type:

ndarray

decode(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) Iterable[source]

Decodes a population matrix into a list/array of solutions.

Parameters:

population (ndarray) – Population that should be decoded.

Returns:

solutions – List/array of solutions.

Return type:

Iterable

decode_params(genotype: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool], copy: bool = True) dict[source]

Extract the auxiliary parameter blocks from a genotype matrix.

Parameters:
  • genotype (MatrixLike) – The full genotype matrix (solution + parameters).

  • copy (bool, optional) – Whether to return copies of the parameter arrays.

Returns:

Dictionary mapping parameter names to their sub-arrays.

Return type:

dict

extract_solution(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Return only the solution part of the genotype matrix.

Parameters:

population_matrix (MatrixLike) – The full genotype matrix.

Returns:

The first dimension columns containing the solution.

Return type:

MatrixLike

extract_params(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Return only the auxiliary-parameter part of the genotype matrix.

Parameters:

population_matrix (MatrixLike) – The full genotype matrix.

Returns:

The columns beyond dimension that contain the extra parameters.

Return type:

MatrixLike

encode_params(param_dict: dict) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Stack a dictionary of parameter arrays into a single matrix.

Parameters:

param_dict (dict) – Mapping from parameter names to 2-D arrays.

Returns:

A (population_size, total_param_size) array.

Return type:

MatrixLike

metaheuristic_designer.encodings.sigmoid_encoding module

Encoding that applies a sigmoid function to enable continuous operators on binary problems.

class SigmoidEncoding(as_probability: bool = True, threshold: float = 0.5, **kwargs)[source]

Bases: Encoding

Encoding that maps binary solutions to continuous values via a sigmoid.

The encoding applies \(\sigma(x) = 1 / (1 + e^{-x})\) pointwise. During encoding, the logit function is applied to the probability parameter (producing real numbers). During decoding, the sigmoid is applied again. This allows real-valued operators (e.g., Gaussian mutation) to be used on binary problems.

Parameters:
  • as_probability (bool, optional) – If True (default), each component is returned as a probability in (0, 1). If False, the probability is thresholded to produce a hard 0/1 value.

  • threshold (float, optional) – Threshold used when as_probability=False. Must be in (0, 1). Default is 0.5.

  • **kwargs – Forwarded to Encoding.

encode(solutions: Iterable) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Encodes a list of solutions to our problem to an population matrix.

Parameters:

solutions (Iterable) – Solutions that should be encoded.

Returns:

population – Population array.

Return type:

ndarray

decode(population: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) Iterable[source]

Decodes a population matrix into a list/array of solutions.

Parameters:

population (ndarray) – Population that should be decoded.

Returns:

solutions – List/array of solutions.

Return type:

Iterable

metaheuristic_designer.encodings.type_cast_encoding module

Encoding that converts between data types (e.g., float ↔ int ↔ bool).

class TypeCastEncoding(encoded_dtype=<class 'int'>, decoded_dtype=<class 'float'>, **kwargs)[source]

Bases: Encoding

Encoding that converts the population to a different NumPy dtype.

During encoding, the solutions are cast to encoded_dtype (the type used internally by operators). During decoding, they are cast to decoded_dtype (the type expected by the objective function).

Parameters:
  • encoded_dtype (NumPy dtype, optional) – The dtype used for the internal genotype (default int).

  • decoded_dtype (NumPy dtype, optional) – The dtype used for the decoded phenotype (default float).

  • **kwargs – Forwarded to Encoding.

encode(solutions: Iterable) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Encodes a list of solutions to our problem to an population matrix.

Parameters:

solutions (Iterable) – Solutions that should be encoded.

Returns:

population – Population array.

Return type:

ndarray

decode(population: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) Iterable[source]

Decodes a population matrix into a list/array of solutions.

Parameters:

population (ndarray) – Population that should be decoded.

Returns:

solutions – List/array of solutions.

Return type:

Iterable

Module contents

Concrete encoding implementations provided by the library.

class CompositeEncoding(encodings: Iterable[Encoding], **kwargs)[source]

Bases: ParameterExtendingEncoding

Encoding that applies a sequence of encodings in order.

Encodings are applied from first to last for decoding, and in reverse order for encoding. This allows stacking transformations such as type casting followed by reshaping or sigmoid mapping.

Parameters:
gather_params()[source]

Overridable thin wrapper around get_params

encode_func(solution: Iterable, params: dict | None = None) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]
decode_func(solutions: Iterable) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]
encode(solutions: Iterable, params: dict | None = None) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Encodes a list of solutions to our problem to an population matrix.

Parameters:

solutions (Iterable) – Solutions that should be encoded.

Returns:

population – Population array.

Return type:

ndarray

decode(population: Iterable) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Decodes a population matrix into a list/array of solutions.

Parameters:

population (ndarray) – Population that should be decoded.

Returns:

solutions – List/array of solutions.

Return type:

Iterable

extract_solution(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Return only the solution part of the genotype matrix.

Parameters:

population_matrix (MatrixLike) – The full genotype matrix.

Returns:

The first dimension columns containing the solution.

Return type:

MatrixLike

extract_params(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Return only the auxiliary-parameter part of the genotype matrix.

Parameters:

population_matrix (MatrixLike) – The full genotype matrix.

Returns:

The columns beyond dimension that contain the extra parameters.

Return type:

MatrixLike

class DefaultEncoding(decode_as_array: bool = True)[source]

Bases: Encoding

Identity encoding - the internal genotype is used directly.

No transformation is applied; encode() and decode() return their arguments unchanged. This is the encoding used when no other is specified.

Parameters:

decode_as_array (bool, optional) – See Encoding. Default True.

encode(solution: Iterable) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Encodes a list of solutions to our problem to an population matrix.

Parameters:

solutions (Iterable) – Solutions that should be encoded.

Returns:

population – Population array.

Return type:

ndarray

decode(population: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) Iterable[source]

Decodes a population matrix into a list/array of solutions.

Parameters:

population (ndarray) – Population that should be decoded.

Returns:

solutions – List/array of solutions.

Return type:

Iterable

class Encoding(decode_as_array: bool = False, name=None, **kwargs)[source]

Bases: ParametrizableMixin, ABC

Translate between internal genotypes and problem-specific phenotypes.

An Encoding is responsible for converting a population matrix (the internal representation used by operators) into a collection of solutions that the objective function understands, and vice versa.

Parameters:
  • decode_as_array (bool, optional) – If True, decode() returns a NumPy array instead of an iterable of arbitrary objects. Default False.

  • name (str, optional) – Display name for this encoding.

  • **kwargs – Additional keyword arguments stored as schedulable parameters.

gather_params() dict[source]

Overridable thin wrapper around get_params

abstract encode(solutions: Iterable) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Encodes a list of solutions to our problem to an population matrix.

Parameters:

solutions (Iterable) – Solutions that should be encoded.

Returns:

population – Population array.

Return type:

ndarray

abstract decode(population: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) Iterable[source]

Decodes a population matrix into a list/array of solutions.

Parameters:

population (ndarray) – Population that should be decoded.

Returns:

solutions – List/array of solutions.

Return type:

Iterable

get_state() dict[source]
class EncodingFromLambda(encode_fn: Callable, decode_fn: Callable, **kwargs)[source]

Bases: Encoding

Encoding built from two callables.

Parameters:
  • encode_fn (callable) – (solutions) -> population_matrix.

  • decode_fn (callable) – (population_matrix) -> solutions.

  • decode_as_array (bool, optional) – See Encoding.

  • **kwargs – Forwarded to Encoding.

encode(solution: Iterable) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Encodes a list of solutions to our problem to an population matrix.

Parameters:

solutions (Iterable) – Solutions that should be encoded.

Returns:

population – Population array.

Return type:

ndarray

decode(population: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) Iterable[source]

Decodes a population matrix into a list/array of solutions.

Parameters:

population (ndarray) – Population that should be decoded.

Returns:

solutions – List/array of solutions.

Return type:

Iterable

class ImageEncoding(shape: Tuple[int, int], color: bool = True, **kwargs)[source]

Bases: Encoding

Encoding that maps between flat genotype vectors and image tensors.

Each individual is reshaped to (height, width, channels). When color is False the channel dimension is omitted (grayscale).

Parameters:
  • shape (tuple of int) – (height, width) of the image.

  • color (bool, optional) – If True (default), the image has 3 colour channels (RGB). If False, it has 1 channel (grayscale).

  • **kwargs – Forwarded to Encoding.

encode(solution: Iterable) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Encodes a list of solutions to our problem to an population matrix.

Parameters:

solutions (Iterable) – Solutions that should be encoded.

Returns:

population – Population array.

Return type:

ndarray

decode(population: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) Iterable[source]

Decodes a population matrix into a list/array of solutions.

Parameters:

population (ndarray) – Population that should be decoded.

Returns:

solutions – List/array of solutions.

Return type:

Iterable

class MatrixEncoding(shape: Tuple[int, int], **kwargs)[source]

Bases: Encoding

Encoding that reshapes flat genotype vectors into 2-D matrices.

Each individual is reshaped according to the given shape.

Parameters:
  • shape (tuple of int) – (rows, cols) of the resulting matrix.

  • **kwargs – Forwarded to Encoding.

encode(solutions: Iterable) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Encodes a list of solutions to our problem to an population matrix.

Parameters:

solutions (Iterable) – Solutions that should be encoded.

Returns:

population – Population array.

Return type:

ndarray

decode(population: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) Iterable[source]

Decodes a population matrix into a list/array of solutions.

Parameters:

population (ndarray) – Population that should be decoded.

Returns:

solutions – List/array of solutions.

Return type:

Iterable

class ParameterExtendingEncoding(dimension: int, param_sizes: Iterable[Tuple[str, int]], base_encoding: Encoding | None = None, verify: bool = False, **kwargs)[source]

Bases: Encoding, ABC

Encoding that appends extra parameters to the solution genotype.

The genotype vector is split into two parts: the first dimension elements hold the actual solution, and the remaining elements store one or more named parameter blocks (e.g., velocity for PSO, mutation strengths for self-adaptation). A base encoding is applied to the solution part; the parameters are stored raw.

Parameters:
  • dimension (int) – Number of decision variables in the solution.

  • param_sizes (iterable of (name, length)) – Named blocks of extra parameters appended to the genotype.

  • base_encoding (Encoding, optional) – Encoding applied to the solution part. Defaults to DefaultEncoding.

  • verify (bool, optional) – If True, additional shape and key checks are performed in encode_params().

  • **kwargs – Forwarded to Encoding.

encode(solution: Iterable, params: dict | None = None) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Encodes a list of solutions to our problem to an population matrix.

Parameters:

solutions (Iterable) – Solutions that should be encoded.

Returns:

population – Population array.

Return type:

ndarray

decode(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) Iterable[source]

Decodes a population matrix into a list/array of solutions.

Parameters:

population (ndarray) – Population that should be decoded.

Returns:

solutions – List/array of solutions.

Return type:

Iterable

decode_params(genotype: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool], copy: bool = True) dict[source]

Extract the auxiliary parameter blocks from a genotype matrix.

Parameters:
  • genotype (MatrixLike) – The full genotype matrix (solution + parameters).

  • copy (bool, optional) – Whether to return copies of the parameter arrays.

Returns:

Dictionary mapping parameter names to their sub-arrays.

Return type:

dict

extract_solution(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Return only the solution part of the genotype matrix.

Parameters:

population_matrix (MatrixLike) – The full genotype matrix.

Returns:

The first dimension columns containing the solution.

Return type:

MatrixLike

extract_params(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Return only the auxiliary-parameter part of the genotype matrix.

Parameters:

population_matrix (MatrixLike) – The full genotype matrix.

Returns:

The columns beyond dimension that contain the extra parameters.

Return type:

MatrixLike

encode_params(param_dict: dict) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Stack a dictionary of parameter arrays into a single matrix.

Parameters:

param_dict (dict) – Mapping from parameter names to 2-D arrays.

Returns:

A (population_size, total_param_size) array.

Return type:

MatrixLike

class SigmoidEncoding(as_probability: bool = True, threshold: float = 0.5, **kwargs)[source]

Bases: Encoding

Encoding that maps binary solutions to continuous values via a sigmoid.

The encoding applies \(\sigma(x) = 1 / (1 + e^{-x})\) pointwise. During encoding, the logit function is applied to the probability parameter (producing real numbers). During decoding, the sigmoid is applied again. This allows real-valued operators (e.g., Gaussian mutation) to be used on binary problems.

Parameters:
  • as_probability (bool, optional) – If True (default), each component is returned as a probability in (0, 1). If False, the probability is thresholded to produce a hard 0/1 value.

  • threshold (float, optional) – Threshold used when as_probability=False. Must be in (0, 1). Default is 0.5.

  • **kwargs – Forwarded to Encoding.

encode(solutions: Iterable) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Encodes a list of solutions to our problem to an population matrix.

Parameters:

solutions (Iterable) – Solutions that should be encoded.

Returns:

population – Population array.

Return type:

ndarray

decode(population: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) Iterable[source]

Decodes a population matrix into a list/array of solutions.

Parameters:

population (ndarray) – Population that should be decoded.

Returns:

solutions – List/array of solutions.

Return type:

Iterable

class TypeCastEncoding(encoded_dtype=<class 'int'>, decoded_dtype=<class 'float'>, **kwargs)[source]

Bases: Encoding

Encoding that converts the population to a different NumPy dtype.

During encoding, the solutions are cast to encoded_dtype (the type used internally by operators). During decoding, they are cast to decoded_dtype (the type expected by the objective function).

Parameters:
  • encoded_dtype (NumPy dtype, optional) – The dtype used for the internal genotype (default int).

  • decoded_dtype (NumPy dtype, optional) – The dtype used for the decoded phenotype (default float).

  • **kwargs – Forwarded to Encoding.

encode(solutions: Iterable) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Encodes a list of solutions to our problem to an population matrix.

Parameters:

solutions (Iterable) – Solutions that should be encoded.

Returns:

population – Population array.

Return type:

ndarray

decode(population: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) Iterable[source]

Decodes a population matrix into a list/array of solutions.

Parameters:

population (ndarray) – Population that should be decoded.

Returns:

solutions – List/array of solutions.

Return type:

Iterable

class PSOEncoding(dimension: int, base_encoding: Encoding | None = None)[source]

Bases: ParameterExtendingEncoding

Encoding for Particle Swarm optimization that stores a velocity vector.

The genotype is split into the solution vector and a velocity vector of the same dimension. Both are used by the PSO operator.

Parameters:
  • dimension (int) – Number of decision variables.

  • base_encoding (Encoding, optional) – Encoding applied to the solution part. Defaults to DefaultEncoding.

class SelfAdaptingESEncoding(dimension: int, single_sigma: bool = True, base_encoding: Encoding | None = None)[source]

Bases: ParameterExtendingEncoding

Encoding for self-adapting Evolution Strategies.

Appends one or more mutation strength values (F) to the solution vector. When single_sigma=True a single step size is shared by all dimensions; otherwise each dimension gets its own step size.

Parameters:
  • dimension (int) – Number of decision variables.

  • single_sigma (bool, optional) – If True (default), a single F value is added. If False, dimension values are added.

  • base_encoding (Encoding, optional) – Encoding applied to the solution part. Defaults to DefaultEncoding.