浏览代码
Merge remote-tracking branch 'origin/hh/develop-variableobs' into develop-var-len-obs-feature
/bullet-hell-barracuda-test-1.3.1
Merge remote-tracking branch 'origin/hh/develop-variableobs' into develop-var-len-obs-feature
/bullet-hell-barracuda-test-1.3.1
vincentpierre
4 年前
当前提交
21481e73
共有 26 个文件被更改,包括 2513 次插入 和 0 次删除
-
1Project/ProjectSettings/TagManager.asset
-
8Project/Assets/ML-Agents/Examples/Arena-Sequence.meta
-
143Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/CollisionCallbacks.cs
-
11Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/CollisionCallbacks.cs.meta
-
8Project/Assets/ML-Agents/Examples/Arena-Sequence/Scripts.meta
-
23Project/Assets/ML-Agents/Examples/Arena-Sequence/Scripts/SequenceEnvController.cs
-
11Project/Assets/ML-Agents/Examples/Arena-Sequence/Scripts/SequenceEnvController.cs.meta
-
22Project/Assets/ML-Agents/Examples/Arena-Sequence/Scripts/SequenceTile.cs
-
11Project/Assets/ML-Agents/Examples/Arena-Sequence/Scripts/SequenceTile.cs.meta
-
247Project/Assets/ML-Agents/Examples/Arena-Sequence/Scripts/SequencerAgent.cs
-
11Project/Assets/ML-Agents/Examples/Arena-Sequence/Scripts/SequencerAgent.cs.meta
-
8Project/Assets/ML-Agents/Examples/Arena-Sequence/TFModels.meta
-
8Project/Assets/ML-Agents/Examples/Arena-Sequence/Scenes.meta
-
8Project/Assets/ML-Agents/Examples/Arena-Sequence/Meshes.meta
-
63Project/Assets/ML-Agents/Examples/Arena-Sequence/Meshes/ArenaWalls.fbx
-
247Project/Assets/ML-Agents/Examples/Arena-Sequence/Meshes/ArenaWalls.fbx.meta
-
8Project/Assets/ML-Agents/Examples/Arena-Sequence/Prefabs.meta
-
1001Project/Assets/ML-Agents/Examples/Arena-Sequence/Prefabs/Area.prefab
-
7Project/Assets/ML-Agents/Examples/Arena-Sequence/Prefabs/Area.prefab.meta
-
9Project/Assets/ML-Agents/Examples/Arena-Sequence/Scenes/ArenaLearnSequence.unity.meta
-
658Project/Assets/ML-Agents/Examples/Arena-Sequence/Scenes/ArenaLearnSequence.unity
|
|||
fileFormatVersion: 2 |
|||
guid: ab46f01a215b74b588a0a3c180a88813 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using UnityEngine; |
|||
using Random = UnityEngine.Random; |
|||
using Unity.MLAgents; |
|||
using UnityEngine.Events; |
|||
|
|||
namespace Unity.MLAgentsExamples |
|||
{ |
|||
/// <summary>
|
|||
/// Utility class to allow target placement and collision detection with an agent
|
|||
/// Add this script to the target you want the agent to touch.
|
|||
/// Callbacks will be triggered any time the target is touched with a collider tagged as 'tagToDetect'
|
|||
/// </summary>
|
|||
public class CollisionCallbacks : MonoBehaviour |
|||
{ |
|||
// [System.Serializable] public class BoolEvent : UnityEvent<bool> { }
|
|||
// [SerializeField] BoolEvent boolEvent = new BoolEvent();
|
|||
// public void OnBoolEvent(bool value)
|
|||
// {
|
|||
// Debug.Log($"OnBoolEvent {value}");
|
|||
// }
|
|||
|
|||
|
|||
[Header("Collider Tag To Detect")] |
|||
public string tagToDetect = "agent"; //collider tag to detect
|
|||
|
|||
// [Header("Target Placement")]
|
|||
// public float spawnRadius; //The radius in which a target can be randomly spawned.
|
|||
// public bool respawnIfTouched; //Should the target respawn to a different position when touched
|
|||
//
|
|||
// [Header("Target Fell Protection")]
|
|||
// public bool respawnIfFallsOffPlatform = true; //If the target falls off the platform, reset the position.
|
|||
// public float fallDistance = 5; //distance below the starting height that will trigger a respawn
|
|||
//
|
|||
//
|
|||
// private Vector3 m_startingPos; //the starting position of the target
|
|||
// private Agent m_agentTouching; //the agent currently touching the target
|
|||
|
|||
[System.Serializable] |
|||
// public class TriggerEvent : UnityEvent<string>
|
|||
public class TriggerEvent : UnityEvent<Collider> |
|||
{ |
|||
} |
|||
|
|||
[Header("Trigger Callbacks")] |
|||
public TriggerEvent onTriggerEnterEvent = new TriggerEvent(); |
|||
public TriggerEvent onTriggerStayEvent = new TriggerEvent(); |
|||
public TriggerEvent onTriggerExitEvent = new TriggerEvent(); |
|||
|
|||
[System.Serializable] |
|||
public class CollisionEvent : UnityEvent<Collision, Transform> |
|||
{ |
|||
} |
|||
|
|||
[Header("Collision Callbacks")] |
|||
public CollisionEvent onCollisionEnterEvent = new CollisionEvent(); |
|||
public CollisionEvent onCollisionStayEvent = new CollisionEvent(); |
|||
public CollisionEvent onCollisionExitEvent = new CollisionEvent(); |
|||
|
|||
// // Start is called before the first frame update
|
|||
// void OnEnable()
|
|||
// {
|
|||
// m_startingPos = transform.position;
|
|||
// if (respawnIfTouched)
|
|||
// {
|
|||
// MoveTargetToRandomPosition();
|
|||
// }
|
|||
// }
|
|||
|
|||
// void Update()
|
|||
// {
|
|||
// if (respawnIfFallsOffPlatform)
|
|||
// {
|
|||
// if (transform.position.y < m_startingPos.y - fallDistance)
|
|||
// {
|
|||
// Debug.Log($"{transform.name} Fell Off Platform");
|
|||
// MoveTargetToRandomPosition();
|
|||
// }
|
|||
// }
|
|||
// }
|
|||
|
|||
// /// <summary>
|
|||
// /// Moves target to a random position within specified radius.
|
|||
// /// </summary>
|
|||
// public void MoveTargetToRandomPosition()
|
|||
// {
|
|||
// var newTargetPos = m_startingPos + (Random.insideUnitSphere * spawnRadius);
|
|||
// newTargetPos.y = m_startingPos.y;
|
|||
// transform.position = newTargetPos;
|
|||
// }
|
|||
|
|||
private void OnCollisionEnter(Collision col) |
|||
{ |
|||
if (col.transform.CompareTag(tagToDetect)) |
|||
{ |
|||
onCollisionEnterEvent.Invoke(col, transform); |
|||
// if (respawnIfTouched)
|
|||
// {
|
|||
// MoveTargetToRandomPosition();
|
|||
// }
|
|||
} |
|||
} |
|||
|
|||
private void OnCollisionStay(Collision col) |
|||
{ |
|||
if (col.transform.CompareTag(tagToDetect)) |
|||
{ |
|||
onCollisionStayEvent.Invoke(col, transform); |
|||
} |
|||
} |
|||
|
|||
private void OnCollisionExit(Collision col) |
|||
{ |
|||
if (col.transform.CompareTag(tagToDetect)) |
|||
{ |
|||
onCollisionExitEvent.Invoke(col, transform); |
|||
} |
|||
} |
|||
|
|||
private void OnTriggerEnter(Collider col) |
|||
{ |
|||
if (col.CompareTag(tagToDetect)) |
|||
{ |
|||
onTriggerEnterEvent.Invoke(col); |
|||
} |
|||
} |
|||
|
|||
private void OnTriggerStay(Collider col) |
|||
{ |
|||
if (col.CompareTag(tagToDetect)) |
|||
{ |
|||
onTriggerStayEvent.Invoke(col); |
|||
} |
|||
} |
|||
|
|||
private void OnTriggerExit(Collider col) |
|||
{ |
|||
if (col.CompareTag(tagToDetect)) |
|||
{ |
|||
onTriggerExitEvent.Invoke(col); |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: d395cc1183d7f4911902a2393004c5d9 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 7e13230b4597f444eb241e0309a786b4 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
|
|||
public class SequenceEnvController : MonoBehaviour |
|||
{ |
|||
// Start is called before the first frame update
|
|||
void Start() |
|||
{ |
|||
|
|||
} |
|||
|
|||
// Update is called once per frame
|
|||
void Update() |
|||
{ |
|||
|
|||
} |
|||
|
|||
public void TouchedTile(Collision col, Transform t) |
|||
{ |
|||
|
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 249d623e928f940b08ae5029e7ca1ab4 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
|
|||
public class SequenceTile : MonoBehaviour |
|||
{ |
|||
public int NumberValue; |
|||
|
|||
// [HideInInspector]
|
|||
public MeshRenderer rend; |
|||
// Start is called before the first frame update
|
|||
void Awake() |
|||
{ |
|||
rend = GetComponentInChildren<MeshRenderer>(); |
|||
} |
|||
|
|||
// Update is called once per frame
|
|||
void Update() |
|||
{ |
|||
|
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 03b2e6d9493cc4a92acf7f3b8b438aa4 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Net.Sockets; |
|||
using UnityEngine; |
|||
using Unity.MLAgents; |
|||
using Unity.MLAgents.Actuators; |
|||
using Unity.MLAgents.Sensors; |
|||
using Random = UnityEngine.Random; |
|||
|
|||
|
|||
public class SequencerAgent : Agent |
|||
{ |
|||
|
|||
// class TileInfo
|
|||
// {
|
|||
// public SequenceTile tile;
|
|||
// public int SpawnIndexPos;
|
|||
// }
|
|||
public bool SelectNewTiles; |
|||
|
|||
public int NumberOfTilesToSpawn; |
|||
PushBlockSettings m_PushBlockSettings; |
|||
Rigidbody m_AgentRb; //cached on initialization
|
|||
|
|||
public List<SequenceTile> SequenceTilesList = new List<SequenceTile>(); |
|||
public List<SequenceTile> CurrentlyVisibleTilesList = new List<SequenceTile>(); |
|||
private List<Transform> AlreadyTouchedList = new List<Transform>(); |
|||
|
|||
public List<int> m_UsedPositionsList = new List<int>(); |
|||
private Vector3 m_StartingPos; |
|||
|
|||
|
|||
|
|||
// private SequenceTile m_NextExpectedTile;
|
|||
public int m_NextExpectedTileIndex; |
|||
public Material TileMaterial; |
|||
public Material SuccessMaterial; |
|||
public override void Initialize() |
|||
{ |
|||
m_PushBlockSettings = FindObjectOfType<PushBlockSettings>(); |
|||
m_AgentRb = GetComponent<Rigidbody>(); |
|||
m_StartingPos = transform.position; |
|||
} |
|||
|
|||
|
|||
/// <summary>
|
|||
/// In the editor, if "Reset On Done" is checked then AgentReset() will be
|
|||
/// called automatically anytime we mark done = true in an agent script.
|
|||
/// </summary>
|
|||
public override void OnEpisodeBegin() |
|||
{ |
|||
|
|||
SelectTilesToShow(); |
|||
SetTilePositions(); |
|||
|
|||
transform.position = m_StartingPos; |
|||
m_AgentRb.velocity = Vector3.zero; |
|||
m_AgentRb.angularVelocity = Vector3.zero; |
|||
} |
|||
|
|||
|
|||
private void Update() |
|||
{ |
|||
//DEBUG
|
|||
if (SelectNewTiles) |
|||
{ |
|||
SelectNewTiles = false; |
|||
SelectTilesToShow(); |
|||
SetTilePositions(); |
|||
} |
|||
} |
|||
|
|||
|
|||
public override void CollectObservations(VectorSensor sensor) |
|||
{ |
|||
// foreach (var item in CurrentlyVisibleTilesList)
|
|||
// {
|
|||
// sensor.AddObservation(item.transform.localRotation.y / 360);
|
|||
// }
|
|||
} |
|||
|
|||
private void OnCollisionEnter(Collision col) |
|||
{ |
|||
if (!col.gameObject.CompareTag("tile")) |
|||
{ |
|||
return; |
|||
} |
|||
if (AlreadyTouchedList.Contains(col.transform)) |
|||
{ |
|||
return; |
|||
} |
|||
if (col.transform.parent != CurrentlyVisibleTilesList[m_NextExpectedTileIndex].transform) |
|||
{ |
|||
//failed
|
|||
AddReward(-1); |
|||
EndEpisode(); |
|||
print("no"); |
|||
} |
|||
else |
|||
{ |
|||
//success
|
|||
print("yes"); |
|||
AddReward(1); |
|||
var tile = col.gameObject.GetComponentInParent<SequenceTile>(); |
|||
tile.rend.sharedMaterial = SuccessMaterial; |
|||
m_NextExpectedTileIndex++; |
|||
|
|||
AlreadyTouchedList.Add(col.transform); |
|||
//We got all of them. Can reset now.
|
|||
if (m_NextExpectedTileIndex == NumberOfTilesToSpawn) |
|||
{ |
|||
EndEpisode(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
void SetTilePositions() |
|||
{ |
|||
|
|||
m_UsedPositionsList.Clear(); |
|||
|
|||
//Disable all. We will enable the ones selected
|
|||
foreach (var item in SequenceTilesList) |
|||
{ |
|||
item.gameObject.SetActive(false); |
|||
} |
|||
|
|||
|
|||
foreach (var item in CurrentlyVisibleTilesList) |
|||
{ |
|||
//Select a rnd spawnAngle
|
|||
bool posChosen = false; |
|||
int rndPosIndx = 0; |
|||
while (!posChosen) |
|||
{ |
|||
rndPosIndx = Random.Range(0, SequenceTilesList.Count); |
|||
if (!m_UsedPositionsList.Contains(rndPosIndx)) |
|||
{ |
|||
m_UsedPositionsList.Add(rndPosIndx); |
|||
posChosen = true; |
|||
} |
|||
} |
|||
item.transform.localRotation = Quaternion.Euler(0, rndPosIndx * (360f / SequenceTilesList.Count), 0); |
|||
item.rend.sharedMaterial = TileMaterial; |
|||
item.gameObject.SetActive(true); |
|||
} |
|||
} |
|||
|
|||
void SelectTilesToShow() |
|||
{ |
|||
// Shuffle(SequenceTilesList);
|
|||
// Random m_RandomTile = new Random();
|
|||
|
|||
CurrentlyVisibleTilesList.Clear(); |
|||
AlreadyTouchedList.Clear(); |
|||
|
|||
int numLeft = NumberOfTilesToSpawn; |
|||
while (numLeft > 0) |
|||
{ |
|||
int rndInt = Random.Range(0, SequenceTilesList.Count); |
|||
var tmp = SequenceTilesList[rndInt]; |
|||
if (!CurrentlyVisibleTilesList.Contains(tmp)) |
|||
{ |
|||
CurrentlyVisibleTilesList.Add(tmp); |
|||
numLeft--; |
|||
} |
|||
} |
|||
|
|||
//Sort Ascending
|
|||
CurrentlyVisibleTilesList.Sort((x, y) => x.NumberValue.CompareTo(y.NumberValue)); |
|||
// m_NextExpectedTile = CurrentlyVisibleTilesList[0];
|
|||
m_NextExpectedTileIndex = 0; |
|||
} |
|||
|
|||
|
|||
/// <summary>
|
|||
/// Moves the agent according to the selected action.
|
|||
/// </summary>
|
|||
public void MoveAgent(ActionSegment<int> act) |
|||
{ |
|||
var dirToGo = Vector3.zero; |
|||
var rotateDir = Vector3.zero; |
|||
|
|||
var action = act[0]; |
|||
|
|||
switch (action) |
|||
{ |
|||
case 1: |
|||
dirToGo = transform.forward * 1f; |
|||
break; |
|||
case 2: |
|||
dirToGo = transform.forward * -1f; |
|||
break; |
|||
case 3: |
|||
rotateDir = transform.up * 1f; |
|||
break; |
|||
case 4: |
|||
rotateDir = transform.up * -1f; |
|||
break; |
|||
case 5: |
|||
dirToGo = transform.right * -0.75f; |
|||
break; |
|||
case 6: |
|||
dirToGo = transform.right * 0.75f; |
|||
break; |
|||
} |
|||
transform.Rotate(rotateDir, Time.fixedDeltaTime * 200f); |
|||
m_AgentRb.AddForce(dirToGo * m_PushBlockSettings.agentRunSpeed, |
|||
ForceMode.VelocityChange); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Called every step of the engine. Here the agent takes an action.
|
|||
/// </summary>
|
|||
public override void OnActionReceived(ActionBuffers actionBuffers) |
|||
|
|||
{ |
|||
// Move the agent using the action.
|
|||
MoveAgent(actionBuffers.DiscreteActions); |
|||
|
|||
// Penalty given each step to encourage agent to finish task quickly.
|
|||
AddReward(-1f / MaxStep); |
|||
} |
|||
|
|||
public override void Heuristic(in ActionBuffers actionsOut) |
|||
{ |
|||
var discreteActionsOut = actionsOut.DiscreteActions; |
|||
discreteActionsOut[0] = 0; |
|||
if (Input.GetKey(KeyCode.D)) |
|||
{ |
|||
discreteActionsOut[0] = 3; |
|||
} |
|||
else if (Input.GetKey(KeyCode.W)) |
|||
{ |
|||
discreteActionsOut[0] = 1; |
|||
} |
|||
else if (Input.GetKey(KeyCode.A)) |
|||
{ |
|||
discreteActionsOut[0] = 4; |
|||
} |
|||
else if (Input.GetKey(KeyCode.S)) |
|||
{ |
|||
discreteActionsOut[0] = 2; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 107ccb3d53379468eb5ba1b7ac443919 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: bff21b61cb59d45ef929ade44ddd0a28 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 4c130af3da8f146a795356f021688b89 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 21f2df9a3b371479883c5f6a9c1f5314 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
Kaydara FBX Binary � M FBXHeaderExtension\ FBXHeaderVersionI� x |
|||
FBXVersionI� � EncryptionTypeI � CreationTimeStamp� VersionI� � YearI� � MonthI DayI ' HourI ? MinuteI W SecondI t MillisecondI5 � . CreatorS) Blender (stable FBX IO) - 2.91.0 - 4.21.3@ ' |