Hello,
I’m using Unity 2019.4.3f1 and ml-agents release-3 and I know I’m doing something wrong but I can’t find it out…
My goal is to train a Gameobject to go from its starting point to a determined area, without colliding with a wall.
The walls are just cubes tagged as “canal”.
I`ve also created RayPerseptionSensor 3D.
My script is:
========================================
public class navio_agent : Agent
{
private Rigidbody rBody;
public GameObject begin;
public GameObject end;
private Vector3 startingPosition;
public bool useVectorObs;
public float speed = 15f;
public float ROT = 10f;
public float beginCourse = 0f;
private float timeAgent;
public event Action OnReset;
private float velocinicial = 15f;
public override void Initialize()
{
rBody = GetComponent<Rigidbody>();
startingPosition = new Vector3(UnityEngine.Random.Range(begin.transform.position.x - 50, begin.transform.position.x + 50), 0f, UnityEngine.Random.Range(begin.transform.position.z - 50, begin.transform.position.z + 50));
rBody.MoveRotation(Quaternion.Euler(0f, UnityEngine.Random.Range(beginCourse-20f, beginCourse+20f), 0f));
}
public override void CollectObservations(VectorSensor sensor) //12
{
sensor.AddObservation(transform.position); //3
sensor.AddObservation(end.transform.position);//3
sensor.AddObservation(transform.rotation); //3
sensor.AddObservation(rBody.velocity);//3
}
public override void Heuristic(float[] actionsOut)
{
actionsOut[0] = 0;
if (Input.GetKey(KeyCode.D))
{
actionsOut[0] = 1;
}
else if (Input.GetKey(KeyCode.A))
{
actionsOut[0] = 2;
}
else if (Input.GetKey(KeyCode.X))
{
actionsOut[0] = 3;
}
else if (Input.GetKey(KeyCode.W))
{
actionsOut[0] = 4;
}
else if (Input.GetKey(KeyCode.S))
{
actionsOut[0] = 5;
}
}
public override void OnActionReceived(float[] vectorAction)
{
AddReward(-0.05f);
Move(vectorAction);
}
public void FixedUpdate()
{
RequestDecision();
}
public override void OnEpisodeBegin()
{
Reset();
}
public void Move(float[] act)
{
float dirToGo = 0f;
var rotateDir = Vector3.zero;
var action = Mathf.FloorToInt(act[0]);
switch (action)
{
case 1:
rotateDir = transform.up * 1f;
rBody.AddRelativeTorque(rotateDir * ROT, ForceMode.Acceleration);
break;
case 2:
rotateDir = transform.up * -1f;
rBody.AddRelativeTorque(rotateDir * ROT, ForceMode.Acceleration);
break;
case 3:
break;
case 4:
dirToGo = 1f * speed;
rBody.AddRelativeForce(new Vector3(0, 0, dirToGo), ForceMode.Acceleration);
break;
case 5:
dirToGo = -1f * speed;
rBody.AddRelativeForce(new Vector3(0,0,dirToGo), ForceMode.Acceleration);
break;
}
}
private void Reset()
{
transform.position = startingPosition;
}
public void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("area") && collision.gameObject.name == end.name)
{
AddReward(+1f);
EndEpisode();
} else if (collision.gameObject.CompareTag("canal"))
{
AddReward(-0.5f);
}
}
}
========================================
I’ve also set the Behavior Parameters like this.
But I don’t see the GameObject trying to avoid the wall. What could be happening? Thank you all!