Method Heuristic
Heuristic(in ActionBuffers)
Implement Heuristic(in ActionBuffers) to choose an action for this agent using a custom heuristic.
Declaration
public virtual void Heuristic(in ActionBuffers actionsOut)
Parameters
| Type | Name | Description |
|---|---|---|
| ActionBuffers | actionsOut | The ActionBuffers which contain the continuous and discrete action buffers to write to. |
Implements
Remarks
Implement this function to provide custom decision making logic or to support manual control of an agent using keyboard, mouse, game controller input, or a script.
Your heuristic implementation can use any decision making logic you specify. Assign decision
values to the ContinuousActions and DiscreteActions
arrays , passed to your function as a parameter.
The same array will be reused between steps. It is up to the user to initialize
the values on each call, for example by calling Array.Clear(actionsOut, 0, actionsOut.Length);.
Add values to the array at the same indexes as they are used in your
OnActionReceived(ActionBuffers) function, which receives this array and
implements the corresponding agent behavior. See Actions for more information
about agent actions.
Note : Do not create a new float array of action in the Heuristic() method,
as this will prevent writing floats to the original action array.
An agent calls this Heuristic() function to make a decision when you set its behavior
type to HeuristicOnly. The agent also calls this function if
you set its behavior type to Default when the
Academy is not connected to an external training process and you do not
assign a trained model to the agent.
To perform imitation learning, implement manual control of the agent in the Heuristic()
function so that you can record the demonstrations required for the imitation learning
algorithms. (Attach a Demonstration Recorder component to the agent's GameObject to
record the demonstration session to a file.)
Even when you don’t plan to use heuristic decisions for an agent or imitation learning, implementing a simple heuristic function can aid in debugging agent actions and interactions with its environment.
Examples
The following example illustrates a Heuristic() function that provides WASD-style
keyboard control for an agent that can move in two dimensions as well as jump. See
Input Manager for more information about the built-in Unity input functions.
You can also use the Input System package, which provides a more flexible and
configurable input system.
public override void Heuristic(in ActionBuffers actionsOut)
{
var continuousActionsOut = actionsOut.ContinuousActions;
continuousActionsOut[0] = Input.GetAxis("Horizontal");
continuousActionsOut[1] = Input.GetKey(KeyCode.Space) ? 1.0f : 0.0f;
continuousActionsOut[2] = Input.GetAxis("Vertical");
}