openelm.environments package

OpenELM Environments module

class openelm.environments.environments.ArrayGenotype(input_array)[source]

Bases: Genotype, ndarray

to_phenotype()[source]
Return type:

Optional[ndarray]

class openelm.environments.environments.BaseEnvironment[source]

Bases: ABC, Generic[GenoType]

__init__()[source]
property behavior_ndim: int
property behavior_space: ndarray
abstract fitness(x)[source]
Return type:

float

property max_fitness: int
abstract mutate(x)[source]
Return type:

list[TypeVar(GenoType, bound= Genotype)]

abstract random()[source]
Return type:

list[TypeVar(GenoType, bound= Genotype)]

class openelm.environments.environments.FunctionOptim(ndim=2)[source]

Bases: BaseEnvironment[ArrayGenotype]

__init__(ndim=2)[source]
fitness(x)[source]
Return type:

float

mutate(x)[source]
Return type:

list[ArrayGenotype]

random()[source]
Return type:

list[ArrayGenotype]

class openelm.environments.environments.Genotype[source]

Bases: ABC

abstract to_phenotype()[source]
Return type:

Optional[ndarray]

class openelm.environments.environments.ImageGeneration(program_str, result_obj)[source]

Bases: Genotype

Genotype for generated images.

__init__(program_str, result_obj)[source]
to_phenotype(mode='3-channel-avg')[source]
Return type:

Optional[ndarray]

validate()[source]
Return type:

bool

class openelm.environments.environments.ImageOptim(seed, config, target_img, diff_model, behavior_mode='3-channel', run_name=None)[source]

Bases: BaseEnvironment[ImageGeneration]

Mutate programs that return images.

Fitness is simply the absolute difference between the returning image and the target image. To map into the behavior space, if behavior_mode==”3-channel”, the image will be divided into blocks (specified in block_size), and average values of RGB channels in each block will be put together as a point in the behavior space (average-pooling).

__init__(seed, config, target_img, diff_model, behavior_mode='3-channel', run_name=None)[source]

Mutate programs that return images.

Fitness is simply the absolute difference between the returning image and the target image. To map into the behavior space, if behavior_mode==”3-channel”, the image will be divided into blocks (specified in block_size), and average values of RGB channels in each block will be put together as a point in the behavior space (average-pooling).

Parameters:
  • seed (dict) – the seed dict.

  • config (Union[str, dict, DictConfig]) – the config file path or dict.

  • target_img (ndarray) – the target image.

  • diff_model – the diff model (or alternatives).

  • behavior_mode (str) – (Optional) a string indicating the way an individual

  • space. (is mapped into behavior) –

  • run_name (Optional[str]) – (Optional) override the run_name in config.

behavior_mode_spec = {'3-channel-avg': {'genotype_ndim': 3}}
default_diff_model_cls

alias of PromptMutationForImgTask

fitness(x)[source]
Return type:

float

generate_program(code_batch)[source]

Call LM to generate a new program and run it.

Return type:

list[ImageGeneration]

Returns:

An ImageGeneration object containing the code, the resulting image and the error code.

mutate(images_list)[source]

Randomly mutate a batch of codes and evaluate their outputs.

Parameters:

x – the individual to be mutated.

Return type:

list[ImageGeneration]

Returns:

a tuple of the code string and the returning result (None if there is an error).

random()[source]

Randomly generate a batch of codes and evaluate their outputs.

Return type:

list[ImageGeneration]

Returns:

a tuple of the code string and the returning result (None if there is error).

class openelm.environments.environments.MatchString(target)[source]

Bases: BaseEnvironment[StringArrayGenotype]

__init__(target)[source]
fitness(x)[source]
Return type:

float

mutate(x)[source]
Return type:

list[StringArrayGenotype]

random()[source]
Return type:

list[StringArrayGenotype]

class openelm.environments.environments.Sodarace(seed, config, diff_model, eval_ms, max_height=1000, max_width=1000, max_mass=2000, ndim=3, run_name=None)[source]

Bases: BaseEnvironment[Sodaracer]

__init__(seed, config, diff_model, eval_ms, max_height=1000, max_width=1000, max_mass=2000, ndim=3, run_name=None)[source]

Sodarace environment.

Parameters:
  • seed (dict) – the seed dict.

  • config (Union[str, dict, DictConfig, BaseConfig]) – the config file path or dict.

  • diff_model – the diff model (or alternatives).

  • eval_ms (int) – The time in ms for sodaracer evaluation.

  • max_height (int) – (Optional) the maximal height.

  • max_width (int) – (Optional) the maximal width.

  • max_mass (int) – (Optional) the maximal mass.

  • ndim (int) – (Optional) the dimension of behavior space.

  • run_name (Optional[str]) – (Optional) override the run_name in config.

default_diff_model_cls

alias of PromptMutationForSodarace

fitness(x)[source]
Return type:

float

generate_program(code_batch)[source]
Return type:

list[Sodaracer]

mutate(sodaracer_list)[source]
Return type:

list[Sodaracer]

random()[source]
Return type:

list[Sodaracer]

class openelm.environments.environments.Sodaracer(program_str, result_obj)[source]

Bases: Genotype

__init__(program_str, result_obj)[source]

The Sodaracer genotype.

Parameters:
  • program_str (str) – the string for the original code.

  • result_obj (dict) – the dict of sodaracer.

evaluate(eval_ms)[source]
Return type:

float

property fitness: Optional[float]
to_phenotype()[source]
Return type:

Optional[ndarray]

class openelm.environments.environments.StringArrayGenotype(input_array)[source]

Bases: ArrayGenotype

to_phenotype()[source]
Return type:

Optional[ndarray]

openelm.environments.environments.ackley(x)[source]
Return type:

ndarray