Skip to content

swarmrl.observables.observable Module API Reference

Parent class for the observable.

Observable

Parent class for observables.

Observables act as inputs to the neural networks.

Source code in swarmrl/observables/observable.py
10
11
12
13
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
89
90
91
92
93
94
95
96
class Observable:
    """
    Parent class for observables.

    Observables act as inputs to the neural networks.
    """

    def __init__(self, particle_type: int):
        """
        Constructor for the observable.
        """
        self._shape = None
        self.particle_type: int = particle_type

    def initialize(self, colloids: List[Colloid]):
        """
        Initialize the observable with starting positions of the colloids.

        The parent method will just pass. This is because some observables
        might not need to be initialized. Those that do need to be initialized
        will override this method.

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

        Returns
        -------
        Updates the class state.
        """
        pass

    def get_colloid_indices(self, colloids: List[Colloid], p_type: int = None):
        """
        Get the indices of the colloids in the observable of a specific type.

        Parameters
        ----------
        colloids : List[Colloid]
                List of colloids from which to get the indices.
        p_type : int (default=None)
                Type of the colloids to get the indices for. If None, the
                particle_type attribute of the class is used.


        Returns
        -------
        indices : List[int]
                List of indices for the colloids of a particular type.
        """
        if p_type is None:
            p_type = self.particle_type

        indices = []
        for i, colloid in enumerate(colloids):
            if colloid.type == p_type:
                indices.append(i)

        return indices

    def compute_observable(self, colloids: List[Colloid]) -> List:
        """
        Compute the current state observable for all colloids.

        Parameters
        ----------
        colloids : List[Colloid] (n_colloids, )
                List of all colloids in the system.

        Returns
        -------
        observables : List[np.ndarray] (n_colloids, dimension)
                List of observables, one for each colloid.

        """
        raise NotImplementedError("Implemented in child class.")

    @property
    def observable_shape(self):
        """
        Unchangeable shape of the observable.
        Returns
        -------

        """
        return self._shape

observable_shape property

Unchangeable shape of the observable. Returns


__init__(particle_type)

Constructor for the observable.

Source code in swarmrl/observables/observable.py
17
18
19
20
21
22
def __init__(self, particle_type: int):
    """
    Constructor for the observable.
    """
    self._shape = None
    self.particle_type: int = particle_type

compute_observable(colloids)

Compute the current state observable for all colloids.

Parameters

colloids : List[Colloid] (n_colloids, ) List of all colloids in the system.

Returns

observables : List[np.ndarray] (n_colloids, dimension) List of observables, one for each colloid.

Source code in swarmrl/observables/observable.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def compute_observable(self, colloids: List[Colloid]) -> List:
    """
    Compute the current state observable for all colloids.

    Parameters
    ----------
    colloids : List[Colloid] (n_colloids, )
            List of all colloids in the system.

    Returns
    -------
    observables : List[np.ndarray] (n_colloids, dimension)
            List of observables, one for each colloid.

    """
    raise NotImplementedError("Implemented in child class.")

get_colloid_indices(colloids, p_type=None)

Get the indices of the colloids in the observable of a specific type.

Parameters

colloids : List[Colloid] List of colloids from which to get the indices. p_type : int (default=None) Type of the colloids to get the indices for. If None, the particle_type attribute of the class is used.

Returns

indices : List[int] List of indices for the colloids of a particular type.

Source code in swarmrl/observables/observable.py
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
def get_colloid_indices(self, colloids: List[Colloid], p_type: int = None):
    """
    Get the indices of the colloids in the observable of a specific type.

    Parameters
    ----------
    colloids : List[Colloid]
            List of colloids from which to get the indices.
    p_type : int (default=None)
            Type of the colloids to get the indices for. If None, the
            particle_type attribute of the class is used.


    Returns
    -------
    indices : List[int]
            List of indices for the colloids of a particular type.
    """
    if p_type is None:
        p_type = self.particle_type

    indices = []
    for i, colloid in enumerate(colloids):
        if colloid.type == p_type:
            indices.append(i)

    return indices

initialize(colloids)

Initialize the observable with starting positions of the colloids.

The parent method will just pass. This is because some observables might not need to be initialized. Those that do need to be initialized will override this method.

Parameters

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

Returns

Updates the class state.

Source code in swarmrl/observables/observable.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def initialize(self, colloids: List[Colloid]):
    """
    Initialize the observable with starting positions of the colloids.

    The parent method will just pass. This is because some observables
    might not need to be initialized. Those that do need to be initialized
    will override this method.

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

    Returns
    -------
    Updates the class state.
    """
    pass