@@ -53,7 +53,8 @@ async def expand_async(self, node: Node, action: str) -> Node:
5353 logger .debug (f"Expanded node with action: { action } " )
5454 return child_node
5555
56- async def simulate_async (self , node : Node ) -> float :
56+ async def simulate_async (self , node : Node ) -> Tuple [Node , float ]:
57+ """Return the evaluated node so the whole rollout can receive its reward."""
5758 current_node = node
5859 depth = 0
5960 logger .debug ("Starting simulation" )
@@ -66,7 +67,7 @@ async def simulate_async(self, node: Node) -> float:
6667 depth += 1
6768 value = self .evaluate (current_node )
6869 logger .debug (f"Simulation complete. Final value: { value } " )
69- return value
70+ return current_node , value
7071
7172 async def mcts_async (self , root_state : str ) -> List [Node ]:
7273 root = Node (root_state , None )
@@ -83,8 +84,8 @@ async def mcts_rollout_async(self, root: Node):
8384 action = random .choice (self .actions )
8485 if len (node .children ) < len (self .actions ):
8586 node = await self .expand_async (node , action )
86- value = await self .simulate_async (node )
87- self .backpropagate (node , value )
87+ terminal_node , value = await self .simulate_async (node )
88+ self .backpropagate (terminal_node , value )
8889
8990 async def solve_async (self , question : str ) -> str :
9091 self .original_question = question
@@ -149,7 +150,8 @@ def expand(self, node: Node, action: str) -> Node:
149150 logger .debug (f"Expanded node with action: { action } " )
150151 return child_node
151152
152- def simulate (self , node : Node ) -> float :
153+ def simulate (self , node : Node ) -> Tuple [Node , float ]:
154+ """Return the evaluated node so the whole rollout can receive its reward."""
153155 current_node = node
154156 depth = 0
155157 logger .debug ("Starting simulation" )
@@ -162,7 +164,7 @@ def simulate(self, node: Node) -> float:
162164 depth += 1
163165 value = self .evaluate (current_node )
164166 logger .debug (f"Simulation complete. Final value: { value } " )
165- return value
167+ return current_node , value
166168
167169 def backpropagate (self , node : Node , value : float ):
168170 logger .debug ("Starting backpropagation" )
@@ -183,8 +185,8 @@ def mcts(self, root_state: str) -> List[Node]:
183185 action = random .choice (self .actions )
184186 if len (node .children ) < len (self .actions ):
185187 node = self .expand (node , action )
186- value = self .simulate (node )
187- self .backpropagate (node , value )
188+ terminal_node , value = self .simulate (node )
189+ self .backpropagate (terminal_node , value )
188190 logger .debug ("MCTS complete" )
189191 return self .extract_trajectories (root )
190192
@@ -347,4 +349,4 @@ def solve(self, question: str) -> str:
347349 """
348350 Synchronous wrapper for solve_async method.
349351 """
350- return asyncio .run (self .solve_async (question ))
352+ return asyncio .run (self .solve_async (question ))
0 commit comments