Code for "Strategic Planning: A Top-Down Approach To Option Generation", Ruiz Lutyten et al (2024).
First, create and activate the conda environment using the provided environment.yml:
conda env create -f environment.yml
conda activate strategistThis repository uses several git submodules (see .gitmodules). To ensure all dependencies are available, initialize and update the submodules after cloning:
git submodule update --init --recursiveThis will fetch the following submodules:
data/crafter(https://github.com/antoninbrthn/crafter.git)data/SmartPlay(https://github.com/maxruizluyten/SmartPlay.git)Eureka(https://github.com/maxruizluyten/Eureka.git)
To use the custom Crafter environments required for this project, follow these steps:
- Clone the forked Crafter repository:
git clone https://github.com/antoninbrthn/crafter.git cd crafter - Select the appropriate branch:
- For Crafter-Easy and Crafter-Medium (farming mod):
git checkout farming-mod
- For the original Crafter environment:
git checkout og
- For Crafter-Easy and Crafter-Medium (farming mod):
- Install the package in editable mode:
pip install -e . - Check the installation:
- The installed package version should be:
1.8.3+farmfor the farming-mod branch1.8.3+ogfor the og branch
- You can check this by running:
python -c "import crafter; print(crafter.__version__)"
- The installed package version should be:
The main workflow for creating a strategy tree and training an agent is handled by scripts/run_full_strategist.py. The process is split into two steps:
Run this first to generate and save the strategy tree for your chosen environment:
python scripts/run_full_strategist.py --difficulty <difficulty> --make_tree True--make_tree: If set toTrue, only creates and saves the strategy tree for the specified environment. No agent training is performed in this step.
Once the tree is created, you can train an agent on a specific branch or node:
python scripts/run_full_strategist.py --difficulty <difficulty> --branch_i <branch_index> --config <config_file> [other options]--run_id <str>: (Optional) Specify the run ID to load a particular strategy tree. By default, this is set to the strategy trees used in the paper for each environment (crafter-mod-farm-easy_gpt-4o,crafter-mod-farm-medium_gpt-4o, etc.), so you only need to provide this if you want to use a custom or previously generated tree.
--config <file>: Path to the agent config YAML (e.g.,configs/rl_agent.yaml,configs/rl_agent-ppo.yaml). This controls the RL algorithm, hyperparameters, and reward model settings.--difficulty <easy|medium|og>: Selects the environment difficulty. This also determines which environment config is loaded (seeconfigs/).--make_tree <bool>: IfTrue, only creates and saves the strategy tree for the specified environment.--run_id <str>: Run ID to load a specific strategy tree. Defaults to the strategy trees used in the paper for each environment.--branch_i <int>: Index of the strategy tree branch to train on. Use this to select a specific subgoal/plan.--node <int>: (Alternative tobranch_i) Specify a node id directly.--alpha <float>: Sets the alpha parameter for reward shaping (overrides config).--alpha_schedule <start> <end> <decay_start> <decay_end>: Schedule for alpha decay (overrides config).--decay_function <str>: Shape of alpha decay (e.g.,linear,poly_0.5).--reward_mode <str>: Reward shaping mode (e.g.,direct,diff,only_custom).--train_ts <int>: Number of training timesteps (overrides config).--log_interval <int>: Logging interval for wandb (overrides config).--wandb_tags <tag1> <tag2> ...: Tags for experiment tracking in Weights & Biases.--seed <int>: Random seed for reproducibility.--verbose <bool>: Verbose output.
- Create the strategy tree:
python scripts/run_full_strategist.py --difficulty medium --make_tree True
- Train an agent on a branch of the tree:
(You can add
python scripts/run_full_strategist.py --difficulty medium --branch_i 0 --config configs/rl_agent.yaml --alpha_schedule 1 0 50_000 1_000_000 --train_ts 2_000_000
--run_id <your_run_id>if you want to use a custom tree.)
- Agent configs: Located in
configs/(e.g.,rl_agent.yaml,rl_agent-ppo.yaml). These files control the RL algorithm, model parameters, training length, and reward model settings. - Environment configs: Also in
configs/, named likecrafter-mod-farm-easy_gpt-4o.yaml, etc. These are selected automatically based on the--difficultyargument. - Prompts: All prompt files are in the
prompts/directory. For example,prompts/crafter-mod-farm-easy.yamlcontains the context and instructions for the easy environment. Reward shaping prompt templates are inprompts/reward_shaper/.
To modify the agent's behavior, edit the relevant config YAML in configs/. To change the environment description or reward shaping instructions, edit the appropriate YAML or text file in prompts/.
To evaluate the performance of the LLM policy agent directly on the Crafter environment, you can use the scripts/evaluate_llm_policy.py script. This script runs the agent for a specified number of episodes and reports the average reward and other metrics.
To run the evaluation, use the following command structure:
python scripts/evaluate_llm_policy.py [OPTIONS]--difficulty: The difficulty level of the Crafter environment. Choose fromeasy,medium. The default iseasy.--model: The LLM model to be used for policy decisions (e.g.,gpt-4o-mini,gpt-4). The default isgpt-4o-mini.--episodes: The number of episodes to run the evaluation for. The default is100.--verbose: If included, it prints detailed logs of the agent's actions and environment states.--seed: An integer to set the random seed for reproducibility. The default is42.
Here is an example command to run the evaluation on the easy difficulty with the gpt-4o-mini model for 10 episodes:
python scripts/evaluate_llm_policy.py --difficulty easy --model gpt-4o-mini --episodes 10 --seed 42The results of the evaluation, including mean reward, success rate, and total API cost, will be printed to the console and saved in a JSON file under the runs/llm_policy_[MODEL]_[DIFFICULTY] directory.
This branch includes tools for running Eureka on the Crafter environment.
Before running Eureka experiments, make sure you are on the correct branch. Switch to the Eureka branch using:
git checkout Eureka --
To run the Eureka experiment, use the following command:
python Eureka/eureka/eureka.py env=crafter-modThe results of the evaluation, including mean reward, success rate, and total API cost, will be printed to the console and saved in a JSON file under the runs/eureka_[MODEL]_[DIFFICULTY] directory.