metaheuristic_designer.utils module#

Utility functions, type aliases, and a JSON encoder used across the library.

exception TerminationException[source]#

Bases: Exception

Custom exception to handle SIGTERM

class NumpyEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]#

Bases: JSONEncoder

JSON encoder that can serialize NumPy scalars, arrays, and Enums.

Use this encoder with json.dumps or json.dump when your data structure contains NumPy integers, floats, or arrays, or when you need to serialize Enum values as their string names.

Methods

default(o)

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

encode(o)

Return a JSON string representation of a Python data structure.

iterencode(o[, _one_shot])

Encode the given object and yield each string representation as available.

default(o)[source]#

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
check_rng(seed)[source]#

Turn seed into an np.random.Generator instance.

Original implementation adapted from: scikit-learn/scikit-learn BSD 3-Clause License, Copyright (c) 2007-2025 The scikit-learn developers.

Return type:

Generator

Parameters:
seedNone, int or instance of Generator

If seed is None, return the Generator singleton used by np.random. If seed is an int, return a new Generator instance seeded with seed. If seed is already a Generator instance, return it. Otherwise raise ValueError.

Returns:
numpy:numpy.random.Generator

The random state object based on seed parameter.

Parameters:

seed (int | Generator | None)

per_individual(func)[source]#

Decorator that applies a function to each row of a 2-D array.

The wrapped function receives a single row (a 1-D array) and any keyword arguments, and must return a 1-D array of the same length. The decorator loops over rows and stacks the results back into a 2-D array.

Parameters:
funccallable

A function (row, **kwargs) -> 1-D array.

Returns:
callable

A function that accepts a 2-D matrix and returns a 2-D array.

per_individual_list(func)[source]#

Decorator that applies a function to each element of a list.

The wrapped function receives a single element and any keyword arguments, and returns a transformed element. The decorator loops over the list and returns a new list of the results.

Parameters:
funccallable

A function (value, **kwargs) -> Any.

Returns:
callable

A function that accepts a list and returns a list.