Hi there
I am new in Unity and trying some ml-agents-based path-finding game.
I trained my agent for some time and got good results (watched the cumulative reward reaches his top value on TensorBoard). But when I implemented my trained weights as the agent’s model, nothing happens.
I set the behavior to ‘Inference only’ and to ‘Default’ and nothing happens, and when I press the errows the agent moves (meaning it uses Heuristic method).
Please help me understand where I go wrong
I also attach the agent’s code
Thank you
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
public class PlayerAgentBasic : Agent
{
[SerializeField]
private float speed = 5.0f;
[SerializeField]
private float distanceRequired = 1.5f;
[SerializeField]
private GameObject target;
[SerializeField]
private Material successMateial;
[SerializeField]
private Material failMateial;
[SerializeField]
private Material defaultMateial;
[SerializeField]
private MeshRenderer groundMeshRenderer;
#region Private Instance Variables
private Rigidbody playerRigidBody;
private Vector3 originalPosition;
private Vector3 originalTargetPosition;
#endregion
public override void Initialize()
{
playerRigidBody = GetComponent<Rigidbody>();
originalPosition = transform.localPosition;
originalTargetPosition = target.transform.localPosition;
}
public override void OnEpisodeBegin()
{
transform.LookAt(transform.transform);
target.transform.localPosition = new Vector3(originalTargetPosition.x, originalTargetPosition.y, Random.Range(-4, 4));
transform.localPosition = originalPosition;
transform.localPosition = new Vector3(originalPosition.x, originalPosition.y, Random.Range(-4, 4));
}
public override void CollectObservations(VectorSensor sensor)
{
sensor.AddObservation(transform.localPosition); // x y z (3 dim)
sensor.AddObservation(target.transform.localPosition); // x y z (3 dim)
sensor.AddObservation(playerRigidBody.velocity.x); // one dimention
sensor.AddObservation(playerRigidBody.velocity.z); // one dimention
}
public override void OnActionReceived(float[] vectorAction)
{
var vectorForce = new Vector3();
vectorForce.x = vectorAction[0];
vectorForce.z = vectorAction[1];
playerRigidBody.AddForce(vectorForce * speed);
var distanceFromTarget = Vector3.Distance(transform.localPosition, target.transform.localPosition);
if(distanceFromTarget < distanceRequired) // we are doing good
{
SetReward(1.0f);
EndEpisode();
StartCoroutine(swapGroundMaterial(successMateial, 0.5f));
}
if (transform.localPosition.y < 0) // falling of the floor
{
AddReward(-.5f);
EndEpisode();
StartCoroutine(swapGroundMaterial(failMateial, 0.5f));
}
// go back and punish the agent for falling
}
public override void Heuristic(float[] actionsOut) // telling the agent how to move
{
actionsOut[0] = Input.GetAxis("Horizontal"); // x
actionsOut[1] = Input.GetAxis("Vertical"); // z
}
private IEnumerator swapGroundMaterial(Material mat, float time)
{
groundMeshRenderer.material = mat;
yield return new WaitForSeconds(time);
groundMeshRenderer.material = defaultMateial;
}
}


