Migrated from GitHub
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- data/LICENSE +21 -0
- data/README.md +303 -0
- data/psltl/__init__.py +0 -0
- data/psltl/baseline_algo/crm/autoexcution.py +50 -0
- data/psltl/baseline_algo/crm/cmd_util.py +139 -0
- data/psltl/baseline_algo/crm/envs/__init__.py +97 -0
- data/psltl/baseline_algo/crm/envs/grids/__init__.py +0 -0
- data/psltl/baseline_algo/crm/envs/grids/craft_world.py +136 -0
- data/psltl/baseline_algo/crm/envs/grids/game_objects.py +66 -0
- data/psltl/baseline_algo/crm/envs/grids/grid_environment.py +262 -0
- data/psltl/baseline_algo/crm/envs/grids/maps/map_0.txt +41 -0
- data/psltl/baseline_algo/crm/envs/grids/maps/map_1.txt +41 -0
- data/psltl/baseline_algo/crm/envs/grids/maps/map_10.txt +41 -0
- data/psltl/baseline_algo/crm/envs/grids/maps/map_2.txt +41 -0
- data/psltl/baseline_algo/crm/envs/grids/maps/map_3.txt +41 -0
- data/psltl/baseline_algo/crm/envs/grids/maps/map_4.txt +41 -0
- data/psltl/baseline_algo/crm/envs/grids/maps/map_5.txt +41 -0
- data/psltl/baseline_algo/crm/envs/grids/maps/map_6.txt +41 -0
- data/psltl/baseline_algo/crm/envs/grids/maps/map_7.txt +41 -0
- data/psltl/baseline_algo/crm/envs/grids/maps/map_8.txt +41 -0
- data/psltl/baseline_algo/crm/envs/grids/maps/map_9.txt +41 -0
- data/psltl/baseline_algo/crm/envs/grids/office_world.py +150 -0
- data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t1.txt +6 -0
- data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t10.txt +18 -0
- data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t2.txt +6 -0
- data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t3.txt +6 -0
- data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t4.txt +6 -0
- data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t5.txt +11 -0
- data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t6.txt +16 -0
- data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t7.txt +16 -0
- data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t8.txt +11 -0
- data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t9.txt +13 -0
- data/psltl/baseline_algo/crm/envs/grids/reward_machines/office/t1.txt +6 -0
- data/psltl/baseline_algo/crm/envs/grids/reward_machines/office/t2.txt +6 -0
- data/psltl/baseline_algo/crm/envs/grids/reward_machines/office/t3.txt +11 -0
- data/psltl/baseline_algo/crm/envs/grids/reward_machines/office/t4.txt +10 -0
- data/psltl/baseline_algo/crm/envs/grids/reward_machines/taxi/t.txt +11 -0
- data/psltl/baseline_algo/crm/envs/grids/taxi_world.py +123 -0
- data/psltl/baseline_algo/crm/envs/grids/value_iteration.py +54 -0
- data/psltl/baseline_algo/crm/envs/mujoco_rm/half_cheetah_environment.py +50 -0
- data/psltl/baseline_algo/crm/envs/mujoco_rm/reward_machines/t1.txt +6 -0
- data/psltl/baseline_algo/crm/envs/mujoco_rm/reward_machines/t2.txt +12 -0
- data/psltl/baseline_algo/crm/envs/water/__init__.py +0 -0
- data/psltl/baseline_algo/crm/envs/water/maps/world_3.pkl +3 -0
- data/psltl/baseline_algo/crm/envs/water/reward_machines/org_t10.txt +8 -0
- data/psltl/baseline_algo/crm/envs/water/reward_machines/t1.txt +6 -0
- data/psltl/baseline_algo/crm/envs/water/reward_machines/t10.txt +8 -0
- data/psltl/baseline_algo/crm/envs/water/reward_machines/t2.txt +6 -0
- data/psltl/baseline_algo/crm/envs/water/reward_machines/t3.txt +6 -0
- data/psltl/baseline_algo/crm/envs/water/reward_machines/t4.txt +22 -0
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2025 safe-autonomy-lab
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Adaptive Reward Design for Reinforcement Learning
|
| 2 |
+
|
| 3 |
+
**Citation:** If you use this code or build upon it, please cite:
|
| 4 |
+
|
| 5 |
+
```bibtex
|
| 6 |
+
@inproceedings{Kwon2025AdaptiveReward,
|
| 7 |
+
title = {Adaptive Reward Design for Reinforcement Learning},
|
| 8 |
+
author = {Kwon, Minjae and ElSayed-Aly, Ingy and Feng, Lu},
|
| 9 |
+
booktitle = {Proceedings of the Conference on Uncertainty in Artificial Intelligence (UAI)},
|
| 10 |
+
year = {2025},
|
| 11 |
+
}
|
| 12 |
+
```
|
| 13 |
+
|
| 14 |
+
**Contact:** For questions or collaborations, please contact:
|
| 15 |
+
|
| 16 |
+
Minjae Kwon - hbt9su@virginia.edu
|
| 17 |
+
|
| 18 |
+
## Overview
|
| 19 |
+
|
| 20 |
+
This repository implements our method for **Adaptive Reward Design for Reinforcement Learning**, addressing the common challenge of sparse rewards when using Linear Temporal Logic (LTL) to specify complex tasks. While LTL provides precision, sparse rewards (e.g., feedback only on full task completion) often make it difficult for RL agents to learn effectively.
|
| 21 |
+
|
| 22 |
+
Our approach overcomes this by:
|
| 23 |
+
* Introducing **LTL-derived reward functions** that provide denser feedback compared to typical goal-achieved reward functions (which might assign 1 for completion and 0 otherwise). Our denser feedback is based on the structure of the Deterministic Finite Automaton (DFA) derived from the LTL formula, where we consider each node in the DFA as a sub-task. With our reward design, this encourages agents to complete as much of a task as possible, not just the final goal.
|
| 24 |
+
* Developing an **adaptive reward shaping mechanism** that dynamically updates these LTL-derived reward functions during the learning process. This guides the agent more effectively based on its progress. While assigning partial rewards for solving sub-tasks (as illustrated in our toy example) can be beneficial, it also risks the policy converging to a sub-optimal solution that completes only early sub-tasks. Our adaptive reward design specifically addresses this issue of getting trapped in sub-optimal policies, a capability supported by theoretical guarantees (see Theorem 1 in our paper).
|
| 25 |
+
|
| 26 |
+
The key idea is to reward incremental progress and adapting the learning signals as the agent explores. This repository contains the code to reproduce the experiments and utilize the proposed methods. Experimental results on a range of benchmark RL environments demonstrate that our approach generally outperforms baselines, achieving earlier convergence to a better policy with higher expected return and task completion rate.
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
## Introduction
|
| 30 |
+
We observed that **optimizing a policy in terms of reward** does not always align with **optimizing a policy for task completion**, particularly when using LTL-based reward shaping. To illustrate this, we provided a toy example in our paper.
|
| 31 |
+
|
| 32 |
+
The following plots illustrate the performance comparison in the toy environment:
|
| 33 |
+
|
| 34 |
+

