使用 Graphviz 生成树形图
(nodes: Dict[str, NodeInfo], root_id: str, output_file: str = "mcts_tree_claude")
| 331 | |
| 332 | |
| 333 | def print_tree_graphviz(nodes: Dict[str, NodeInfo], root_id: str, output_file: str = "mcts_tree_claude"): |
| 334 | """ |
| 335 | 使用 Graphviz 生成树形图 |
| 336 | """ |
| 337 | try: |
| 338 | from graphviz import Digraph |
| 339 | except ImportError: |
| 340 | print("Error: graphviz not installed. Install with: pip install graphviz") |
| 341 | return |
| 342 | |
| 343 | # 找到最佳节点 |
| 344 | best_node_id = None |
| 345 | best_nodes = [n for n in nodes.values() if n.metric is not None] |
| 346 | if best_nodes: |
| 347 | best = min(best_nodes, key=lambda n: n.metric) |
| 348 | best_node_id = best.id |
| 349 | |
| 350 | g = Digraph( |
| 351 | name='MCTS_Tree_Claude', |
| 352 | comment='Claude Code MCTS Search Tree', |
| 353 | format='png', |
| 354 | graph_attr={ |
| 355 | 'rankdir': 'TB', |
| 356 | 'splines': 'ortho', |
| 357 | 'nodesep': '0.5', |
| 358 | 'ranksep': '1.0', |
| 359 | 'bgcolor': '#f5f5f5' |
| 360 | }, |
| 361 | node_attr={ |
| 362 | 'shape': 'box', |
| 363 | 'style': 'rounded,filled', |
| 364 | 'fontsize': '10', |
| 365 | 'fontname': 'Arial' |
| 366 | }, |
| 367 | edge_attr={ |
| 368 | 'arrowhead': 'vee', |
| 369 | 'color': '#666666', |
| 370 | 'penwidth': '1.5' |
| 371 | } |
| 372 | ) |
| 373 | |
| 374 | def add_node_recursive(node_id: str): |
| 375 | if node_id not in nodes: |
| 376 | return |
| 377 | |
| 378 | node = nodes[node_id] |
| 379 | |
| 380 | # 构建标签 |
| 381 | status_sym = "✓" if node.run_status == "SUCCESS" else "✗" if node.run_status == "FAILED" else "?" |
| 382 | stage_name = {"root": "ROOT", "draft": "DRAFT", "improve": "IMPROVE"}.get(node.stage, node.stage.upper()) |
| 383 | |
| 384 | label_lines = [ |
| 385 | f"{status_sym} {stage_name}", |
| 386 | f"ID: {node.id[:8]}", |
| 387 | ] |
| 388 | |
| 389 | if node.metric is not None: |
| 390 | label_lines.append(f"metric: {node.metric:.4f}") |
no test coverage detected