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 System.Linq;
using Unity.Mathematics; using Unity.Mathematics;
using Unity.VisualScripting; using Unity.VisualScripting;
using System.Reflection;
public class AgentController : Agent public class AgentController : Agent
{ {
@@ -19,11 +20,16 @@ public class AgentController : Agent
public float autoBrake = 100; public float autoBrake = 100;
WheelControl[] wheels; WheelControl[] wheels;
Rigidbody rigidBody; Rigidbody rigidBody;
public Transform Target; //(35..39, 0.25, -20..-30) public List<GameObject> checkpoints;
Vector3 startPosition; Vector3 startPosition;
Quaternion startRotation; Quaternion startRotation;
int currentStep = 0;
int stepsSinceCheckpoint = 0;
public int maxStepsPerCheckpoint = 300;
// Start is called before the first frame update // Start is called before the first frame update
[System.Obsolete]
void Start() void Start()
{ {
rigidBody = GetComponent<Rigidbody>(); rigidBody = GetComponent<Rigidbody>();
@@ -35,6 +41,7 @@ public class AgentController : Agent
public override void OnEpisodeBegin() public override void OnEpisodeBegin()
{ {
int stepsSinceCheckpoint = 0;
// reset wheels // reset wheels
foreach (var wheel in wheels) foreach (var wheel in wheels)
{ {
@@ -43,61 +50,68 @@ public class AgentController : Agent
wheel.WheelCollider.steerAngle = 0; wheel.WheelCollider.steerAngle = 0;
} }
// reset car
transform.localPosition = startPosition; transform.localPosition = startPosition;
transform.localRotation = startRotation; transform.localRotation = startRotation;
rigidBody.velocity = Vector3.zero; rigidBody.velocity = Vector3.zero;
rigidBody.angularVelocity = Vector3.zero; rigidBody.angularVelocity = Vector3.zero;
Target.localPosition = new Vector3(UnityEngine.Random.Range(35f, 39f), // reset checkpoints
0.25f,
UnityEngine.Random.Range(-30f, -20f)); foreach (GameObject checkpoint in checkpoints)
{
checkpoint.GetComponent<Checkpoint>().isCollected = false;
}
} }
public override void CollectObservations(VectorSensor sensor) public override void CollectObservations(VectorSensor sensor)
{ {
// Target and Agent positions Transform currentCheckpoint = checkpoints[0].transform;
sensor.AddObservation(Target.localPosition.x); foreach (GameObject checkpoint in checkpoints)
sensor.AddObservation(Target.localPosition.z); {
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.x);
sensor.AddObservation(transform.localPosition.z); sensor.AddObservation(transform.localPosition.z);
sensor.AddObservation(transform.rotation.eulerAngles.y);
// Agent velocity // Agent velocity
// calculate forward velocity
var FullVelocityMagnitude = rigidBody.velocity.magnitude; // Velocity including angular 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
// add obserevations
if (forwardMagnitude >= 0.001)
sensor.AddObservation(forwardMagnitude);
else
sensor.AddObservation(FullVelocityMagnitude); sensor.AddObservation(FullVelocityMagnitude);
sensor.AddObservation(angularMagnitude);
} // sensor.AddObservation(wheels[0].WheelCollider.motorTorque);
// sensor.AddObservation(wheels[0].WheelCollider.brakeTorque);
// sensor.AddObservation(wheels[0].WheelCollider.steerAngle);
void Update() // // calculate forward velocity
{ // var FullVelocityMagnitude = rigidBody.velocity.magnitude; // Velocity including angular velocity
if (Input.GetKeyDown("space")) // var angularMagnitude = rigidBody.angularVelocity.magnitude;
{
foreach (var wheel in wheels) // var forwardMagnitude = Mathf.Sqrt( Mathf.Pow(FullVelocityMagnitude, 2) - Mathf.Pow(angularMagnitude, 2)); // Agent velocity in forward direction
{
wheel.WheelCollider.brakeTorque = 0; // // add obserevations
wheel.WheelCollider.motorTorque = 0; // if (forwardMagnitude >= 0.001)
wheel.WheelCollider.steerAngle = 0; // sensor.AddObservation(forwardMagnitude);
} // else
transform.localPosition = startPosition; // sensor.AddObservation(FullVelocityMagnitude);
transform.localRotation = startRotation;
rigidBody.velocity = Vector3.zero; // sensor.AddObservation(angularMagnitude);
rigidBody.angularVelocity = Vector3.zero;
Target.localPosition = new Vector3(UnityEngine.Random.Range(35f, 39f),
0.25f,
UnityEngine.Random.Range(-30f, -20f));
}
} }
public override void OnActionReceived(ActionBuffers actions) public override void OnActionReceived(ActionBuffers actions)
@@ -169,8 +183,6 @@ public class AgentController : Agent
wheel.WheelCollider.brakeTorque = 0; wheel.WheelCollider.brakeTorque = 0;
} }
if (isStopping) if (isStopping)
{ {
// If the user is trying to go in the opposite direction // 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.brakeTorque = (Mathf.Abs(vInput) * brakeTorque + autoBrake) * 5;
} }
// wheel.WheelCollider.motorTorque = 0;
} }
} }
// rewards // rewards
float distanceToTarget = Vector3.Distance(transform.localPosition, Target.localPosition);
if (distanceToTarget < 0.5f) Transform currentCheckpoint = checkpoints[0].transform;
foreach (GameObject checkpoint in checkpoints)
{ {
bool isCollected = checkpoint.GetComponent<Checkpoint>().isCollected;
if (!isCollected)
{
currentCheckpoint = checkpoint.transform;
break;
}
}
var closestPoint = currentCheckpoint.GetComponent<Collider>().ClosestPointOnBounds(transform.localPosition);
var distanceToCheckpoint = Vector3.Distance(transform.localPosition, closestPoint);
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); SetReward(1.0f);
}
currentStep += 1;
stepsSinceCheckpoint += 1;
if (stepsSinceCheckpoint >= maxStepsPerCheckpoint)
{
stepsSinceCheckpoint = 0;
EndEpisode(); EndEpisode();
} }
} }
@@ -200,8 +246,6 @@ public class AgentController : Agent
{ {
var discreteActionsOut = actionsOut.DiscreteActions; var discreteActionsOut = actionsOut.DiscreteActions;
Debug.Log(Input.GetAxis("Vertical"));
discreteActionsOut[0] = 2; discreteActionsOut[0] = 2;
discreteActionsOut[1] = 2; discreteActionsOut[1] = 2;
@@ -215,83 +259,4 @@ public class AgentController : Agent
if (Input.GetAxis("Horizontal") > 0.5) if (Input.GetAxis("Horizontal") > 0.5)
discreteActionsOut[1] = 1; 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;
// }
// }
// }
} }

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d34ba0f52820a6c439d6ae11a916c6f6
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 2e6dea9e239c01840b87be0d1b43c8bb
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData: ' (Unity.MLAgents.Demonstrations.DemonstrationSummary)'
assetBundleName:
assetBundleVariant:
script: {fileID: 11500000, guid: 7bd65ce151aaa4a41a45312543c56be1, type: 3}

