Hello. I tried to make a new environment for ML-Agents, but after writing a code, the errors happened :
‘AgentControl.Heuristic(float[ ])’: no suitable method found to override
‘AgentControl.OnActionReceived(float[ ])’: no suitable method found to override
I’m using Unity 2019.4, ML-Agents 2.0.0 exp-1.
My code is below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;
public class AgentControl : Agent
{
public Transform target1;
public Transform target2;
public Transform target3;
public Transform target4;
Rigidbody rBody;
private bool onEpisode = false;
private string closestTarget;
public override void Initialize()
{
this.rBody = GetComponent<Rigidbody>();
onEpisode = false;
}
public override void OnEpisodeBegin()
{
this.rBody.angularVelocity = Vector3.zero;
this.rBody.velocity = Vector3.zero;
this.transform.rotation = Quaternion.identity;
this.transform.localPosition = new Vector3(Random.Range(-14.0f, 14.0f), 0.5f, Random.Range(-9.0f, 9.0f));
onEpisode = true;
float dis1 = Vector3.Distance(this.transform.localPosition, target1.localPosition);
float dis2 = Vector3.Distance(this.transform.localPosition, target2.localPosition);
float dis3 = Vector3.Distance(this.transform.localPosition, target3.localPosition);
float dis4 = Vector3.Distance(this.transform.localPosition, target4.localPosition);
float leastDis = Mathf.Min(dis1, dis2, dis3, dis4);
if (leastDis == dis1) closestTarget = "Target1";
if (leastDis == dis2) closestTarget = "Target2";
if (leastDis == dis3) closestTarget = "Target3";
if (leastDis == dis4) closestTarget = "Target4";
}
public override void OnActionReceived(float[] vectorAction)
{
Vector3 dirToGo = Vector3.zero;
Vector3 rotateDir = Vector3.zero;
int action = (int)vectorAction[0];
if (action == 1) dirToGo = transform.forward;
if (action == 2) dirToGo = transform.forward * -1.0f;
if (action == 3) rotateDir = transform.up;
if (action == 4) rotateDir = transform.up * -1.0f;
this.transform.Rotate(rotateDir, Time.deltaTime * 200f);
this.rBody.AddForce(dirToGo * 0.5f, ForceMode.VelocityChange);
if (this.transform.localPosition.y < 0)
{
AddReward(-0.3f);
EndEpisode();
}
AddReward(-1.0f / MaxStep);
}
public void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "target")
{
if (onEpisode)
{
if (collision.gameObject.name == closestTarget)
{
AddReward(1.0f);
EndEpisode();
}
else
{
AddReward(0.3f);
EndEpisode();
}
}
else
{
this.transform.localPosition = new Vector3(Random.Range(-14.0f, 14.0f), 0.5f, Random.Range(-9.0f, 9.0f));
}
}
}
public override void Heuristic(float[] actionsOut)
{
actionsOut[0] = 0;
if (Input.GetKey(KeyCode.UpArrow)) actionsOut[0] = 1;
if (Input.GetKey(KeyCode.DownArrow)) actionsOut[0] = 2;
if (Input.GetKey(KeyCode.RightArrow)) actionsOut[0] = 3;
if (Input.GetKey(KeyCode.LeftArrow)) actionsOut[0] = 4;
}
}
Assets\Hummingbird\Scripts\HummingbirdAgent.cs(116,43): error CS0246: The type or namespace name ‘ActionBuffers’ could not be found (are you missing a using directive or an assembly reference?)
Assets\Hummingbird\Scripts\HummingbirdAgent.cs(192,39): error CS0246: The type or namespace name ‘ActionBuffers’ could not be found (are you missing a using directive or an assembly reference?)
Those specific errors should be fixed by adding this using directive at the top of the file:
using Unity.MLAgents.Actuators;
Are you migrating the Hummingbird project to ML-Agents 2.0? There’s probably quite a bit of work required to do that, you might be better off using ML-Agents 1.0 if you want to use that project.
it says “Cannot apply indexing with [ ] to an expression of type ‘ActionBuffers’” I only used [ ] in actionsOut[0] and actionsOut[1]. send help please.
Errors #1:
Assets\Scripts\BounceAgent.cs(30,25): error CS0115: 'BounceAgent.OnActionReceived(float[ ])': no suitable method found to override & Assets\Scripts\BounceAgent.cs(49,25): error CS0115: ‘BounceAgent.Heuristic(float[ ])’: no suitable method found to override ========================================================================================= I understand that this code is deprecated - but when I change it to OnActionReceived(ActionBuffers actions) or Heuristic(in ActionBuffers actionsOut), I get the following error: Error CS0021 Cannot apply indexing with [ ] to an expression of type ‘ActionBuffers’ This is because I have not declared an array - but when I do declare an array, I get the following errors: Error CS1503 Argument 1: cannot convert from ‘Unity.MLAgents.Actuators.ActionBuffers’ to ‘float’ & Error CS0029 Cannot implicitly convert type ‘Unity.MLAgents.Actuators.ActionBuffers’ to ‘float’ & Error CS0019 Operator ‘*’ cannot be applied to operands of type ‘ActionBuffers’ and ‘ActionBuffers’ I don’t know how to change ActionBuffers to a float…(or if that would help)… I tried downgrading my packages, but after 3 days I ended up getting everything back, so I’m not going to use that option. Here is a screenshot of the problematic code…
using UnityEngine;
using UnityEngine.SceneManagement;
public class MainMenu : MonoBehaviour
{
public void Play()
{
SceneManager.LoadScene(1);
}
public void Options()
{
SceneManager.LoadScene(2);
Debug.Log("You Open Setings");
}
public void Exit()
{
Application.Quit();
}
public void ExitSettings()
{
SceneManager.LoadScene(0);
}
public void Esc()
{
if(Input.GetKeyDown(Esc))
SceneManager.LoadScene(3);