Ejemplo n.º 1
0
    def _set_agent_to_initial_position(self, agent: EnvAgent, new_position: IntVector2D):
        """
        Sets the agent to its initial position. Updates the agent object and the position
        of the agent inside the global agent_position numpy array

        Parameters
        -------
        agent: EnvAgent object
        new_position: IntVector2D
        """
        agent.position = new_position
        self.agent_positions[agent.position] = agent.handle
Ejemplo n.º 2
0
    def _remove_agent_from_scene(self, agent: EnvAgent):
        """
        Remove the agent from the scene. Updates the agent object and the position
        of the agent inside the global agent_position numpy array

        Parameters
        -------
        agent: EnvAgent object
        """
        self.agent_positions[agent.position] = -1
        if self.remove_agents_at_target:
            agent.position = None
            agent.status = RailAgentStatus.DONE_REMOVED
Ejemplo n.º 3
0
    def _move_agent_to_new_position(self, agent: EnvAgent, new_position: IntVector2D):
        """
        Move the agent to the a new position. Updates the agent object and the position
        of the agent inside the global agent_position numpy array

        Parameters
        -------
        agent: EnvAgent object
        new_position: IntVector2D
        """
        agent.position = new_position
        self.agent_positions[agent.old_position] = -1
        self.agent_positions[agent.position] = agent.handle
Ejemplo n.º 4
0
    def click_agent(self, cell_row_col):
        """ The user has clicked on a cell -
            * If there is an agent, select it
              * If that agent was already selected, then deselect it
            * If there is no agent selected, and no agent in the cell, create one
            * If there is an agent selected, and no agent in the cell, move the selected agent to the cell
        """

        # Has the user clicked on an existing agent?
        agent_idx = self.find_agent_at(cell_row_col)

        # This is in case we still have a selected agent even though the env has been recreated
        # with no agents.
        if (self.selected_agent is not None) and (self.selected_agent > len(self.env.agents)):
            self.selected_agent = None

        # Defensive coding below - for cell_row_col to be a tuple, not a numpy array:
        # numpy array breaks various things when loading the env.

        if agent_idx is None:
            # No
            if self.selected_agent is None:
                # Create a new agent and select it.
                agent = EnvAgent(initial_position=tuple(cell_row_col),
                    initial_direction=0, 
                    direction=0,
                    target=tuple(cell_row_col), 
                    moving=False,
                    )
                self.selected_agent = self.env.add_agent(agent)
                # self.env.set_agent_active(agent)
                self.view.oRT.update_background()
            else:
                # Move the selected agent to this cell
                agent = self.env.agents[self.selected_agent]
                agent.initial_position = tuple(cell_row_col)
                agent.position = tuple(cell_row_col)
                agent.old_position = tuple(cell_row_col)
        else:
            # Yes
            # Have they clicked on the agent already selected?
            if self.selected_agent is not None and agent_idx == self.selected_agent:
                # Yes - deselect the agent
                self.selected_agent = None
            else:
                # No - select the agent
                self.selected_agent = agent_idx

        self.redraw()
Ejemplo n.º 5
0
    def click_agent(self, cell_row_col):
        """ The user has clicked on a cell -
            * If there is an agent, select it
              * If that agent was already selected, then deselect it
            * If there is no agent selected, and no agent in the cell, create one
            * If there is an agent selected, and no agent in the cell, move the selected agent to the cell
        """

        # Has the user clicked on an existing agent?
        agent_idx = self.find_agent_at(cell_row_col)

        if agent_idx is None:
            # No
            if self.selected_agent is None:
                # Create a new agent and select it.
                agent = EnvAgent(position=cell_row_col,
                                 direction=0,
                                 target=cell_row_col,
                                 moving=False)
                self.selected_agent = self.env.add_agent(agent)
                self.view.oRT.update_background()
            else:
                # Move the selected agent to this cell
                agent = self.env.agents[self.selected_agent]
                agent.position = cell_row_col
                agent.old_position = cell_row_col
        else:
            # Yes
            # Have they clicked on the agent already selected?
            if self.selected_agent is not None and agent_idx == self.selected_agent:
                # Yes - deselect the agent
                self.selected_agent = None
            else:
                # No - select the agent
                self.selected_agent = agent_idx

        self.redraw()