i modified the rewards for the agent, my agent began to collect food and be afraid of…
- User
i modified the rewards for the agent, my agent began to collect food and be afraid of the walls. but now he is afraid to collect food, which is right next to the walls.
here's my PlayerAgent.cs:
using UnityEngine; using Unity.MLAgents; using Unity.MLAgents.Actuators; using Unity.MLAgents.Sensors; using System.Collections.Generic;
public class PlayerAgent : Agent { [SerializeField] private float moveSpeed = 5.0f; [SerializeField] private float maxEpisodeTime = 20.0f; private TrainingArea trainingArea; private float episodeTime = 0.0f;
public override void Initialize() { trainingArea = GetComponentInParent<TrainingArea>(); } public override void OnEpisodeBegin() { trainingArea.ResetArea(); episodeTime = 0.0f; } public override void CollectObservations(VectorSensor sensor) { // Add player position sensor.AddObservation(transform.localPosition); // Add food positions relative to the player position List<GameObject> allFood = trainingArea.GetFoodInstances(); foreach (GameObject food in allFood) { Vector3 relativeFoodPosition = Vector3.zero; if (food != null) { relativeFoodPosition = food.transform.localPosition - transform.localPosition; } // If a food instance is eaten, add its relative position as (0, 0, 0) sensor.AddObservation(relativeFoodPosition); } } public override void OnActionReceived(ActionBuffers actions) { float moveX = Mathf.Clamp(actions.ContinuousActions[0], -1, 1); float moveY = Mathf.Clamp(actions.ContinuousActions[1], -1, 1); Vector3 move = new Vector3(moveX, moveY, 0) * moveSpeed * Time.fixedDeltaTime; Vector3 newPosition = transform.position + move; Collider2D wallCollider = Physics2D.OverlapCircle(newPosition, 0.5f, LayerMask.GetMask("Wall")); if (wallCollider != null && wallCollider.CompareTag("Wall")) { // Add a minor negative reward when hitting the wall SetReward(-0.5f); } else { // Decrease the negative reward SetReward(-0.01f); transform.position = newPosition; } Collider2D foodCollider = Physics2D.OverlapCircle(newPosition, 0.5f, LayerMask.GetMask("Food")); if (foodCollider != null && foodCollider.CompareTag("Food")) { if (trainingArea.GetFoodInstances().Contains(foodCollider.gameObject)) { SetReward(1f); Destroy(foodCollider.gameObject); // Add bonus reward if all food has been eaten if (trainingArea.AllFoodEaten()) { SetReward(5f); } } } } public override void Heuristic(in ActionBuffers actionsOut) { ActionSegment<float> continuousActions = actionsOut.ContinuousActions; continuousActions[0] = Input.GetAxis("Horizontal"); continuousActions[1] = Input.GetAxis("Vertical"); } private void FixedUpdate() { episodeTime += Time.fixedDeltaTime; if (episodeTime >= maxEpisodeTime || trainingArea.AllFoodEaten()) { EndEpisode(); } } - Assistant
It looks like the player agent is actually receiving a negative reward (-1) every step for being close to the wall. Since your agent is programmed to avoid negative rewards, he will also avoid the food near the walls. In order to fix this behavior, you can decrease the negative reward when the agent is near the wall, so the positive reward of collecting food outweighs the fear of the wall. Additionally, consider only applying a negative reward if the agent is trying to move into the wall.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.