blossom.simulation package
Subpackages
- blossom.simulation.organism_behavior package
Submodules
blossom.simulation.dataset_io module
Load information from a certain dataset, e.g. to resume a simulation, and write world and organism data back to file.
- class blossom.simulation.dataset_io.NPEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]
Bases:
JSONEncoder
Class to help serialize numpy types to json.
- default(obj)[source]
Implement this method in a subclass such that it returns a serializable object for
o
, or calls the base implementation (to raise aTypeError
).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)
- blossom.simulation.dataset_io.load_universe(fn, seed=None)[source]
Load dataset file from JSON.
- Parameters:
fn (str) – Input filename of saved universe dataset
seed (int, Generator, optional) – Random seed for the simulation
- Returns:
population_dict (dict) – A dict of Organism objects reconstructed from the saved dataset
world (World) – World object reconstructed from the saved dataset
seed (int, Generator) – Numpy random number generator from last timestep
blossom.simulation.default_fields module
Built-in dictionaries with world, species, and organism parameters (or fields).
Both Organism and World objects are initialized based on these field dictionaries, and values are either populated from parameter files or take on the default values specified in this module.
blossom.simulation.organism module
- class blossom.simulation.organism.Organism(init_dict={}, seed=None)[source]
Bases:
object
A basic organism structure for all species.
- act(universe)[source]
Method that decides and calls an action for the current timestep. Searches through custom methods and built-in movement methods. The action method specifically selects an action to take, from “move”, “reproduce”, “drink”, and “eat”. Then the appropriate instance method from this class is executed to yield the final list of affect organisms.
- Parameters:
universe (Universe) – Universe containing organism
- Returns:
affected_organisms – Organism or list of organisms affected by this organism’s action.
- Return type:
Organisms, or list of Organisms
- die(cause, in_place=False, original=None)[source]
Method that “kills” organism.
- Parameters:
cause (str) – Cause of this organism’s death.
in_place (bool) – If True, modifies self, otherwise, copy organism and return new Organism object.
- Returns:
dead_organism – New “dead” state of this organism.
- Return type:
- drink(universe)[source]
Method for handling drinking. Searches through custom methods and built-in drinking methods.
- Parameters:
universe (Universe) – Universe containing organism
- Returns:
affected_organisms – Organism or list of organisms affected by this organism’s drinking.
- Return type:
Organisms, or list of Organisms
- eat(universe)[source]
Method for handling eating. Searches through custom methods and built-in eating methods.
- Parameters:
universe (Universe) – Universe containing organism
- Returns:
affected_organisms – Organism or list of organisms affected by this organism’s eating.
- Return type:
Organisms, or list of Organisms
- get_child(other_parent=None, seed=None)[source]
Creates an Organism object with similar properties to self, and can add another parent if it exists. Note that this doesn’t assume anything about how much food / water the child is left with, so these should be set with custom / default reproduction methods.
- is_at_death(cause)[source]
Check various conditions for death.
- Parameters:
cause (str) – Potential cause of this organism’s death.
- Returns:
is_dead – Returns True if organism is dead from the specified cause, False otherwise.
- Return type:
bool
- move(universe)[source]
Method for handling movement. Searches through custom methods and built-in movement methods.
- Parameters:
universe (Universe) – Universe containing organism
- Returns:
affected_organisms – Organism or list of organisms affected by this organism’s movement.
- Return type:
Organisms, or list of Organisms
- reproduce(universe)[source]
Method for handling reproduction. Searches through custom methods and built-in reproduction methods.
- Parameters:
universe (Universe) – Universe containing organism
- Returns:
affected_organisms – Organism or list of organisms affected by this organism’s reproduction. For example, this would include both parent and child organisms.
- Return type:
Organisms, or list of Organisms
- step(universe, do_action=True)[source]
Steps through one time step for this organism. Reflects changes based on actions / behaviors and updates to health parameters.
Returns a list of organisms that the action produced (either new or altered organisms).
- Parameters:
universe (Universe) – Universe containing organism
do_action (bool) – If True, this organism will act, otherwise, it will not.
- Returns:
affected_organisms – List of organisms affected by this organism’s actions or health. This could be an updated version of this organism, especially if the organism dies during the time step, but could also be multiple other organisms affected by actions (i.e. children from reproduction).
- Return type:
list of Organisms
- step_without_acting()[source]
Steps through one time step without acting for this organism.
- Returns:
Note that this returns an Organism object, not a list.
- Return type:
organism
- update_parameter(parameter, value, method='set', in_place=False, original=None)[source]
Update a specific parameter of the organism.
- Parameters:
parameter (string) – Parameter to update.
value – Value with which to update.
method (string) – Method types are: ‘set’, ‘add’, ‘subtract’, ‘append’.
in_place (bool) – If True, modifies self, otherwise, copy organism and return new Organism object.
original (Organism or None) – Original organism we are changing. If it is the original, clone organism so that we aren’t editing the original.
- Returns:
updated_organism – Organism object with updated parameter.
- Return type:
blossom.simulation.parameter_io module
Load information from parameter files and construct world and organism objects at the initial timestep.
- blossom.simulation.parameter_io.create_organisms(species_init_dict, init_world=<blossom.simulation.world.World object>, location_callback=None, seed=None)[source]
Make organism list from an species_init_dict either provided directly or scraped from parameter file. All organisms are from a single species.
- blossom.simulation.parameter_io.load_from_config(fn, seed=None)[source]
Create initial population and world from .yml configuration file.
- blossom.simulation.parameter_io.load_species(fns=None, init_dicts=[{}], init_world=<blossom.simulation.world.World object>, custom_module_fns=[])[source]
Load organisms from available species parameter files or dictionaries.
- Parameters:
fns (list of str) – Input filenames of species parameter files. Different species get different species parameter files, from which the individual organisms are initialized.
init_dicts (list of dict) – Parameter dicts for each species.
init_world (World) – Initial World instance for this Universe.
custom_module_fns (list of str) – List of external Python scripts containing custom organism behaviors.
blossom
will search for methods within each filename included here.
- Returns:
population_dict – A dict of Organism objects constructed from the parameter file.
- Return type:
dict of Organisms
- blossom.simulation.parameter_io.load_species_from_dict(init_dicts, init_world, custom_module_fns=None, seed=None)[source]
Create a list of organisms loaded from Python dicts.
- Parameters:
init_dicts (list of dict) – Input species dictionaries from which the individual organisms are initialized. Each dictionary is for a different species.
init_world (World) – Initial World instance for this Universe.
custom_module_fns (list of str) – List of external Python scripts containing custom organism behaviors.
blossom
will search for methods within each filename included here.seed (int, Generator, optional) – Random seed for the simulation
- Returns:
population_dict – A dict of Organism objects constructed from the parameter file.
- Return type:
dict of Organisms
- blossom.simulation.parameter_io.load_species_from_param_files(fns, init_world, custom_module_fns=None, seed=None)[source]
Load all available species parameter files.
- Parameters:
fns (list of str) – Input filenames of species parameter files. Different species get different species parameter files, from which the individual organisms are initialized.
init_world (World) – Initial World instance for this Universe.
custom_module_fns (list of str) – List of external Python scripts containing custom organism behaviors.
blossom
will search for methods within each filename included here.seed (int, Generator, optional) – Random seed for the simulation
- Returns:
population_dict – A dict of Organism objects constructed from the parameter file.
- Return type:
dict of Organisms
- blossom.simulation.parameter_io.load_world(fn=None, init_dict={})[source]
Load world from either parameter file or dictionary and construct initial World object.
- Parameters:
fn (str) – Input filename of parameter file.
init_dict (dict) – Dictionary containing world parameters.
- Returns:
world – World object constructed from the parameter file.
- Return type:
blossom.simulation.parse_intent module
- blossom.simulation.parse_intent.parse(intent_list, organism_list, seed=None)[source]
Determine whether the intent list is valid and fix it in the event of conflicts.
- Parameters:
intent_list (list of lists of Organisms) – List of lists of organisms with proposed organism states, after each organism has ‘acted’. Length equals number of organisms in the current time step.
organism_list (list of Organisms) – List of current organisms
seed (int, Generator, optional) – Random seed
- Returns:
updated_organism_list (list of Organisms) – List of updated organisms, where organism states that conflict between
intent_list
andorganism_list
are resolved.Conflicts may be cases in which an organism has different states in the
intent list, perhaps arrising from the actions of other organisms that
somehow effect its state. This method resolves those conflicts, so that
there is only one organism with a given organism id present in the final
output list at all times.
blossom.simulation.population_funcs module
- blossom.simulation.population_funcs.get_organism_list(population_dict)[source]
Constructs organism list from population dict data structure.
- blossom.simulation.population_funcs.get_population_dict(organism_list, species_names)[source]
Constructs population dict from organism list data structure.
- blossom.simulation.population_funcs.hash_by_id(organism_list)[source]
Simple hashing by organism id over a list of organisms.
- blossom.simulation.population_funcs.hash_by_location(organism_list)[source]
Simple hashing by organism location over a list of organisms.
- blossom.simulation.population_funcs.organism_filter(organism_list, *conditions)[source]
Selects organisms from organism list according to a set of conditions. Each condition should be a function that receives an Organism object as input and returns a boolean as output.
Example:
organism_filter( population_dict['prey1']['organisms'], lambda organism: organism.alive )
blossom.simulation.universe module
- class blossom.simulation.universe.Universe(dataset_fn=None, config_fn=None, world_param_fn=None, species_param_fns=None, world_param_dict={}, species_param_dicts=[{}], custom_module_fns=None, current_time=0, end_time=1000, project_dir='datasets/', pad_zeros=4, seed=None, **kwargs)[source]
Bases:
object
Create the universe of the simulation.
blossom.simulation.utils module
Common utilities used throughout blossom
blossom.simulation.world module
blossom.simulation.world_generator module
- blossom.simulation.world_generator.constant_2d_list(val, size)[source]
Generate a constant-valued two dimensional array.