#Ball Balance AI Not Tuned Well

1 messages · Page 1 of 1 (latest)

karmic plinth
#

Hi! I'm trying to make an ai that can balance a ball on a plate (images attached). However, the physics seem a bit off and the ball keeps jumping. Additionally, the ai doesn't seem to be training very well. Are there any settings or code changes I need? Any help is appreciated.

Agent Code (removed some of it to save space but most of it is just setting angles and resetting the ball):

using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Sensors;
using UnityEngine.UIElements;
using Unity.VisualScripting;

public class BallBalanceAgent : Agent
{
    [SerializeField] public GameObject ball;
    [SerializeField] public GameObject a1;
    [SerializeField] public GameObject a2;
    [SerializeField] public GameObject a3;
    [SerializeField] public Vector3 start;
    public GameObject platform;
    public Material good;
    public Material medium;
    public Material bad;
    private float r;

    public override void CollectObservations(VectorSensor sensor)
    {
        sensor.AddObservation(GetAngle(a1));
        sensor.AddObservation(GetAngle(a2));
        sensor.AddObservation(GetAngle(a3));

        Vector2 pos;
        Ray ray = new Ray(ball.transform.position, Vector3.down);
        if (Physics.Raycast(ray, out RaycastHit hit))
        {
            Vector3 hitPos = hit.point;
            pos = GetRelative(hitPos);
        }
        else
        {
            pos = new Vector2(-1, -1);
        }
        
        sensor.AddObservation(pos);
    }

    void Update()
    {
        Ray ray = new Ray(ball.transform.position, Vector3.down);
        if (Physics.Raycast(ray, out RaycastHit hit))
        {
            AddReward(0.1f);
        }
        else
        {
            EndEpisode();
            Debug.Log("Ending...");
        }
    }
}