Running an agent

Using Python to train works well, but it is not something a real game would ever use. Ideally, what we want to be able to do is build a TensorFlow graph and use it in Unity. Fortunately, a library was constructed, called TensorFlowSharp, that allows .NET to consume TensorFlow graphs. This allows us to build offline TFModels and later inject them into our game. Unfortunately, we can only use trained models and not train in this manner, at least not yet.

Let's see how this works by using the graph we just trained for the GridWorld environment and use it as an internal brain in Unity. Follow the exercise in the next section to set up and use an internal brain:

  1. Download the TFSharp plugin from this link: https://s3.amazonaws.com/unity-ml-agents/0.5/TFSharpPlugin.unitypackage.
If this link does not work, consult the Unity docs or the Asset Store for a new one. The current version is described as experimental and subject to change.
  1. From the editor menu, select Assets | Import Package | Custom Package... 
  2. Locate the asset package you just downloaded and use the import dialogs to load the plugin into the project. If you need help with these basic Unity tasks, there is plenty of help online that can guide you further.
  1. From the menu, select Edit | Project Settings. This will open the Settings window (new in 2018.3)
  2. Locate under the Player options the Scripting Define Symbols and set the text to ENABLE_TENSORFLOW and enable Allow Unsafe Code, as shown in this screenshot:
Setting the ENABLE_TENSORFLOW flag
  1. Locate the GridWorldAcademy object in the Hierarchy window and make sure it is using the Brains GridWorldLearning. Turn the Control option off under the Brains section of the Grid Academy script.
  2. Locate the GridWorldLearning brain in the Assets/Examples/GridWorld/Brains folder and make sure the Model parameter is set in the Inspector window, as shown in this screenshot:
Setting the model for the brain to use
  1. The Model should already be set to the GridWorldLearning model. In this example, we are using the TFModel that is shipped with the GridWorld example. You could also easily use the model we had trained from the earlier example by just importing it into the project and then setting it as the model.
  2. Press Play to run the editor and watch the agent control the cube.

Right now, we are running the environment with the pre-trained Unity brain. In the next section, we will look at how to use the brain we trained in the previous section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset