<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def get_agents(self, state_id=None, limit_neighbors=False):
 """Returns list of agents based on their state and connectedness Parameters state_id : int, str, or array-like, optional Used to select agents that have the same specified "state". If state = None, returns all agents regardless of its current state limit_neighbors : bool, optional Returns agents based on whether they are connected to this agent or not. If limit_neighbors = False, returns all agents whether or not it is directly connected to this agent """

        if limit_neighbors:
            agents = self.global_topology.neighbors(self.id)
        else:
            agents = self.get_all_nodes()

        if state_id is None:
            return [self.global_topology.node[_]['agent'] for _ in agents]  # return all regardless of state
        else:
            return [self.global_topology.node[_]['agent'] for _ in agents
                    if self.global_topology.node[_]['agent'].state['id'] == state_id]