|
| 35 |
+
|
| 36 |
+
The plots on the left and right show the average reward during training and the success rate over time. Comparisons with QRM, CRM, and HRM in more complex environments are presented in our paper.
|
| 37 |
+
|
| 38 |
+
## Installation
|
| 39 |
+
|
| 40 |
+
We have tested with Python 3.8.18 and Conda 23.7.4 on Ubuntu 20.04. We recommend using an Anaconda virtual environment
|
| 41 |
+
|
| 42 |
+
You might need `pythonx.x-dev` package matching your python version installed with apt-get, and
|
| 43 |
+
the following.
|
| 44 |
+
```bash
|
| 45 |
+
sudo apt-get update
|
| 46 |
+
sudo apt-get install build-essential
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
```bash
|
| 50 |
+
conda create -n psltl python=3.8
|
| 51 |
+
```
|
| 52 |
+
### Download this folder
|
| 53 |
+
|
| 54 |
+
Go to Files - Github.
|
| 55 |
+
Click 'Download this folder' to download zip file.
|
| 56 |
+
<!-- ### Clone the Repository
|
| 57 |
+
|
| 58 |
+
```bash
|
| 59 |
+
git clone https://github.com/IngyN/PartialSatLTL.git
|
| 60 |
+
``` -->
|
| 61 |
+
|
| 62 |
+
### Install the Package
|
| 63 |
+
```bash
|
| 64 |
+
cd AdaptiveRewardShaping
|
| 65 |
+
pip install wheel==0.38.4
|
| 66 |
+
pip install setuptools==65.5.0
|
| 67 |
+
pip3 install -e .
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
### Mujoco Installation
|
| 71 |
+
|
| 72 |
+
Please follow the instructions on the webpage: https://github.com/openai/mujoco-py
|
| 73 |
+
|
| 74 |
+
### Troubleshooting Possible Errors
|
| 75 |
+
If you face errors while building the gym package's wheel, such as:
|
| 76 |
+
```bash
|
| 77 |
+
wheel.vendored.packaging.requirements.InvalidRequirement: Expected end or semicolon (after version specifier) opencv-python>=3.
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
Please refer to this GitHub issue for solutions (https://github.com/openai/gym/issues/3202).
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
## Testing the Installation
|
| 85 |
+
|
| 86 |
+
### Running Tests
|
| 87 |
+
|
| 88 |
+
#### List of Arguments
|
| 89 |
+
- `--algo_name`: str (RL algorithm)
|
| 90 |
+
- `--missing`: bool (Test with infeasible environment)
|
| 91 |
+
- `--noise_level`: float (Determine likelihood of noisy action)
|
| 92 |
+
- `--use_one_hot`: bool (Use one-hot encoding for automaton states)
|
| 93 |
+
- `--node_embedding`: bool (Use node embedding for automaton states)
|
| 94 |
+
- `--default_setting`: bool (Use default hyperparameters for each algorithm)
|
| 95 |
+
- `--env_name`: str (e.g. office, water, toy, cheetah, etc.)
|
| 96 |
+
- `--reward_types`: p (Options: p - progress, h - hybrid, n - naive)
|
| 97 |
+
- `--use_adrs`: bool (Enable or disable adaptive reward shaping)
|
| 98 |
+
- `--hybrid_eta`: float (Trade-off factor between negative and positive feedback)
|
| 99 |
+
- `--ards_update`: int (Frequency of adaptive reward shaping updates)
|
| 100 |
+
- `--adrs_mu`: float (Parameter for the trade-off between past and upcoming experiences)
|
| 101 |
+
- `--episode_step`: int (Maximum steps per episode)
|
| 102 |
+
- `--total_timesteps`: int (Total timesteps for each run)
|
| 103 |
+
- `--total_run`: int (Number of times to run the same environment for accurate performance measurement considering standard deviation)
|
| 104 |
+
- Numerous other hyperparameters available for RL algorithm settings.
|
| 105 |
+
|
| 106 |
+
#### Usage Examples:
|
| 107 |
+
Vary `--reward_types` n, p, h to test with reward functions: naive, progress, and hybrid, respectively.
|
| 108 |
+
|
| 109 |
+
**Note** `--default_setting True` automatically use hyperparameter reported in the paper. We have used seeds 0 to 9 for 10 independent runs.
|
| 110 |
+
For noisy or infeasible environments runs, additional arguments like `--noise_level=0.1` or `--missing=True` can be appended.
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
##### Toy
|
| 114 |
+
```bash
|
| 115 |
+
python run.py --env_name toy --total_timesteps 10000 --total_run 1 --episode_step 25 --reward_types p --default_setting True --seed 0 --algo_name dqn --use_adrs True --node_embedding True --eval_freq 100
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
```bash
|
| 119 |
+
python run.py --env_name toy --total_timesteps 10000 --total_run 1 --episode_step 25 --reward_types p --default_setting True --seed 0 --algo_name dqn --node_embedding True --eval_freq 100
|
| 120 |
+
```
|
| 121 |
+
|
| 122 |
+
##### Office
|
| 123 |
+
```bash
|
| 124 |
+
python run.py --env_name office --total_timesteps 60000 --total_run 1 --episode_step 100 --reward_types p --default_setting True --seed 0 --algo_name dqn --adrs_update 25 --use_adrs True --node_embedding True --eval_freq 100
|
| 125 |
+
```
|
| 126 |
+
|
| 127 |
+
##### Taxi
|
| 128 |
+
```bash
|
| 129 |
+
python run.py --env_name taxi --total_timesteps 500000 --total_run 1 --episode_step 200 --reward_types p --default_setting True --seed 0 --algo_name dqn --use_adrs True --node_embedding True --eval_freq 1000
|
| 130 |
+
```
|
| 131 |
+
|
| 132 |
+
##### Water
|
| 133 |
+
```bash
|
| 134 |
+
python run.py --env_name water --total_timesteps 2000000 --total_run 1 --episode_step 600 --reward_types p --default_setting True --seed 0 --algo_name ddqn --adrs_update 1000 --use_adrs True --use_one_hot True --map_id 3 --eval_freq 1000
|
| 135 |
+
```
|
| 136 |
+
|
| 137 |
+
##### HalfCheetah
|
| 138 |
+
For DDPG,
|
| 139 |
+
```bash
|
| 140 |
+
python run.py --algo_name ddpg --env_name cheetah --total_timesteps 2000000 --total_run 1 --episode_step 1000 --reward_types p --default_setting True --seed 0 --adrs_update 100 --use_adrs True --use_one_hot True --eval_freq 1000
|
| 141 |
+
```
|
| 142 |
+
|
| 143 |
+
For A2C,
|
| 144 |
+
```bash
|
| 145 |
+
python run.py --algo_name a2c --env_name cheetah --total_timesteps 2000000 --total_run 1 --episode_step 1000 --reward_types p --default_setting True --seed 0 --adrs_update 500 --use_adrs True --use_one_hot True --eval_freq 1000
|
| 146 |
+
```
|
| 147 |
+
|
| 148 |
+
For PPO,
|
| 149 |
+
```bash
|
| 150 |
+
python run.py --algo_name ppo --env_name cheetah --total_timesteps 2000000 --total_run 1 --episode_step 1000 --reward_types p --default_setting True --seed 0 --adrs_update 500 --use_adrs True --use_one_hot True --eval_freq 1000
|
| 151 |
+
```
|
| 152 |
+
|
| 153 |
+
### Baseline Runs
|
| 154 |
+
For Baselines run, please refer to the following GitHub repositories:
|
| 155 |
+
|
| 156 |
+
QRM: https://bitbucket.org/RToroIcarte/qrm/src/master/
|
| 157 |
+
|
| 158 |
+
CRM: https://github.com/RodrigoToroIcarte/reward_machines
|
| 159 |
+
|
| 160 |
+
#### For QRM:
|
| 161 |
+
Change your current directory to `./psltl/baseline_algo/qrm/src` and use the following commands:
|
| 162 |
+
|
| 163 |
+
##### Office
|
| 164 |
+
- Deterministic:
|
| 165 |
+
```bash
|
| 166 |
+
python run.py --algorithm="qrm-rs" --world="office" --map=0 --num_times=10 --batch_size=1 --buffer_size=1
|
| 167 |
+
```
|
| 168 |
+
- Noise:
|
| 169 |
+
```bash
|
| 170 |
+
python run.py --algorithm="qrm-rs" --world="office" --map=0 --num_times=10 --batch_size=1 --buffer_size=1 --noise_level=0.1
|
| 171 |
+
```
|
| 172 |
+
- Infeasible:
|
| 173 |
+
```bash
|
| 174 |
+
python run.py --algorithm="qrm-rs" --world="office" --map=0 --num_times=10 --batch_size=1 --buffer_size=1 --missing=True
|
| 175 |
+
```
|
| 176 |
+
|
| 177 |
+
##### Taxi
|
| 178 |
+
- Deterministic:
|
| 179 |
+
```bash
|
| 180 |
+
python run.py --algorithm="qrm-rs" --world="taxi" --map=0 --num_times=10 --batch_size=1 --buffer_size=1
|
| 181 |
+
```
|
| 182 |
+
|
| 183 |
+
##### Water
|
| 184 |
+
- Deterministic:
|
| 185 |
+
```bash
|
| 186 |
+
python run.py --algorithm="qrm-rs" --world="water" --map=3 --num_times=10 --batch_size=32 --buffer_size=50000
|
| 187 |
+
```
|
| 188 |
+
|
| 189 |
+
For noisy or infeasible environments runs, additional arguments like `--noise_level=0.1` or `--missing=True` can be appended.
|
| 190 |
+
|
| 191 |
+
#### CRM and HRM
|
| 192 |
+
Change your current directory to `./psltl/baseline_algo/crm` and use the following commands:
|
| 193 |
+
|
| 194 |
+
##### Office
|
| 195 |
+
- Deterministic:
|
| 196 |
+
```bash
|
| 197 |
+
python run.py --alg=qlearning --env=Office-single-v0 --num_timesteps=6e4 --gamma=0.95 --env_name="office" --seed 0 --use_crm --eval_freq=100 --use_rs
|
| 198 |
+
```
|
| 199 |
+
- Noise:
|
| 200 |
+
```bash
|
| 201 |
+
python run.py --alg=qlearning --env=Office-single-v0 --num_timesteps=6e4 --gamma=0.95 --env_name="office" --seed 0 --use_crm --eval_freq=100 --use_rs --noise_level 0.1
|
| 202 |
+
```
|
| 203 |
+
- Infeasible:
|
| 204 |
+
```bash
|
| 205 |
+
python run.py --alg=qlearning --env=Office-single-v0 --num_timesteps=6e4 --gamma=0.95 --env_name="office" --seed 0 --use_crm --eval_freq=100 --use_rs --missing True
|
| 206 |
+
```
|
| 207 |
+
|
| 208 |
+
##### Taxi
|
| 209 |
+
- Deterministic:
|
| 210 |
+
```bash
|
| 211 |
+
python run.py --alg=qlearning --env=Taxi-v0 --num_timesteps=5e5 --gamma=0.9 --env_name="taxi" --seed 0 --use_rs --use_crm --eval_freq=1000
|
| 212 |
+
```
|
| 213 |
+
|
| 214 |
+
##### Water
|
| 215 |
+
- Deterministic:
|
| 216 |
+
```bash
|
| 217 |
+
python run.py --alg=deepq --env=Water-single-M3-v0 --num_timesteps=2e6 --gamma=0.9 --env_name="water" --use_crm --seed 0 --use_rs
|
| 218 |
+
```
|
| 219 |
+
|
| 220 |
+
##### HalfCheetah
|
| 221 |
+
- Deterministic:
|
| 222 |
+
```bash
|
| 223 |
+
python run.py --alg=ddpg --env=Half-Cheetah-RM2-v0 --num_timesteps=2e6 --gamma=0.99 --env_name="cheetah" --use_crm --seed 0 --normalize_observations=True
|
| 224 |
+
```
|
| 225 |
+
For noisy or infeasible environments runs, additional arguments like `--noise_level=0.1` or `--missing=True` can be appended.
|
| 226 |
+
|
| 227 |
+
**Note**: For CRM run, only one run will be executed. To test multiple run results, change the seed. We have used seeds 0 to 9 for 10 independent runs
|
| 228 |
+
For HRM run, simply change `--alg=qlearning` command to 1) `--alg=hrm` for Office and Taxi worlds, and 2) `--alg=dhrm` for Water and HalfCheetah worlds.
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
## Scalability
|
| 232 |
+
|
| 233 |
+
| Algorithm | On-Policy | Off-Policy | Compatability |
|
| 234 |
+
| -------- | :--------: | :--------: | ----------------|
|
| 235 |
+
| QRM | - | ✓ | DQN, DDQN |
|
| 236 |
+
| HRM, CRM | - | ✓ | DDPG, DQN, DDQN |
|
| 237 |
+
| Ours | ✓ | ✓ | DDPG, TD3, SAC, PPO, A2C, DQN, DDQN (customized from stable-baseline3) |
|
| 238 |
+
|
| 239 |
+
|
| 240 |
+
## Reproducibility for the results
|
| 241 |
+
|
| 242 |
+
### Computing Resources
|
| 243 |
+
**Note**: Experiments were primarily conducted on a server using the Slurm scheduler. We ran experiments in parallel, specifying `--total_run 1` while varying the seed from `--seed 0` to `--seed 9` for both CRM and our approach. For QRM, we conducted 10 runs specifying `--num_times=10`. The execution times for each run varied based on the specific environment:
|
| 244 |
+
- Office World: Each run took between 5 to 10 minutes.
|
| 245 |
+
- Taxi World: Each run required approximately one hour.
|
| 246 |
+
- Water World: Each run took more than a day to complete.
|
| 247 |
+
- HalfCheetah World: Each run also took more than a day.
|
| 248 |
+
|
| 249 |
+
Note that the running time varied depending on the type of reward function used.
|
| 250 |
+
Empirically, we observed that runs with hybrid reward functions typically took significantly more time.
|
| 251 |
+
In the case of Water and Cheetah worlds with hybrid functions, each run took approximately 2 to 4 days to complete.
|
| 252 |
+
|
| 253 |
+
If you have limited computing resources, we suggest using Progress reward function, as they require less time to run and still demonstrate good performance compared to Reward Machine methods.
|
| 254 |
+
|
| 255 |
+
We note the hardware specifications upon which all experiments were conducted:
|
| 256 |
+
#### Hardware Specification
|
| 257 |
+
When running without a GPU, we recommend focusing on the grid world examples (Office and Taxi) with at least a 10th gen i7 CPU and 8GB of RAM.
|
| 258 |
+
However, for the other environments, we recommend using a GPU enabled server or desktop with 16GB RAM and a CUDA enabled GPU such as an RTX 2080 or later.
|
| 259 |
+
|
| 260 |
+
### How to Plot
|
| 261 |
+
To ensure the reproducibility of the result plots presented in the paper, we have organized the relevant plot-related files into the results_plot folder.
|
| 262 |
+
|
| 263 |
+
For monitoring and tracking reward and success rates during training, we have implemented custom callbacks and utilized evaluate_policy.py, which is derived from the stable-baselines3 library. These functionalities can be found in the following files: `psltl/rl_agents/common/callbacks.py` and `psltl/rl_agents/common/evaluation.py`.
|
| 264 |
+
|
| 265 |
+
The results obtained from the callbacks, including reward and success rate, are stored in the `/log` folder with a specific format: `/log/environment's name/reward function name, adrs, automaton representation/seed/evaluations.npz`. Additionally, the trained model is saved as `RL algorithm.zip` in the same log folder.
|
| 266 |
+
|
| 267 |
+
In order to facilitate result visualization and comparison across all algorithms, we convert the saved npz files into CSV format. Please note that the saved file types may vary for QRM and CRM, as they are based on the original implementations. For more detailed instructions, please refer to the README.md file included in the corresponding folder.
|
| 268 |
+
|
| 269 |
+
## Repository Structure
|
| 270 |
+
|
| 271 |
+
### Environments
|
| 272 |
+
1. `psltl/envs/skeletons`: Core files for RM or LTL environments (not specific to environments like office, toy, etc.).
|
| 273 |
+
2. `psltl/envs/common`: Specific environment designs (state, dynamic, action) like office, toy, mujoco, etc.
|
| 274 |
+
3. `psltl/envs/ltl_envs`: LTL environments based on the designs in the "common" folder. These can be continuous control or grid world environments.
|
| 275 |
+
|
| 276 |
+
### Linear Temporal Logic
|
| 277 |
+
1. `psltl/ltl/ltl_infos`: Saved LTL information for each environment (number of states, transitions, etc.).
|
| 278 |
+
2. `psltl/ltl`: Include python files to encode LTL formular with DFA using lydia library (`generate_ltl.ipynb` and `partial_sat_atm.py`), and load the saved DFA (`partial_sat_atm_load.py`). **Note**: In order to run generate_ltl.ipynb, you should use docker, and follow the instruction from here: https://github.com/whitemech/logaut. If you are using virtual environment, you should execute the following terminal command in the directory of your virtual environment;
|
| 279 |
+
```
|
| 280 |
+
echo '#!/usr/bin/env sh' > lydia
|
| 281 |
+
echo 'docker run -v$(pwd):/home/default whitemech/lydia lydia "$@"' >> lydia
|
| 282 |
+
sudo chmod u+x lydia
|
| 283 |
+
```
|
| 284 |
+
|
| 285 |
+
For example, my virtual environment directory is 'home/mj/anaconda3/envs/psltl/bin', and I type the terminal command on the directory.
|
| 286 |
+
|
| 287 |
+
### Reward Functions
|
| 288 |
+
1. `psltl/reward_functions/reward_function_standards.py`: Contains naive, progress, hybrid reward function classes.
|
| 289 |
+
|
| 290 |
+
### Algorithms
|
| 291 |
+
1. `psltl/rl_agents`: Customized RL agents for generic environments, typically used for LTL environments. Includes a custom evaluation method for success rate tracking.
|
| 292 |
+
|
| 293 |
+
### Training
|
| 294 |
+
1. `psltl/learner/learner.py`: Executes the algorithm.
|
| 295 |
+
2. `psltl/learner/learning_param.py`: Defines learning parameters.
|
| 296 |
+
3. `psltl/learner/ltl_learner.py`: Sets up the LTL environment and RL algorithms
|
| 297 |
+
|
| 298 |
+
### Plot
|
| 299 |
+
1. `results_plot`: Plot Results
|
| 300 |
+
|
| 301 |
+
## License
|
| 302 |
+
|
| 303 |
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
data/psltl/__init__.py
ADDED
|
File without changes
|
data/psltl/baseline_algo/crm/autoexcution.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
from itertools import product
|
| 3 |
+
import sys
|
| 4 |
+
|
| 5 |
+
# python run.py --alg=hrm --env=Taxi-v0 --num_timesteps=5e5 --gamma=0.9 --env_name="taxi" --seed 0
|
| 6 |
+
# python run.py --alg=dhrm --env=Water-single-M3-v0 --num_timesteps=2e6 --gamma=0.9 --env_name="water" --use_rs --seed 0
|
| 7 |
+
# python run.py --alg=dhrm --num_timesteps=2e6 --gamma=0.99 --use_crm --env=Half-Cheetah-RM2-v0 --normalize_observations=True --seed=0 --env_name=cheetah
|
| 8 |
+
|
| 9 |
+
algs = ["hrm"]
|
| 10 |
+
# grid worlds
|
| 11 |
+
envs = ["Taxi-v0", "Office-single-v0"]
|
| 12 |
+
envs2names = {"Taxi-v0": "taxi", "Office-single-v0": "office"}
|
| 13 |
+
names2steps = {"taxi": 5e5, "office": 6e4}
|
| 14 |
+
names2gamma = {"taxi": 0.9, "office": 0.95}
|
| 15 |
+
env_types = ["complete", "noise", "missing"]
|
| 16 |
+
# continous worlds
|
| 17 |
+
# envs = ["Water-single-M3-v0", "Half-Cheetah-RM2-v0"]
|
| 18 |
+
envs = ["Water-single-M3-v0"]
|
| 19 |
+
envs2names = {"Water-single-M3-v0": "water", "Half-Cheetah-RM2-v0": "cheetah"}
|
| 20 |
+
names2steps = {"water": 2e6, "cheetah": 2e6}
|
| 21 |
+
names2gamma = {"water": 0.9, "cheetah": 0.99}
|
| 22 |
+
env_types = ["complete", "noise", "missing"]
|
| 23 |
+
env_types = ["complete"]
|
| 24 |
+
|
| 25 |
+
# env_names = ["office", "taxi", "cheetah"]
|
| 26 |
+
# env_names = ["office", "taxi", "water", "cheetah"]
|
| 27 |
+
|
| 28 |
+
total_comb = list(
|
| 29 |
+
product([i for i in algs], [i for i in envs], [i for i in env_types])
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
with open("temp_experiments.txt", "w") as f:
|
| 33 |
+
f.write("ArrayTaskID,AlgoName,Env,TimeSteps,Gamma,EnvName,Seed,EnvType\n")
|
| 34 |
+
idx = 0
|
| 35 |
+
for element in total_comb:
|
| 36 |
+
env = element[1]
|
| 37 |
+
name = envs2names[env]
|
| 38 |
+
if name in ["office", "taxi", "water", "cheetah"]:
|
| 39 |
+
for seed in range(10):
|
| 40 |
+
idx += 1
|
| 41 |
+
steps = names2steps[name]
|
| 42 |
+
gamma = names2gamma[name]
|
| 43 |
+
env_type = element[-1]
|
| 44 |
+
f.write(f"{idx},{element[0]},{env},{steps},{gamma},{name},{seed},{env_type}\n")
|
| 45 |
+
|
| 46 |
+
command = ['awk', 'BEGIN {FS=OFS=","} {gsub(/,/, "\t"); print}', 'temp_experiments.txt']
|
| 47 |
+
output_file = "hrm_experiments.txt"
|
| 48 |
+
subprocess.run(command, stdout=open(output_file, 'w'))
|
| 49 |
+
del_command = ['rm', 'temp_experiments.txt']
|
| 50 |
+
subprocess.run(del_command, stdout=subprocess.PIPE)
|
data/psltl/baseline_algo/crm/cmd_util.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Helpers for scripts like run_atari.py.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
try:
|
| 7 |
+
from mpi4py import MPI
|
| 8 |
+
except ImportError:
|
| 9 |
+
MPI = None
|
| 10 |
+
|
| 11 |
+
import gym
|
| 12 |
+
from gym.wrappers import FlattenObservation, FilterObservation
|
| 13 |
+
from baselines import logger
|
| 14 |
+
from baselines.bench import Monitor
|
| 15 |
+
from baselines.common import set_global_seeds
|
| 16 |
+
from baselines.common.atari_wrappers import make_atari, wrap_deepmind
|
| 17 |
+
from baselines.common.vec_env.subproc_vec_env import SubprocVecEnv
|
| 18 |
+
from baselines.common.vec_env.dummy_vec_env import DummyVecEnv
|
| 19 |
+
from baselines.common import retro_wrappers
|
| 20 |
+
from baselines.common.wrappers import ClipActionsWrapper
|
| 21 |
+
from baselines.common.cmd_util import arg_parser
|
| 22 |
+
|
| 23 |
+
from psltl.baseline_algo.crm.reward_machines.rm_environment import RewardMachineWrapper, HierarchicalRMWrapper
|
| 24 |
+
|
| 25 |
+
def make_vec_env(env_id, env_type, num_env, seed, args,
|
| 26 |
+
wrapper_kwargs=None,
|
| 27 |
+
env_kwargs=None,
|
| 28 |
+
start_index=0,
|
| 29 |
+
reward_scale=1.0,
|
| 30 |
+
flatten_dict_observations=True,
|
| 31 |
+
gamestate=None,
|
| 32 |
+
initializer=None,
|
| 33 |
+
force_dummy=False):
|
| 34 |
+
"""
|
| 35 |
+
Create a wrapped, monitored SubprocVecEnv for Atari and MuJoCo.
|
| 36 |
+
"""
|
| 37 |
+
wrapper_kwargs = wrapper_kwargs or {}
|
| 38 |
+
env_kwargs = env_kwargs or {}
|
| 39 |
+
mpi_rank = MPI.COMM_WORLD.Get_rank() if MPI else 0
|
| 40 |
+
seed = seed + 10000 * mpi_rank if seed is not None else None
|
| 41 |
+
logger_dir = logger.get_dir()
|
| 42 |
+
def make_thunk(rank, initializer=None):
|
| 43 |
+
return lambda: make_env(
|
| 44 |
+
env_id=env_id,
|
| 45 |
+
env_type=env_type,
|
| 46 |
+
args=args,
|
| 47 |
+
mpi_rank=mpi_rank,
|
| 48 |
+
subrank=rank,
|
| 49 |
+
seed=seed,
|
| 50 |
+
reward_scale=reward_scale,
|
| 51 |
+
gamestate=gamestate,
|
| 52 |
+
flatten_dict_observations=flatten_dict_observations,
|
| 53 |
+
wrapper_kwargs=wrapper_kwargs,
|
| 54 |
+
env_kwargs=env_kwargs,
|
| 55 |
+
logger_dir=logger_dir,
|
| 56 |
+
initializer=initializer
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
set_global_seeds(seed)
|
| 60 |
+
if not force_dummy and num_env > 1:
|
| 61 |
+
return SubprocVecEnv([make_thunk(i + start_index, initializer=initializer) for i in range(num_env)])
|
| 62 |
+
else:
|
| 63 |
+
return DummyVecEnv([make_thunk(i + start_index, initializer=None) for i in range(num_env)])
|
| 64 |
+
|
| 65 |
+
def make_env(env_id, env_type, args, mpi_rank=0, subrank=0, seed=None, reward_scale=1.0, gamestate=None, flatten_dict_observations=True, wrapper_kwargs=None, env_kwargs=None, logger_dir=None, initializer=None):
|
| 66 |
+
if initializer is not None:
|
| 67 |
+
initializer(mpi_rank=mpi_rank, subrank=subrank)
|
| 68 |
+
|
| 69 |
+
wrapper_kwargs = wrapper_kwargs or {}
|
| 70 |
+
env_kwargs = env_kwargs or {}
|
| 71 |
+
if ':' in env_id:
|
| 72 |
+
import re
|
| 73 |
+
import importlib
|
| 74 |
+
module_name = re.sub(':.*','',env_id)
|
| 75 |
+
env_id = re.sub('.*:', '', env_id)
|
| 76 |
+
importlib.import_module(module_name)
|
| 77 |
+
|
| 78 |
+
env = gym.make(env_id, **env_kwargs)
|
| 79 |
+
|
| 80 |
+
# Adding RM wrappers if needed
|
| 81 |
+
if args.alg.endswith("hrm") or args.alg.endswith("dhrm"):
|
| 82 |
+
env = RewardMachineWrapper(env, args.use_crm, args.use_rs, args.gamma, args.rs_gamma, args.missing)
|
| 83 |
+
env = HierarchicalRMWrapper(env, args.r_min, args.r_max, args.use_self_loops, args.use_rs, args.gamma, args.rs_gamma,)
|
| 84 |
+
eval_env = HierarchicalRMWrapper(env, args.r_min, args.r_max, args.use_self_loops, args.use_rs, args.gamma, args.rs_gamma,)
|
| 85 |
+
else:
|
| 86 |
+
env = RewardMachineWrapper(env, args.use_crm, args.use_rs, args.gamma, args.rs_gamma, args.missing)
|
| 87 |
+
env = HierarchicalRMWrapper(env, args.r_min, args.r_max, args.use_self_loops, args.use_rs, args.gamma, args.rs_gamma,)
|
| 88 |
+
|
| 89 |
+
# elif args.use_rs or args.use_crm or args.missing:
|
| 90 |
+
# env = RewardMachineWrapper(env, args.use_crm, args.use_rs, args.gamma, args.rs_gamma, args.missing)
|
| 91 |
+
|
| 92 |
+
if flatten_dict_observations and isinstance(env.observation_space, gym.spaces.Dict):
|
| 93 |
+
env = FlattenObservation(env)
|
| 94 |
+
|
| 95 |
+
env.seed(seed + subrank if seed is not None else None)
|
| 96 |
+
# env = Monitor(env,
|
| 97 |
+
# logger_dir and os.path.join(logger_dir, str(mpi_rank) + '.' + str(subrank)),
|
| 98 |
+
# allow_early_resets=True)
|
| 99 |
+
|
| 100 |
+
if isinstance(env.action_space, gym.spaces.Box):
|
| 101 |
+
env = ClipActionsWrapper(env)
|
| 102 |
+
|
| 103 |
+
if reward_scale != 1:
|
| 104 |
+
env = retro_wrappers.RewardScaler(env, reward_scale)
|
| 105 |
+
|
| 106 |
+
return env
|
| 107 |
+
|
| 108 |
+
def common_arg_parser():
|
| 109 |
+
"""
|
| 110 |
+
Create an argparse.ArgumentParser.
|
| 111 |
+
"""
|
| 112 |
+
parser = arg_parser()
|
| 113 |
+
parser.add_argument('--env', help='environment ID', type=str, default='Reacher-v2')
|
| 114 |
+
parser.add_argument('--env_name', help='environment name', type=str, default='office')
|
| 115 |
+
parser.add_argument('--missing', help='missing goal', type=bool, default=False)
|
| 116 |
+
parser.add_argument('--noise_level', help='noise_level in environment', type=float, default=0.)
|
| 117 |
+
parser.add_argument('--env_type', help='type of environment, used when the environment type cannot be automatically determined', type=str)
|
| 118 |
+
parser.add_argument('--seed', help='RNG seed', type=int, default=None)
|
| 119 |
+
parser.add_argument('--alg', help='Algorithm', type=str, default='ppo2')
|
| 120 |
+
parser.add_argument('--num_timesteps', type=float, default=1e6),
|
| 121 |
+
parser.add_argument('--network', help='network type (mlp, cnn, lstm, cnn_lstm, conv_only)', default=None)
|
| 122 |
+
parser.add_argument('--gamestate', help='game state to load (so far only used in retro games)', default=None)
|
| 123 |
+
parser.add_argument('--num_env', help='Number of environment copies being run in parallel. When not specified, set to number of cpus for Atari, and to 1 for Mujoco', default=None, type=int)
|
| 124 |
+
parser.add_argument('--reward_scale', help='Reward scale factor. Default: 1.0', default=1.0, type=float)
|
| 125 |
+
parser.add_argument('--save_path', help='Path to save trained model to', default=None, type=str)
|
| 126 |
+
parser.add_argument('--save_video_interval', help='Save video every x steps (0 = disabled)', default=0, type=int)
|
| 127 |
+
parser.add_argument('--save_video_length', help='Length of recorded video. Default: 200', default=200, type=int)
|
| 128 |
+
parser.add_argument('--log_path', help='Directory to save learning curve data.', default=None, type=str)
|
| 129 |
+
parser.add_argument('--test', default=False, help='we only test environment with saved models')
|
| 130 |
+
parser.add_argument('--play', default=False, action='store_true')
|
| 131 |
+
# RM-related arguments
|
| 132 |
+
parser.add_argument("--use_rs", help="Use reward shaping", action="store_true", default=False)
|
| 133 |
+
parser.add_argument("--use_crm", help="Use counterfactual experience", action="store_true", default=False)
|
| 134 |
+
parser.add_argument('--gamma', help="Discount factor", type=float, default=0.9)
|
| 135 |
+
parser.add_argument('--rs_gamma', help="Discount factor used for reward shaping", type=float, default=0.9)
|
| 136 |
+
parser.add_argument('--r_min', help="R-min reward used for training option policies in hrm", type=float, default=0.0)
|
| 137 |
+
parser.add_argument('--r_max', help="R-max reward used for training option policies in hrm", type=float, default=1.0)
|
| 138 |
+
parser.add_argument("--use_self_loops", help="Add option policies for self-loops in the RMs", action="store_true", default=False)
|
| 139 |
+
return parser
|
data/psltl/baseline_algo/crm/envs/__init__.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from gym.envs.registration import register
|
| 2 |
+
import gym
|
| 3 |
+
env_dict = gym.envs.registration.registry.env_specs.copy()
|
| 4 |
+
for env in env_dict:
|
| 5 |
+
if 'Half-Cheetah-RM1-v0' in env:
|
| 6 |
+
print("Remove {} from registry".format(env))
|
| 7 |
+
del gym.envs.registration.registry.env_specs[env]
|
| 8 |
+
if 'Half-Cheetah-RM2-v0' in env:
|
| 9 |
+
print("Remove {} from registry".format(env))
|
| 10 |
+
del gym.envs.registration.registry.env_specs[env]
|
| 11 |
+
|
| 12 |
+
for i in range(11):
|
| 13 |
+
w_id = 'Water-single-M%d-v0'%i
|
| 14 |
+
if w_id in env:
|
| 15 |
+
print("Remove {} from registry".format(env))
|
| 16 |
+
del gym.envs.registration.registry.env_specs[env]
|
| 17 |
+
for i in range(11):
|
| 18 |
+
w_id = 'Water-M%d-v0'%i
|
| 19 |
+
if w_id in env:
|
| 20 |
+
print("Remove {} from registry".format(env))
|
| 21 |
+
del gym.envs.registration.registry.env_specs[env]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# ----------------------------------------- Half-Cheetah
|
| 27 |
+
|
| 28 |
+
register(
|
| 29 |
+
id='Half-Cheetah-RM1-v0',
|
| 30 |
+
entry_point='envs.mujoco_rm.half_cheetah_environment:MyHalfCheetahEnvRM1',
|
| 31 |
+
max_episode_steps=1000,
|
| 32 |
+
)
|
| 33 |
+
register(
|
| 34 |
+
id='Half-Cheetah-RM2-v0',
|
| 35 |
+
entry_point='envs.mujoco_rm.half_cheetah_environment:MyHalfCheetahEnvRM2',
|
| 36 |
+
max_episode_steps=1000,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# ----------------------------------------- WATER
|
| 42 |
+
for i in range(11):
|
| 43 |
+
w_id = 'Water-M%d-v0'%i
|
| 44 |
+
w_en = 'envs.water.water_environment:WaterRMEnvM%d'%i
|
| 45 |
+
register(
|
| 46 |
+
id=w_id,
|
| 47 |
+
entry_point=w_en,
|
| 48 |
+
max_episode_steps=600
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
for i in range(11):
|
| 52 |
+
w_id = 'Water-single-M%d-v0'%i
|
| 53 |
+
w_en = 'envs.water.water_environment:WaterRM10EnvM%d'%i
|
| 54 |
+
register(
|
| 55 |
+
id=w_id,
|
| 56 |
+
entry_point=w_en,
|
| 57 |
+
max_episode_steps=600
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# # ----------------------------------------- OFFICE
|
| 61 |
+
# register(
|
| 62 |
+
# id='Office-v0',
|
| 63 |
+
# entry_point='envs.grids.grid_environment:OfficeRMEnv',
|
| 64 |
+
# max_episode_steps=1000
|
| 65 |
+
# )
|
| 66 |
+
|
| 67 |
+
register(
|
| 68 |
+
id='Office-single-v0',
|
| 69 |
+
entry_point='envs.grids.grid_environment:OfficeRM3Env',
|
| 70 |
+
max_episode_steps=100
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# # ----------------------------------------- CRAFT
|
| 74 |
+
# for i in range(11):
|
| 75 |
+
# w_id = 'Craft-M%d-v0'%i
|
| 76 |
+
# w_en = 'envs.grids.grid_environment:CraftRMEnvM%d'%i
|
| 77 |
+
# register(
|
| 78 |
+
# id=w_id,
|
| 79 |
+
# entry_point=w_en,
|
| 80 |
+
# max_episode_steps=1000
|
| 81 |
+
# )
|
| 82 |
+
|
| 83 |
+
# for i in range(11):
|
| 84 |
+
# w_id = 'Craft-single-M%d-v0'%i
|
| 85 |
+
# w_en = 'envs.grids.grid_environment:CraftRM10EnvM%d'%i
|
| 86 |
+
# register(
|
| 87 |
+
# id=w_id,
|
| 88 |
+
# entry_point=w_en,
|
| 89 |
+
# max_episode_steps=1000
|
| 90 |
+
# )
|
| 91 |
+
|
| 92 |
+
# ----------------------------------------- Taxi
|
| 93 |
+
register(
|
| 94 |
+
id='Taxi-v0',
|
| 95 |
+
entry_point='envs.grids.grid_environment:TaxiRMEnv',
|
| 96 |
+
max_episode_steps=200
|
| 97 |
+
)
|
data/psltl/baseline_algo/crm/envs/grids/__init__.py
ADDED
|
File without changes
|
data/psltl/baseline_algo/crm/envs/grids/craft_world.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from psltl.baseline_algo.crm.envs.grids.game_objects import *
|
| 2 |
+
import random, math, os
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
class CraftWorld:
|
| 6 |
+
|
| 7 |
+
def __init__(self, file_map):
|
| 8 |
+
self.file_map = file_map
|
| 9 |
+
self._load_map(file_map)
|
| 10 |
+
self.env_game_over = False
|
| 11 |
+
|
| 12 |
+
def reset(self):
|
| 13 |
+
self.agent.reset()
|
| 14 |
+
|
| 15 |
+
def execute_action(self, a):
|
| 16 |
+
"""
|
| 17 |
+
We execute 'action' in the game
|
| 18 |
+
"""
|
| 19 |
+
agent = self.agent
|
| 20 |
+
ni,nj = agent.i, agent.j
|
| 21 |
+
|
| 22 |
+
# Getting new position after executing action
|
| 23 |
+
ni,nj = self._get_next_position(ni,nj,a)
|
| 24 |
+
|
| 25 |
+
# Interacting with the objects that is in the next position (this doesn't include monsters)
|
| 26 |
+
action_succeeded = self.map_array[ni][nj].interact(agent)
|
| 27 |
+
|
| 28 |
+
# So far, an action can only fail if the new position is a wall
|
| 29 |
+
if action_succeeded:
|
| 30 |
+
agent.change_position(ni,nj)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def _get_next_position(self, ni, nj, a):
|
| 34 |
+
"""
|
| 35 |
+
Returns the position where the agent would be if we execute action
|
| 36 |
+
"""
|
| 37 |
+
action = Actions(a)
|
| 38 |
+
|
| 39 |
+
# OBS: Invalid actions behave as NO-OP
|
| 40 |
+
if action == Actions.up : ni-=1
|
| 41 |
+
if action == Actions.down : ni+=1
|
| 42 |
+
if action == Actions.left : nj-=1
|
| 43 |
+
if action == Actions.right: nj+=1
|
| 44 |
+
|
| 45 |
+
return ni,nj
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def get_true_propositions(self):
|
| 49 |
+
"""
|
| 50 |
+
Returns the string with the propositions that are True in this state
|
| 51 |
+
"""
|
| 52 |
+
ret = str(self.map_array[self.agent.i][self.agent.j]).strip()
|
| 53 |
+
return ret
|
| 54 |
+
|
| 55 |
+
def get_features(self):
|
| 56 |
+
"""
|
| 57 |
+
Returns the features of the current state (i.e., the location of the agent)
|
| 58 |
+
"""
|
| 59 |
+
return np.array([self.agent.i,self.agent.j])
|
| 60 |
+
|
| 61 |
+
def show(self):
|
| 62 |
+
"""
|
| 63 |
+
Prints the current map
|
| 64 |
+
"""
|
| 65 |
+
r = ""
|
| 66 |
+
for i in range(self.map_height):
|
| 67 |
+
s = ""
|
| 68 |
+
for j in range(self.map_width):
|
| 69 |
+
if self.agent.idem_position(i,j):
|
| 70 |
+
s += str(self.agent)
|
| 71 |
+
else:
|
| 72 |
+
s += str(self.map_array[i][j])
|
| 73 |
+
if(i > 0):
|
| 74 |
+
r += "\n"
|
| 75 |
+
r += s
|
| 76 |
+
print(r)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def get_model(self):
|
| 80 |
+
"""
|
| 81 |
+
This method returns a model of the environment.
|
| 82 |
+
We use the model to compute optimal policies using value iteration.
|
| 83 |
+
The optimal policies are used to set the average reward per step of each task to 1.
|
| 84 |
+
"""
|
| 85 |
+
S = [(x,y) for x in range(1,40) for y in range(1,40)] # States
|
| 86 |
+
A = self.actions.copy() # Actions
|
| 87 |
+
L = dict([((x,y),str(self.map_array[x][y]).strip()) for x,y in S]) # Labeling function
|
| 88 |
+
T = {} # Transitions (s,a) -> s' (they are deterministic)
|
| 89 |
+
for s in S:
|
| 90 |
+
x,y = s
|
| 91 |
+
for a in A:
|
| 92 |
+
x2,y2 = self._get_next_position(x,y,a)
|
| 93 |
+
T[(s,a)] = s if str(self.map_array[x2][y2]) == "X" else (x2,y2)
|
| 94 |
+
return S,A,L,T # SALT xD
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def _load_map(self,file_map):
|
| 98 |
+
"""
|
| 99 |
+
This method adds the following attributes to the game:
|
| 100 |
+
- self.map_array: array containing all the static objects in the map (no monsters and no agent)
|
| 101 |
+
- e.g. self.map_array[i][j]: contains the object located on row 'i' and column 'j'
|
| 102 |
+
- self.agent: is the agent!
|
| 103 |
+
- self.map_height: number of rows in every room
|
| 104 |
+
- self.map_width: number of columns in every room
|
| 105 |
+
The inputs:
|
| 106 |
+
- file_map: path to the map file
|
| 107 |
+
"""
|
| 108 |
+
# contains all the actions that the agent can perform
|
| 109 |
+
self.actions = [Actions.up.value, Actions.right.value, Actions.down.value, Actions.left.value]
|
| 110 |
+
# loading the map
|
| 111 |
+
self.map_array = []
|
| 112 |
+
self.class_ids = {} # I use the lower case letters to define the features
|
| 113 |
+
f = open(file_map)
|
| 114 |
+
i,j = 0,0
|
| 115 |
+
for l in f:
|
| 116 |
+
# I don't consider empty lines!
|
| 117 |
+
if(len(l.rstrip()) == 0): continue
|
| 118 |
+
|
| 119 |
+
# this is not an empty line!
|
| 120 |
+
row = []
|
| 121 |
+
j = 0
|
| 122 |
+
for e in l.rstrip():
|
| 123 |
+
if e in "abcdefghijklmnopqrstuvwxyzH":
|
| 124 |
+
entity = Empty(i,j,label=e)
|
| 125 |
+
if e not in self.class_ids:
|
| 126 |
+
self.class_ids[e] = len(self.class_ids)
|
| 127 |
+
if e in " A": entity = Empty(i,j)
|
| 128 |
+
if e == "X": entity = Obstacle(i,j)
|
| 129 |
+
if e == "A": self.agent = Agent(i,j,self.actions)
|
| 130 |
+
row.append(entity)
|
| 131 |
+
j += 1
|
| 132 |
+
self.map_array.append(row)
|
| 133 |
+
i += 1
|
| 134 |
+
f.close()
|
| 135 |
+
# height width
|
| 136 |
+
self.map_height, self.map_width = len(self.map_array), len(self.map_array[0])
|
data/psltl/baseline_algo/crm/envs/grids/game_objects.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from enum import Enum
|
| 2 |
+
import random
|
| 3 |
+
|
| 4 |
+
"""
|
| 5 |
+
The following classes are the types of objects that we are currently supporting
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
class Entity:
|
| 9 |
+
def __init__(self,i,j): #row and column
|
| 10 |
+
self.i = i
|
| 11 |
+
self.j = j
|
| 12 |
+
|
| 13 |
+
def change_position(self,i,j):
|
| 14 |
+
self.i = i
|
| 15 |
+
self.j = j
|
| 16 |
+
|
| 17 |
+
def idem_position(self,i,j):
|
| 18 |
+
return self.i==i and self.j==j
|
| 19 |
+
|
| 20 |
+
def interact(self, agent):
|
| 21 |
+
return True
|
| 22 |
+
|
| 23 |
+
class Agent(Entity):
|
| 24 |
+
def __init__(self,i,j,actions):
|
| 25 |
+
super().__init__(i,j)
|
| 26 |
+
self.actions = actions
|
| 27 |
+
self.initial_position = (i,j)
|
| 28 |
+
|
| 29 |
+
def reset(self):
|
| 30 |
+
self.change_position(*self.initial_position)
|
| 31 |
+
|
| 32 |
+
def get_actions(self):
|
| 33 |
+
return self.actions
|
| 34 |
+
|
| 35 |
+
def __str__(self):
|
| 36 |
+
return "A"
|
| 37 |
+
|
| 38 |
+
class Obstacle(Entity):
|
| 39 |
+
def __init__(self,i,j):
|
| 40 |
+
super().__init__(i,j)
|
| 41 |
+
|
| 42 |
+
def interact(self, agent):
|
| 43 |
+
return False
|
| 44 |
+
|
| 45 |
+
def __str__(self):
|
| 46 |
+
return "X"
|
| 47 |
+
|
| 48 |
+
class Empty(Entity):
|
| 49 |
+
def __init__(self,i,j,label=" "):
|
| 50 |
+
super().__init__(i,j)
|
| 51 |
+
self.label = label
|
| 52 |
+
|
| 53 |
+
def __str__(self):
|
| 54 |
+
return self.label
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
"""
|
| 58 |
+
Enum with the actions that the agent can execute
|
| 59 |
+
"""
|
| 60 |
+
class Actions(Enum):
|
| 61 |
+
up = 0 # move up
|
| 62 |
+
right = 1 # move right
|
| 63 |
+
down = 2 # move down
|
| 64 |
+
left = 3 # move left
|
| 65 |
+
none = 4 # none or pick
|
| 66 |
+
drop = 5
|
data/psltl/baseline_algo/crm/envs/grids/grid_environment.py
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gym, random
|
| 2 |
+
from gym import spaces
|
| 3 |
+
import numpy as np
|
| 4 |
+
from psltl.baseline_algo.crm.reward_machines.rm_environment import RewardMachineEnv
|
| 5 |
+
from psltl.baseline_algo.crm.envs.grids.craft_world import CraftWorld
|
| 6 |
+
from psltl.baseline_algo.crm.envs.grids.office_world import OfficeWorld
|
| 7 |
+
from psltl.baseline_algo.crm.envs.grids.value_iteration import value_iteration
|
| 8 |
+
from psltl.baseline_algo.crm.envs.grids.taxi_world import Taxi
|
| 9 |
+
|
| 10 |
+
class GridEnv(gym.Env):
|
| 11 |
+
def __init__(self, env, env_name: str="office"):
|
| 12 |
+
self.env = env
|
| 13 |
+
N,M = self.env.map_height, self.env.map_width
|
| 14 |
+
if env_name == "office":
|
| 15 |
+
self.action_space = spaces.Discrete(4) # up, right, down, left
|
| 16 |
+
elif env_name == "taxi":
|
| 17 |
+
self.action_space = spaces.Discrete(6) # up, right, down, left, pickup, drop-off
|
| 18 |
+
self.observation_space = spaces.Box(low=0, high=max([N,M]), shape=(2,), dtype=np.uint8)
|
| 19 |
+
|
| 20 |
+
def get_events(self):
|
| 21 |
+
return self.env.get_true_propositions()
|
| 22 |
+
|
| 23 |
+
def step(self, action):
|
| 24 |
+
self.env.execute_action(action)
|
| 25 |
+
obs = self.env.get_features()
|
| 26 |
+
reward = 0 # all the reward comes from the RM
|
| 27 |
+
done = False
|
| 28 |
+
info = {}
|
| 29 |
+
return obs, reward, done, info
|
| 30 |
+
|
| 31 |
+
def reset(self):
|
| 32 |
+
self.env.reset()
|
| 33 |
+
return self.env.get_features()
|
| 34 |
+
|
| 35 |
+
def show(self):
|
| 36 |
+
self.env.show()
|
| 37 |
+
|
| 38 |
+
def get_model(self):
|
| 39 |
+
return self.env.get_model()
|
| 40 |
+
|
| 41 |
+
class GridRMEnv(RewardMachineEnv):
|
| 42 |
+
def __init__(self, env, rm_files):
|
| 43 |
+
super().__init__(env, rm_files)
|
| 44 |
+
|
| 45 |
+
def render(self, mode='human'):
|
| 46 |
+
if mode == 'human':
|
| 47 |
+
# commands
|
| 48 |
+
str_to_action = {"w":0,"d":1,"s":2,"a":3}
|
| 49 |
+
|
| 50 |
+
# play the game!
|
| 51 |
+
done = True
|
| 52 |
+
while True:
|
| 53 |
+
if done:
|
| 54 |
+
print("New episode --------------------------------")
|
| 55 |
+
obs = self.reset()
|
| 56 |
+
print("Current task:", self.rm_files[self.current_rm_id])
|
| 57 |
+
self.env.show()
|
| 58 |
+
print("Features:", obs)
|
| 59 |
+
print("RM state:", self.current_u_id)
|
| 60 |
+
print("Events:", self.env.get_events())
|
| 61 |
+
|
| 62 |
+
print("\nAction? (WASD keys or q to quite) ", end="")
|
| 63 |
+
a = input()
|
| 64 |
+
print()
|
| 65 |
+
if a == 'q':
|
| 66 |
+
break
|
| 67 |
+
# Executing action
|
| 68 |
+
if a in str_to_action:
|
| 69 |
+
obs, rew, done, _ = self.step(str_to_action[a])
|
| 70 |
+
self.env.show()
|
| 71 |
+
print("Features:", obs)
|
| 72 |
+
print("Reward:", rew)
|
| 73 |
+
print("RM state:", self.current_u_id)
|
| 74 |
+
print("Events:", self.env.get_events())
|
| 75 |
+
else:
|
| 76 |
+
print("Forbidden action")
|
| 77 |
+
else:
|
| 78 |
+
raise NotImplementedError
|
| 79 |
+
|
| 80 |
+
def test_optimal_policies(self, num_episodes, epsilon, gamma):
|
| 81 |
+
"""
|
| 82 |
+
This code computes optimal policies for each reward machine and evaluates them using epsilon-greedy exploration
|
| 83 |
+
|
| 84 |
+
PARAMS
|
| 85 |
+
----------
|
| 86 |
+
num_episodes(int): Number of evaluation episodes
|
| 87 |
+
epsilon(float): Epsilon constant for exploring the environment
|
| 88 |
+
gamma(float): Discount factor
|
| 89 |
+
|
| 90 |
+
RETURNS
|
| 91 |
+
----------
|
| 92 |
+
List with the optimal average-reward-per-step per reward machine
|
| 93 |
+
"""
|
| 94 |
+
S,A,L,T = self.env.get_model()
|
| 95 |
+
print("\nComputing optimal policies... ", end='', flush=True)
|
| 96 |
+
optimal_policies = [value_iteration(S,A,L,T,rm,gamma) for rm in self.reward_machines]
|
| 97 |
+
print("Done!")
|
| 98 |
+
optimal_ARPS = [[] for _ in range(len(optimal_policies))]
|
| 99 |
+
print("\nEvaluating optimal policies.")
|
| 100 |
+
for ep in range(num_episodes):
|
| 101 |
+
if ep % 100 == 0 and ep > 0:
|
| 102 |
+
print("%d/%d"%(ep,num_episodes))
|
| 103 |
+
self.reset()
|
| 104 |
+
s = tuple(self.obs)
|
| 105 |
+
u = self.current_u_id
|
| 106 |
+
rm_id = self.current_rm_id
|
| 107 |
+
rewards = []
|
| 108 |
+
done = False
|
| 109 |
+
while not done:
|
| 110 |
+
a = random.choice(A) if random.random() < epsilon else optimal_policies[rm_id][(s,u)]
|
| 111 |
+
_, r, done, _ = self.step(a)
|
| 112 |
+
rewards.append(r)
|
| 113 |
+
s = tuple(self.obs)
|
| 114 |
+
u = self.current_u_id
|
| 115 |
+
optimal_ARPS[rm_id].append(sum(rewards)/len(rewards))
|
| 116 |
+
print("Done!\n")
|
| 117 |
+
|
| 118 |
+
return [sum(arps)/len(arps) for arps in optimal_ARPS]
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
class OfficeRMEnv(GridRMEnv):
|
| 122 |
+
def __init__(self):
|
| 123 |
+
rm_files = ["./envs/grids/reward_machines/office/t%d.txt"%i for i in range(1,5)]
|
| 124 |
+
env = OfficeWorld()
|
| 125 |
+
super().__init__(GridEnv(env, "office"),rm_files)
|
| 126 |
+
|
| 127 |
+
class OfficeRM3Env(GridRMEnv):
|
| 128 |
+
def __init__(self):
|
| 129 |
+
rm_files = ["./envs/grids/reward_machines/office/t3.txt"]
|
| 130 |
+
env = OfficeWorld()
|
| 131 |
+
super().__init__(GridEnv(env, "office"),rm_files)
|
| 132 |
+
|
| 133 |
+
class TaxiRMEnv(GridRMEnv):
|
| 134 |
+
def __init__(self):
|
| 135 |
+
rm_files = ["./envs/grids/reward_machines/taxi/t.txt"]
|
| 136 |
+
env = Taxi()
|
| 137 |
+
super().__init__(GridEnv(env, "taxi"),rm_files)
|
| 138 |
+
|
| 139 |
+
class CraftRMEnv(GridRMEnv):
|
| 140 |
+
def __init__(self, file_map):
|
| 141 |
+
rm_files = ["./envs/grids/reward_machines/craft/t%d.txt"%i for i in range(1,11)]
|
| 142 |
+
env = CraftWorld(file_map)
|
| 143 |
+
super().__init__(GridEnv(env), rm_files)
|
| 144 |
+
|
| 145 |
+
class CraftRMEnvM0(CraftRMEnv):
|
| 146 |
+
def __init__(self):
|
| 147 |
+
file_map = "./envs/grids/maps/map_0.txt"
|
| 148 |
+
super().__init__(file_map)
|
| 149 |
+
|
| 150 |
+
class CraftRMEnvM1(CraftRMEnv):
|
| 151 |
+
def __init__(self):
|
| 152 |
+
file_map = "./envs/grids/maps/map_1.txt"
|
| 153 |
+
super().__init__(file_map)
|
| 154 |
+
|
| 155 |
+
class CraftRMEnvM2(CraftRMEnv):
|
| 156 |
+
def __init__(self):
|
| 157 |
+
file_map = "./envs/grids/maps/map_2.txt"
|
| 158 |
+
super().__init__(file_map)
|
| 159 |
+
|
| 160 |
+
class CraftRMEnvM3(CraftRMEnv):
|
| 161 |
+
def __init__(self):
|
| 162 |
+
file_map = "./envs/grids/maps/map_3.txt"
|
| 163 |
+
super().__init__(file_map)
|
| 164 |
+
|
| 165 |
+
class CraftRMEnvM4(CraftRMEnv):
|
| 166 |
+
def __init__(self):
|
| 167 |
+
file_map = "./envs/grids/maps/map_4.txt"
|
| 168 |
+
super().__init__(file_map)
|
| 169 |
+
|
| 170 |
+
class CraftRMEnvM5(CraftRMEnv):
|
| 171 |
+
def __init__(self):
|
| 172 |
+
file_map = "./envs/grids/maps/map_5.txt"
|
| 173 |
+
super().__init__(file_map)
|
| 174 |
+
|
| 175 |
+
class CraftRMEnvM6(CraftRMEnv):
|
| 176 |
+
def __init__(self):
|
| 177 |
+
file_map = "./envs/grids/maps/map_6.txt"
|
| 178 |
+
super().__init__(file_map)
|
| 179 |
+
|
| 180 |
+
class CraftRMEnvM7(CraftRMEnv):
|
| 181 |
+
def __init__(self):
|
| 182 |
+
file_map = "./envs/grids/maps/map_7.txt"
|
| 183 |
+
super().__init__(file_map)
|
| 184 |
+
|
| 185 |
+
class CraftRMEnvM8(CraftRMEnv):
|
| 186 |
+
def __init__(self):
|
| 187 |
+
file_map = "./envs/grids/maps/map_8.txt"
|
| 188 |
+
super().__init__(file_map)
|
| 189 |
+
|
| 190 |
+
class CraftRMEnvM9(CraftRMEnv):
|
| 191 |
+
def __init__(self):
|
| 192 |
+
file_map = "./envs/grids/maps/map_9.txt"
|
| 193 |
+
super().__init__(file_map)
|
| 194 |
+
|
| 195 |
+
class CraftRMEnvM10(CraftRMEnv):
|
| 196 |
+
def __init__(self):
|
| 197 |
+
file_map = "./envs/grids/maps/map_10.txt"
|
| 198 |
+
super().__init__(file_map)
|
| 199 |
+
|
| 200 |
+
# ----------------------------------------------- SINGLE TASK
|
| 201 |
+
|
| 202 |
+
class CraftRM10Env(GridRMEnv):
|
| 203 |
+
def __init__(self, file_map):
|
| 204 |
+
rm_files = ["./envs/grids/reward_machines/craft/t10.txt"]
|
| 205 |
+
env = CraftWorld(file_map)
|
| 206 |
+
super().__init__(GridEnv(env), rm_files)
|
| 207 |
+
|
| 208 |
+
class CraftRM10EnvM0(CraftRM10Env):
|
| 209 |
+
def __init__(self):
|
| 210 |
+
file_map = "./envs/grids/maps/map_0.txt"
|
| 211 |
+
super().__init__(file_map)
|
| 212 |
+
|
| 213 |
+
class CraftRM10EnvM1(CraftRM10Env):
|
| 214 |
+
def __init__(self):
|
| 215 |
+
file_map = "./envs/grids/maps/map_1.txt"
|
| 216 |
+
super().__init__(file_map)
|
| 217 |
+
|
| 218 |
+
class CraftRM10EnvM2(CraftRM10Env):
|
| 219 |
+
def __init__(self):
|
| 220 |
+
file_map = "./envs/grids/maps/map_2.txt"
|
| 221 |
+
super().__init__(file_map)
|
| 222 |
+
|
| 223 |
+
class CraftRM10EnvM3(CraftRM10Env):
|
| 224 |
+
def __init__(self):
|
| 225 |
+
file_map = "./envs/grids/maps/map_3.txt"
|
| 226 |
+
super().__init__(file_map)
|
| 227 |
+
|
| 228 |
+
class CraftRM10EnvM4(CraftRM10Env):
|
| 229 |
+
def __init__(self):
|
| 230 |
+
file_map = "./envs/grids/maps/map_4.txt"
|
| 231 |
+
super().__init__(file_map)
|
| 232 |
+
|
| 233 |
+
class CraftRM10EnvM5(CraftRM10Env):
|
| 234 |
+
def __init__(self):
|
| 235 |
+
file_map = "./envs/grids/maps/map_5.txt"
|
| 236 |
+
super().__init__(file_map)
|
| 237 |
+
|
| 238 |
+
class CraftRM10EnvM6(CraftRM10Env):
|
| 239 |
+
def __init__(self):
|
| 240 |
+
file_map = "./envs/grids/maps/map_6.txt"
|
| 241 |
+
super().__init__(file_map)
|
| 242 |
+
|
| 243 |
+
class CraftRM10EnvM7(CraftRM10Env):
|
| 244 |
+
def __init__(self):
|
| 245 |
+
file_map = "./envs/grids/maps/map_7.txt"
|
| 246 |
+
super().__init__(file_map)
|
| 247 |
+
|
| 248 |
+
class CraftRM10EnvM8(CraftRM10Env):
|
| 249 |
+
def __init__(self):
|
| 250 |
+
file_map = "./envs/grids/maps/map_8.txt"
|
| 251 |
+
super().__init__(file_map)
|
| 252 |
+
|
| 253 |
+
class CraftRM10EnvM9(CraftRM10Env):
|
| 254 |
+
def __init__(self):
|
| 255 |
+
file_map = "./envs/grids/maps/map_9.txt"
|
| 256 |
+
super().__init__(file_map)
|
| 257 |
+
|
| 258 |
+
class CraftRM10EnvM10(CraftRM10Env):
|
| 259 |
+
def __init__(self):
|
| 260 |
+
file_map = "./envs/grids/maps/map_10.txt"
|
| 261 |
+
super().__init__(file_map)
|
| 262 |
+
|
data/psltl/baseline_algo/crm/envs/grids/maps/map_0.txt
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
| 2 |
+
X X
|
| 3 |
+
X X
|
| 4 |
+
X c X
|
| 5 |
+
X X
|
| 6 |
+
X h f X
|
| 7 |
+
X d X
|
| 8 |
+
X d X
|
| 9 |
+
X X
|
| 10 |
+
X X
|
| 11 |
+
X X
|
| 12 |
+
X X
|
| 13 |
+
X X
|
| 14 |
+
X X
|
| 15 |
+
X X
|
| 16 |
+
X X
|
| 17 |
+
X h g X
|
| 18 |
+
X f f X
|
| 19 |
+
X X
|
| 20 |
+
X d X
|
| 21 |
+
X d A X
|
| 22 |
+
X X
|
| 23 |
+
X X
|
| 24 |
+
X X
|
| 25 |
+
X X
|
| 26 |
+
X b X
|
| 27 |
+
X b X
|
| 28 |
+
X X
|
| 29 |
+
X f X
|
| 30 |
+
X d X
|
| 31 |
+
X X
|
| 32 |
+
X c a X
|
| 33 |
+
X f X
|
| 34 |
+
X a e X
|
| 35 |
+
X X
|
| 36 |
+
X X
|
| 37 |
+
Xa a X
|
| 38 |
+
X g X
|
| 39 |
+
X e X
|
| 40 |
+
X a X
|
| 41 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
data/psltl/baseline_algo/crm/envs/grids/maps/map_1.txt
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
| 2 |
+
X f X
|
| 3 |
+
X a a X
|
| 4 |
+
X X
|
| 5 |
+
X X
|
| 6 |
+
X c X
|
| 7 |
+
X X
|
| 8 |
+
X h e X
|
| 9 |
+
X e X
|
| 10 |
+
X b X
|
| 11 |
+
X X
|
| 12 |
+
X X
|
| 13 |
+
X X
|
| 14 |
+
X X
|
| 15 |
+
X X
|
| 16 |
+
X d X
|
| 17 |
+
X X
|
| 18 |
+
X X
|
| 19 |
+
X a X
|
| 20 |
+
X f g X
|
| 21 |
+
X A X
|
| 22 |
+
X f X
|
| 23 |
+
X h X
|
| 24 |
+
X a X
|
| 25 |
+
X X
|
| 26 |
+
X c X
|
| 27 |
+
X X
|
| 28 |
+
X g X
|
| 29 |
+
X f dX
|
| 30 |
+
X bd X
|
| 31 |
+
X X
|
| 32 |
+
X X
|
| 33 |
+
X X
|
| 34 |
+
X X
|
| 35 |
+
X a X
|
| 36 |
+
Xd X
|
| 37 |
+
X f X
|
| 38 |
+
X X
|
| 39 |
+
X d X
|
| 40 |
+
X X
|
| 41 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
data/psltl/baseline_algo/crm/envs/grids/maps/map_10.txt
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
| 2 |
+
X X
|
| 3 |
+
X X
|
| 4 |
+
X a X
|
| 5 |
+
X X
|
| 6 |
+
X h X
|
| 7 |
+
X X
|
| 8 |
+
X X
|
| 9 |
+
X X
|
| 10 |
+
X dX
|
| 11 |
+
X X
|
| 12 |
+
X e X
|
| 13 |
+
X f X
|
| 14 |
+
X f X
|
| 15 |
+
X b X
|
| 16 |
+
X X
|
| 17 |
+
X d h f X
|
| 18 |
+
X d X
|
| 19 |
+
X X
|
| 20 |
+
X X
|
| 21 |
+
X A X
|
| 22 |
+
X d X
|
| 23 |
+
X X
|
| 24 |
+
X f X
|
| 25 |
+
X a X
|
| 26 |
+
X f X
|
| 27 |
+
X X
|
| 28 |
+
X a X
|
| 29 |
+
X c X
|
| 30 |
+
X X
|
| 31 |
+
X d X
|
| 32 |
+
X X
|
| 33 |
+
X c X
|
| 34 |
+
X X
|
| 35 |
+
X a X
|
| 36 |
+
X g X
|
| 37 |
+
X g X
|
| 38 |
+
Xe b X
|
| 39 |
+
Xa X
|
| 40 |
+
X X
|
| 41 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
data/psltl/baseline_algo/crm/envs/grids/maps/map_2.txt
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
| 2 |
+
X X
|
| 3 |
+
X f f X
|
| 4 |
+
X e X
|
| 5 |
+
X b X
|
| 6 |
+
X X
|
| 7 |
+
X c X
|
| 8 |
+
X X
|
| 9 |
+
X X
|
| 10 |
+
X d X
|
| 11 |
+
X X
|
| 12 |
+
X e a X
|
| 13 |
+
X h X
|
| 14 |
+
X X
|
| 15 |
+
X cX
|
| 16 |
+
X X
|
| 17 |
+
X d X
|
| 18 |
+
X bX
|
| 19 |
+
X d X
|
| 20 |
+
X X
|
| 21 |
+
X A X
|
| 22 |
+
X a X
|
| 23 |
+
X X
|
| 24 |
+
X X
|
| 25 |
+
X f h X
|
| 26 |
+
X d X
|
| 27 |
+
X d X
|
| 28 |
+
X g X
|
| 29 |
+
X X
|
| 30 |
+
X a X
|
| 31 |
+
X a X
|
| 32 |
+
X X
|
| 33 |
+
X X
|
| 34 |
+
X f g X
|
| 35 |
+
X f X
|
| 36 |
+
X X
|
| 37 |
+
X a X
|
| 38 |
+
X X
|
| 39 |
+
X X
|
| 40 |
+
X X
|
| 41 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
data/psltl/baseline_algo/crm/envs/grids/maps/map_3.txt
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
| 2 |
+
X d e X
|
| 3 |
+
X d X
|
| 4 |
+
X h a X
|
| 5 |
+
X X
|
| 6 |
+
X cX
|
| 7 |
+
X X
|
| 8 |
+
X X
|
| 9 |
+
X X
|
| 10 |
+
X X
|
| 11 |
+
X X
|
| 12 |
+
X f X
|
| 13 |
+
X X
|
| 14 |
+
X X
|
| 15 |
+
X X
|
| 16 |
+
X f d X
|
| 17 |
+
X b X
|
| 18 |
+
X h a X
|
| 19 |
+
X X
|
| 20 |
+
X X
|
| 21 |
+
X A X
|
| 22 |
+
X X
|
| 23 |
+
X X
|
| 24 |
+
X X
|
| 25 |
+
X g eX
|
| 26 |
+
X a X
|
| 27 |
+
X d d X
|
| 28 |
+
X X
|
| 29 |
+
X X
|
| 30 |
+
X f X
|
| 31 |
+
X X
|
| 32 |
+
X f bfX
|
| 33 |
+
X g X
|
| 34 |
+
X X
|
| 35 |
+
X a X
|
| 36 |
+
X c X
|
| 37 |
+
X a X
|
| 38 |
+
X X
|
| 39 |
+
X X
|
| 40 |
+
X X
|
| 41 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
data/psltl/baseline_algo/crm/envs/grids/maps/map_4.txt
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
| 2 |
+
X X
|
| 3 |
+
X f c X
|
| 4 |
+
X X
|
| 5 |
+
X a X
|
| 6 |
+
X X
|
| 7 |
+
X b f aX
|
| 8 |
+
X c X
|
| 9 |
+
X X
|
| 10 |
+
X X
|
| 11 |
+
X X
|
| 12 |
+
X d X
|
| 13 |
+
X a a X
|
| 14 |
+
X h X
|
| 15 |
+
X X
|
| 16 |
+
X X
|
| 17 |
+
X b X
|
| 18 |
+
X d X
|
| 19 |
+
X a X
|
| 20 |
+
X f h X
|
| 21 |
+
Xg A g X
|
| 22 |
+
X X
|
| 23 |
+
X d X
|
| 24 |
+
X X
|
| 25 |
+
X f X
|
| 26 |
+
X X
|
| 27 |
+
X X
|
| 28 |
+
X X
|
| 29 |
+
X X
|
| 30 |
+
X X
|
| 31 |
+
X X
|
| 32 |
+
X e d X
|
| 33 |
+
X X
|
| 34 |
+
X f X
|
| 35 |
+
X d X
|
| 36 |
+
X X
|
| 37 |
+
X e X
|
| 38 |
+
X X
|
| 39 |
+
X X
|
| 40 |
+
X X
|
| 41 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
data/psltl/baseline_algo/crm/envs/grids/maps/map_5.txt
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
| 2 |
+
Xd f X
|
| 3 |
+
X c X
|
| 4 |
+
X X
|
| 5 |
+
X X
|
| 6 |
+
X X
|
| 7 |
+
X X
|
| 8 |
+
X X
|
| 9 |
+
X X
|
| 10 |
+
X a X
|
| 11 |
+
X X
|
| 12 |
+
X f b ha X
|
| 13 |
+
X d X
|
| 14 |
+
X d f X
|
| 15 |
+
X a f X
|
| 16 |
+
X X
|
| 17 |
+
X e e X
|
| 18 |
+
X g X
|
| 19 |
+
X X
|
| 20 |
+
X d X
|
| 21 |
+
X f A X
|
| 22 |
+
X X
|
| 23 |
+
X h X
|
| 24 |
+
X b X
|
| 25 |
+
X g c X
|
| 26 |
+
X X
|
| 27 |
+
X X
|
| 28 |
+
X a X
|
| 29 |
+
X X
|
| 30 |
+
X X
|
| 31 |
+
X X
|
| 32 |
+
X X
|
| 33 |
+
X X
|
| 34 |
+
X X
|
| 35 |
+
X X
|
| 36 |
+
X a X
|
| 37 |
+
X X
|
| 38 |
+
X d X
|
| 39 |
+
X X
|
| 40 |
+
X X
|
| 41 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
data/psltl/baseline_algo/crm/envs/grids/maps/map_6.txt
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
| 2 |
+
X X
|
| 3 |
+
X X
|
| 4 |
+
Xe d X
|
| 5 |
+
X X
|
| 6 |
+
X X
|
| 7 |
+
X f X
|
| 8 |
+
X a X
|
| 9 |
+
X X
|
| 10 |
+
X X
|
| 11 |
+
X b X
|
| 12 |
+
X X
|
| 13 |
+
X X
|
| 14 |
+
X f d X
|
| 15 |
+
X X
|
| 16 |
+
X X
|
| 17 |
+
X X
|
| 18 |
+
X f X
|
| 19 |
+
X a X
|
| 20 |
+
X a X
|
| 21 |
+
X A g X
|
| 22 |
+
X e X
|
| 23 |
+
X a X
|
| 24 |
+
X X
|
| 25 |
+
X g d X
|
| 26 |
+
X X
|
| 27 |
+
X X
|
| 28 |
+
X X
|
| 29 |
+
X X
|
| 30 |
+
X X
|
| 31 |
+
X X
|
| 32 |
+
X c X
|
| 33 |
+
X a c X
|
| 34 |
+
X f d X
|
| 35 |
+
X X
|
| 36 |
+
X f X
|
| 37 |
+
X X
|
| 38 |
+
X b d X
|
| 39 |
+
X h X
|
| 40 |
+
X h X
|
| 41 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
data/psltl/baseline_algo/crm/envs/grids/maps/map_7.txt
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
| 2 |
+
X X
|
| 3 |
+
X X
|
| 4 |
+
X a d X
|
| 5 |
+
X a d X
|
| 6 |
+
X f e X
|
| 7 |
+
X a X
|
| 8 |
+
X b h X
|
| 9 |
+
X X
|
| 10 |
+
X f X
|
| 11 |
+
X X
|
| 12 |
+
X X
|
| 13 |
+
X X
|
| 14 |
+
X X
|
| 15 |
+
X X
|
| 16 |
+
X a X
|
| 17 |
+
X X
|
| 18 |
+
X X
|
| 19 |
+
X X
|
| 20 |
+
X X
|
| 21 |
+
X A X
|
| 22 |
+
X b X
|
| 23 |
+
X X
|
| 24 |
+
X X
|
| 25 |
+
X h X
|
| 26 |
+
X X
|
| 27 |
+
X c X
|
| 28 |
+
X a X
|
| 29 |
+
X d d X
|
| 30 |
+
X X
|
| 31 |
+
X X
|
| 32 |
+
X X
|
| 33 |
+
X X
|
| 34 |
+
X e X
|
| 35 |
+
X X
|
| 36 |
+
X d X
|
| 37 |
+
X g X
|
| 38 |
+
X f g f X
|
| 39 |
+
X c f X
|
| 40 |
+
X X
|
| 41 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
data/psltl/baseline_algo/crm/envs/grids/maps/map_8.txt
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
| 2 |
+
X X
|
| 3 |
+
X a a X
|
| 4 |
+
X h X
|
| 5 |
+
X a X
|
| 6 |
+
X g X
|
| 7 |
+
X b X
|
| 8 |
+
X d X
|
| 9 |
+
X a X
|
| 10 |
+
X X
|
| 11 |
+
X hX
|
| 12 |
+
X X
|
| 13 |
+
X X
|
| 14 |
+
X e X
|
| 15 |
+
X e g X
|
| 16 |
+
X b X
|
| 17 |
+
X d c X
|
| 18 |
+
X X
|
| 19 |
+
X X
|
| 20 |
+
X X
|
| 21 |
+
X A X
|
| 22 |
+
X X
|
| 23 |
+
X X
|
| 24 |
+
X X
|
| 25 |
+
X X
|
| 26 |
+
X f c f X
|
| 27 |
+
X d X
|
| 28 |
+
X X
|
| 29 |
+
X X
|
| 30 |
+
X X
|
| 31 |
+
X X
|
| 32 |
+
X f X
|
| 33 |
+
X f d f X
|
| 34 |
+
X X
|
| 35 |
+
X d X
|
| 36 |
+
X X
|
| 37 |
+
X X
|
| 38 |
+
X a X
|
| 39 |
+
X X
|
| 40 |
+
X X
|
| 41 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
data/psltl/baseline_algo/crm/envs/grids/maps/map_9.txt
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
| 2 |
+
X e X
|
| 3 |
+
X X
|
| 4 |
+
X X
|
| 5 |
+
X X
|
| 6 |
+
X a X
|
| 7 |
+
X X
|
| 8 |
+
X d X
|
| 9 |
+
X d hX
|
| 10 |
+
X h c X
|
| 11 |
+
X X
|
| 12 |
+
X f X
|
| 13 |
+
X X
|
| 14 |
+
Xd X
|
| 15 |
+
X f X
|
| 16 |
+
X X
|
| 17 |
+
X a g X
|
| 18 |
+
X X
|
| 19 |
+
X d X
|
| 20 |
+
X X
|
| 21 |
+
X A X
|
| 22 |
+
X X
|
| 23 |
+
X a e X
|
| 24 |
+
X X
|
| 25 |
+
X b X
|
| 26 |
+
X a g X
|
| 27 |
+
X f X
|
| 28 |
+
X f a X
|
| 29 |
+
X X
|
| 30 |
+
X d X
|
| 31 |
+
X X
|
| 32 |
+
X X
|
| 33 |
+
X X
|
| 34 |
+
X b f X
|
| 35 |
+
X X
|
| 36 |
+
X X
|
| 37 |
+
X X
|
| 38 |
+
X X
|
| 39 |
+
X X
|
| 40 |
+
X c X
|
| 41 |
+
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
data/psltl/baseline_algo/crm/envs/grids/office_world.py
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from psltl.baseline_algo.crm.envs.grids.game_objects import Actions
|
| 2 |
+
import random, math, os
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class OfficeWorld:
|
| 7 |
+
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self._load_map()
|
| 10 |
+
self.map_height, self.map_width = 12,9
|
| 11 |
+
|
| 12 |
+
def reset(self):
|
| 13 |
+
self.agent = (2,1)
|
| 14 |
+
|
| 15 |
+
def execute_action(self, a):
|
| 16 |
+
"""
|
| 17 |
+
We execute 'action' in the game
|
| 18 |
+
"""
|
| 19 |
+
x,y = self.agent
|
| 20 |
+
self.agent = self._get_new_position(x,y,a)
|
| 21 |
+
|
| 22 |
+
def _get_new_position(self, x, y, a):
|
| 23 |
+
action = Actions(a)
|
| 24 |
+
# executing action
|
| 25 |
+
if (x,y,action) not in self.forbidden_transitions:
|
| 26 |
+
if action == Actions.up : y+=1
|
| 27 |
+
if action == Actions.down : y-=1
|
| 28 |
+
if action == Actions.left : x-=1
|
| 29 |
+
if action == Actions.right: x+=1
|
| 30 |
+
return x,y
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def get_true_propositions(self):
|
| 34 |
+
"""
|
| 35 |
+
Returns the string with the propositions that are True in this state
|
| 36 |
+
"""
|
| 37 |
+
ret = ""
|
| 38 |
+
if self.agent in self.objects:
|
| 39 |
+
ret += self.objects[self.agent]
|
| 40 |
+
return ret
|
| 41 |
+
|
| 42 |
+
def get_features(self):
|
| 43 |
+
"""
|
| 44 |
+
Returns the features of the current state (i.e., the location of the agent)
|
| 45 |
+
"""
|
| 46 |
+
x,y = self.agent
|
| 47 |
+
return np.array([x,y])
|
| 48 |
+
|
| 49 |
+
def show(self):
|
| 50 |
+
for y in range(8,-1,-1):
|
| 51 |
+
if y % 3 == 2:
|
| 52 |
+
for x in range(12):
|
| 53 |
+
if x % 3 == 0:
|
| 54 |
+
print("_",end="")
|
| 55 |
+
if 0 < x < 11:
|
| 56 |
+
print("_",end="")
|
| 57 |
+
if (x,y,Actions.up) in self.forbidden_transitions:
|
| 58 |
+
print("_",end="")
|
| 59 |
+
else:
|
| 60 |
+
print(" ",end="")
|
| 61 |
+
print()
|
| 62 |
+
for x in range(12):
|
| 63 |
+
if (x,y,Actions.left) in self.forbidden_transitions:
|
| 64 |
+
print("|",end="")
|
| 65 |
+
elif x % 3 == 0:
|
| 66 |
+
print(" ",end="")
|
| 67 |
+
if (x,y) == self.agent:
|
| 68 |
+
print("A",end="")
|
| 69 |
+
elif (x,y) in self.objects:
|
| 70 |
+
print(self.objects[(x,y)],end="")
|
| 71 |
+
else:
|
| 72 |
+
print(" ",end="")
|
| 73 |
+
if (x,y,Actions.right) in self.forbidden_transitions:
|
| 74 |
+
print("|",end="")
|
| 75 |
+
elif x % 3 == 2:
|
| 76 |
+
print(" ",end="")
|
| 77 |
+
print()
|
| 78 |
+
if y % 3 == 0:
|
| 79 |
+
for x in range(12):
|
| 80 |
+
if x % 3 == 0:
|
| 81 |
+
print("_",end="")
|
| 82 |
+
if 0 < x < 11:
|
| 83 |
+
print("_",end="")
|
| 84 |
+
if (x,y,Actions.down) in self.forbidden_transitions:
|
| 85 |
+
print("_",end="")
|
| 86 |
+
else:
|
| 87 |
+
print(" ",end="")
|
| 88 |
+
print()
|
| 89 |
+
|
| 90 |
+
def get_model(self):
|
| 91 |
+
"""
|
| 92 |
+
This method returns a model of the environment.
|
| 93 |
+
We use the model to compute optimal policies using value iteration.
|
| 94 |
+
The optimal policies are used to set the average reward per step of each task to 1.
|
| 95 |
+
"""
|
| 96 |
+
S = [(x,y) for x in range(12) for y in range(9)] # States
|
| 97 |
+
A = self.actions.copy() # Actions
|
| 98 |
+
L = self.objects.copy() # Labeling function
|
| 99 |
+
T = {} # Transitions (s,a) -> s' (they are deterministic)
|
| 100 |
+
for s in S:
|
| 101 |
+
x,y = s
|
| 102 |
+
for a in A:
|
| 103 |
+
T[(s,a)] = self._get_new_position(x,y,a)
|
| 104 |
+
return S,A,L,T # SALT xD
|
| 105 |
+
|
| 106 |
+
def _load_map(self):
|
| 107 |
+
# Creating the map
|
| 108 |
+
self.objects = {}
|
| 109 |
+
self.objects[(1,1)] = "a"
|
| 110 |
+
self.objects[(1,7)] = "b"
|
| 111 |
+
self.objects[(10,7)] = "c"
|
| 112 |
+
self.objects[(10,1)] = "d"
|
| 113 |
+
self.objects[(7,4)] = "e" # MAIL
|
| 114 |
+
# self.objects[(6,4)] = "n" # PLANT
|
| 115 |
+
# self.objects[(3,5)] = "n" # PLANT
|
| 116 |
+
self.objects[(8,2)] = "f" # COFFEE
|
| 117 |
+
self.objects[(3,6)] = "f" # COFFEE
|
| 118 |
+
self.objects[(6,4)] = "n" # PLANT
|
| 119 |
+
|
| 120 |
+
self.objects[(4,4)] = "g" # OFFICE
|
| 121 |
+
self.objects[(4,1)] = "n" # PLANT
|
| 122 |
+
self.objects[(7,1)] = "n" # PLANT
|
| 123 |
+
self.objects[(4,7)] = "n" # PLANT
|
| 124 |
+
self.objects[(7,7)] = "n" # PLANT
|
| 125 |
+
self.objects[(1,4)] = "n" # PLANT
|
| 126 |
+
self.objects[(10,4)] = "n" # PLANT
|
| 127 |
+
# Adding walls
|
| 128 |
+
self.forbidden_transitions = set()
|
| 129 |
+
# general grid
|
| 130 |
+
for x in range(12):
|
| 131 |
+
for y in [0,3,6]:
|
| 132 |
+
self.forbidden_transitions.add((x,y,Actions.down))
|
| 133 |
+
self.forbidden_transitions.add((x,y+2,Actions.up))
|
| 134 |
+
for y in range(9):
|
| 135 |
+
for x in [0,3,6,9]:
|
| 136 |
+
self.forbidden_transitions.add((x,y,Actions.left))
|
| 137 |
+
self.forbidden_transitions.add((x+2,y,Actions.right))
|
| 138 |
+
# adding 'doors'
|
| 139 |
+
for y in [1,7]:
|
| 140 |
+
for x in [2,5,8]:
|
| 141 |
+
self.forbidden_transitions.remove((x,y,Actions.right))
|
| 142 |
+
self.forbidden_transitions.remove((x+1,y,Actions.left))
|
| 143 |
+
for x in [1,4,7,10]:
|
| 144 |
+
self.forbidden_transitions.remove((x,5,Actions.up))
|
| 145 |
+
self.forbidden_transitions.remove((x,6,Actions.down))
|
| 146 |
+
for x in [1,10]:
|
| 147 |
+
self.forbidden_transitions.remove((x,2,Actions.up))
|
| 148 |
+
self.forbidden_transitions.remove((x,3,Actions.down))
|
| 149 |
+
# Adding the agent
|
| 150 |
+
self.actions = [Actions.up.value,Actions.right.value,Actions.down.value,Actions.left.value]
|
data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t1.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 # initial state
|
| 2 |
+
[2] # terminal state
|
| 3 |
+
(0,0,'!a',ConstantRewardFunction(0))
|
| 4 |
+
(0,1,'a',ConstantRewardFunction(0))
|
| 5 |
+
(1,1,'!b',ConstantRewardFunction(0))
|
| 6 |
+
(1,2,'b',ConstantRewardFunction(1))
|
data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t10.txt
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 # initial state
|
| 2 |
+
[2] # terminal state
|
| 3 |
+
(0,0,'!a&!f',ConstantRewardFunction(0))
|
| 4 |
+
(0,1,'a',ConstantRewardFunction(0))
|
| 5 |
+
(0,4,'f',ConstantRewardFunction(0))
|
| 6 |
+
(1,1,'!c&!f',ConstantRewardFunction(0))
|
| 7 |
+
(1,3,'c',ConstantRewardFunction(0))
|
| 8 |
+
(1,5,'f',ConstantRewardFunction(0))
|
| 9 |
+
(3,3,'!f',ConstantRewardFunction(0))
|
| 10 |
+
(3,6,'f',ConstantRewardFunction(0))
|
| 11 |
+
(4,4,'!a',ConstantRewardFunction(0))
|
| 12 |
+
(4,5,'a',ConstantRewardFunction(0))
|
| 13 |
+
(5,5,'!c',ConstantRewardFunction(0))
|
| 14 |
+
(5,6,'c',ConstantRewardFunction(0))
|
| 15 |
+
(6,6,'!b',ConstantRewardFunction(0))
|
| 16 |
+
(6,7,'b',ConstantRewardFunction(0))
|
| 17 |
+
(7,2,'h',ConstantRewardFunction(1))
|
| 18 |
+
(7,7,'!h',ConstantRewardFunction(0))
|
data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t2.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 # initial state
|
| 2 |
+
[2] # terminal state
|
| 3 |
+
(0,0,'!a',ConstantRewardFunction(0))
|
| 4 |
+
(0,1,'a',ConstantRewardFunction(0))
|
| 5 |
+
(1,1,'!c',ConstantRewardFunction(0))
|
| 6 |
+
(1,2,'c',ConstantRewardFunction(1))
|
data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t3.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 # initial state
|
| 2 |
+
[2] # terminal state
|
| 3 |
+
(0,0,'!d',ConstantRewardFunction(0))
|
| 4 |
+
(0,1,'d',ConstantRewardFunction(0))
|
| 5 |
+
(1,1,'!e',ConstantRewardFunction(0))
|
| 6 |
+
(1,2,'e',ConstantRewardFunction(1))
|
data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t4.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 # initial state
|
| 2 |
+
[2] # terminal state
|
| 3 |
+
(0,0,'!d',ConstantRewardFunction(0))
|
| 4 |
+
(0,1,'d',ConstantRewardFunction(0))
|
| 5 |
+
(1,1,'!b',ConstantRewardFunction(0))
|
| 6 |
+
(1,2,'b',ConstantRewardFunction(1))
|
data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t5.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 # initial state
|
| 2 |
+
[1] # terminal state
|
| 3 |
+
(0,0,'!a&!f',ConstantRewardFunction(0))
|
| 4 |
+
(0,2,'f',ConstantRewardFunction(0))
|
| 5 |
+
(0,3,'a',ConstantRewardFunction(0))
|
| 6 |
+
(2,2,'!a',ConstantRewardFunction(0))
|
| 7 |
+
(2,4,'a',ConstantRewardFunction(0))
|
| 8 |
+
(3,3,'!f',ConstantRewardFunction(0))
|
| 9 |
+
(3,4,'f',ConstantRewardFunction(0))
|
| 10 |
+
(4,1,'e',ConstantRewardFunction(1))
|
| 11 |
+
(4,4,'!e',ConstantRewardFunction(0))
|
data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t6.txt
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 # initial state
|
| 2 |
+
[2] # terminal state
|
| 3 |
+
(0,0,'!a&!d',ConstantRewardFunction(0))
|
| 4 |
+
(0,1,'a',ConstantRewardFunction(0))
|
| 5 |
+
(0,4,'d',ConstantRewardFunction(0))
|
| 6 |
+
(1,1,'!b&!d',ConstantRewardFunction(0))
|
| 7 |
+
(1,3,'b',ConstantRewardFunction(0))
|
| 8 |
+
(1,5,'d',ConstantRewardFunction(0))
|
| 9 |
+
(3,3,'!d',ConstantRewardFunction(0))
|
| 10 |
+
(3,6,'d',ConstantRewardFunction(0))
|
| 11 |
+
(4,4,'!a',ConstantRewardFunction(0))
|
| 12 |
+
(4,5,'a',ConstantRewardFunction(0))
|
| 13 |
+
(5,5,'!b',ConstantRewardFunction(0))
|
| 14 |
+
(5,6,'b',ConstantRewardFunction(0))
|
| 15 |
+
(6,2,'c',ConstantRewardFunction(1))
|
| 16 |
+
(6,6,'!c',ConstantRewardFunction(0))
|
data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t7.txt
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 # initial state
|
| 2 |
+
[2] # terminal state
|
| 3 |
+
(0,0,'!a&!f',ConstantRewardFunction(0))
|
| 4 |
+
(0,1,'a',ConstantRewardFunction(0))
|
| 5 |
+
(0,4,'f',ConstantRewardFunction(0))
|
| 6 |
+
(1,1,'!c&!f',ConstantRewardFunction(0))
|
| 7 |
+
(1,3,'c',ConstantRewardFunction(0))
|
| 8 |
+
(1,5,'f',ConstantRewardFunction(0))
|
| 9 |
+
(3,3,'!f',ConstantRewardFunction(0))
|
| 10 |
+
(3,6,'f',ConstantRewardFunction(0))
|
| 11 |
+
(4,4,'!a',ConstantRewardFunction(0))
|
| 12 |
+
(4,5,'a',ConstantRewardFunction(0))
|
| 13 |
+
(5,5,'!c',ConstantRewardFunction(0))
|
| 14 |
+
(5,6,'c',ConstantRewardFunction(0))
|
| 15 |
+
(6,2,'b',ConstantRewardFunction(1))
|
| 16 |
+
(6,6,'!b',ConstantRewardFunction(0))
|
data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t8.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 # initial state
|
| 2 |
+
[1] # terminal state
|
| 3 |
+
(0,0,'!a&!f',ConstantRewardFunction(0))
|
| 4 |
+
(0,2,'a',ConstantRewardFunction(0))
|
| 5 |
+
(0,3,'f',ConstantRewardFunction(0))
|
| 6 |
+
(2,2,'!f',ConstantRewardFunction(0))
|
| 7 |
+
(2,4,'f',ConstantRewardFunction(0))
|
| 8 |
+
(3,3,'!a',ConstantRewardFunction(0))
|
| 9 |
+
(3,4,'a',ConstantRewardFunction(0))
|
| 10 |
+
(4,1,'c',ConstantRewardFunction(1))
|
| 11 |
+
(4,4,'!c',ConstantRewardFunction(0))
|
data/psltl/baseline_algo/crm/envs/grids/reward_machines/craft/t9.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 # initial state
|
| 2 |
+
[1] # terminal state
|
| 3 |
+
(0,0,'!a&!f',ConstantRewardFunction(0))
|
| 4 |
+
(0,2,'f',ConstantRewardFunction(0))
|
| 5 |
+
(0,3,'a',ConstantRewardFunction(0))
|
| 6 |
+
(2,2,'!a',ConstantRewardFunction(0))
|
| 7 |
+
(2,4,'a',ConstantRewardFunction(0))
|
| 8 |
+
(3,3,'!f',ConstantRewardFunction(0))
|
| 9 |
+
(3,4,'f',ConstantRewardFunction(0))
|
| 10 |
+
(4,4,'!e',ConstantRewardFunction(0))
|
| 11 |
+
(4,5,'e',ConstantRewardFunction(0))
|
| 12 |
+
(5,1,'g',ConstantRewardFunction(1))
|
| 13 |
+
(5,5,'!g',ConstantRewardFunction(0))
|
data/psltl/baseline_algo/crm/envs/grids/reward_machines/office/t1.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 # initial state
|
| 2 |
+
[2] # terminal state
|
| 3 |
+
(0,0,'!f&!n',ConstantRewardFunction(0))
|
| 4 |
+
(0,1,'f&!n',ConstantRewardFunction(0))
|
| 5 |
+
(1,1,'!g&!n',ConstantRewardFunction(0))
|
| 6 |
+
(1,2,'g&!n',ConstantRewardFunction(1))
|
data/psltl/baseline_algo/crm/envs/grids/reward_machines/office/t2.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 # initial state
|
| 2 |
+
[2] # terminal state
|
| 3 |
+
(0,0,'!e&!n',ConstantRewardFunction(0))
|
| 4 |
+
(0,1,'e&!n',ConstantRewardFunction(0))
|
| 5 |
+
(1,1,'!g&!n',ConstantRewardFunction(0))
|
| 6 |
+
(1,2,'g&!n',ConstantRewardFunction(1))
|
data/psltl/baseline_algo/crm/envs/grids/reward_machines/office/t3.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 # initial state
|
| 2 |
+
[1] # terminal state
|
| 3 |
+
(0,0,'!e&!f&!n',ConstantRewardFunction(0))
|
| 4 |
+
(0,2,'e&!n',ConstantRewardFunction(0))
|
| 5 |
+
(0,3,'!e&f&!n',ConstantRewardFunction(0))
|
| 6 |
+
(2,2,'!f&!n',ConstantRewardFunction(0))
|
| 7 |
+
(2,4,'f&!n',ConstantRewardFunction(0))
|
| 8 |
+
(3,3,'!e&!n',ConstantRewardFunction(0))
|
| 9 |
+
(3,4,'e&!n',ConstantRewardFunction(0))
|
| 10 |
+
(4,1,'g&!n',ConstantRewardFunction(1))
|
| 11 |
+
(4,4,'!g&!n',ConstantRewardFunction(0))
|
data/psltl/baseline_algo/crm/envs/grids/reward_machines/office/t4.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 # initial state
|
| 2 |
+
[4] # terminal state
|
| 3 |
+
(0,0,'!a&!n',ConstantRewardFunction(0))
|
| 4 |
+
(0,1,'a&!n',ConstantRewardFunction(0))
|
| 5 |
+
(1,1,'!b&!n',ConstantRewardFunction(0))
|
| 6 |
+
(1,2,'b&!n',ConstantRewardFunction(0))
|
| 7 |
+
(2,2,'!c&!n',ConstantRewardFunction(0))
|
| 8 |
+
(2,3,'c&!n',ConstantRewardFunction(0))
|
| 9 |
+
(3,3,'!d&!n',ConstantRewardFunction(0))
|
| 10 |
+
(3,4,'d&!n',ConstantRewardFunction(1))
|
data/psltl/baseline_algo/crm/envs/grids/reward_machines/taxi/t.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 # initial state
|
| 2 |
+
[3] # terminal state
|
| 3 |
+
(0,0,'g&l&!d&!p|g&!d&!l&!p|l&!d&!g&!p|!d&!g&!l&!p',ConstantRewardFunction(0))
|
| 4 |
+
(0,1,'d&g&p&!l|g&p&!d&!l|p&!d&!g&!l',ConstantRewardFunction(0))
|
| 5 |
+
(0,2,'l&p&!d&!g',ConstantRewardFunction(0)) # 2
|
| 6 |
+
(0,3,'d&g&l&p|g&l&p&!d',ConstantRewardFunction(0)) # 3
|
| 7 |
+
(1,1,'g&!d&!l|d&g&p&!l|!d&!g&!l',ConstantRewardFunction(0))
|
| 8 |
+
(1,2,'l&!d&!g',ConstantRewardFunction(0)) # 2
|
| 9 |
+
(1,3,'g&l&!d|d&g&l&p',ConstantRewardFunction(0)) # 3
|
| 10 |
+
(2,2,'!d&!g',ConstantRewardFunction(0)) # 2
|
| 11 |
+
(2,3,'g&!d|d&g&p',ConstantRewardFunction(1)) # 3
|
data/psltl/baseline_algo/crm/envs/grids/taxi_world.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
if __name__ == '__main__':
|
| 2 |
+
# This is a terrible hack just to be able to execute this file directly
|
| 3 |
+
import sys
|
| 4 |
+
sys.path.insert(0, '../')
|
| 5 |
+
|
| 6 |
+
from psltl.baseline_algo.crm.envs.grids.game_objects import Actions
|
| 7 |
+
import random, math, os
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
import gymnasium as gym
|
| 11 |
+
import numpy as np
|
| 12 |
+
|
| 13 |
+
# render_mode will be 'human', 'None'
|
| 14 |
+
|
| 15 |
+
class TaxiWorldParams:
|
| 16 |
+
def __init__(self, max_count:int = 20, seed: int = 0):
|
| 17 |
+
self.max_count = max_count
|
| 18 |
+
self.seed = seed
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class Taxi(gym.Wrapper):
|
| 22 |
+
def __init__(self, max_count: int=20, seed=0):
|
| 23 |
+
env = gym.make('Taxi-v3', render_mode=None)
|
| 24 |
+
super().__init__(env)
|
| 25 |
+
self.seed = seed
|
| 26 |
+
self.env = env
|
| 27 |
+
self.curr_mdp_state, _ = env.reset()
|
| 28 |
+
self.curr_label = ""
|
| 29 |
+
self.max_count = max_count
|
| 30 |
+
self.map_height = 1
|
| 31 |
+
self.map_width = 500
|
| 32 |
+
self.episode_step = 0
|
| 33 |
+
|
| 34 |
+
# actions [0, 1, 2, 3, 4, 5]
|
| 35 |
+
self.pickup_count = 0 # action is 4
|
| 36 |
+
self.dropoff_count = 0 # action is 5
|
| 37 |
+
self.actions = [i for i in range(self.action_space.n)]
|
| 38 |
+
self.reset()
|
| 39 |
+
|
| 40 |
+
def get_events(self):
|
| 41 |
+
|
| 42 |
+
label = self.curr_label
|
| 43 |
+
return label
|
| 44 |
+
|
| 45 |
+
def get_actions(self):
|
| 46 |
+
|
| 47 |
+
return self.actions
|
| 48 |
+
|
| 49 |
+
def get_count(self):
|
| 50 |
+
|
| 51 |
+
total_count = self.pickup_count + self.dropoff_count
|
| 52 |
+
return total_count
|
| 53 |
+
|
| 54 |
+
def get_location(self, idx):
|
| 55 |
+
|
| 56 |
+
return self.env.locs[idx]
|
| 57 |
+
|
| 58 |
+
def get_features(self):
|
| 59 |
+
|
| 60 |
+
return self.curr_mdp_state
|
| 61 |
+
|
| 62 |
+
def get_vector_features(self):
|
| 63 |
+
|
| 64 |
+
return np.identity(self.map_height * self.map_width)[self.curr_mdp_state]
|
| 65 |
+
|
| 66 |
+
def get_true_propositions(self):
|
| 67 |
+
|
| 68 |
+
return self.curr_label
|
| 69 |
+
|
| 70 |
+
def get_state(self):
|
| 71 |
+
return None # we are only using "simple reward machines" for the craft domain
|
| 72 |
+
|
| 73 |
+
def execute_action(self, action):
|
| 74 |
+
self.step(action)
|
| 75 |
+
|
| 76 |
+
def step(self, action):
|
| 77 |
+
self.episode_step += 1
|
| 78 |
+
|
| 79 |
+
next_state, reward, _, done, info = self.env.step(action)
|
| 80 |
+
self.curr_mdp_state = next_state
|
| 81 |
+
taxi_row, taxi_col, pass_loc, dest_idx = self.env.decode(self.curr_mdp_state)
|
| 82 |
+
self.curr_label = ""
|
| 83 |
+
|
| 84 |
+
# reach destination regardless of passengers
|
| 85 |
+
if ((taxi_row, taxi_col) == self.get_location(dest_idx)):
|
| 86 |
+
self.curr_label += "l"
|
| 87 |
+
|
| 88 |
+
if pass_loc == 4:
|
| 89 |
+
self.curr_label += "p"
|
| 90 |
+
self.have_passenger = 1
|
| 91 |
+
|
| 92 |
+
# while the taxi has passenger
|
| 93 |
+
if self.have_passenger:
|
| 94 |
+
self.curr_label += "p"
|
| 95 |
+
if action == 5:
|
| 96 |
+
self.curr_label += "d"
|
| 97 |
+
self.have_passenger = 0
|
| 98 |
+
# drop-off to the destinaion
|
| 99 |
+
if "l" in self.curr_label:
|
| 100 |
+
self.curr_label += "g"
|
| 101 |
+
|
| 102 |
+
else:
|
| 103 |
+
if action == 5:
|
| 104 |
+
self.curr_label += "d"
|
| 105 |
+
# pick up passenger
|
| 106 |
+
elif action == 4 and ((taxi_row, taxi_col) == self.get_location(pass_loc)):
|
| 107 |
+
self.curr_label += "p" # get passenger
|
| 108 |
+
self.have_passenger = 1
|
| 109 |
+
|
| 110 |
+
self.env_game_over = done
|
| 111 |
+
return next_state, reward, done, info
|
| 112 |
+
|
| 113 |
+
def reset(self):
|
| 114 |
+
init_state = self.env.reset()
|
| 115 |
+
self.curr_mdp_state, _ = init_state
|
| 116 |
+
self.curr_label = ""
|
| 117 |
+
self.episode_step = 0
|
| 118 |
+
self.pickup_count = 0
|
| 119 |
+
self.dropoff_count = 0
|
| 120 |
+
self.have_passenger = 0
|
| 121 |
+
self.done = False
|
| 122 |
+
|
| 123 |
+
return self.curr_mdp_state
|
data/psltl/baseline_algo/crm/envs/grids/value_iteration.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
def value_iteration(S,A,L,T,rm,gamma):
|
| 3 |
+
"""
|
| 4 |
+
Standard value iteration to compute optimal policies for the grid environments.
|
| 5 |
+
|
| 6 |
+
PARAMS
|
| 7 |
+
----------
|
| 8 |
+
S: List of states
|
| 9 |
+
A: List of actions
|
| 10 |
+
L: Labeling function (it is a dictionary from states to events)
|
| 11 |
+
T: Transitions (it is a dictionary from SxA -> S)
|
| 12 |
+
rm: Reward machine
|
| 13 |
+
gamma: Discount factor
|
| 14 |
+
|
| 15 |
+
RETURNS
|
| 16 |
+
----------
|
| 17 |
+
Optimal deterministic policy (dictionary maping from states (SxU) to actions)
|
| 18 |
+
"""
|
| 19 |
+
U = rm.get_states() # RM states
|
| 20 |
+
V = dict([((s,u),0) for s in S for u in U])
|
| 21 |
+
V_error = 1
|
| 22 |
+
|
| 23 |
+
# Computing the optimal value function
|
| 24 |
+
while V_error > 0.0000001:
|
| 25 |
+
V_error = 0
|
| 26 |
+
for s1 in S:
|
| 27 |
+
for u1 in U:
|
| 28 |
+
q_values = []
|
| 29 |
+
for a in A:
|
| 30 |
+
s2 = T[(s1,a)]
|
| 31 |
+
l = '' if s2 not in L else L[s2]
|
| 32 |
+
u2, r, done = rm.step(u1, l, None)
|
| 33 |
+
if done: q_values.append(r)
|
| 34 |
+
else: q_values.append(r+gamma*V[(s2,u2)])
|
| 35 |
+
v_new = max(q_values)
|
| 36 |
+
V_error = max([V_error, abs(v_new-V[(s1,u1)])])
|
| 37 |
+
V[(s1,u1)] = v_new
|
| 38 |
+
|
| 39 |
+
# Extracting the optimal policy
|
| 40 |
+
policy = {}
|
| 41 |
+
for s1 in S:
|
| 42 |
+
for u1 in U:
|
| 43 |
+
q_values = []
|
| 44 |
+
for a in A:
|
| 45 |
+
s2 = T[(s1,a)]
|
| 46 |
+
l = '' if s2 not in L else L[s2]
|
| 47 |
+
u2, r, done = rm.step(u1, l, None)
|
| 48 |
+
if done: q_values.append(r)
|
| 49 |
+
else: q_values.append(r+gamma*V[(s2,u2)])
|
| 50 |
+
a_i = max((x,i) for i,x in enumerate(q_values))[1] # argmax over the q-valies
|
| 51 |
+
policy[(s1,u1)] = A[a_i]
|
| 52 |
+
|
| 53 |
+
return policy
|
| 54 |
+
|
data/psltl/baseline_algo/crm/envs/mujoco_rm/half_cheetah_environment.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
This code add event detectors to the Ant3 Environment
|
| 3 |
+
"""
|
| 4 |
+
import gym
|
| 5 |
+
import numpy as np
|
| 6 |
+
from gym.envs.mujoco.half_cheetah_v3 import HalfCheetahEnv
|
| 7 |
+
from psltl.baseline_algo.crm.reward_machines.rm_environment import RewardMachineEnv
|
| 8 |
+
|
| 9 |
+
class MyHalfCheetahEnv(gym.Wrapper):
|
| 10 |
+
def __init__(self):
|
| 11 |
+
# Note that the current position is key for our tasks
|
| 12 |
+
super().__init__(HalfCheetahEnv(exclude_current_positions_from_observation=False))
|
| 13 |
+
|
| 14 |
+
def step(self, action):
|
| 15 |
+
# executing the action in the environment
|
| 16 |
+
next_obs, original_reward, env_done, info = self.env.step(action)
|
| 17 |
+
self.info = info
|
| 18 |
+
return next_obs, original_reward, env_done, info
|
| 19 |
+
|
| 20 |
+
def get_events(self):
|
| 21 |
+
events = ''
|
| 22 |
+
if self.info['x_position'] < -10:
|
| 23 |
+
events+='b'
|
| 24 |
+
if self.info['x_position'] > 10:
|
| 25 |
+
events+='a'
|
| 26 |
+
if self.info['x_position'] < -2:
|
| 27 |
+
events+='d'
|
| 28 |
+
if self.info['x_position'] > 2:
|
| 29 |
+
events+='c'
|
| 30 |
+
if self.info['x_position'] > 4:
|
| 31 |
+
events+='e'
|
| 32 |
+
if self.info['x_position'] > 6:
|
| 33 |
+
events+='f'
|
| 34 |
+
if self.info['x_position'] > 8:
|
| 35 |
+
events+='g'
|
| 36 |
+
|
| 37 |
+
return events
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class MyHalfCheetahEnvRM1(RewardMachineEnv):
|
| 41 |
+
def __init__(self):
|
| 42 |
+
env = MyHalfCheetahEnv()
|
| 43 |
+
rm_files = ["./envs/mujoco_rm/reward_machines/t1.txt"]
|
| 44 |
+
super().__init__(env, rm_files)
|
| 45 |
+
|
| 46 |
+
class MyHalfCheetahEnvRM2(RewardMachineEnv):
|
| 47 |
+
def __init__(self):
|
| 48 |
+
env = MyHalfCheetahEnv()
|
| 49 |
+
rm_files = ["./envs/mujoco_rm/reward_machines/t2.txt"]
|
| 50 |
+
super().__init__(env, rm_files)
|
data/psltl/baseline_algo/crm/envs/mujoco_rm/reward_machines/t1.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 # initial state
|
| 2 |
+
[] # terminal state
|
| 3 |
+
(0,0,'!c',RewardControl())
|
| 4 |
+
(0,1,'c',RewardControl())
|
| 5 |
+
(1,1,'!d',RewardControl())
|
| 6 |
+
(1,0,'d',ConstantRewardFunction(1000))
|
data/psltl/baseline_algo/crm/envs/mujoco_rm/reward_machines/t2.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 # initial state
|
| 2 |
+
[5] # terminal state
|
| 3 |
+
(0,0,'!c',RewardControl())
|
| 4 |
+
(0,1,'c',RewardControl())
|
| 5 |
+
(1,1,'!e',RewardControl())
|
| 6 |
+
(1,2,'e',RewardControl())
|
| 7 |
+
(2,2,'!f',RewardControl())
|
| 8 |
+
(2,3,'f',RewardControl())
|
| 9 |
+
(3,3,'!g',RewardControl())
|
| 10 |
+
(3,4,'g',RewardControl())
|
| 11 |
+
(4,4,'!a',RewardControl())
|
| 12 |
+
(4,5,'a',ConstantRewardFunction(1000))
|
data/psltl/baseline_algo/crm/envs/water/__init__.py
ADDED
|
File without changes
|
data/psltl/baseline_algo/crm/envs/water/maps/world_3.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1b92992768eb0e257d3c657a2441abf4b403418e0a1e34bdfbb67402feb3a3c8
|
| 3 |
+
size 1908
|
data/psltl/baseline_algo/crm/envs/water/reward_machines/org_t10.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
1 # initial state
|
| 2 |
+
[0] # terminal state
|
| 3 |
+
(1,1,'!a&!b&!c&!d&!e&!f',ConstantRewardFunction(0))
|
| 4 |
+
(1,2,'!a&!b&!c&d&!e&!f',ConstantRewardFunction(0))
|
| 5 |
+
(2,2,'!a&!b&!c&!d&!e&!f',ConstantRewardFunction(0))
|
| 6 |
+
(2,3,'!a&!b&!c&!d&e&!f',ConstantRewardFunction(0))
|
| 7 |
+
(3,0,'!a&!b&!c&!d&!e&f',ConstantRewardFunction(1))
|
| 8 |
+
(3,3,'!a&!b&!c&!d&!e&!f',ConstantRewardFunction(0))
|
data/psltl/baseline_algo/crm/envs/water/reward_machines/t1.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
1 # initial state
|
| 2 |
+
[2] # terminal state
|
| 3 |
+
(0,0,'!b',ConstantRewardFunction(0))
|
| 4 |
+
(0,2,'b',ConstantRewardFunction(1))
|
| 5 |
+
(1,1,'!a',ConstantRewardFunction(0))
|
| 6 |
+
(1,0,'a',ConstantRewardFunction(0))
|
data/psltl/baseline_algo/crm/envs/water/reward_machines/t10.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
1 # initial state
|
| 2 |
+
[0] # terminal states
|
| 3 |
+
(1,1,'!a&!b&!c&!d&!e&!f',ConstantRewardFunction(0))
|
| 4 |
+
(1,2,'a&!b&!c&!d&!e&!f',ConstantRewardFunction(0)) # red strict
|
| 5 |
+
(2,2,'!a&!b&!c&!d&!e&!f',ConstantRewardFunction(0))
|
| 6 |
+
(2,3,'!a&b&!c&!d&!e&!f',ConstantRewardFunction(0)) # green strcit
|
| 7 |
+
(3,0,'c',ConstantRewardFunction(1)) # touch blue
|
| 8 |
+
(3,3,'!c',ConstantRewardFunction(0))
|
data/psltl/baseline_algo/crm/envs/water/reward_machines/t2.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
1 # initial state
|
| 2 |
+
[2] # terminal state
|
| 3 |
+
(0,0,'!d',ConstantRewardFunction(0))
|
| 4 |
+
(0,2,'d',ConstantRewardFunction(1))
|
| 5 |
+
(1,1,'!c',ConstantRewardFunction(0))
|
| 6 |
+
(1,0,'c',ConstantRewardFunction(0))
|
data/psltl/baseline_algo/crm/envs/water/reward_machines/t3.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
1 # initial state
|
| 2 |
+
[2] # terminal state
|
| 3 |
+
(0,0,'!f',ConstantRewardFunction(0))
|
| 4 |
+
(0,2,'f',ConstantRewardFunction(1))
|
| 5 |
+
(1,1,'!e',ConstantRewardFunction(0))
|
| 6 |
+
(1,0,'e',ConstantRewardFunction(0))
|
data/psltl/baseline_algo/crm/envs/water/reward_machines/t4.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
1 # initial state
|
| 2 |
+
[4] # terminal state
|
| 3 |
+
(0,0,'!b&!c',ConstantRewardFunction(0))
|
| 4 |
+
(0,6,'b',ConstantRewardFunction(0))
|
| 5 |
+
(0,2,'!b&c',ConstantRewardFunction(0))
|
| 6 |
+
(1,1,'!a&!c',ConstantRewardFunction(0))
|
| 7 |
+
(1,0,'a',ConstantRewardFunction(0))
|
| 8 |
+
(1,3,'!a&c',ConstantRewardFunction(0))
|
| 9 |
+
(2,2,'!d&!b',ConstantRewardFunction(0))
|
| 10 |
+
(2,7,'d',ConstantRewardFunction(0))
|
| 11 |
+
(2,5,'!d&b',ConstantRewardFunction(0))
|
| 12 |
+
(3,3,'!d&!a',ConstantRewardFunction(0))
|
| 13 |
+
(3,8,'d',ConstantRewardFunction(0))
|
| 14 |
+
(3,2,'!d&a',ConstantRewardFunction(0))
|
| 15 |
+
(5,5,'!d',ConstantRewardFunction(0))
|
| 16 |
+
(5,4,'d',ConstantRewardFunction(1))
|
| 17 |
+
(6,6,'!c',ConstantRewardFunction(0))
|
| 18 |
+
(6,5,'c',ConstantRewardFunction(0))
|
| 19 |
+
(7,7,'!b',ConstantRewardFunction(0))
|
| 20 |
+
(7,4,'b',ConstantRewardFunction(1))
|
| 21 |
+
(8,8,'!a',ConstantRewardFunction(0))
|
| 22 |
+
(8,7,'a',ConstantRewardFunction(0))
|