55import importlib .util
66import numpy as np
77import time
8- import multiprocessing
8+ import concurrent . futures
99import traceback
10+ import signal
1011
1112
1213def run_with_timeout (func , args = (), kwargs = {}, timeout_seconds = 5 ):
@@ -22,31 +23,13 @@ def run_with_timeout(func, args=(), kwargs={}, timeout_seconds=5):
2223 Returns:
2324 Result of the function or raises TimeoutError
2425 """
25-
26- def wrapper ( queue , func , args , kwargs ):
26+ with concurrent . futures . ThreadPoolExecutor ( max_workers = 1 ) as executor :
27+ future = executor . submit ( func , * args , ** kwargs )
2728 try :
28- result = func (* args , ** kwargs )
29- queue .put (("success" , result ))
30- except Exception as e :
31- queue .put (("error" , e ))
32-
33- queue = multiprocessing .Queue ()
34- process = multiprocessing .Process (target = wrapper , args = (queue , func , args , kwargs ))
35- process .start ()
36- process .join (timeout = timeout_seconds )
37-
38- if process .is_alive ():
39- process .terminate ()
40- process .join ()
41- raise TimeoutError (f"Function timed out after { timeout_seconds } seconds" )
42-
43- if queue .empty ():
44- raise TimeoutError ("Function ended without returning a result" )
45-
46- status , result = queue .get ()
47- if status == "error" :
48- raise result
49- return result
29+ result = future .result (timeout = timeout_seconds )
30+ return result
31+ except concurrent .futures .TimeoutError :
32+ raise TimeoutError (f"Function timed out after { timeout_seconds } seconds" )
5033
5134
5235def safe_float (value ):
@@ -107,15 +90,27 @@ def evaluate(program_path):
10790 # Run with timeout
10891 result = run_with_timeout (program .run_search , timeout_seconds = 5 )
10992
110- # Check if we got a tuple of 3 values
111- if not isinstance (result , tuple ) or len (result ) != 3 :
93+ # Handle different result formats
94+ if isinstance (result , tuple ):
95+ if len (result ) == 3 :
96+ x , y , value = result
97+ elif len (result ) == 2 :
98+ # Assume it's (x, y) and calculate value
99+ x , y = result
100+ # Calculate the function value since it wasn't returned
101+ value = np .sin (x ) * np .cos (y ) + np .sin (x * y ) + (x ** 2 + y ** 2 ) / 20
102+ print (f"Trial { trial } : Got 2 values, calculated function value: { value } " )
103+ else :
104+ print (
105+ f"Trial { trial } : Invalid result format, expected tuple of 2 or 3 values but got { len (result )} "
106+ )
107+ continue
108+ else :
112109 print (
113- f"Trial { trial } : Invalid result format, expected tuple of 3 values but got { type (result )} "
110+ f"Trial { trial } : Invalid result format, expected tuple but got { type (result )} "
114111 )
115112 continue
116113
117- x , y , value = result
118-
119114 end_time = time .time ()
120115
121116 # Ensure all values are float
@@ -264,15 +259,25 @@ def evaluate_stage1(program_path):
264259 # Run a single trial with timeout
265260 result = run_with_timeout (program .run_search , timeout_seconds = 5 )
266261
267- # Check if we got a tuple of 3 values
268- if not isinstance (result , tuple ) or len (result ) != 3 :
269- print (
270- f"Stage 1: Invalid result format, expected tuple of 3 values but got { type (result )} "
271- )
262+ # Handle different result formats
263+ if isinstance (result , tuple ):
264+ if len (result ) == 3 :
265+ x , y , value = result
266+ elif len (result ) == 2 :
267+ # Assume it's (x, y) and calculate value
268+ x , y = result
269+ # Calculate the function value since it wasn't returned
270+ value = np .sin (x ) * np .cos (y ) + np .sin (x * y ) + (x ** 2 + y ** 2 ) / 20
271+ print (f"Stage 1: Got 2 values, calculated function value: { value } " )
272+ else :
273+ print (
274+ f"Stage 1: Invalid result format, expected tuple of 2 or 3 values but got { len (result )} "
275+ )
276+ return {"runs_successfully" : 0.0 , "error" : "Invalid result format" }
277+ else :
278+ print (f"Stage 1: Invalid result format, expected tuple but got { type (result )} " )
272279 return {"runs_successfully" : 0.0 , "error" : "Invalid result format" }
273280
274- x , y , value = result
275-
276281 # Ensure all values are float
277282 x = safe_float (x )
278283 y = safe_float (y )
0 commit comments