predictions.py 6.8 KB
Newer Older
u214892's avatar
u214892 committed
1
2
3
4
"""
Collection of environment-specific PredictionBuilder.
"""

u214892's avatar
u214892 committed
5
6
import numpy as np

u214892's avatar
u214892 committed
7
from flatland.core.env_prediction_builder import PredictionBuilder
u214892's avatar
u214892 committed
8
from flatland.envs.rail_env import RailEnvActions
u214892's avatar
u214892 committed
9
10
11
12
13
14
15
16
17
18


class DummyPredictorForRailEnv(PredictionBuilder):
    """
    DummyPredictorForRailEnv object.

    This object returns predictions for agents in the RailEnv environment.
    The prediction acts as if no other agent is in the environment and always takes the forward action.
    """

19
    def get(self, distancemap, handle=None):
u214892's avatar
u214892 committed
20
        """
u214892's avatar
u214892 committed
21
        Called whenever predict is called on the environment.
u214892's avatar
u214892 committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

        Parameters
        -------
        handle : int (optional)
            Handle of the agent for which to compute the observation vector.

        Returns
        -------
        function
            Returns a dictionary index by the agent handle and for each agent a vector of 5 elements:
            - time_offset
            - position axis 0
            - position axis 1
            - direction
            - action taken to come here
        """
        agents = self.env.agents
        if handle:
            agents = [self.env.agents[handle]]

        prediction_dict = {}
u214892's avatar
u214892 committed
43

u214892's avatar
u214892 committed
44
        for agent in agents:
45
            action_priorities = [RailEnvActions.MOVE_FORWARD, RailEnvActions.MOVE_LEFT, RailEnvActions.MOVE_RIGHT]
u214892's avatar
u214892 committed
46
47
            _agent_initial_position = agent.position
            _agent_initial_direction = agent.direction
48
            prediction = np.zeros(shape=(self.max_depth + 1, 5))
u214892's avatar
u214892 committed
49
            prediction[0] = [0, _agent_initial_position[0], _agent_initial_position[1], _agent_initial_direction, 0]
50
            for index in range(1, self.max_depth + 1):
u214892's avatar
u214892 committed
51
                action_done = False
52
53
54
55
56
57
                # if we're at the target, stop moving...
                if agent.position == agent.target:
                    prediction[index] = [index, agent.target[0], agent.target[1], agent.direction,
                                         RailEnvActions.STOP_MOVING]

                    continue
u214892's avatar
u214892 committed
58
                for action in action_priorities:
u214892's avatar
u214892 committed
59
60
                    cell_isFree, new_cell_isValid, new_direction, new_position, transition_isValid = \
                        self.env._check_action_on_agent(action, agent)
u214892's avatar
u214892 committed
61
62
63
64
65
66
67
68
69
                    if all([new_cell_isValid, transition_isValid]):
                        # move and change direction to face the new_direction that was
                        # performed
                        agent.position = new_position
                        agent.direction = new_direction
                        prediction[index] = [index, new_position[0], new_position[1], new_direction, action]
                        action_done = True
                        break
                if not action_done:
70
                    raise Exception("Cannot move further. Something is wrong")
u214892's avatar
u214892 committed
71
72
73
74
            prediction_dict[agent.handle] = prediction
            agent.position = _agent_initial_position
            agent.direction = _agent_initial_direction
        return prediction_dict
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119


class ShortestPathPredictorForRailEnv(PredictionBuilder):
    """
    DummyPredictorForRailEnv object.

    This object returns predictions for agents in the RailEnv environment.
    The prediction acts as if no other agent is in the environment and always takes the forward action.
    """

    def get(self, distancemap, handle=None):
        """
        Called whenever predict is called on the environment.

        Parameters
        -------
        handle : int (optional)
            Handle of the agent for which to compute the observation vector.

        Returns
        -------
        function
            Returns a dictionary index by the agent handle and for each agent a vector of 5 elements:
            - time_offset
            - position axis 0
            - position axis 1
            - direction
            - action taken to come here
        """
        agents = self.env.agents
        if handle:
            agents = [self.env.agents[handle]]

        prediction_dict = {}
        agent_idx = 0
        for agent in agents:
            _agent_initial_position = agent.position
            _agent_initial_direction = agent.direction
            prediction = np.zeros(shape=(self.max_depth + 1, 5))
            prediction[0] = [0, _agent_initial_position[0], _agent_initial_position[1], _agent_initial_direction, 0]
            for index in range(1, self.max_depth + 1):
                # if we're at the target, stop moving...
                if agent.position == agent.target:
                    prediction[index] = [index, agent.target[0], agent.target[1], agent.direction,
                                         RailEnvActions.STOP_MOVING]
120
121
122
123
                    continue
                if not agent.moving:
                    prediction[index] = [index, agent.position[0], agent.position[1], agent.direction,
                                         RailEnvActions.STOP_MOVING]
124
125
126
127
128
129
130
                    continue
                # Take shortest possible path
                cell_transitions = self.env.rail.get_transitions((*agent.position, agent.direction))

                if np.sum(cell_transitions) == 1:
                    new_direction = np.argmax(cell_transitions)
                    new_position = self._new_position(agent.position, new_direction)
131
132
                elif np.sum(cell_transitions) > 1:
                    min_dist = np.inf
133
                    for direct in range(4):
134
135
136
137
138
                        if cell_transitions[direct] == 1:
                            target_dist = distancemap[agent_idx, agent.position[0], agent.position[1], direct]
                            if target_dist < min_dist:
                                min_dist = target_dist
                                new_direction = direct
139
                    new_position = self._new_position(agent.position, new_direction)
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

                agent.position = new_position
                agent.direction = new_direction
                prediction[index] = [index, new_position[0], new_position[1], new_direction, 0]
                action_done = True
                if not action_done:
                    raise Exception("Cannot move further. Something is wrong")
            prediction_dict[agent.handle] = prediction
            agent.position = _agent_initial_position
            agent.direction = _agent_initial_direction
            agent_idx += 1

        return prediction_dict

    def _new_position(self, position, movement):
        """
        Utility function that converts a compass movement over a 2D grid to new positions (r, c).
        """
        if movement == 0:  # NORTH
            return (position[0] - 1, position[1])
        elif movement == 1:  # EAST
            return (position[0], position[1] + 1)
        elif movement == 2:  # SOUTH
            return (position[0] + 1, position[1])
        elif movement == 3:  # WEST
            return (position[0], position[1] - 1)