OpenELM API Reference

Subpackages

Submodules

ELM

class openelm.elm.ELM(cfg, diff_model_cls=None, env_args=None)[source]

Bases: object

__init__(cfg, diff_model_cls=None, env_args=None)[source]

The main class of ELM.

This class will load a diff model, an environment, and a QD algorithm from the passed config.

Parameters:
  • cfg – The config (e.g. OmegaConf who uses dot to access members).

  • diff_model_cls – (Optional) The class of diff model. One can apply

  • comparison. (alternative models here for) –

  • env_args (Optional[dict]) – (Optional) The argument dict for Environment.

run()[source]

Run the ELM algorithm to evolve the population in the environment.

Returns:

A string representing the maximum fitness genotype. The qd_algorithm class attribute will be updated.

Return type:

str

MAP-Elites

class openelm.map_elites.MAPElites(env, n_bins, init_map=None, history_length=1, save_history=False)[source]

Bases: object

Class implementing MAP-Elites, a quality-diversity algorithm.

MAP-Elites creates a map of high perfoming solutions at each point in a discretized behavior space. First, the algorithm generates some initial random solutions, and evaluates them in the environment. Then, it repeatedly mutates the solutions in the map, and places the mutated solutions in the map if they outperform the solutions already in their niche.

__init__(env, n_bins, init_map=None, history_length=1, save_history=False)[source]

Class implementing MAP-Elites, a quality-diversity algorithm.

Parameters:
  • env (BaseEnvironment) – The environment to evaluate solutions in. This

  • BaseEnvironment (should be a subclass of) –

  • implement (and should) –

  • solutions (mutate existing) –

  • solutions

:param : :param and evaluate solutions for their fitness in the environment.: :type n_bins: int :param n_bins: Number of bins to partition the behavior space into. :type n_bins: int :type init_map: Optional[Map] :param init_map: A map to use for the algorithm. If not passed, :type init_map: Map, optional :param a new map will be created. Defaults to None.: :type history_length: int :param history_length: Length of history to store for each niche (cell) :type history_length: int :param in the map. This acts as a circular buffer: :param so after storing: :param history_length items: :param the buffer starts overwriting the oldest: :param items.: :type save_history: bool :param save_history: Whether to save the history of all :type save_history: bool, optional :param generated solutions: :param even if they are not inserted into the map.: :param Defaults to False.:

maximum_fitness()[source]

Get the maximum fitness value in the map.

niches_filled()[source]

Get the number of niches that have been explored in the map.

qd_score()[source]

Get the quality-diversity score of the map.

The quality-diversity score is the sum of the performance of all solutions in the map.

random_selection()[source]

Randomly select a niche (cell) in the map that has been explored.

Return type:

Optional[tuple]

search(initsteps, totalsteps, atol=1.0)[source]

Run the MAP-Elites search algorithm.

Parameters:
  • initsteps (int) – Number of initial random solutions to generate.

  • totalsteps (int) – Total number of steps to run the algorithm for, including initial steps.

  • atol (float, optional) – Tolerance for how close the best performing solution has to be to the maximum possible fitness before the search stops early. Defaults to 1.

Returns:

A string representation of the best perfoming solution. The

best performing solution object can be accessed via the current_max_genome class attribute.

Return type:

str

to_mapindex(b)[source]

Converts a phenotype (position in behaviour space) to a map index.

Return type:

Optional[tuple]

class openelm.map_elites.Map(dims, fill_value, dtype=<class 'numpy.float32'>, history_length=1)[source]

Bases: object

Class to represent a map of any dimensionality, backed by a numpy array.

This class is necessary to handle the circular buffer for the history dimension.

__init__(dims, fill_value, dtype=<class 'numpy.float32'>, history_length=1)[source]

Class to represent a map of any dimensionality, backed by a numpy array.

This class is a wrapper around a numpy array that handles the circular buffer for the history dimension. We use it for the array of fitnesses, genomes, and to track whether a niche in the map has been explored or not.

Parameters:
  • dims (tuple) – Tuple of ints representing the dimensions of the map.

  • fill_value (float) – Fill value to initialize the array.

  • dtype (type, optional) – Type to pass to the numpy array to initialize it. For example, we initialize the map of genomes with type object. Defaults to np.float32.

  • history_length (int, optional) – Length of history to store for each niche (cell) in the map. This acts as a circular buffer, so after storing history_length items, the buffer starts overwriting the oldest items. Defaults to 1.

property map_size: int

Returns the product of the dimension sizes, not including history.

property maximum: float

Returns the maximum value in the map.

property niches_filled: int

Returns the number of niches in the map that have been explored.

property qd_score: float

Returns the quality-diversity score of the map.

property shape: tuple

Wrapper around the shape of the numpy array.

Diff Models

class openelm.diff_model.DiffModel(cfg, function_template, sandbox_server='http://localhost:5000')[source]

Bases: PromptMutationModel

__init__(cfg, function_template, sandbox_server='http://localhost:5000')[source]
construct_prompt(code)[source]

Construct a prompt from a code string.

Parameters:

code (str) – The code string.

Return type:

tuple[str, str]

Returns:

A tuple of the prompt string and imports plus instruction.

generate_program(code_batch)[source]

Generate a new program for a diff model from a batch of programs.

Given a piece of code, do prompt mutation, execute the code, and return the result.

Parameters:

code (str) – The full code string.

Return type:

list[dict]

Returns:

A numpy array (if successful) or the exception object.

class openelm.diff_model.DiffModelForSodarace(cfg, sandbox_server='http://localhost:5000')[source]

Bases: DiffModel

__init__(cfg, sandbox_server='http://localhost:5000')[source]
class openelm.diff_model.FunctionTemplate(func_name, import_line, func_preamble, instruction)[source]

Bases: object

A function template for a mutation model.

func_name

(str) The name of the function that we want to execute.

import_line

(str) The import lines we add to the code.

func_preamble

(str) The function definition, as well as potentially a

few initial lines to generate code.
instruction

The instruction we give to the model, before the

Type:

str

preamble.
__init__(func_name, import_line, func_preamble, instruction)
func_name: str
func_preamble: str
import_line: str
instruction: str
class openelm.diff_model.MutationModel[source]

Bases: ABC

Base model class for all mutation models.

abstract generate_program(code_batch)[source]
Return type:

list[dict]

class openelm.diff_model.PromptMutationForImgTask(cfg, sandbox_server='http://localhost:5000')[source]

Bases: PromptMutationModel

__init__(cfg, sandbox_server='http://localhost:5000')[source]
reset_shape(shape)[source]
class openelm.diff_model.PromptMutationForSodarace(cfg, sandbox_server='http://localhost:5000')[source]

Bases: PromptMutationModel

__init__(cfg, sandbox_server='http://localhost:5000')[source]
class openelm.diff_model.PromptMutationModel(cfg, function_template, sandbox_server='http://localhost:5000')[source]

Bases: MutationModel

Mutation model that uses prompts to change a seed.

__init__(cfg, function_template, sandbox_server='http://localhost:5000')[source]
construct_prompt(code)[source]

Construct a prompt from a code string.

Parameters:

code (str) – The code string.

Return type:

tuple[str, str]

Returns:

A tuple of the prompt string and imports plus instruction.

generate_program(code_batch)[source]

Generate a new program from a batch of programs.

Given a piece of code, do prompt mutation, execute the code, and return the result.

Parameters:

code (str) – The full code string.

Return type:

list[dict]

Returns:

A numpy array (if successful) or the exception object.