Execute nodes topologically for `num_rounds` and return final answers.
(
self,
inputs: Any,
num_rounds:int = 1,
max_tries: int = 3,
max_time: int = 600,
)
| 211 | out_node.add_successor(in_node,'temporal') |
| 212 | |
| 213 | def run( |
| 214 | self, |
| 215 | inputs: Any, |
| 216 | num_rounds:int = 1, |
| 217 | max_tries: int = 3, |
| 218 | max_time: int = 600, |
| 219 | ) -> List[Any]: |
| 220 | """Execute nodes topologically for `num_rounds` and return final answers.""" |
| 221 | for round in range(num_rounds): |
| 222 | self.construct_spatial_connection() |
| 223 | self.construct_temporal_connection(round) |
| 224 | |
| 225 | in_degree = {node_id: len(node.spatial_predecessors) for node_id, node in self.nodes.items()} |
| 226 | zero_in_degree_queue = [node_id for node_id, deg in in_degree.items() if deg == 0] |
| 227 | |
| 228 | while zero_in_degree_queue: |
| 229 | current_node_id = zero_in_degree_queue.pop(0) |
| 230 | tries = 0 |
| 231 | while tries < max_tries: |
| 232 | try: |
| 233 | self.nodes[current_node_id].execute(inputs) |
| 234 | break |
| 235 | except Exception as e: |
| 236 | logger.exception( |
| 237 | "Error during execution of node {}: {}", |
| 238 | current_node_id, |
| 239 | e, |
| 240 | ) |
| 241 | tries += 1 |
| 242 | for successor in self.nodes[current_node_id].spatial_successors: |
| 243 | if successor.id not in self.nodes.keys(): |
| 244 | continue |
| 245 | in_degree[successor.id] -= 1 |
| 246 | if in_degree[successor.id] == 0: |
| 247 | zero_in_degree_queue.append(successor.id) |
| 248 | |
| 249 | self.update_memory() |
| 250 | if self.decision_node: |
| 251 | self.connect_decision_node() |
| 252 | self.decision_node.execute(inputs) |
| 253 | final_answers = self.decision_node.outputs |
| 254 | if len(final_answers) == 0: |
| 255 | final_answers.append("No answer of the decision node") |
| 256 | else: |
| 257 | final_answers = self.nodes[list(self.nodes.keys())[-1]].outputs |
| 258 | |
| 259 | return final_answers |
| 260 | |
| 261 | async def arun( |
| 262 | self, |
no test coverage detected