Onehot功能完善,修正

OneHot功能完善,更改TaySensor探测到Tag的index表示。与之前不通用!
修正PlayMode中的scenePrefabSet未定位错误
修正Raysensor中TagToInt不包含rayTagResult中的-1
This commit is contained in:
Koha9 2023-12-31 07:49:58 +09:00
parent f3c5f539ec
commit bf1d1d8d3b
9 changed files with 90 additions and 70 deletions

View File

@ -1 +1 @@
{"count":1,"self":26.837244799999997,"total":27.174796399999998,"children":{"InitializeActuators":{"count":2,"self":0.0020071999999999998,"total":0.0020071999999999998,"children":null},"InitializeSensors":{"count":2,"self":0.001,"total":0.001,"children":null},"AgentSendState":{"count":1115,"self":0.008007499999999999,"total":0.0535482,"children":{"CollectObservations":{"count":1115,"self":0.0410334,"total":0.0410334,"children":null},"WriteActionMask":{"count":1115,"self":0.0025057,"total":0.0025057,"children":null},"RequestDecision":{"count":1115,"self":0.0020016,"total":0.0020016,"children":null}}},"DecideAction":{"count":1115,"self":0.0070135,"total":0.0070135,"children":null},"AgentAct":{"count":1115,"self":0.2734714,"total":0.2734714,"children":null}},"gauges":{},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1702212209","unity_version":"2021.3.14f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2021.3.14f1\\Editor\\Unity.exe -projectpath C:\\Users\\UCUNI\\OneDrive\\Unity\\ML-Agents\\Aimbot-ParallelEnv -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-4ll_RrA1WPOxOo53NVlUn -hubSessionId 51847070-2fd3-4dd9-bd34-395d4d996780 -accessToken az5jhjWoFTvfMlF175YCJCcakxJHc-9-nobp1oGkD0w00ef","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"Play","end_time_seconds":"1702212236"}}
{"count":1,"self":37.9889472,"total":39.023077799999996,"children":{"InitializeActuators":{"count":2,"self":0.0015018,"total":0.0015018,"children":null},"InitializeSensors":{"count":2,"self":0.0010004999999999999,"total":0.0010004999999999999,"children":null},"AgentSendState":{"count":1687,"self":0.0150586,"total":0.5907354,"children":{"CollectObservations":{"count":1687,"self":0.0797692,"total":0.0797692,"children":null},"WriteActionMask":{"count":1687,"self":0.0030843999999999997,"total":0.0030843999999999997,"children":null},"RequestDecision":{"count":1687,"self":0.49282319999999996,"total":0.49282319999999996,"children":null}}},"DecideAction":{"count":1687,"self":0.0135447,"total":0.0135447,"children":null},"AgentAct":{"count":1687,"self":0.42734659999999997,"total":0.42734659999999997,"children":null}},"gauges":{"AKMAgent.CumulativeReward":{"count":1,"max":-2445.63965,"min":-2445.63965,"runningAverage":-2445.63965,"value":-2445.63965,"weightedAverage":-2445.63965}},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1703975981","unity_version":"2021.3.14f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2021.3.14f1\\Editor\\Unity.exe -projectpath C:\\Users\\UCUNI\\OneDrive\\Unity\\ML-Agents\\Aimbot-ParallelEnv -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-DCdlO2Cen1HBnuMk-PngQ -hubSessionId 0c43c1fc-cbb5-493c-917c-268bb9f763ac -accessToken oH33AMMXsHSxS_kgYl5oWSBhB4gjrdFpyQqpsEitl9o00ef","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"Play","end_time_seconds":"1703976020"}}

View File

