metaheuristic_designer.encoding module#

Base class for the Encoding module.

This module implements a way to have a different representation in the inner working of the algorithm and the result of the procedure.

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 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 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])