Der Agent startet bei einem Ziel ein Projektil unter Schwerkraft. Der Agent hat nur eine Aktion - den Winkel des Schusses. Die Startkraft ist konstant. Ich variiere auch noch nicht die Position des Ziels. Daher sollte dies trivial sein, da das Modell nur den richtigen Winkel zum Schießen lernen muss. Aber nach 300000 Trainingsschritten schießt das Modell immer noch unregelmäßig.
Agent: < /p>
using Unity.MLAgents;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Sensors;
using UnityEngine;
public class ProjectileAgent : Agent
{
public Transform target; // Stationary target with a 2D collider & "Target" tag
public Transform launchPoint; // Where the projectile is spawned
public GameObject projectilePrefab; // Prefab with Rigidbody2D & ProjectileCollision script
public float fixedForce = 500f; // Constant force applied to the projectile
private bool hasLaunched = false;
public override void OnEpisodeBegin()
{
hasLaunched = false;
RequestDecision(); // Request one decision at start of each episode
}
public override void CollectObservations(VectorSensor sensor)
{
// Observe the relative position from launchPoint to target (x,y)
Vector2 diff = target.position - launchPoint.position;
sensor.AddObservation(diff.x);
sensor.AddObservation(diff.y);
}
public override void OnActionReceived(ActionBuffers actions)
{
if (!hasLaunched)
{
// One continuous action (0..1) mapped to [0..180] degrees
float angle01 = Mathf.Clamp01(actions.ContinuousActions[0]);
float angleDegrees = Mathf.Lerp(0f, 180f, angle01);
LaunchProjectile(angleDegrees);
hasLaunched = true;
}
}
private void LaunchProjectile(float angleDegrees)
{
GameObject projObj = Instantiate(projectilePrefab, launchPoint.position, Quaternion.identity);
ProjectileCollision projScript = projObj.GetComponent
();
projScript.agent = this;
Rigidbody2D rb = projObj.GetComponent();
float rad = angleDegrees * Mathf.Deg2Rad;
Vector2 direction = new Vector2(Mathf.Cos(rad), Mathf.Sin(rad));
rb.AddForce(direction * fixedForce);
}
// Called by projectile on hitting the target
public void OnHitTarget()
{
AddReward(1.0f);
EndEpisode();
}
// Called by projectile on missing the target
public void OnMiss(Vector2 projectilePosition)
{
float distance = Vector2.Distance(projectilePosition, target.position);
float maxDistance = 10f; // Adjust as needed
float proximity = 1f - (distance / maxDistance);
proximity = Mathf.Clamp01(proximity);
// Partial reward for getting close
AddReward(proximity * 0.5f);
// Small penalty for a miss
AddReward(-0.1f);
EndEpisode();
}
// Heuristic for testing in Unity Editor (random angle)
public override void Heuristic(in ActionBuffers actionsOut)
{
actionsOut.ContinuousActions[0] = Random.value;
}
}
< /code>
Projektil: < /p>
using UnityEngine;
public class ProjectileCollision : MonoBehaviour
{
public ProjectileAgent agent;
private void Start()
{
// Destroy after a short time so we can register a miss
Destroy(gameObject, lifetime);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Target"))
{
agent.OnHitTarget();
}
else
{
agent.OnMiss(transform.position);
}
Destroy(gameObject);
}
}
Was Ich habe es versucht
Belohnungsformung:
+1 für das Treffen des Ziels, teilweise entfernungsbasierte Belohnung für Beinahetreffer und ein kleiner Minuspunkt für fehlt.
Ich habe die erhöht Trefferbelohnung bis zu +3, Fehlschussstrafe verringert usw.
Trainingsschritte:
Ich bin mit PPO bis zu 300.000+ Schritte gelaufen.
Kollisionsprüfungen:
Protokolle bestätigen, dass OnHitTarget() und OnMiss() zum erwarteten Zeitpunkt abgefeuert werden.
Feste Kraft und Schwerkraft:
Verifiziert, dass der Pfeil erreichen kann das Ziel manuell durch Hartcodierte Winkel.
Die Schwerkraft ist so eingestellt, dass es physikalisch möglich ist, zu treffen.
Kein zufälliges Ziel:
Das Ziel ist derzeit an einer Stelle fixiert, um es einfach zu halten.< /li>
Der Agent startet bei einem Ziel ein Projektil unter Schwerkraft. Der Agent hat nur eine Aktion - den Winkel des Schusses. Die Startkraft ist konstant. Ich variiere auch noch nicht die Position des Ziels. Daher sollte dies trivial sein, da das Modell nur den richtigen Winkel zum Schießen lernen muss. Aber nach 300000 Trainingsschritten schießt das Modell immer noch unregelmäßig. Agent: < /p> [code]using Unity.MLAgents; using Unity.MLAgents.Actuators; using Unity.MLAgents.Sensors; using UnityEngine;
public class ProjectileAgent : Agent { public Transform target; // Stationary target with a 2D collider & "Target" tag public Transform launchPoint; // Where the projectile is spawned public GameObject projectilePrefab; // Prefab with Rigidbody2D & ProjectileCollision script public float fixedForce = 500f; // Constant force applied to the projectile
private bool hasLaunched = false;
public override void OnEpisodeBegin() { hasLaunched = false; RequestDecision(); // Request one decision at start of each episode }
public override void CollectObservations(VectorSensor sensor) { // Observe the relative position from launchPoint to target (x,y) Vector2 diff = target.position - launchPoint.position; sensor.AddObservation(diff.x); sensor.AddObservation(diff.y); }
public override void OnActionReceived(ActionBuffers actions) { if (!hasLaunched) { // One continuous action (0..1) mapped to [0..180] degrees float angle01 = Mathf.Clamp01(actions.ContinuousActions[0]); float angleDegrees = Mathf.Lerp(0f, 180f, angle01);
Rigidbody2D rb = projObj.GetComponent(); float rad = angleDegrees * Mathf.Deg2Rad; Vector2 direction = new Vector2(Mathf.Cos(rad), Mathf.Sin(rad)); rb.AddForce(direction * fixedForce); }
// Called by projectile on hitting the target public void OnHitTarget() { AddReward(1.0f); EndEpisode(); }
// Called by projectile on missing the target public void OnMiss(Vector2 projectilePosition) { float distance = Vector2.Distance(projectilePosition, target.position); float maxDistance = 10f; // Adjust as needed float proximity = 1f - (distance / maxDistance); proximity = Mathf.Clamp01(proximity);
// Partial reward for getting close AddReward(proximity * 0.5f);
// Small penalty for a miss AddReward(-0.1f); EndEpisode(); }
// Heuristic for testing in Unity Editor (random angle) public override void Heuristic(in ActionBuffers actionsOut) { actionsOut.ContinuousActions[0] = Random.value; } } < /code> Projektil: < /p> using UnityEngine;
public class ProjectileCollision : MonoBehaviour { public ProjectileAgent agent;
private void Start() { // Destroy after a short time so we can register a miss Destroy(gameObject, lifetime); }
private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Target")) { agent.OnHitTarget(); } else { agent.OnMiss(transform.position); } Destroy(gameObject); } } [/code] [img]https://i.sstatic.net/QsLWVcJn.png[/img] Was Ich habe es versucht [list] [*]Belohnungsformung: +1 für das Treffen des Ziels, teilweise entfernungsbasierte Belohnung für Beinahetreffer und ein kleiner Minuspunkt für fehlt. [*]Ich habe die erhöht Trefferbelohnung bis zu +3, Fehlschussstrafe verringert usw. Trainingsschritte: [*]Ich bin mit PPO bis zu 300.000+ Schritte gelaufen. [*]Kollisionsprüfungen: Protokolle bestätigen, dass OnHitTarget() und OnMiss() zum erwarteten Zeitpunkt abgefeuert werden. [*]Feste Kraft und Schwerkraft: Verifiziert, dass der Pfeil erreichen kann das Ziel manuell durch Hartcodierte Winkel. Die Schwerkraft ist so eingestellt, dass es physikalisch möglich ist, zu treffen. Kein zufälliges Ziel: Das Ziel ist derzeit an einer Stelle fixiert, um es einfach zu halten.< /li> [/list]
Ich arbeite daran, ein neuronales Netzwerk von Grund auf in js zu programmieren, und habe Folgendes implementiert:
class Network{
constructor(layerSizes, activation){
this.activation = activation...
Ich folgte Zpfu30TByks Tutorial über ML -Agenten. Ich habe nicht fast etwas aus dem Code geändert, außer für die OnePisodebin -Funktion, in der ich Vector3.Zero ersetzt habe. mit der Ausgangsposition...
Zeile Nummer 3 des angegebenen Codes – aus langchain_community.agents import create_sql_agent
throws
ImportError: cannot import name 'create_sql_agent' from 'langchain_community.agents' error
from...
Ich habe ml-agents in Betrieb. Wenn ich mlagents-learn über die Befehlszeile ausführe und das Spiel über den Unity-Editor spiele, funktioniert alles einwandfrei (der Agent trainiert). Aber wenn ich...
Ich versuche, in meinem Python-Projekt einen benutzerdefinierten Agenten mit Langgraph und OpenAI zu erstellen, aber bei der Verwendung der Funktion „create_react_agent“ tritt ein Fehler auf. Hier...