@ -1 +1 @@
{"count":1,"self":205.4770944,"total":208.10505619999998,"children":{"InitializeActuators":{"count":2,"self":0.00049909999999999993,"total":0.00049909999999999993,"children":null},"InitializeSensors":{"count":2,"self":0.0010067,"total":0.0010067,"children":null},"AgentSendState":{"count":10048,"self":0.0530292,"total":0.4927436,"children":{"CollectObservations":{"count":10048,"self":0.40063,"total":0.40063,"children":null},"WriteActionMask":{"count":10048,"self":0.0085086,"total":0.0085086,"children":null},"RequestDecision":{"count":10048,"self":0.0305758,"total":0.0305758,"children":null}}},"DecideAction":{"count":10048,"self":0.0760386,"total":0.0760386,"children":null},"AgentAct":{"count":10048,"self":2.0576727999999997,"total":2.0576729,"children":null}},"gauges":{"AKMAgent.CumulativeReward":{"count":6,"max":-2525.47656,"min":-4704.462,"runningAverage":-3577.26147,"value":-3631.13135,"weightedAverage":-3725.37671}},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1700194831","unity_version":"2021.3.14f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2021.3.14f1\\Editor\\Unity.exe -projectpath C:\\Users\\UCUNI\\OneDrive\\Unity\\ML-Agents\\Aimbot-ParallelEnv -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-FDy4mXKexQ6X1xL3HWyzB -hubSessionId 8d021f9c-c18a-42ae-b0f7-4f4093f87cc2 -accessToken K_UtjVRjVp9Nhbf1MZoN5JxRN3F_Qnk7hi8pO7re9pw00ef","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"StartScene","end_time_seconds":"1700195039"}}
{"count":1,"self":102.1098624,"total":102.21653599999999,"children":{"InitializeActuators":{"count":2,"self":0.0010015,"total":0.0010015,"children":null},"InitializeSensors":{"count":2,"self":0.0014971,"total":0.0014971,"children":null},"AgentSendState":{"count":383,"self":0.0035099999999999997,"total":0.0150215,"children":{"CollectObservations":{"count":383,"self":0.0090049999999999991,"total":0.0090049999999999991,"children":null},"WriteActionMask":{"count":383,"self":0.0005009,"total":0.0005009,"children":null},"RequestDecision":{"count":383,"self":0.0020055999999999997,"total":0.0020055999999999997,"children":null}}},"DecideAction":{"count":383,"self":0.0015799,"total":0.0015799,"children":null},"AgentAct":{"count":383,"self":0.0875719,"total":0.0875719,"children":null}},"gauges":{},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1703877610","unity_version":"2021.3.14f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2021.3.14f1\\Editor\\Unity.exe -projectpath C:\\Users\\UCUNI\\OneDrive\\Unity\\ML-Agents\\Aimbot-ParallelEnv -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-DCdlO2Cen1HBnuMk-PngQ -hubSessionId 0c43c1fc-cbb5-493c-917c-268bb9f763ac -accessToken oH33AMMXsHSxS_kgYl5oWSBhB4gjrdFpyQqpsEitl9o00ef","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"StartScene","end_time_seconds":"1703877712"}}

View File

@ -1 +1 @@
{"count":1,"self":57.934591999999995,"total":63.885060599999996,"children":{"InitializeActuators":{"count":12,"self":0.001501,"total":0.001501,"children":null},"InitializeSensors":{"count":12,"self":0.0015037,"total":0.0015037,"children":null},"AgentSendState":{"count":2938,"self":0.0366666,"total":0.3843504,"children":{"CollectObservations":{"count":17628,"self":0.31354499999999996,"total":0.31354499999999996,"children":null},"WriteActionMask":{"count":17628,"self":0.0070104,"total":0.0070104,"children":null},"RequestDecision":{"count":17628,"self":0.0271284,"total":0.0271284,"children":null}}},"DecideAction":{"count":2938,"self":2.5410512,"total":2.5410512,"children":null},"AgentAct":{"count":2938,"self":3.0220654,"total":3.0220654,"children":null}},"gauges":{"AKMAgent.CumulativeReward":{"count":6,"max":-1099.64661,"min":-3249.88,"runningAverage":-2600.68726,"value":-2739.38037,"weightedAverage":-2673.02979}},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1703171035","unity_version":"2021.3.14f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2021.3.14f1\\Editor\\Unity.exe -projectpath C:\\Users\\UCUNI\\OneDrive\\Unity\\ML-Agents\\Aimbot-ParallelEnv -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-DCdlO2Cen1HBnuMk-PngQ -hubSessionId 0c43c1fc-cbb5-493c-917c-268bb9f763ac -accessToken 8py-opTGbwVFWF-QmaeXuVLWUZD3ZUdDRnQkQgsPfJU00ef","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"Train","end_time_seconds":"1703171098"}}
{"count":1,"self":14.0473248,"total":14.6998947,"children":{"InitializeActuators":{"count":12,"self":0.0010011,"total":0.0010011,"children":null},"InitializeSensors":{"count":12,"self":0.00099739999999999985,"total":0.00099739999999999985,"children":null},"AgentSendState":{"count":521,"self":0.0040127,"total":0.0661283,"children":{"CollectObservations":{"count":3126,"self":0.0551018,"total":0.0551018,"children":null},"WriteActionMask":{"count":3126,"self":0.0005002,"total":0.0005002,"children":null},"RequestDecision":{"count":3126,"self":0.0065136,"total":0.0065136,"children":null}}},"DecideAction":{"count":521,"self":0.0095329,"total":0.0095329,"children":null},"AgentAct":{"count":521,"self":0.5749103,"total":0.5749103,"children":null}},"gauges":{},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1703877312","unity_version":"2021.3.14f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2021.3.14f1\\Editor\\Unity.exe -projectpath C:\\Users\\UCUNI\\OneDrive\\Unity\\ML-Agents\\Aimbot-ParallelEnv -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-DCdlO2Cen1HBnuMk-PngQ -hubSessionId 0c43c1fc-cbb5-493c-917c-268bb9f763ac -accessToken oH33AMMXsHSxS_kgYl5oWSBhB4gjrdFpyQqpsEitl9o00ef","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"Train","end_time_seconds":"1703877326"}}

