0% found this document useful (0 votes)
8 views5 pages

AIUNITY

The document outlines a Unity C# script for an ML-Agent, detailing the imports, class definition, and methods for agent behavior, including observation collection, action handling, and collision detection. Key functionalities include resetting the agent's position, determining target locations, and managing rewards based on interactions with the environment. The script facilitates training an agent to navigate towards a target while avoiding obstacles.

Uploaded by

sid.souhil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

AIUNITY

The document outlines a Unity C# script for an ML-Agent, detailing the imports, class definition, and methods for agent behavior, including observation collection, action handling, and collision detection. Key functionalities include resetting the agent's position, determining target locations, and managing rewards based on interactions with the environment. The script facilitates training an agent to navigate towards a target while avoiding obstacles.

Uploaded by

sid.souhil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Imports

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Sensors;

1. using System.Collections; and using System.Collections.Generic;


o These namespaces are for handling collections such as arrays and lists, which may
be useful for game objects or agent logic.
2. using UnityEngine;
o Gives access to Unity's core classes, such as Transform, GameObject, and other
components.
3. using Unity.MLAgents;
o Provides access to Unity's ML-Agents Toolkit, enabling the creation of machine
learning agents.
4. using Unity.MLAgents.Actuators;
o Contains classes for defining and controlling actions an agent can take (e.g.,
continuous or discrete actions).
5. using Unity.MLAgents.Sensors;
o Provides tools for defining what observations the agent collects from the
environment.

Class Definition
public class AgentController : Agent

6. public class AgentController : Agent


o Declares the class AgentController and inherits from Unity's Agent class,
which is the base class for ML-Agents.
o The Agent class provides methods to define the agent's behavior, collect
observations, handle rewards, and take actions.

Target Reference
public Transform target;

7. public Transform target;


o A public reference to the target object (Transform type) that the agent must
reach.
o This variable will be assigned in the Unity Editor.
OnEpisodeBegin()

public override void OnEpisodeBegin()

8. public override void OnEpisodeBegin()


o This method is called automatically at the start of each training episode.
o It is used to reset the environment or initialize variables.

transform.localPosition = new Vector3(0, 0.5f, 0);

9. transform.localPosition = new Vector3(0, 0.5f, 0);


o Resets the agent's position to the center of the environment at a height of 0.5 on
the Y-axis.

int rand = Random.Range(0, 2);

10. int rand = Random.Range(0, 2);


o Generates a random integer (rand) that is either 0 or 1.

if(rand == 0)
{
target.localPosition = new Vector3(2.5f, 0.5f, 0);
}

11. if (rand == 0)
o If the random number is 0, the target's position is set to the right (x = 2.5).

if(rand == 1)
{
target.localPosition = new Vector3(-2.5f, 0.5f, 0);
}

12. if (rand == 1)
o If the random number is 1, the target's position is set to the left (x = -2.5).

CollectObservations()

public override void CollectObservations(VectorSensor sensor)

13. public override void CollectObservations(VectorSensor sensor)


o This method collects observations from the environment and passes them to the
agent.
o Observations are numerical values that represent the state of the environment.

sensor.AddObservation(transform.localPosition);

14. sensor.AddObservation(transform.localPosition);
o Adds the agent's position as an observation.
o The agent will learn to consider its position in relation to the target and obstacles.

sensor.AddObservation(target.localPosition);

15. sensor.AddObservation(target.localPosition);
o Adds the target's position as an observation.

OnActionReceived()

public override void OnActionReceived(ActionBuffers actions)

16. public override void OnActionReceived(ActionBuffers actions)


o Defines the actions taken by the agent in response to its policy or heuristic.

float move = actions.ContinuousActions[0];

17. float move = actions.ContinuousActions[0];


o Retrieves the continuous action value from the first index of the ActionBuffers.

float speed = 2;

18. float speed = 2;


o Sets the movement speed multiplier.

transform.localPosition += new Vector3(move, 0) * speed * Time.deltaTime;

19. transform.localPosition += new Vector3(move, 0) * speed *


Time.deltaTime;
o Updates the agent's position based on the action taken.
o move controls the direction and magnitude, scaled by speed and frame time.
Heuristic()

public override void Heuristic(in ActionBuffers actions)

20. public override void Heuristic(in ActionBuffers actions)


o Provides manual control for testing and debugging the agent.

ActionSegment<float> continuousActions = actions.ContinuousActions;

21. ActionSegment<float> continuousActions = actions.ContinuousActions;


o Accesses the ContinuousActions buffer to store manual inputs.

continuousActions[0] = Input.GetAxisRaw("Horizontal");

22. continuousActions[0] = Input.GetAxisRaw("Horizontal");


o Maps the horizontal input (A/D keys or left/right arrow keys) to the continuous
action.

Collision Handling
private void OnTriggerEnter(Collider other)

23. private void OnTriggerEnter(Collider other)


o Detects when the agent collides with other objects.

if (other.CompareTag("sphere"))

24. if (other.CompareTag("sphere"))
o Checks if the agent collides with an object tagged as sphere (the target).

AddReward(1);
EndEpisode();

25. AddReward(1);
o Rewards the agent with a positive value (1) for reaching the target.
26. EndEpisode();
o Ends the current episode, signaling the environment to reset.

if (other.CompareTag("wall"))
27. if (other.CompareTag("wall"))
o Checks if the agent collides with a wall.

AddReward(-1);
EndEpisode();

28. AddReward(-1);
o Penalizes the agent with a negative reward (-1) for hitting a wall.
o Ends the episode as a failure.

You might also like