Download openevolve-fixed/examples/function_minimization/initial_program.py from zhouk777/evolve: direct link, hf CLI and curl.
- Browser
- Download file 1.37 kB
-
https://huggingface.co/datasets/zhouk777/evolve/resolve/main/openevolve-fixed/examples/function_minimization/initial_program.py
- Command line
-
hf download hf://datasets/zhouk777/evolve/openevolve-fixed/examples/function_minimization/initial_program.py
-
curl -L -o initial_program.py https://huggingface.co/datasets/zhouk777/evolve/resolve/main/openevolve-fixed/examples/function_minimization/initial_program.py
1.37 kB
| # EVOLVE-BLOCK-START | |
| """Function minimization example for OpenEvolve""" | |
| import numpy as np | |
| def search_algorithm(iterations=1000, bounds=(-5, 5)): | |
| """ | |
| A simple random search algorithm that often gets stuck in local minima. | |
| Args: | |
| iterations: Number of iterations to run | |
| bounds: Bounds for the search space (min, max) | |
| Returns: | |
| Tuple of (best_x, best_y, best_value) | |
| """ | |
| # Initialize with a random point | |
| best_x = np.random.uniform(bounds[0], bounds[1]) | |
| best_y = np.random.uniform(bounds[0], bounds[1]) | |
| best_value = evaluate_function(best_x, best_y) | |
| for _ in range(iterations): | |
| # Simple random search | |
| x = np.random.uniform(bounds[0], bounds[1]) | |
| y = np.random.uniform(bounds[0], bounds[1]) | |
| value = evaluate_function(x, y) | |
| if value < best_value: | |
| best_value = value | |
| best_x, best_y = x, y | |
| return best_x, best_y, best_value | |
| # EVOLVE-BLOCK-END | |
| # This part remains fixed (not evolved) | |
| def evaluate_function(x, y): | |
| """The complex function we're trying to minimize""" | |
| return np.sin(x) * np.cos(y) + np.sin(x * y) + (x**2 + y**2) / 20 | |
| def run_search(): | |
| x, y, value = search_algorithm() | |
| return x, y, value | |
| if __name__ == "__main__": | |
| x, y, value = run_search() | |
| print(f"Found minimum at ({x}, {y}) with value {value}") | |