View File

@@ -1 +1 @@
{"count":1,"self":46.1017856,"total":46.9356809,"children":{"InitializeActuators":{"count":1,"self":0,"total":0,"children":null},"InitializeSensors":{"count":1,"self":0.0010064,"total":0.0010064,"children":null},"AgentSendState":{"count":2095,"self":0.035713499999999995,"total":0.067968199999999993,"children":{"CollectObservations":{"count":2095,"self":0.0191258,"total":0.0191258,"children":null},"WriteActionMask":{"count":2095,"self":0.0051049,"total":0.0051049,"children":null},"RequestDecision":{"count":2095,"self":0.008024,"total":0.008024,"children":null}}},"DecideAction":{"count":2095,"self":0.7322124,"total":0.7322124,"children":null},"AgentAct":{"count":2095,"self":0.0317066,"total":0.0317066,"children":null}},"gauges":{"Benson v1.CumulativeReward":{"count":6,"max":1,"min":1,"runningAverage":1,"value":1,"weightedAverage":1}},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1712338807","unity_version":"2022.3.11f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2022.3.11f1\\Editor\\Unity.exe -projectpath C:\\Users\\noahk\\Documents\\Unity projects\\Racesm -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-noahk -hubSessionId 61390a08-deee-455b-9b78-7c702050fabf -accessToken HPacrWVInUHR0G5x93m6zxWFWNrjAqy08EExnpTeleY005f","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"AI training","end_time_seconds":"1712338854"}} {"count":1,"self":7.2964823999999995,"total":60.9954573,"children":{"InitializeActuators":{"count":3,"self":0.001513,"total":0.001513,"children":null},"InitializeSensors":{"count":3,"self":0.0010095,"total":0.0010095,"children":null},"AgentSendState":{"count":2184,"self":0.0263912,"total":0.1014805,"children":{"CollectObservations":{"count":6552,"self":0.0160938,"total":0.0160938,"children":null},"WriteActionMask":{"count":6552,"self":0.0020112,"total":0.0020112,"children":null},"RequestDecision":{"count":6552,"self":0.019229899999999998,"total":0.056984299999999995,"children":{"AgentInfo.ToProto":{"count":6552,"self":0.016433299999999998,"total":0.0377544,"children":{"GenerateSensorData":{"count":6552,"self":0.0213211,"total":0.0213211,"children":null}}}}}}},"DecideAction":{"count":2184,"self":53.5099136,"total":53.509915199999995,"children":null},"AgentAct":{"count":2184,"self":0.0840575,"total":0.0840575,"children":{"AgentInfo.ToProto":{"count":21,"self":0,"total":0,"children":{"GenerateSensorData":{"count":21,"self":0,"total":0,"children":null}}}}}},"gauges":{"BensonImitationV1.CumulativeReward":{"count":21,"max":0,"min":0,"runningAverage":0,"value":0,"weightedAverage":0}},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1712357698","unity_version":"2022.3.11f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2022.3.11f1\\Editor\\Unity.exe -projectpath C:\\Users\\noahk\\Documents\\Unity projects\\Racesm -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-noahk -hubSessionId 61390a08-deee-455b-9b78-7c702050fabf -accessToken HPacrWVInUHR0G5x93m6zxWFWNrjAqy08EExnpTeleY005f","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"AI training","end_time_seconds":"1712357759"}}

