Skip to content

swarmrl.observables.multi_sensing Module API Reference

Class for an observable which computes several observables.

MultiSensing

Bases: Observable, ABC

Takes several observables and returns them as a list of observables.

Source code in swarmrl/observables/multi_sensing.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class MultiSensing(Observable, ABC):
    """
    Takes several observables and returns them as a list of observables.
    """

    def __init__(
        self,
        observables: List[Observable],
    ):
        """
        Constructor for the observable.

        In this observables, the order with which the observables are
        passed to the constructor is the order in which they are
        concatenated.

        Parameters
        ----------
        Observables : List[Observable]
                List of observables.
        """
        self.observables = observables

    def initialize(self, colloids: List[Colloid]):
        """
        Initialize the observables as needed.

        Parameters
        ----------
        colloids : List[Colloid]
                List of colloids with which to initialize the observable.

        Returns
        -------
        Some of the observables passed to the constructor might need to be
        initialized with the positions of the colloids. This method does
        that.
        """
        for item in self.observables:
            item.initialize(colloids)

    def compute_observable(self, colloids: List[Colloid]) -> List:
        """
        Computes all observables and returns them in a concatenated list.

        Parameters
        ----------
        colloids : list of all colloids.

        Returns
        -------
        List of observables, computed in the order that they were given
        at initialization.

        Notes
        -----
        This may not work well for observables that return different
        shapes as they must be lists. We should consider a different
        return type such as a dict. This however needs to be tested
        on neural networks so the slower lists will work for now.
        """
        # Get the observables for each colloid.
        unshaped_observable = []  # shape (n_obs, n_colloids, ...)
        for item in self.observables:
            unshaped_observable.append(item.compute_observable(colloids))

        n_colloids = len(unshaped_observable[0])

        # Reshape the observables to be (n_colloids, n_observables, )
        observable = [[] for _ in range(n_colloids)]
        for i, item in enumerate(unshaped_observable):
            for j, colloid in enumerate(item):
                observable[j].append(colloid)

        return onp.array(observable)

__init__(observables)

Constructor for the observable.

In this observables, the order with which the observables are passed to the constructor is the order in which they are concatenated.

Parameters

Observables : List[Observable] List of observables.

Source code in swarmrl/observables/multi_sensing.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def __init__(
    self,
    observables: List[Observable],
):
    """
    Constructor for the observable.

    In this observables, the order with which the observables are
    passed to the constructor is the order in which they are
    concatenated.

    Parameters
    ----------
    Observables : List[Observable]
            List of observables.
    """
    self.observables = observables

compute_observable(colloids)

Computes all observables and returns them in a concatenated list.

Parameters

colloids : list of all colloids.

Returns

List of observables, computed in the order that they were given at initialization.

Notes

This may not work well for observables that return different shapes as they must be lists. We should consider a different return type such as a dict. This however needs to be tested on neural networks so the slower lists will work for now.

Source code in swarmrl/observables/multi_sensing.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def compute_observable(self, colloids: List[Colloid]) -> List:
    """
    Computes all observables and returns them in a concatenated list.

    Parameters
    ----------
    colloids : list of all colloids.

    Returns
    -------
    List of observables, computed in the order that they were given
    at initialization.

    Notes
    -----
    This may not work well for observables that return different
    shapes as they must be lists. We should consider a different
    return type such as a dict. This however needs to be tested
    on neural networks so the slower lists will work for now.
    """
    # Get the observables for each colloid.
    unshaped_observable = []  # shape (n_obs, n_colloids, ...)
    for item in self.observables:
        unshaped_observable.append(item.compute_observable(colloids))

    n_colloids = len(unshaped_observable[0])

    # Reshape the observables to be (n_colloids, n_observables, )
    observable = [[] for _ in range(n_colloids)]
    for i, item in enumerate(unshaped_observable):
        for j, colloid in enumerate(item):
            observable[j].append(colloid)

    return onp.array(observable)

initialize(colloids)

Initialize the observables as needed.

Parameters

colloids : List[Colloid] List of colloids with which to initialize the observable.

Returns

Some of the observables passed to the constructor might need to be initialized with the positions of the colloids. This method does that.

Source code in swarmrl/observables/multi_sensing.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def initialize(self, colloids: List[Colloid]):
    """
    Initialize the observables as needed.

    Parameters
    ----------
    colloids : List[Colloid]
            List of colloids with which to initialize the observable.

    Returns
    -------
    Some of the observables passed to the constructor might need to be
    initialized with the positions of the colloids. This method does
    that.
    """
    for item in self.observables:
        item.initialize(colloids)