Utils

exception llamea.utils.NoCodeException

Bases: Exception

Could not extract generated code.

llamea.utils.apply_code_delta(text: str, base_code: str) tuple[str, bool, float]

Assuming the LLM follows the intructions properly, following format of response is expected. ```diff <- (diff may appear sometimes.) # A series of following search replace pattern will appear. <<<<<<< SEARCH for i in range(m):

for j in range(p):
for k in range(n):

C[i, j] += A[i, k] * B[k, j]

for k in range(n):
for j in range(p):

C[i, j] += A[i, k] * B[k, j]

>>>>>>> REPLACE ```

Args:

text: LLM response.text. base_code: Base code to be mutated.

Returns:

Code: updated code, after applying diff. bool: Success of diff mode implementation. float: Ratio of new code similar to the original base_code.

llamea.utils.clean_local_namespace(local_namespace: dict[str, Any], global_namespace: dict[str, Any])

The exec command upon execution, adds global_namespace parameters to local_namespace parameters. This function returns local_ns - gobal_ns, so that sweeping for object type never returns a library imported objects.

Args:

local_namespace : dict[str, Any]: Dictionary that was passed as local_namespace to exec block. global_namespace : dict[str, Any]: Dictionary/Mapping passed as global_namespace to exec block.

Returns:

Original local_namespace, that is local_namespace - global_namespace.

llamea.utils.code_distance(a, b)

Return a rough distance between two solutions based on their ASTs.

The function accepts either Solution objects or raw code strings and computes 1 - similarity of their abstract syntax trees using difflib.SequenceMatcher on the dumped AST representations. 1.0 is returned on parsing errors or when the inputs cannot be processed.

Args:

a: The first solution or Python source code. b: The second solution or Python source code.

Returns:

float: A value in [0, 1] indicating dissimilarity of the code.

llamea.utils.discrete_power_law_distribution(n, beta)

Power law distribution function from: # Benjamin Doerr, Huu Phuoc Le, Régis Makhmara, and Ta Duy Nguyen. 2017. # Fast genetic algorithms. # In Proceedings of the Genetic and Evolutionary Computation Conference (GECCO ‘17). # Association for Computing Machinery, New York, NY, USA, 777–784. # https://doi.org/10.1145/3071178.3071301

llamea.utils.handle_timeout(signum, frame)

Raise a timeout exception

llamea.utils.prepare_namespace(code: str, allowed: list[str], logger: Any = None) tuple[dict[str, Any], str | None]
Prepare exec global_namespace, with the libraries imported in the text, code parameter accepts.

If the imports are not allowed in the environment, a generic object is provided.

Args:

code: str: Code parameter that is to be passed to exec function. allowed: list[str]: A list of allowed pip installable libraries, that are acceptable to be imported. logger: Any: Logger with log_import_fail(list[str]) method in it, LLaMEA has this feature in llamea.loggers.ExperimentLogger.

Returns:

Returns a prepared global_namespace dictionary for exec, of type dict[str, Any], along with an str, potential_issue, which can be passed out to feedback to LLM when exec throws.