Scripting the TestingAgent

Of course, our testing (or however far we want to take this simulation) won't do much without an agent to interact with the environment and learn. In the next exercise, we will define the script that describes the TestingAgent component:

  1. Click inside the HoDLG | Scripts folder, and click on the Create button to open the menu. 
  2. From the menu, select C# Script and name the script TestingAgent.
  3. Open the script in your editor and start to script it with the following code:
using MLAgents;
using UnityEngine;

namespace Packt.HoDLG
{
public class TestingAgent : Agent
{
public string[] axisAction;
protected Vector3 resetPos;
protected Quaternion resetRot;
}
}
  1. This starts our class; this time, it's extended from Agent, another base class. Then, we define some base fields for setting variables and recording the agent's start position and rotation.
  1. Next, we move on to define the InitializeAgent method. This method is called once, to set up the agent and make sure that the action lengths are the same; we will get to that shortly. We remember the position/rotation from which the agent started, so that we can restore it later. The code is as follows:
public override void InitializeAgent()
{
base.InitializeAgent();
if (axisAction.Length != brain.brainParameters.vectorActionSize[0])
throw new MLAgents.UnityAgentsException("Axis actions must match agent actions");

resetPos = transform.position;
resetRot = transform.rotation;
}
  1. Next, we define an empty method called CollectObservations. This is typically where the agent observes the environment; since we plan to use visual observations, we can leave this empty. The code is as follows:
public override void CollectObservations(){  }
  1. Next, we define another required method: AgentAction. This is the method where we add the negative step reward and move the agent, as shown in the following code snippet:
public override void AgentAction(float[] vectorAction, string textAction)
{
AddReward(-1f / agentParameters.maxStep);
MoveAgent(vectorAction);
}

public void MoveAgent(float[] act)
{
for(int i=0;i<act.Length;i++)
{
var val = Mathf.Clamp(act[i], -1f, 1f);
TestingInput.Instance.setAxis(val,axisAction[i]);
}
}
  1. The code here is what deciphers the actions from the brain and injects them back into a new class (which we will build shortly), called TestingInputTestingInput is a helper class that we will use to override the input system of the game.
  2. Save the script, and, again, ignore any compiler errors. Again, we have a new dependency, TestingInput, that we will define shortly.

With the new script in hand, we can begin to set up the TestingAgent component in the next section.

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

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