This example demonstrates how to use OpenEvolve to evolve a simple reinforcement-learning workload on Gymnasium's LunarLander environment. The workspace is split into two Git repositories (submodules) to clearly separate the stable evaluation environment from the evolving agent code.
- Env (this repository): Stable evaluator and tooling. It should not be modified by the evolutionary process.
- Agent (submodule): The actual code under evolution. OpenEvolve operates on this Git repository and creates commits as organisms.
This separation ensures reproducible and comparable scoring while allowing free evolution of the agent implementation.
openevolve-example/
evaluator.py # Entry point for OpenEvolve; returns metrics with 'combined_score'
env/
env_config.yaml # Evaluator runtime config (episodes, steps, seed, render)
main.py # Visualizer: run current agent with human/rgb_array rendering
pyproject.toml # Env-side dependencies (gymnasium[box2d], swig)
openevolve-example-lunarlander/ # Submodule: the evolving agent repository
agent.py # Minimal baseline agent (random policy)
The agent repository must provide agent.py
with the following minimal interface:
class Agent:
def __init__(self, action_space, observation_space=None, config=None):
...
def reset(self):
...
def act(self, observation): # returns a valid action for the environment
...
def close(self):
...
The baseline provided here samples random actions. OpenEvolve will evolve commits in this repository to improve performance.
- Entry:
evaluator.py
exposesevaluate(repo_root: str) -> dict
- Behavior: loads
Agent
fromrepo_root/agent.py
, runsnum_episodes
ofLunarLander-v3
, and returns metrics including:combined_score
: mean total reward across episodes (primary fitness)mean_reward
,std_reward
,best_reward
,worst_reward
,mean_steps
,episodes
,runtime_sec
- Config: set via
env/env_config.yaml
(keys:env_id
,num_episodes
,max_steps
,seed
,render_mode
)
- Install OpenEvolve (from the repo root):
pip install -e <path-to-openevolve-repo>
- Install example dependencies (any one of the following):
# Option A: editable install of the example (uses pyproject.toml)
pip install -e <path-to-openevolve-repo>/openevolve-example
# Option B: install minimal extras directly
pip install "gymnasium[box2d]>=1.1.1" swig>=4.3.1
If you encounter build issues with Box2D on macOS, ensure swig
is installed system-wide:
brew install swig
Run the visualizer to observe the agent's behavior:
python <path-to-openevolve-repo>/openevolve-example/main.py \
--agent-path <path-to-openevolve-repo>/openevolve-example/openevolve-example-lunarlander \
--episodes 3 \
--render-mode human
CLI flags:
--agent-path
: path to the agent repository (must containagent.py
)--episodes
: number of episodes to run (default 1)--max-steps
: step cap per episode (default 1000)--render-mode
:human
orrgb_array
(defaulthuman
)--seed
: base seed; episode i usesseed + i
(default 42)
Use the agent submodule as the evolving repo and this evaluator as the scoring entry:
python <path-to-openevolve-repo>/openevolve-run.py \
<path-to-openevolve-repo>/openevolve-example/openevolve-example-lunarlander \
<path-to-openevolve-repo>/openevolve-example/evaluator.py \
--config <path-to-openevolve-repo>/configs/island_config_example.yaml \
--iterations 50 \
--evolution-target "Improve LunarLander mean return"
OpenEvolve will create commits in the agent repository, evaluate each candidate via evaluator.py
, and track the best program by combined_score
.
If you cloned this example separately and need to initialize submodules:
git submodule update --init --recursive
This example is provided under the repository's license files.