View File

@ -1755,8 +1755,6 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 34839c2831b759d4a8347ab655b00f36, type: 3}
m_Name:
m_EditorClassIdentifier:
persistThroughScene: 0
hudObj: {fileID: 2082200184}
lockMouse: 0
damage: 50
fireRate: 0.5
@ -1793,6 +1791,7 @@ MonoBehaviour:
movePenalty: 0.1
spinPenalty: 0.08
mousePenalty: 0.06
scenePrefabSet: {fileID: 11400000, guid: 6ebbd27eb466c4a41bd2584c1b9c2e1f, type: 2}
--- !u!4 &220347586
Transform:
m_ObjectHideFlags: 0
@ -3303,7 +3302,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0.000030517578, y: -246.2016}
m_AnchoredPosition: {x: 0.000030517578, y: -246.2022}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0, y: 0}
--- !u!114 &324069808
@ -12790,7 +12789,7 @@ MonoBehaviour:
m_HandleRect: {fileID: 538300382}
m_Direction: 2
m_Value: 0
m_Size: 0.99989116
m_Size: 0.99999
m_NumberOfSteps: 0
m_OnValueChanged:
m_PersistentCalls:

View File

@ -100,7 +100,7 @@ public override void CollectObservations(VectorSensor sensor)
myObserve[2] = transform.localPosition.z;
myObserve[3] = transform.eulerAngles.y / 36f;
rayTagResult = raySensors.rayTagResult;// 探测用RayTag类型结果 float[](raySensorNum,1)
rayTagResultOnehot = raySensors.rayTagResultOneHot.ToArray(); // 探测用RayTagonehot结果 List<int>[](raySensorNum*Tags,1)
rayTagResultOnehot = raySensors.rayTagResultOneHot; // 探测用RayTagonehot结果 List<int>[](raySensorNum*Tags,1)
rayDisResult = raySensors.rayDisResult; // 探测用RayDis距离结果 float[](raySensorNum,1)
targetStates = targetController.targetState; // (6) targettype, target x,y,z, firebasesAreaDiameter
remainTime = targetController.leftTime;

View File

