Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions examples/function_minimization/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def run_with_timeout(func, args=(), kwargs={}, timeout_seconds=5):
result = future.result(timeout=timeout_seconds)
return result
except concurrent.futures.TimeoutError:
raise TimeoutError(f"Function timed out after {timeout_seconds} seconds")
raise TimeoutError(
f"Function timed out after {timeout_seconds} seconds")


def safe_float(value):
Expand Down Expand Up @@ -88,7 +89,8 @@ def evaluate(program_path):
start_time = time.time()

# Run with timeout
result = run_with_timeout(program.run_search, timeout_seconds=5)
result = run_with_timeout(
program.run_search, timeout_seconds=5)

# Handle different result formats
if isinstance(result, tuple):
Expand All @@ -101,14 +103,10 @@ def evaluate(program_path):
value = np.sin(x) * np.cos(y) + np.sin(x * y) + (x**2 + y**2) / 20
print(f"Trial {trial}: Got 2 values, calculated function value: {value}")
else:
print(
f"Trial {trial}: Invalid result format, expected tuple of 2 or 3 values but got {len(result)}"
)
print(f"Trial {trial}: Invalid result format, expected tuple of 2 or 3 values but got {len(result)}")
continue
else:
print(
f"Trial {trial}: Invalid result format, expected tuple but got {type(result)}"
)
print(f"Trial {trial}: Invalid result format, expected tuple but got {type(result)}")
continue

end_time = time.time()
Expand Down Expand Up @@ -148,9 +146,7 @@ def evaluate(program_path):
except IndexError as e:
# Specifically handle IndexError which often happens with early termination checks
print(f"Trial {trial}: IndexError - {str(e)}")
print(
"This is likely due to a list index check before the list is fully populated."
)
print("This is likely due to a list index check before the list is fully populated.")
continue
except Exception as e:
print(f"Trial {trial}: Error - {str(e)}")
Expand All @@ -173,7 +169,8 @@ def evaluate(program_path):
avg_time = float(np.mean(times)) if times else 1.0

# Convert to scores (higher is better)
value_score = float(1.0 / (1.0 + abs(avg_value - GLOBAL_MIN_VALUE))) # Normalize and invert
# Normalize and invert
value_score = float(1.0 / (1.0 + abs(avg_value - GLOBAL_MIN_VALUE)))
distance_score = float(1.0 / (1.0 + avg_distance))
speed_score = float(1.0 / avg_time) if avg_time > 0 else 0.0

Expand Down Expand Up @@ -267,12 +264,12 @@ def evaluate_stage1(program_path):
# Assume it's (x, y) and calculate value
x, y = result
# Calculate the function value since it wasn't returned
value = np.sin(x) * np.cos(y) + np.sin(x * y) + (x**2 + y**2) / 20
print(f"Stage 1: Got 2 values, calculated function value: {value}")
else:
value = np.sin(x) * np.cos(y) + \
np.sin(x * y) + (x**2 + y**2) / 20
print(
f"Stage 1: Invalid result format, expected tuple of 2 or 3 values but got {len(result)}"
)
f"Stage 1: Got 2 values, calculated function value: {value}")
else:
print(f"Stage 1: Invalid result format, expected tuple of 2 or 3 values but got {len(result)}")
return {"runs_successfully": 0.0, "error": "Invalid result format"}
else:
print(f"Stage 1: Invalid result format, expected tuple but got {type(result)}")
Expand All @@ -292,7 +289,8 @@ def evaluate_stage1(program_path):
or np.isinf(y)
or np.isinf(value)
):
print(f"Stage 1 validation: Invalid result, got x={x}, y={y}, value={value}")
print(
f"Stage 1 validation: Invalid result, got x={x}, y={y}, value={value}")
return {"runs_successfully": 0.5, "error": "Invalid result values"}

# Calculate distance safely
Expand Down