utils

Utility functions for MIP optimization.

Functions

sort_replacements

Sort layer replacements by parent layer indices.

get_nested_key

Access nested dictionary values using dot notation.

consecutive_ngrams

Generate all consecutive n-grams from range(l).

exception InfeasibleError

Bases: Exception

Exception raised when optimization problem is infeasible.

consecutive_ngrams(l, n)

Generate all consecutive n-grams from range(l).

Splits range(l) into all consecutive n-grams.

Parameters:
  • l (int) – Length of the range

  • n (int) – Size of each n-gram

Returns:

List of consecutive n-grams

Return type:

list[list[int]]

Example

consecutive_ngrams(7, 2) = [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6]]

get_nested_key(dictionary, nested_key)

Access nested dictionary values using dot notation.

If nested_key is “a.b.c” returns dictionary[“a”][“b”][“c”]

Parameters:
  • dictionary (dict[str, Any]) – Dictionary to access

  • nested_key (str) – Dot-separated key path (e.g., “a.b.c”)

Returns:

Value at the nested key location

Return type:

Any

sort_replacements(layer_replacements)

Sort layer replacements by parent layer indices.

Parameters:

layer_replacements (list[dict]) – List of replacement dictionaries containing “parent_layer_indices”

Returns:

Sorted list of replacements

Return type:

list[dict]