# ML-Agents
## Installation
https://github.com/Unity-Technologies/ml-agents/blob/develop/docs/Installation.md
```
conda create -n mlagents python=3.8
conda activate mlagents
pip install mlagents==0.30.0
pip install protobuf==3.20.0
```
test if installation success:
```
mlagents-learn --help
```
## Run Train
https://github.com/Unity-Technologies/ml-agents/blob/develop/docs/Getting-Started.md
Install Unity version 2021.3.11f1 first.
```
git clone --branch release_20 https://github.com/Unity-Technologies/ml-agents.git
cd ml-agents
(open scene at ml-agents/Project/Assets/ML-Agents/Examples/3DBall/Scenes in Unity)
mlagents-learn config/ppo/3DBall.yaml --run-id=first3DBallRun
mlagents-learn config/ppo/FoodCollector.yaml --run-id=firstFoodCollector
```
## Install Environment Executable
https://github.com/Unity-Technologies/ml-agents/blob/develop/docs/Learning-Environment-Executable.md

Click build. And save to folder named "ball_exe".

Then you can run train:
```
mlagents-learn config/ppo/3DBall.yaml --env=ball_exe --run-id=firstRunExe
```
## Python Gym Interface
https://github.com/Unity-Technologies/ml-agents/blob/develop/docs/Python-Gym-API.md
Unity ML-Agents Gym Wrapper only support an environment with a **single agent**. Use 3DBall as example, there are 12 agents by default. You have to delete other 11 agents, and build it to executable following above procedure. I build it and save to folder named “single_ball_exe”.
* For Windows:
* Have to provide folder name in UnityEnvironment.
```
import gym
from mlagents_envs.environment import UnityEnvironment
from mlagents_envs.envs.unity_gym_env import UnityToGymWrapper
def main():
unity_env = UnityEnvironment("single_ball_exe")
env = UnityToGymWrapper(unity_env)
obs = env.reset()
print(obs)
print(env.action_space)
if __name__ == '__main__':
main()
```

* For Ubuntu:
* Have to provide file name without .x86_64 in UnityEnvironment.
* Dedicated Server Build:
* 
```
import gym
from mlagents_envs.environment import UnityEnvironment
from mlagents_envs.envs.unity_gym_env import UnityToGymWrapper
def main():
unity_env = UnityEnvironment("3dball_single_server/3dball")
env = UnityToGymWrapper(unity_env)
obs = env.reset()
print(obs)
print(env.action_space)
if __name__ == '__main__':
main()
```

## Create Container
```
podman run -d -v $PWD:/root -w /root --name mlagents -it ct3_release tmux
podman exec -it mlagents tmux a
(Install Anaconda:)
wget https://repo.anaconda.com/archive/Anaconda3-2022.05-Linux-x86_64.sh
chmod 777 Anaconda3-2022.05-Linux-x86_64.sh
./Anaconda3-2022.05-Linux-x86_64.sh
(Install mlagents:)
conda create -n mlagents python=3.8
conda activate mlagents
pip install mlagents==0.30.0
pip install protobuf==3.20.0
(Run test:)
python test_env.py
```
## Ubuntu Troubleshoot
Problem:
* no usable version of libssl was found.
Solve:
https://forum.unity.com/threads/workaround-for-libssl-issue-on-ubuntu-22-04.1271405/
```
sudo dpkg -i libssl1.0.0_1.0.2n-1ubuntu5_amd64.deb
```