Many Thanks m4l4 for your input.
So what I did is, I already put the decision requester attached to my car but i think what i was missing is decision period which i set to 1 which take action after every one second and check mark “take actions between decisions”
In behaviour parameters option i changed the space size to 3 (which means i have 3 actions accelerate, brake and do nothing)
For future reference if someone is having same problem
the updated CarAgent Script is as follows:
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Policies;
using UnityStandardAssets.CrossPlatformInput;
namespace UnityStandardAssets.Vehicles.Car
{
[RequireComponent(typeof(CarController))]
public class CarAgent : Agent
{
private Vector3 originalPosition;
private Vector3 Targetorginalposition;
private BehaviorParameters behaviorParameters;
private CarController carController;
private Rigidbody rbody;
public Transform Target;
public override void Initialize()
{
originalPosition = this.transform.localPosition;
Targetorginalposition = Target.localPosition;
// here write the orginal position of target
behaviorParameters = GetComponent();
carController = GetComponent();
rbody = carController.GetComponent();
Reset();
}
public override void OnEpisodeBegin()
{
Reset();
}
private void Reset()
{
this.transform.localPosition = originalPosition;
Target.localPosition = Targetorginalposition;
// here write the code for the position of the target
}
public override void CollectObservations(VectorSensor sensor)
{
sensor.AddObservation(Target.localPosition);
sensor.AddObservation(this.transform.localPosition);
}
public override void OnActionReceived(float[ ] vectorAction)
{
var direction = Mathf.FloorToInt(vectorAction[0]);
switch (direction)
{
case 0: // do nothing so basically means idle
break;
case 1: // move forward
carController.Move(0f, 1f, 0f, 0f);
break;
case 2: // Move backwards
carController.Move(0f, 0f, -1f, 0f);
break;
}
float distanceToTarget = Vector3.Distance(this.transform.localPosition, Target.localPosition);
if (distanceToTarget == 8.0f)
{
SetReward(1.0f);
}
else if (distanceToTarget >= 7.5f || distanceToTarget <= 7.9f) // yahn per koi error a sakta hai.
{
SetReward(0.5f);
}
else if (distanceToTarget < 7.5f)
{
EndEpisode(); // could be Reset
}
else if (Target.localPosition.z == 1990.486f)
{
EndEpisode();
}
// AddReward(-1f/ MaxStep);
}
public override void Heuristic(float[ ] actionsOut)
{
actionsOut[0] = 0;
if (Input.GetKey(KeyCode.UpArrow))
{
actionsOut[0] = 1;
}
else if (Input.GetKey(KeyCode.DownArrow))
{
actionsOut[0] = 2;
// carController.Move(0f, 0f, -1f, 0);
}