metaheuristic_designer.encodings package#

Subpackages#

Submodules#

Module contents#

Concrete encoding implementations provided by the library.

class CompositeEncoding(encodings, **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:
encodingsiterable of Encoding

The encodings to apply in sequence.

**kwargs

Forwarded to ParameterExtendingEncoding.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:

encodings (Iterable[Encoding])

Methods

decode(population)

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

decode_params(genotype[, copy])

Extract the auxiliary parameter blocks from a genotype matrix.

encode(solutions[, params])

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

encode_params(param_dict)

Stack a dictionary of parameter arrays into a single matrix.

extract_params(population_matrix)

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

extract_solution(population_matrix)

Return only the solution part of the genotype matrix.

gather_params()

Overridable thin wrapper around get_params

get_params()

Return a copy of the current parameter dictionary.

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

gather_params()[source]#

Overridable thin wrapper around get_params

encode(solutions, params=None)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
solutions: Iterable

Solutions that should be encoded.

Returns:
population: ndarray

Population array.

Parameters:
  • solutions (Iterable)

  • params (dict | None)

decode(population)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
population: ndarray

Population that should be decoded.

Returns:
solutions: Iterable

List/array of solutions.

Parameters:

population (Iterable)

extract_solution(population_matrix)[source]#

Return only the solution part of the genotype matrix.

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
population_matrixMatrixLike

The full genotype matrix.

Returns:
MatrixLike

The first dimension columns containing the solution.

Parameters:

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

extract_params(population_matrix)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
population_matrixMatrixLike

The full genotype matrix.

Returns:
MatrixLike

The columns beyond dimension that contain the extra parameters.

Parameters:

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

class DefaultEncoding(decode_as_array=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_arraybool, optional

See Encoding. Default True.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:

decode_as_array (bool)

Methods

decode(population)

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

encode(solution)

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

gather_params()

Overridable thin wrapper around get_params

get_params()

Return a copy of the current parameter dictionary.

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

encode(solution)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
solutions: Iterable

Solutions that should be encoded.

Returns:
population: ndarray

Population array.

Parameters:

solution (Iterable)

decode(population)[source]#

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

Return type:

Iterable

Parameters:
population: ndarray

Population that should be decoded.

Returns:
solutions: Iterable

List/array of solutions.

Parameters:

population (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

class Encoding(decode_as_array=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_arraybool, optional

If True, decode() returns a NumPy array instead of an iterable of arbitrary objects. Default False.

namestr, optional

Display name for this encoding.

**kwargs

Additional keyword arguments stored as schedulable parameters.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:

decode_as_array (bool)

Methods

decode(population)

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

encode(solutions)

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

gather_params()

Overridable thin wrapper around get_params

get_params()

Return a copy of the current parameter dictionary.

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

gather_params()[source]#

Overridable thin wrapper around get_params

Return type:

dict

abstract encode(solutions)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
solutions: Iterable

Solutions that should be encoded.

Returns:
population: ndarray

Population array.

Parameters:

solutions (Iterable)

abstract decode(population)[source]#

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

Return type:

Iterable

Parameters:
population: ndarray

Population that should be decoded.

Returns:
solutions: Iterable

List/array of solutions.

Parameters:

population (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

get_state()[source]#
Return type:

dict

class EncodingFromLambda(encode_fn, decode_fn, **kwargs)[source]#

Bases: Encoding

Encoding built from two callables.

Parameters:
encode_fncallable

(solutions) -> population_matrix.

decode_fncallable

(population_matrix) -> solutions.

decode_as_arraybool, optional

See Encoding.

**kwargs

Forwarded to Encoding.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:
  • encode_fn (Callable)

  • decode_fn (Callable)

Methods

decode(population)

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

encode(solution)

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

gather_params()

Overridable thin wrapper around get_params

get_params()

Return a copy of the current parameter dictionary.

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

encode(solution)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
solutions: Iterable

Solutions that should be encoded.

Returns:
population: ndarray

Population array.

Parameters:

solution (Iterable)

decode(population)[source]#

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

Return type:

Iterable

Parameters:
population: ndarray

Population that should be decoded.

Returns:
solutions: Iterable

List/array of solutions.

Parameters:

population (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

class ImageEncoding(shape, color=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:
shapetuple of int

(height, width) of the image.

colorbool, optional

If True (default), the image has 3 colour channels (RGB). If False, it has 1 channel (grayscale).

**kwargs

Forwarded to Encoding.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:
  • shape (Tuple[int, int])

  • color (bool)

Methods

decode(population)

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

encode(solution)

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

gather_params()

Overridable thin wrapper around get_params

get_params()

Return a copy of the current parameter dictionary.

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

encode(solution)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
solutions: Iterable

Solutions that should be encoded.

Returns:
population: ndarray

Population array.

Parameters:

solution (Iterable)

decode(population)[source]#

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

Return type:

Iterable

Parameters:
population: ndarray

Population that should be decoded.

Returns:
solutions: Iterable

List/array of solutions.

Parameters:

population (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

class MatrixEncoding(shape, **kwargs)[source]#

Bases: Encoding

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

Each individual is reshaped according to the given shape.

Parameters:
shapetuple of int

(rows, cols) of the resulting matrix.

**kwargs

Forwarded to Encoding.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:

shape (Tuple[int, int])

Methods

decode(population)

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

encode(solutions)

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

gather_params()

Overridable thin wrapper around get_params

get_params()

Return a copy of the current parameter dictionary.

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

encode(solutions)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
solutions: Iterable

Solutions that should be encoded.

Returns:
population: ndarray

Population array.

Parameters:

solutions (Iterable)

decode(population)[source]#

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

Return type:

Iterable

Parameters:
population: ndarray

Population that should be decoded.

Returns:
solutions: Iterable

List/array of solutions.

Parameters:

population (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

class ParameterExtendingEncoding(dimension, param_sizes, base_encoding=None, verify=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:
dimensionint

Number of decision variables in the solution.

param_sizesiterable of (name, length)

Named blocks of extra parameters appended to the genotype.

base_encodingEncoding, optional

Encoding applied to the solution part. Defaults to DefaultEncoding.

verifybool, optional

If True, additional shape and key checks are performed in encode_params().

**kwargs

Forwarded to Encoding.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:
  • dimension (int)

  • param_sizes (Iterable[Tuple[str, int]])

  • base_encoding (Optional[Encoding])

  • verify (bool)

Methods

decode(population_matrix)

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

decode_params(genotype[, copy])

Extract the auxiliary parameter blocks from a genotype matrix.

encode(solution[, params])

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

encode_params(param_dict)

Stack a dictionary of parameter arrays into a single matrix.

extract_params(population_matrix)

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

extract_solution(population_matrix)

Return only the solution part of the genotype matrix.

gather_params()

Overridable thin wrapper around get_params

get_params()

Return a copy of the current parameter dictionary.

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

encode(solution, params=None)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
solutions: Iterable

Solutions that should be encoded.

Returns:
population: ndarray

Population array.

Parameters:
  • solution (Iterable)

  • params (dict | None)

decode(population_matrix)[source]#

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

Return type:

Iterable

Parameters:
population: ndarray

Population that should be decoded.

Returns:
solutions: Iterable

List/array of solutions.

Parameters:

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

decode_params(genotype, copy=True)[source]#

Extract the auxiliary parameter blocks from a genotype matrix.

Return type:

dict

Parameters:
genotypeMatrixLike

The full genotype matrix (solution + parameters).

copybool, optional

Whether to return copies of the parameter arrays.

Returns:
dict

Dictionary mapping parameter names to their sub-arrays.

Parameters:
  • genotype (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

  • copy (bool)

extract_solution(population_matrix)[source]#

Return only the solution part of the genotype matrix.

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
population_matrixMatrixLike

The full genotype matrix.

Returns:
MatrixLike

The first dimension columns containing the solution.

Parameters:

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

extract_params(population_matrix)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
population_matrixMatrixLike

The full genotype matrix.

Returns:
MatrixLike

The columns beyond dimension that contain the extra parameters.

Parameters:

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

encode_params(param_dict)[source]#

Stack a dictionary of parameter arrays into a single matrix.

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
param_dictdict

Mapping from parameter names to 2-D arrays.

Returns:
MatrixLike

A (population_size, total_param_size) array.

Parameters:

param_dict (dict)

class SigmoidEncoding(as_probability=True, threshold=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_probabilitybool, 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.

thresholdfloat, optional

Threshold used when as_probability=False. Must be in (0, 1). Default is 0.5.

**kwargs

Forwarded to Encoding.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:
  • as_probability (bool)

  • threshold (float)

Methods

decode(population)

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

encode(solutions)

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

gather_params()

Overridable thin wrapper around get_params

get_params()

Return a copy of the current parameter dictionary.

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

encode(solutions)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
solutions: Iterable

Solutions that should be encoded.

Returns:
population: ndarray

Population array.

Parameters:

solutions (Iterable)

decode(population)[source]#

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

Return type:

Iterable

Parameters:
population: ndarray

Population that should be decoded.

Returns:
solutions: Iterable

List/array of solutions.

Parameters:

population (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | 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_dtypeNumPy dtype, optional

The dtype used for the internal genotype (default int).

decoded_dtypeNumPy dtype, optional

The dtype used for the decoded phenotype (default float).

**kwargs

Forwarded to Encoding.

Attributes:
params

Access parameter values by attribute-style lookup.

Methods

decode(population)

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

encode(solutions)

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

gather_params()

Overridable thin wrapper around get_params

get_params()

Return a copy of the current parameter dictionary.

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

encode(solutions)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
solutions: Iterable

Solutions that should be encoded.

Returns:
population: ndarray

Population array.

Parameters:

solutions (Iterable)

decode(population)[source]#

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

Return type:

Iterable

Parameters:
population: ndarray

Population that should be decoded.

Returns:
solutions: Iterable

List/array of solutions.

Parameters:

population (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

class PSOEncoding(dimension, base_encoding=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:
dimensionint

Number of decision variables.

base_encodingEncoding, optional

Encoding applied to the solution part. Defaults to DefaultEncoding.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:
  • dimension (int)

  • base_encoding (Optional[Encoding])

Methods

decode(population_matrix)

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

decode_params(genotype[, copy])

Extract the auxiliary parameter blocks from a genotype matrix.

encode(solution[, params])

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

encode_params(param_dict)

Stack a dictionary of parameter arrays into a single matrix.

extract_params(population_matrix)

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

extract_solution(population_matrix)

Return only the solution part of the genotype matrix.

gather_params()

Overridable thin wrapper around get_params

get_params()

Return a copy of the current parameter dictionary.

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

class SelfAdaptingESEncoding(dimension, single_sigma=True, base_encoding=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:
dimensionint

Number of decision variables.

single_sigmabool, optional

If True (default), a single F value is added. If False, dimension values are added.

base_encodingEncoding, optional

Encoding applied to the solution part. Defaults to DefaultEncoding.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:
  • dimension (int)

  • single_sigma (bool)

  • base_encoding (Optional[Encoding])

Methods

decode(population_matrix)

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

decode_params(genotype[, copy])

Extract the auxiliary parameter blocks from a genotype matrix.

encode(solution[, params])

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

encode_params(param_dict)

Stack a dictionary of parameter arrays into a single matrix.

extract_params(population_matrix)

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

extract_solution(population_matrix)

Return only the solution part of the genotype matrix.

gather_params()

Overridable thin wrapper around get_params

get_params()

Return a copy of the current parameter dictionary.

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state