8
Assets/Materials.meta Normal file
View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 33d5db520164fcf439d7d2743ad4c2c5
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

83
Assets/Materials/Red.mat Normal file
View File

@@ -0,0 +1,83 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 8
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Red
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_Parent: {fileID: 0}
m_ModifiedSerializedProperties: 0
m_ValidKeywords: []
m_InvalidKeywords: []
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_LockedProperties:
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 1, g: 0, b: 0, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
m_BuildTextureStacks: []

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 41c5a4f3f73f58c48a21d723635a3f63
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 3664a8beed80ac848a9377d9ee14cb9b
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,8 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Checkpoint : MonoBehaviour
{
public bool isCollected = false;
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5cb7e797faf91a145ace5b3e5953a682
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,18 @@
{
"MonoBehaviour": {
"Version": 4,
"EnableBurstCompilation": true,
"EnableOptimisations": true,
"EnableSafetyChecks": false,
"EnableDebugInAllBuilds": false,
"DebugDataKind": 1,
"EnableArmv9SecurityFeatures": false,
"CpuMinTargetX32": 0,
"CpuMaxTargetX32": 0,
"CpuMinTargetX64": 0,
"CpuMaxTargetX64": 0,
"CpuTargetsX32": 6,
"CpuTargetsX64": 72,
"OptimizeFor": 0
}
}

View File

@@ -0,0 +1,6 @@
{
"MonoBehaviour": {
"Version": 4,
"DisabledWarnings": ""
}
}