Skip to content

Commit 12894b0

Browse files
committed
f
1 parent 409f61c commit 12894b0

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Configuration for function minimization example
2+
max_iterations: 100
3+
checkpoint_interval: 10
4+
log_level: "INFO"
5+
6+
# LLM configuration
7+
llm:
8+
primary_model: "gemini-2.0-flash-lite"
9+
primary_model_weight: 0.8
10+
secondary_model: "gemini-2.0-flash"
11+
secondary_model_weight: 0.2
12+
api_base: "https://generativelanguage.googleapis.com/v1beta/openai/"
13+
temperature: 0.7
14+
top_p: 0.95
15+
max_tokens: 4096
16+
17+
# Prompt configuration
18+
prompt:
19+
system_message: "You are an expert programmer specializing in optimization algorithms. Your task is to improve a function minimization algorithm to find the global minimum of a complex function with many local minima. The function is f(x, y) = sin(x) * cos(y) + sin(x*y) + (x^2 + y^2)/20. Focus on improving the search_algorithm function to reliably find the global minimum, escaping local minima that might trap simple algorithms."
20+
num_top_programs: 3
21+
use_template_stochasticity: true
22+
23+
# Database configuration
24+
database:
25+
population_size: 50
26+
archive_size: 20
27+
num_islands: 3
28+
elite_selection_ratio: 0.2
29+
exploitation_ratio: 0.7
30+
31+
# Evaluator configuration
32+
evaluator:
33+
timeout: 60
34+
cascade_evaluation: true
35+
cascade_thresholds: [0.5, 0.75]
36+
parallel_evaluations: 4
37+
use_llm_feedback: false
38+
39+
# Evolution settings
40+
diff_based_evolution: true
41+
allow_full_rewrites: false
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""
2+
Evaluator for the function minimization example
3+
"""
4+
import importlib.util
5+
import numpy as np
6+
import time
7+
8+
def evaluate(program_path):
9+
"""
10+
Evaluate the program by running it multiple times and checking how close
11+
it gets to the known global minimum.
12+
13+
Args:
14+
program_path: Path to the program file
15+
16+
Returns:
17+
Dictionary of metrics
18+
"""
19+
# Known global minimum (approximate)
20+
GLOBAL_MIN_X = -1.76
21+
GLOBAL_MIN_Y = -1.03
22+
GLOBAL_MIN_VALUE = -2.104
23+
24+
# Load the program
25+
spec = importlib.util.spec_from_file_location("program", program_path)
26+
program = importlib.util.module_from_spec(spec)
27+
spec.loader.exec_module(program)
28+
29+
# Run multiple trials
30+
num_trials = 10
31+
values = []
32+
distances = []
33+
times = []
34+
35+
for _ in range(num_trials):
36+
start_time = time.time()
37+
x, y, value = program.run_search()
38+
end_time = time.time()
39+
40+
# Calculate metrics
41+
distance_to_global = np.sqrt((x - GLOBAL_MIN_X)**2 + (y - GLOBAL_MIN_Y)**2)
42+
value_difference = abs(value - GLOBAL_MIN_VALUE)
43+
44+
values.append(value)
45+
distances.append(distance_to_global)
46+
times.append(end_time - start_time)
47+
48+
# Calculate metrics
49+
avg_value = np.mean(values)
50+
avg_distance = np.mean(distances)
51+
avg_time = np.mean(times)
52+
53+
# Convert to scores (higher is better)
54+
value_score = 1.0 / (1.0 + abs(avg_value - GLOBAL_MIN_VALUE)) # Normalize and invert
55+
distance_score = 1.0 / (1.0 + avg_distance)
56+
speed_score = 1.0 / avg_time
57+
58+
# Normalize speed score (so it doesn't dominate)
59+
speed_score = min(speed_score, 10.0) / 10.0
60+
61+
return {
62+
"value_score": value_score,
63+
"distance_score": distance_score,
64+
"speed_score": speed_score,
65+
"combined_score": 0.6 * value_score + 0.3 * distance_score + 0.1 * speed_score
66+
}
67+
68+
# Stage-based evaluation for cascade evaluation
69+
def evaluate_stage1(program_path):
70+
"""First stage evaluation with fewer trials"""
71+
# Quick check to see if the program runs without errors
72+
try:
73+
# Load the program
74+
spec = importlib.util.spec_from_file_location("program", program_path)
75+
program = importlib.util.module_from_spec(spec)
76+
spec.loader.exec_module(program)
77+
78+
# Run a single trial
79+
x, y, value = program.run_search()
80+
81+
# Basic metrics
82+
return {
83+
"runs_successfully": 1.0,
84+
"value": float(value)
85+
}
86+
except Exception as e:
87+
print(f"Stage 1 evaluation failed: {e}")
88+
return {"runs_successfully": 0.0}
89+
90+
def evaluate_stage2(program_path):
91+
"""Second stage evaluation with more thorough testing"""
92+
# Full evaluation as in the main evaluate function
93+
return evaluate(program_path)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# EVOLVE-BLOCK-START
2+
"""Function minimization example for OpenEvolve"""
3+
import numpy as np
4+
5+
def search_algorithm(iterations=1000, bounds=(-5, 5)):
6+
"""
7+
A simple random search algorithm that often gets stuck in local minima.
8+
9+
Args:
10+
iterations: Number of iterations to run
11+
bounds: Bounds for the search space (min, max)
12+
13+
Returns:
14+
Tuple of (best_x, best_y, best_value)
15+
"""
16+
best_x, best_y = 0, 0
17+
best_value = evaluate_function(best_x, best_y)
18+
19+
for _ in range(iterations):
20+
# Simple random search
21+
x = np.random.uniform(bounds[0], bounds[1])
22+
y = np.random.uniform(bounds[0], bounds[1])
23+
value = evaluate_function(x, y)
24+
25+
if value < best_value:
26+
best_value = value
27+
best_x, best_y = x, y
28+
29+
return best_x, best_y, best_value
30+
31+
def evaluate_function(x, y):
32+
"""The complex function we're trying to minimize"""
33+
return np.sin(x) * np.cos(y) + np.sin(x*y) + (x**2 + y**2)/20
34+
# EVOLVE-BLOCK-END
35+
36+
# This part remains fixed (not evolved)
37+
def run_search():
38+
x, y, value = search_algorithm()
39+
return x, y, value
40+
41+
if __name__ == "__main__":
42+
x, y, value = run_search()
43+
print(f"Found minimum at ({x}, {y}) with value {value}")
44+
# The global minimum is around (-1.76, -1.03) with value -2.104

0 commit comments

Comments
 (0)