Training Area is ready

This commit is contained in:
2024-04-06 01:16:28 +02:00
parent 33a7230828
commit eeb46585e7
15 changed files with 4744 additions and 3614 deletions

View File

@@ -8,6 +8,7 @@ using UnityEngine.UIElements;
using System.Linq;
using Unity.Mathematics;
using Unity.VisualScripting;
using System.Reflection;
public class AgentController : Agent
{
@@ -19,11 +20,16 @@ public class AgentController : Agent
public float autoBrake = 100;
WheelControl[] wheels;
Rigidbody rigidBody;
public Transform Target; //(35..39, 0.25, -20..-30)
public List<GameObject> checkpoints;
Vector3 startPosition;
Quaternion startRotation;
int currentStep = 0;
int stepsSinceCheckpoint = 0;
public int maxStepsPerCheckpoint = 300;
// Start is called before the first frame update
[System.Obsolete]
void Start()
{
rigidBody = GetComponent<Rigidbody>();
@@ -35,6 +41,7 @@ public class AgentController : Agent
public override void OnEpisodeBegin()
{
int stepsSinceCheckpoint = 0;
// reset wheels
foreach (var wheel in wheels)
{
@@ -43,61 +50,68 @@ public class AgentController : Agent
wheel.WheelCollider.steerAngle = 0;
}
// reset car
transform.localPosition = startPosition;
transform.localRotation = startRotation;
rigidBody.velocity = Vector3.zero;
rigidBody.angularVelocity = Vector3.zero;
Target.localPosition = new Vector3(UnityEngine.Random.Range(35f, 39f),
0.25f,
UnityEngine.Random.Range(-30f, -20f));
// reset checkpoints
foreach (GameObject checkpoint in checkpoints)
{
checkpoint.GetComponent<Checkpoint>().isCollected = false;
}
}
public override void CollectObservations(VectorSensor sensor)
{
// Target and Agent positions
sensor.AddObservation(Target.localPosition.x);
sensor.AddObservation(Target.localPosition.z);
Transform currentCheckpoint = checkpoints[0].transform;
foreach (GameObject checkpoint in checkpoints)
{
bool isCollected = checkpoint.GetComponent<Checkpoint>().isCollected;
if (!isCollected)
{
currentCheckpoint = checkpoint.transform;
break;
}
}
sensor.AddObservation(currentCheckpoint.localPosition.x);
sensor.AddObservation(currentCheckpoint.localPosition.z);
// agent
sensor.AddObservation(transform.localPosition.x);
sensor.AddObservation(transform.localPosition.z);
sensor.AddObservation(transform.rotation.eulerAngles.y);
// Agent velocity
// calculate forward velocity
var FullVelocityMagnitude = rigidBody.velocity.magnitude; // Velocity including angular velocity
var angularMagnitude = rigidBody.angularVelocity.magnitude;
var forwardMagnitude = Mathf.Sqrt( Mathf.Pow(FullVelocityMagnitude, 2) - Mathf.Pow(angularMagnitude, 2)); // Agent velocity in forward direction
sensor.AddObservation(FullVelocityMagnitude);
// add obserevations
if (forwardMagnitude >= 0.001)
sensor.AddObservation(forwardMagnitude);
else
sensor.AddObservation(FullVelocityMagnitude);
// sensor.AddObservation(wheels[0].WheelCollider.motorTorque);
// sensor.AddObservation(wheels[0].WheelCollider.brakeTorque);
// sensor.AddObservation(wheels[0].WheelCollider.steerAngle);
sensor.AddObservation(angularMagnitude);
// // calculate forward velocity
// var FullVelocityMagnitude = rigidBody.velocity.magnitude; // Velocity including angular velocity
// var angularMagnitude = rigidBody.angularVelocity.magnitude;
}
// var forwardMagnitude = Mathf.Sqrt( Mathf.Pow(FullVelocityMagnitude, 2) - Mathf.Pow(angularMagnitude, 2)); // Agent velocity in forward direction
void Update()
{
if (Input.GetKeyDown("space"))
{
foreach (var wheel in wheels)
{
wheel.WheelCollider.brakeTorque = 0;
wheel.WheelCollider.motorTorque = 0;
wheel.WheelCollider.steerAngle = 0;
}
transform.localPosition = startPosition;
transform.localRotation = startRotation;
rigidBody.velocity = Vector3.zero;
rigidBody.angularVelocity = Vector3.zero;
// // add obserevations
// if (forwardMagnitude >= 0.001)
// sensor.AddObservation(forwardMagnitude);
// else
// sensor.AddObservation(FullVelocityMagnitude);
// sensor.AddObservation(angularMagnitude);
Target.localPosition = new Vector3(UnityEngine.Random.Range(35f, 39f),
0.25f,
UnityEngine.Random.Range(-30f, -20f));
}
}
public override void OnActionReceived(ActionBuffers actions)
@@ -169,8 +183,6 @@ public class AgentController : Agent
wheel.WheelCollider.brakeTorque = 0;
}
if (isStopping)
{
// If the user is trying to go in the opposite direction
@@ -182,16 +194,50 @@ public class AgentController : Agent
wheel.WheelCollider.brakeTorque = (Mathf.Abs(vInput) * brakeTorque + autoBrake) * 5;
}
// wheel.WheelCollider.motorTorque = 0;
}
}
// rewards
Transform currentCheckpoint = checkpoints[0].transform;
foreach (GameObject checkpoint in checkpoints)
{
bool isCollected = checkpoint.GetComponent<Checkpoint>().isCollected;
if (!isCollected)
{
currentCheckpoint = checkpoint.transform;
break;
}
}
// rewards
float distanceToTarget = Vector3.Distance(transform.localPosition, Target.localPosition);
var closestPoint = currentCheckpoint.GetComponent<Collider>().ClosestPointOnBounds(transform.localPosition);
var distanceToCheckpoint = Vector3.Distance(transform.localPosition, closestPoint);
if (distanceToTarget < 0.5f)
if (distanceToCheckpoint < 0.3f)
{
Debug.Log(currentCheckpoint.name);
currentCheckpoint.GetComponent<Checkpoint>().isCollected = true;
stepsSinceCheckpoint = 0;
if (currentCheckpoint == checkpoints[checkpoints.Count - 1].transform)
{
SetReward(10f);
EndEpisode();
Debug.Log("END");
}
SetReward(1.0f);
}
currentStep += 1;
stepsSinceCheckpoint += 1;
if (stepsSinceCheckpoint >= maxStepsPerCheckpoint)
{
stepsSinceCheckpoint = 0;
EndEpisode();
}
}
@@ -200,8 +246,6 @@ public class AgentController : Agent
{
var discreteActionsOut = actionsOut.DiscreteActions;
Debug.Log(Input.GetAxis("Vertical"));
discreteActionsOut[0] = 2;
discreteActionsOut[1] = 2;
@@ -215,83 +259,4 @@ public class AgentController : Agent
if (Input.GetAxis("Horizontal") > 0.5)
discreteActionsOut[1] = 1;
}
// // Update is called once per frame
// void FixedUpdate()
// {
// float vInput = Input.GetAxis("Vertical");
// float hInput = Input.GetAxis("Horizontal");
// // Calculate current speed in relation to the forward direction of the car
// // (this returns a negative number when traveling backwards)
// float forwardSpeed = Vector3.Dot(transform.forward, rigidBody.velocity);
// // Calculate how close the car is to top speed
// // as a number from zero to one
// float speedFactor = Mathf.InverseLerp(0, maxSpeed / 4, forwardSpeed);
// // Use that to calculate how much torque is available
// // (zero torque at top speed)
// float currentMotorTorque = Mathf.Lerp(motorTorque, 0, speedFactor);
// // …and to calculate how much to steer
// // (the car steers more gently at top speed)
// float currentSteerRange = Mathf.Lerp(steeringRange, steeringRangeAtMaxSpeed, speedFactor);
// // Check whether the user input is in the same direction
// // as the car's velocity
// bool isAccelerating = Mathf.Sign(vInput) == Mathf.Sign(forwardSpeed);
// bool isStopping = vInput == 0; // range
// bool isBraking = (vInput < 0 && forwardSpeed > 0) || (vInput > 0 && forwardSpeed < 0);
// if (vInput > 0 && forwardSpeed < 0)
// {
// isAccelerating = false;
// }
// foreach (var wheel in wheels)
// {
// // Apply steering to Wheel colliders that have "Steerable" enabled
// if (wheel.steerable)
// {
// wheel.WheelCollider.steerAngle = hInput * currentSteerRange;
// }
// if (isBraking)
// {
// wheel.WheelCollider.brakeTorque = Mathf.Abs(vInput) * brakeTorque;
// //wheel.WheelCollider.motorTorque = 0;
// }
// if (isAccelerating)
// {
// // Apply torque to Wheel colliders that have "Motorized" enabled
// if (wheel.motorized)
// {
// wheel.WheelCollider.motorTorque = vInput * currentMotorTorque;
// }
// wheel.WheelCollider.brakeTorque = 0;
// }
// if (isStopping)
// {
// // If the user is trying to go in the opposite direction
// // apply brakes to all wheels
// wheel.WheelCollider.brakeTorque = Mathf.Abs(vInput) * brakeTorque + autoBrake;
// if (forwardSpeed < 0)
// {
// wheel.WheelCollider.brakeTorque = (Mathf.Abs(vInput) * brakeTorque + autoBrake) * 5;
// }
// // wheel.WheelCollider.motorTorque = 0;
// }
// }
// }
}
}