Skip to content

swarmrl.tasks.multi_tasking Module API Reference

Class for multi-tasking.

MultiTasking

Bases: Task

Class for handling multiple tasks.

Source code in swarmrl/tasks/multi_tasking.py
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
class MultiTasking(Task):
    """
    Class for handling multiple tasks.
    """

    def __init__(self, particle_type: int = 0, tasks: List[Task] = []):
        """
        Constructor for multi-tasking.
        """
        super().__init__(particle_type)
        self.tasks = tasks

    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.tasks:
            item.initialize(colloids)

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

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

        Returns
        -------
        rewards : np.ndarray of shape (num_colloids, )
                Array of rewards for each colloid.
        """
        species_indices = self.get_colloid_indices(colloids)
        rewards = np.zeros(len(species_indices))
        for task in self.tasks:
            ts = task(colloids)
            rewards += ts

        return rewards

__call__(colloids)

Computes all observables and returns them in a concatenated list.

Parameters

colloids : list of all colloids.

Returns

rewards : np.ndarray of shape (num_colloids, ) Array of rewards for each colloid.

Source code in swarmrl/tasks/multi_tasking.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def __call__(self, colloids: List[Colloid]) -> np.ndarray:
    """
    Computes all observables and returns them in a concatenated list.

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

    Returns
    -------
    rewards : np.ndarray of shape (num_colloids, )
            Array of rewards for each colloid.
    """
    species_indices = self.get_colloid_indices(colloids)
    rewards = np.zeros(len(species_indices))
    for task in self.tasks:
        ts = task(colloids)
        rewards += ts

    return rewards

__init__(particle_type=0, tasks=[])

Constructor for multi-tasking.

Source code in swarmrl/tasks/multi_tasking.py
18
19
20
21
22
23
def __init__(self, particle_type: int = 0, tasks: List[Task] = []):
    """
    Constructor for multi-tasking.
    """
    super().__init__(particle_type)
    self.tasks = tasks

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/tasks/multi_tasking.py
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 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.tasks:
        item.initialize(colloids)