palaestrai_mosaik.environment package

Submodules

palaestrai_mosaik.environment.loader module

This module contains the function load_funcs() to load a mosaik environment.

This comprises to instantiate a world object und retrieve sensor and actuator descriptions

palaestrai_mosaik.environment.loader.load_funcs(params)

Load the description functions.

Expects a dictionary containing the keys “module”, “description_func”, and “instance_func”. “module” can either be a python module or a python class. The path segments for modules are separated by a dot “.” and a class is separated by a colon “:”, e.g., if descriptor is a module:

{
    "module": "midas.adapter.harlequin.descriptor",
    "description_func": "describe",
    "instance_func": "get_world",
}

or, if Descriptor is a class:

{
    "module": "midas.adapter.harlequin:Descriptor",
    "description_func": "describe",
    "instance_func": "get_world",
}
Parameters

params (dict) – A dict containing the keys as described above.

Returns

A tuple of the description function and the instance function.

Return type

tuple

palaestrai_mosaik.environment.mosaik_ifs module

This module contains the MosaikInterfaceComponent, which cares for all the precious sensors and actuators.

class palaestrai_mosaik.environment.mosaik_ifs.MosaikInterfaceComponent(sensors, actuators, seed=None)

Bases: object

The mosaik interface component stores all sensors and actuators.

Parameters
  • sensors (list) –

    List of sensor descriptions. A sensor description should either be an instance of harlequin.agent.sensor_information.SensorInformation or a dict like:

    sensor = {
        # Unique identifier of the sensor containing the
        # Simulator ID, the Entity ID, and the attribute name
        "sensor_id": <sid.eid.attr>,
        # String description of an *harlequin.types* object
        "observation_space": "Box(low=0.0, high=1.2, "
                             "shape=(1,), dtype=np.float32)",
    }
    

  • actuators (list) –

    List of actuator descriptions. An actuator description should either be an instance of harlequin.agent.actuator_information.ActuatorInformation or a dict like:

    actuator = {
        # Unique identifier of the actuator containing the
        # Simulator ID, the Entity ID, and the attribute name
        "actuator_id": <sid.eid.attr>,
        # String description of an *harlequin.types* object
        "action_space": "Box(low=0.0, high=1.2, "
                        "shape=(1,), dtype=np.float32)",
    }
    

  • seed (int, optional) – A seed for the random number generator (rng). When more than one agent provides values for the same actuator, one of the values is randomly chosen.

sensors

A list containing all SensorInformation objects.

Type

list

actuators

A list containing all ActuatorInformation objects.

Type

list

senmap

A dict that stores the index of each sensor as value.

Type

dict

actmap

A dict that stores the index of each actuator as value.

Type

dict

create_actuators()

Create actuators from the actuator description.

The description is provided during initialization.

Returns

The list containing the created actuator objects.

Return type

list

create_sensors()

Create sensors from the sensor description.

The description is provided during initialization.

Returns

The list containing the created sensor objects.

Return type

list

get_sensor_readings()

Get all current sensor readings.

This method is called from within the communication process.

Returns

The list with all sensors and updated values.

Return type

list

trigger_actuators(sim, input_data)

Forward the actuator values to the specific simulators.

This method is called from within the mosaik process. In contrast to the sensor calls, the actuators can only be triggered per simulator. Each simulator calls the mosaik scheduler to fetch its input_data. After the normal fetching process, the input_data dict is filled with inputs for the simulator sim.

In this method, all actuators are checked wether they have “better” data or the specific input of the simulator. These values are updated and the old values will be overwritten. Then the actuators are resetted, i.e., setpoints are set to None.

The input_data is then returned to the scheduler, which passes them to the calling simulator.

Notes

It is possible that some simulators may rely on multiple inputs for a certain attribute and throw an error after the ARL injection, or filter for specific data provider and, therefore, ignore the ARL input. As soon as this case occurs, this method should be adapted accordingly.

Parameters
  • sim (mosaik.scenario.ModelMock) – The ModelMock of the simulator calling the get_input_data() function of the mosaik scheduler.

  • input_data (dict) – The currently gathered input data for this simulator. ARL tries to manipulate them.

trigger_sensors(world, time)

Fill the sensors with fresh data from mosaik.

This method is called from within the mosaik process. At each sync point (i.e., the ARLSyncSimulator is stepping), all current outputs of the simulators are written into the sensors.

Parameters
  • world (mosaik.scenario.World) – The current world object.

  • time (int) – The current simulation time

update_actuators(actuators)

Update the actuator values.

This method is called from within the communication process. All actuator values from actuators are written into actuator_map.

Parameters

actuators (list) – A list of actuator values. The values should be wrapped in ActuatorInformation objects, otherwise no guarantee can be given that the values are assigned correctly.

palaestrai_mosaik.environment.mosaik_ifs.check_value(val)
palaestrai_mosaik.environment.mosaik_ifs.restore_value(val)

palaestrai_mosaik.environment.mosaik_world module

This module contains the MosaikWorld class, which is home of the mosaik world object and cares for setup and shutdown of the world.

class palaestrai_mosaik.environment.mosaik_world.MosaikWorld(env)

Bases: object

The home for mosaik worlds.

This class creates sensor and actuator descriptions and the world objectby calling the description_func() and instance_func() defined in the params dictionary.

Furthermore, the ARLSyncSimulator is injected into the world object and integrated into the mosaik dependency graph.

Parameters

env (MosaikEnvironment) – A reference to the environment creating this object.

env

Stores the reference to the environment creating this object.

Type

MosaikEnvironment

world

The world object created during the setup method.

Type

mosaik.scenario.World

end

The number of simulation steps mosaik will simulate.

Type

int

mifs

Stores all sensors and actuators and manages their data.

Type

MosaikInterfaceComponent

connect(agent)

Establishs mosaik connections from sensor entities.

The collected data is not used since the data is directly gathered from the world object. Instead, this step is required to get the simulator in sync with the other simulators. For this purpose, it would be enought to connect to a single other simulator, but to be sure we connect to every simulator that provides sensors.

This is also on reason, why there will be no actuator connections. Furthermore, this would require to make use of the time_shifted parameter of the mosaik.scenario.World.connect() method, which needs to be provided with initial_data, which we not have. Eventually, the actuator data will be injected directly into the world object.

Notes

While the sid has kind of standardized naming, this is not true for the eid, since it is task of each simulator to provide eids. Therefore, this might fail, although the sensor description SHOULD have the correct name.

The connect method of the mosaik world currently does not use the entity’s children. Therefore, passing an empty list is not a problem. This may change in future mosaik versions.

Parameters

agent (mosaik.scenario.Entity) – The agent entity to be connected with other entities.

inject_arl(step_size)

Prepare the world for ARL.

The world object is extended by an instance of the ARLSyncSimulator which controls an ARLAgent mosaik entity.

Returns

The agent entity object created by mosaik.

Return type

mosaik.scenario.Entity

setup(params)

Setup the mosaik environment.

A couple of things need to be done to prepare the environment.

Parameters

params (dict) – A dict with information about the mosaik world to be created.

start()

Start the mosaik simulation process.

palaestrai_mosaik.environment.mosaik_world.get_end(end)

Read the end value from the params dict.

The end value is an integer, but sometimes it is provided as float, or as str like ‘15*60’. In the latter case, the str is evaluated (i.e., multiplied). In any case, end is returned as int.

Module contents