Skip to content

AmineAndam04/Unity-ML-LLAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

Unity-ML-LLAPI

Instructions to interact with a Unity environment with ML agents using Python Low-level API

  1. Go to Unity Hub and create a new 3D project
  2. In Unity, go to Window, then choose Package Manager. At the top you can see Packages: In Project, click on it and choose Packages: Unity Registry. Now look for ML Agents. Install the package.
Screenshot 2023-10-11 at 14 56 59
  1. close Unity and re-open it again.
  2. Create a suitable environment. (We use the environment below) :
Screenshot 2023-10-11 at 15 15 20
  1. Create a new c# script, at its header add using Unity.MLAgents ;, and change the MonoBehaviour to Agent.
Screenshot 2023-10-11 at 15 24 53
  1. Add the appropriate logic you want. In our case, we aim to train our agent (the cube) to reach a goal (the sphere). Our code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents ;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;



public class AgentScript : Agent
    
{
    
    [SerializeField] private Transform  targetTransform ;

    public override void CollectObservations(VectorSensor sensor)
    {
        sensor.AddObservation(transform.localPosition);
        sensor.AddObservation(targetTransform.localPosition);
    }

    public override void OnActionReceived(ActionBuffers actions)
    {
        float moveX = actions.ContinuousActions[0];
        float moveZ = actions.ContinuousActions[1];
        float moveSpeed = 7f; 
        transform.localPosition += new Vector3(moveX,0,moveZ)*Time.deltaTime*moveSpeed ;
    }
    public override void OnEpisodeBegin()
    {
        transform.position = new Vector3 (0f,1f,0f);
    }

    private void OnTriggerEnter(Collider other) {
        if (other.tag == "Wall")
        {
            Debug.Log("Collision with the Wall");
            SetReward(-1f);
            EndEpisode();
        }

        if (other.tag == "Goal")
        {
            Debug.Log("Collision with the goal");
            SetReward(1f);
            EndEpisode();
        }
    }
}
  1. Select the object to which you assigned the agent script. In the inspector window set the parameters for the Behavior Parameters :

    Screenshot 2023-10-11 at 15 31 02
  2. Add a Decision Requester

  3. For now, we only have numerical observations. In some scenarios, we may be interested in having visual observations, i.e. to use an architecture with CNNs. In the inspector of your agent, go to Add Component, and look for Camera Sensor.

Screenshot 2023-10-11 at 15 38 54
  1. Set your camera in a suitable location to capture relevant observations that may help your model:
Screenshot 2023-10-11 at 15 39 50
  1. In the Hierarchy panel, duplicate the camera, and change its name (we changed it to SensorCamera.It's advisable to disable the Audio Listener in the inspector of SensorCamera. Then add the duplicated camera to the Camera field in the Camera Sensor field in the inspector of the agent:
Screenshot 2023-10-11 at 15 44 14
  1. Now let's export the environment so that we can interact with it using Python. Go to File, and choose Build Settings. In the window that pops up, set the Target Platform and the Architecture. Don't forget to check the Development Build. Then click on Build at the bottom and choose a folder.
Screenshot 2023-10-11 at 16 06 02

About

Instructions to interact with a Unity environment with ML agents using Python Low level API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published