AI before training

This commit is contained in:
2024-04-05 19:41:43 +02:00
parent 20e33b69ca
commit 410b29c0c6
10 changed files with 3856 additions and 1032 deletions

297
Assets/AgentController.cs Normal file
View File

@@ -0,0 +1,297 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;
using UnityEngine.UIElements;
using System.Linq;
using Unity.Mathematics;
using Unity.VisualScripting;
public class AgentController : Agent
{
public float motorTorque = 300;
public float brakeTorque = 500;
public float maxSpeed = 400;
public float steeringRange = 9;
public float steeringRangeAtMaxSpeed = 7;
public float autoBrake = 100;
WheelControl[] wheels;
Rigidbody rigidBody;
public Transform Target; //(35..39, 0.25, -20..-30)
Vector3 startPosition;
Quaternion startRotation;
// Start is called before the first frame update
void Start()
{
rigidBody = GetComponent<Rigidbody>();
// Find all child GameObjects that have the WheelControl script attached
wheels = GetComponentsInChildren<WheelControl>();
startPosition = transform.localPosition;
startRotation = transform.localRotation;
}
public override void OnEpisodeBegin()
{
// reset wheels
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;
Target.localPosition = new Vector3(UnityEngine.Random.Range(35f, 39f),
0.25f,
UnityEngine.Random.Range(-30f, -20f));
}
public override void CollectObservations(VectorSensor sensor)
{
// Target and Agent positions
sensor.AddObservation(Target.localPosition.x);
sensor.AddObservation(Target.localPosition.z);
sensor.AddObservation(transform.localPosition.x);
sensor.AddObservation(transform.localPosition.z);
// 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
// add obserevations
if (forwardMagnitude >= 0.001)
sensor.AddObservation(forwardMagnitude);
else
sensor.AddObservation(FullVelocityMagnitude);
sensor.AddObservation(angularMagnitude);
}
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;
Target.localPosition = new Vector3(UnityEngine.Random.Range(35f, 39f),
0.25f,
UnityEngine.Random.Range(-30f, -20f));
}
}
public override void OnActionReceived(ActionBuffers actions)
{
// Actions size = 2 [vertical speed, horizontal speed] = [-1..1, -1..1] // discrete = [{0, 1, 2}, {0, 1, 2}] = [{-1, 0, 1}...]
float vInput = 0;
float hInput = 0;
if (actions.DiscreteActions[0] == 0)
vInput = -1f;
if (actions.DiscreteActions[0] == 1)
vInput = 1f;
if (actions.DiscreteActions[1] == 0)
hInput = -1f;
if (actions.DiscreteActions[1] == 1)
hInput = 1f;
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;
}
}
// rewards
float distanceToTarget = Vector3.Distance(transform.localPosition, Target.localPosition);
if (distanceToTarget < 0.5f)
{
SetReward(1.0f);
EndEpisode();
}
}
public override void Heuristic(in ActionBuffers actionsOut)
{
var discreteActionsOut = actionsOut.DiscreteActions;
Debug.Log(Input.GetAxis("Vertical"));
discreteActionsOut[0] = 2;
discreteActionsOut[1] = 2;
if (Input.GetAxis("Vertical") < -0.5)
discreteActionsOut[0] = 0;
if (Input.GetAxis("Vertical") > 0.5)
discreteActionsOut[0] = 1;
if (Input.GetAxis("Horizontal") < -0.5)
discreteActionsOut[1] = 0;
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;
// }
// }
// }
}

View File

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

8
Assets/ML-Agents.meta Normal file
View File

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

View File

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

View File

@@ -0,0 +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"}}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: b3e7c528271b51048b86778e7cf3411d
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: c3f3ba311620559429297d0d45d4586d guid: 6920d5b57a97ee446b0912bdc5ab3585
DefaultImporter: DefaultImporter:
externalObjects: {} externalObjects: {}
userData: userData:

View File

@@ -3,6 +3,7 @@
"com.unity.cinemachine": "2.9.7", "com.unity.cinemachine": "2.9.7",
"com.unity.collab-proxy": "2.3.1", "com.unity.collab-proxy": "2.3.1",
"com.unity.feature.development": "1.0.1", "com.unity.feature.development": "1.0.1",
"com.unity.ml-agents": "2.0.1",
"com.unity.textmeshpro": "3.0.6", "com.unity.textmeshpro": "3.0.6",
"com.unity.timeline": "1.7.5", "com.unity.timeline": "1.7.5",
"com.unity.ugui": "1.0.0", "com.unity.ugui": "1.0.0",

View File

@@ -1,5 +1,25 @@
{ {
"dependencies": { "dependencies": {
"com.unity.barracuda": {
"version": "3.0.0",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.burst": "1.6.0",
"com.unity.modules.jsonserialize": "1.0.0",
"com.unity.modules.imageconversion": "1.0.0"
},
"url": "https://packages.unity.com"
},
"com.unity.burst": {
"version": "1.8.9",
"depth": 2,
"source": "registry",
"dependencies": {
"com.unity.mathematics": "1.2.1"
},
"url": "https://packages.unity.com"
},
"com.unity.cinemachine": { "com.unity.cinemachine": {
"version": "2.9.7", "version": "2.9.7",
"depth": 0, "depth": 0,
@@ -69,6 +89,24 @@
"dependencies": {}, "dependencies": {},
"url": "https://packages.unity.com" "url": "https://packages.unity.com"
}, },
"com.unity.mathematics": {
"version": "1.2.6",
"depth": 3,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.ml-agents": {
"version": "2.0.1",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.barracuda": "2.0.0",
"com.unity.modules.imageconversion": "1.0.0",
"com.unity.modules.jsonserialize": "1.0.0"
},
"url": "https://packages.unity.com"
},
"com.unity.performance.profile-analyzer": { "com.unity.performance.profile-analyzer": {
"version": "1.2.2", "version": "1.2.2",
"depth": 1, "depth": 1,