@ -30,7 +30,7 @@ public class RaySensors : MonoBehaviour
[Header("RayCastResult")]
public float[] rayTagResult;
public List<float> rayTagResultOneHot;
public float[] rayTagResultOneHot;
public float[] rayDisResult;
[System.NonSerialized] public int totalRayNum;
@ -49,6 +49,7 @@ private void Start()
totalRayNum = halfOuterRayNum * 2 + focusRayNum;
rayTagResult = new float[totalRayNum];
rayDisResult = new float[totalRayNum];
rayTagResultOneHot = new float[totalRayNum*ObjectTags.Tags.Count];
linesOBJ = new GameObject[totalRayNum];
lineRenderers = new LineRenderer[totalRayNum];
rayInfoOBJ = new GameObject[totalRayNum];
@ -70,85 +71,60 @@ private void Start()
}
}
public int TagToInt(string tag)
{
switch (tag)
{
case "Wall":
return 1;
default:
if (tag != myTag)
{
return 2;
}
return 0;
}
}
private void SingleRaycastUpdate(Ray ray, LineRenderer lineRenderer, RayInfoUI rayInfoUI, out float rayTagResult, out float rayDisResult)
private void SingleRaycastUpdate(Ray ray,int rayIndex)
{
// get Raycast hit infomation and return Tag and distance
RaycastHit nowHit;
Color rayColor = Color.cyan;
Color rayColor = Color.gray;
float lineLength = viewDistance;
string rayInfoText = "";
Vector3 rayInfoPosition;
if (Physics.Raycast(ray, out nowHit, viewDistance)) // 若在viewDistance范围内有碰撞
{
rayInfoText = nowHit.collider.tag;
rayTagResult = TagToInt(rayInfoText);
rayTagResultOneHot.AddRange(oneHotTags.Encoder(rayInfoText));
rayDisResult = nowHit.distance;
lineLength = rayDisResult;
rayInfoText += "\n" + Convert.ToString(rayDisResult);
// rayDisResult = rayDisResult / viewDistance; // Normalization!
// 输出log
switch (rayTagResult)
// Tag
string thisTag = nowHit.collider.tag;
// result update
rayTagResult[rayIndex] = ObjectTags.TagToInt(thisTag);
rayDisResult[rayIndex] = nowHit.distance;
int oneHotIndex = rayIndex * ObjectTags.Tags.Count + ObjectTags.TagToInt(thisTag);
rayTagResultOneHot[oneHotIndex] = 1f;
if (rayTagResult[rayIndex] == ObjectTags.TagToInt("Enemy"))
{
case 1:// Wall
rayColor = Color.white;
break;
case 2: // Enemy
rayColor = Color.red;
inViewEnemies.Add(nowHit.transform.gameObject);
break;
case -1: // Hit Nothing
rayColor = Color.gray;
break;
default: // default,got wrong
rayColor = Color.cyan;
break;
}
// ingame info update
lineLength = nowHit.distance;
rayInfoText = thisTag + "\n" + Convert.ToString(nowHit.distance);
rayColor = ObjectTags.TagToCololr(thisTag);
}
else // 若在viewDistance范围无碰撞
{
rayTagResultOneHot.AddRange(oneHotTags.Encoder());
rayTagResult = -1f;
rayDisResult = -1f;
//输出log
//Debug.Log(0);
//Debug.Log(0);
// Result update
// rayTagResultOneHot keep zero
rayTagResult[rayIndex] = -1;
rayDisResult[rayIndex] = -1;
// Info
rayInfoText = "Empty";
}
// draw Info In Game
rayInfoPosition = ray.origin + (ray.direction * lineLength);
if (showInGameRay)
{
DrawLine(ray, lineLength, lineRenderer, rayColor);
DrawLine(ray, lineLength, lineRenderers[rayIndex], rayColor);
}
else
{
TurnOffLine(lineRenderer, rayColor);
TurnOffLine(lineRenderers[rayIndex], rayColor);
}
// drawRay in game
if (showInGameRayInfo) rayInfoUI.UpdateInfo(rayInfoText, rayInfoPosition, rayColor, TPSCam);
if (showInGameRayInfo) rayInfoUIs[rayIndex].UpdateInfo(rayInfoText, rayInfoPosition, rayColor, TPSCam);
// Show log
if (showDebugRay) Debug.DrawRay(ray.origin, ray.direction * viewDistance, rayColor); // drawRay in debug
// Debug.Log(ray.origin + ray.direction);
// Debug.Log(rayTagResult);
// Debug.Log(tagToInt(nowHit.collider.tag));
// Debug.Log(thisRayTagResult);
// Debug.Log(tagToInt(nowHit.collider.thisTag));
}
private void DrawLine(Ray ray, float lineLength, LineRenderer lineRenderer, Color lineColor)
@ -177,24 +153,31 @@ public void UpdateRayInfo()
float focusREdge = agentCam.pixelWidth * (1 + focusRange) / 2;
float camPixelHeight = agentCam.pixelHeight;
inViewEnemies.Clear();
rayTagResultOneHot.Clear();
ClearResult();
for (int i = 0; i < halfOuterRayNum; i++) // create left outside rays; 0 ~ focusLeftEdge
{
Vector3 point = new Vector3(i * focusLEdge / (halfOuterRayNum - 1), camPixelHeight / 2, 0);
Ray nowRay = agentCam.ScreenPointToRay(point);
SingleRaycastUpdate(nowRay, lineRenderers[i], rayInfoUIs[i], out rayTagResult[i], out rayDisResult[i]);
SingleRaycastUpdate(nowRay,i);
}
for (int i = 0; i < halfOuterRayNum; i++) // create right outside rays; focusRightEdge ~ MaxPixelHeight
{
Vector3 point = new Vector3(focusREdge + (i * focusLEdge / (halfOuterRayNum - 1)), camPixelHeight / 2, 0);
Ray nowRay = agentCam.ScreenPointToRay(point);
SingleRaycastUpdate(nowRay, lineRenderers[halfOuterRayNum + i], rayInfoUIs[halfOuterRayNum + i], out rayTagResult[halfOuterRayNum + i], out rayDisResult[halfOuterRayNum + i]);
SingleRaycastUpdate(nowRay, halfOuterRayNum + i);
}
for (int i = 0; i < focusRayNum; i++) // create center focus rays; focusLeftEdge ~ focusLeftEdge
{
Vector3 point = new Vector3(focusLEdge + ((i + 1) * (focusREdge - focusLEdge) / (focusRayNum + 1)), camPixelHeight / 2, 0);
Ray nowRay = agentCam.ScreenPointToRay(point);
SingleRaycastUpdate(nowRay, lineRenderers[halfOuterRayNum * 2 + i], rayInfoUIs[halfOuterRayNum * 2 + i], out rayTagResult[halfOuterRayNum * 2 + i], out rayDisResult[halfOuterRayNum * 2 + i]);
SingleRaycastUpdate(nowRay, halfOuterRayNum * 2 + i);
}
}
private void ClearResult()
{
rayTagResult = new float[totalRayNum];
rayDisResult = new float[totalRayNum];
rayTagResultOneHot = new float[totalRayNum * ObjectTags.Tags.Count];
}
}

