This code is the bare necessity needed to connect to a Unity ML agent.
It is important to note, this code is from The Unity ML-Agents repository. The difference is the added train_your_agent.py file which is a script I stitched together from their tutorials. The script connects to a Unity environment, retrieves input and reward data, and sends output data back to the Unity environment to be performed. As is, when the script is run, a small dislay for the unity environment will open and a few iterations will be ran, taking random actions. Then the display window will close. These random actions represent the decisions of a python AI and should be replaced by your own python AI's decision making function. More will be discussed of the script itself later.
This tutorial uses an older version of Unity ML-Agents. These steps may not work with the most recent version of ML-Agents. For that, it is recommended that you use the version in the repository. It is also recommended that you create a virtual environment for this project. This will give you a clean slate for the installing the packages. If you do not use a virtual environment and you have already installed a version of unityagents, it may not work with tutorial.
The meat of this repository is the train_your_agent.py file located in the ml-agents-master/python/ folder. If you're just interested in how python interacts with Unity in the simplest of examples, check out this file. Else, continue with the setup process to get everything up and running. Keep in mind, this python script will not work alone.
Install
- Clone this repository.
- If you're using a virtual environment, activate it now.
- In the terminal, navigate to the python/ folder located in the ml-agents-master/ folder.
- Once inside the python/ folder, run the command 'pip3 install .' (don't forget the period) to install everything you need for this tutorial.
Unity
-
Now that the installs are complete, open the Unity editor and create a new project.
-
Once open, go to Edit -> Project Settings -> Player. Then, in the inspector, under 'Other Settings', make sure that Scripting Runtime Version is set to '.NET 4.x Equivalent'.
-
This should prompt you to restart the Unity editor. Do so and continue.
-
In your file explorer (or terminal), locate the ML-Agents/ folder in the ml-agents-master/unity-environment/Assets/ folder. Once found, copy the ML-Agents/ folder to the Assets/ folder of your new Untiy project.
-
In the Unity editor (REMEMBER, this is now in the Unity editor, not your terminal or file explorer), navigate to the 3DBall/ folder located in the Assets/ML-Agents/Examples/ folder. Once in the 3DBall/ folder, double click the 3DBall.unity object to the open the 3DBall environment in the hierarchy.
-
In the project Hierarchy, select the Ball3DBrain object within the Ball3DAcademy object.
-
Now, in the inspector, make sure Brain Type is set to 'External'. This is important for your python script to interact with the Unity brain.
-
Finally, go to File -> Build Settings. Select the intended Platform (most likely PC, Mac & Linux Standalone) and select a Taget Platform and Architecture in the dropdown menus. Then hit Build, give it a name, and set the location to the same python/ folder from above, located in the ml-agents-master/ folder. The location is important so make sure this step is done correctly.
Run
- Once the build is successful, navigate back to the python/ folder, again, located in the ml-agents-master/ folder.
- There you will find a python file named train_your_agent.py. This is the script that contains the bare-necessities needed to connect your python AI with Unity. Open the train_your_agent.py file in any text editor.
- The first line should say something like 'env_name = "unity_executable"'. Replace the 'unity_executable' within the double quotes with the name of the Unity executable you just built. Note, you do NOT need the include the .extention with the name.
- Also note that, if you were following along correctly, the train_your_agent.py and the Unity executable you just built, are in the same folder. This is important or the script will not be able to find the Unity executable. (unless you specify the correct path youreslf)
- Last but not least, while still in the python/ folder, again, located in the ml-agents-master/ folder, run 'python3 train_your_agent.py' in the terminal.
A small window should open and run the Unity environment for 20 episodes and close. The terminal will show a few details as well.
More details explaining the python code within the script itself are soon to come...