1

I am doing a reinforcement learning test using an ML agent, and I even called the reward function from an external script, but the actual reward and the episode does not end.(only Hello, Agent is printed) Perhaps there is a conflict with override void, but I would like to ask if there is a solution. Thank you in advance.

------------below is Agent script--------------

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 using Unity.MLAgents;
 using Unity.MLAgents.Actuators;
 using Unity.MLAgents.Sensors;




  public class MirrorAgent : Agent
   {

     Rigidbody rBody;
     float degree;
  
    public void reward()
      {
        Debug.Log("Hello, Agent");
        SetReward(1.0f);    
        EndEpisode(); 

      }



void Start()
    
   {       
     rBody = GetComponent<Rigidbody>();
    //  Invoke("RandomThing", 100);
     degree = Random.Range(35.0f, 150.0f);
   }

    public Transform Target;

    public override void OnEpisodeBegin()
   {     
   
    if(this.transform.localPosition.y < -0.5 || this.transform.localPosition.y > 0.3 || this.transform.localPosition.x > 0.4 || this.transform.localPosition.x < -0.4 ) 
   {
    this.transform.localRotation = Quaternion.Euler(new Vector3(0.0f, 0.0f, 0.0f));
     
   }
          
   }


public override void CollectObservations(VectorSensor sensor)  
 {
   sensor.AddObservation(Target.localPosition);       
   sensor.AddObservation(this.transform.localPosition);    
   sensor.AddObservation(this.transform.rotation.z);    

   sensor.AddObservation(rBody.velocity.x);       
   sensor.AddObservation(rBody.velocity.y);      
   sensor.AddObservation(rBody.transform.rotation.z); 

   }


   public float forceMultiplier = 5;              
   public override void OnActionReceived(ActionBuffers actionBuffers)     
  {   
   Vector3 controlSignal = Vector3.zero;
   controlSignal.x = actionBuffers.ContinuousActions[0];   
   controlSignal.y = actionBuffers.ContinuousActions[1];     
   rBody.AddForce(controlSignal * forceMultiplier);         
   if (this.transform.localPosition.y < 1 || this.transform.localPosition.y > 4 || this.transform.localPosition.x > 1 || this.transform.localPosition.x < 0 )     
   {
      EndEpisode(); 
   }
 }

 public override void Heuristic(in ActionBuffers actionsOut)
{
   var continuousActionsOut = actionsOut.ContinuousActions;
   continuousActionsOut[0] = Input.GetAxis("Horizontal");
   continuousActionsOut[1] = Input.GetAxis("Vertical");
}

}

John
  • 13
  • 3

0 Answers0