View File

@ -0,0 +1,27 @@
using System.Collections.Generic;
using UnityEngine;
public static class ObjectTags
{
public static List<string> Tags = new List<string>() { "Wall", "Enemy", "Player" };
public static int TagToInt(string tag)
{
return Tags.IndexOf(tag);
}
public static Color TagToCololr(string tag)
{
switch (tag)
{
case "Wall":
return Color.white;
case "Enemy":
return Color.red;
case "Player":
return Color.green;
default:
return Color.cyan;
}
}
}

View File

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

View File

@ -9,13 +9,13 @@ EditorUserSettings:
value: 55090405535008090b5608764626074415151c79747c74602b7c1861e4b96c6c
flags: 0
RecentlyUsedSceneGuid-1:
value: 5703025650035e5d090f087148715d4443161e2c7e2c20357b281b31b0e43060
value: 06570c0704040b0e5a575520137b5e44154f192e292d22367c2c4866b7b3376f
flags: 0
RecentlyUsedSceneGuid-2:
value: 5453565f53020f085f5d0e72157a0844454f4c2b757d7265757a4f64b7b4313c
value: 5703025650035e5d090f087148715d4443161e2c7e2c20357b281b31b0e43060
flags: 0
RecentlyUsedSceneGuid-3:
value: 06570c0704040b0e5a575520137b5e44154f192e292d22367c2c4866b7b3376f
value: 5453565f53020f085f5d0e72157a0844454f4c2b757d7265757a4f64b7b4313c
flags: 0
RecentlyUsedScenePath-0:
value: 22424703114646680e0b0227036c6c111b07142f1f2b233e2867083debf42d