浏览代码

init

/hh-develop-pushblockcollab
HH 4 年前
当前提交
ae14f170
共有 9 个文件被更改,包括 1645 次插入0 次删除
  1. 1
      Project/Assets/ML-Agents/Examples/Hallway/TFModels/HallwayCollab.onnx.meta
  2. 1001
      Project/Assets/ML-Agents/Examples/PushBlock/Scenes/PushBlockCollab.unity
  3. 9
      Project/Assets/ML-Agents/Examples/PushBlock/Scenes/PushBlockCollab.unity.meta
  4. 255
      Project/Assets/ML-Agents/Examples/PushBlock/Scripts/PushAgentCollab.cs
  5. 12
      Project/Assets/ML-Agents/Examples/PushBlock/Scripts/PushAgentCollab.cs.meta
  6. 202
      Project/Assets/ML-Agents/Examples/PushBlock/Scripts/PushBlockEnvController.cs
  7. 11
      Project/Assets/ML-Agents/Examples/PushBlock/Scripts/PushBlockEnvController.cs.meta
  8. 143
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/CollisionCallbacks.cs
  9. 11
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/CollisionCallbacks.cs.meta

1
Project/Assets/ML-Agents/Examples/Hallway/TFModels/HallwayCollab.onnx.meta


fileIDToRecycleName:
11400000: main obj
11400002: model data
2186277476908879412: ImportLogs
externalObjects: {}
userData:
assetBundleName:

1001
Project/Assets/ML-Agents/Examples/PushBlock/Scenes/PushBlockCollab.unity
文件差异内容过多而无法显示
查看文件

9
Project/Assets/ML-Agents/Examples/PushBlock/Scenes/PushBlockCollab.unity.meta


fileFormatVersion: 2
guid: b9c81cbb0f0ac40649eddfef0971e81b
timeCreated: 1506808980
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

255
Project/Assets/ML-Agents/Examples/PushBlock/Scripts/PushAgentCollab.cs


//Put this script on your blue cube.
using System.Collections;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Actuators;
public class PushAgentCollab : Agent
{
// /// <summary>
// /// The ground. The bounds are used to spawn the elements.
// /// </summary>
// public GameObject ground;
//
// public GameObject area;
//
// /// <summary>
// /// The area bounds.
// /// </summary>
// [HideInInspector]
// public Bounds areaBounds;
PushBlockSettings m_PushBlockSettings;
/// <summary>
/// The goal to push the block to.
/// </summary>
public GameObject goal;
/// <summary>
/// The block to be pushed to the goal.
/// </summary>
public GameObject block;
/// <summary>
/// Detects when the block touches the goal.
/// </summary>
[HideInInspector]
public GoalDetect goalDetect;
public bool useVectorObs;
// Rigidbody m_BlockRb; //cached on initialization
Rigidbody m_AgentRb; //cached on initialization
// Material m_GroundMaterial; //cached on Awake()
//
// /// <summary>
// /// We will be changing the ground material based on success/failue
// /// </summary>
// Renderer m_GroundRenderer;
// EnvironmentParameters m_ResetParams;
void Awake()
{
m_PushBlockSettings = FindObjectOfType<PushBlockSettings>();
}
public override void Initialize()
{
goalDetect = block.GetComponent<GoalDetect>();
// goalDetect.agent = this;
// Cache the agent rigidbody
m_AgentRb = GetComponent<Rigidbody>();
// Cache the block rigidbody
// m_BlockRb = block.GetComponent<Rigidbody>();
// // Get the ground's bounds
// areaBounds = ground.GetComponent<Collider>().bounds;
// // Get the ground renderer so we can change the material when a goal is scored
// m_GroundRenderer = ground.GetComponent<Renderer>();
// // Starting material
// m_GroundMaterial = m_GroundRenderer.material;
// m_ResetParams = Academy.Instance.EnvironmentParameters;
// SetResetParameters();
}
// /// <summary>
// /// Use the ground's bounds to pick a random spawn position.
// /// </summary>
// public Vector3 GetRandomSpawnPos()
// {
// var foundNewSpawnLocation = false;
// var randomSpawnPos = Vector3.zero;
// while (foundNewSpawnLocation == false)
// {
// var randomPosX = Random.Range(-areaBounds.extents.x * m_PushBlockSettings.spawnAreaMarginMultiplier,
// areaBounds.extents.x * m_PushBlockSettings.spawnAreaMarginMultiplier);
//
// var randomPosZ = Random.Range(-areaBounds.extents.z * m_PushBlockSettings.spawnAreaMarginMultiplier,
// areaBounds.extents.z * m_PushBlockSettings.spawnAreaMarginMultiplier);
// randomSpawnPos = ground.transform.position + new Vector3(randomPosX, 1f, randomPosZ);
// if (Physics.CheckBox(randomSpawnPos, new Vector3(2.5f, 0.01f, 2.5f)) == false)
// {
// foundNewSpawnLocation = true;
// }
// }
// return randomSpawnPos;
// }
// /// <summary>
// /// Called when the agent moves the block into the goal.
// /// </summary>
// public void ScoredAGoal()
// {
// // We use a reward of 5.
// AddReward(5f);
//
// // By marking an agent as done AgentReset() will be called automatically.
// EndEpisode();
//
// // Swap ground material for a bit to indicate we scored.
// StartCoroutine(GoalScoredSwapGroundMaterial(m_PushBlockSettings.goalScoredMaterial, 0.5f));
// }
// /// <summary>
// /// Swap ground material, wait time seconds, then swap back to the regular material.
// /// </summary>
// IEnumerator GoalScoredSwapGroundMaterial(Material mat, float time)
// {
// m_GroundRenderer.material = mat;
// yield return new WaitForSeconds(time); // Wait for 2 sec
// m_GroundRenderer.material = m_GroundMaterial;
// }
/// <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;
}
}
// /// <summary>
// /// Resets the block position and velocities.
// /// </summary>
// void ResetBlock()
// {
// // Get a random position for the block.
// block.transform.position = GetRandomSpawnPos();
//
// // Reset block velocity back to zero.
// m_BlockRb.velocity = Vector3.zero;
//
// // Reset block angularVelocity back to zero.
// m_BlockRb.angularVelocity = Vector3.zero;
// }
/// <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()
{
// var rotation = Random.Range(0, 4);
// var rotationAngle = rotation * 90f;
// area.transform.Rotate(new Vector3(0f, rotationAngle, 0f));
//
// ResetBlock();
// transform.position = GetRandomSpawnPos();
// m_AgentRb.velocity = Vector3.zero;
// m_AgentRb.angularVelocity = Vector3.zero;
// SetResetParameters();
}
// public void SetGroundMaterialFriction()
// {
// var groundCollider = ground.GetComponent<Collider>();
//
// groundCollider.material.dynamicFriction = m_ResetParams.GetWithDefault("dynamic_friction", 0);
// groundCollider.material.staticFriction = m_ResetParams.GetWithDefault("static_friction", 0);
// }
//
// public void SetBlockProperties()
// {
// var scale = m_ResetParams.GetWithDefault("block_scale", 2);
// //Set the scale of the block
// m_BlockRb.transform.localScale = new Vector3(scale, 0.75f, scale);
//
// // Set the drag of the block
// m_BlockRb.drag = m_ResetParams.GetWithDefault("block_drag", 0.5f);
// }
//
// void SetResetParameters()
// {
// SetGroundMaterialFriction();
// SetBlockProperties();
// }
}

12
Project/Assets/ML-Agents/Examples/PushBlock/Scripts/PushAgentCollab.cs.meta


fileFormatVersion: 2
guid: d94a85eca2e074578943301959c555ba
timeCreated: 1506829537
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

202
Project/Assets/ML-Agents/Examples/PushBlock/Scripts/PushBlockEnvController.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PushBlockEnvController : MonoBehaviour
{
[System.Serializable]
public class AgentInfo
{
public PushAgentCollab Agent;
public Vector3 StartingPos;
public Quaternion StartingRot;
public Rigidbody Rb;
}
[System.Serializable]
public class BlockInfo
{
public Transform T;
public Vector3 StartingPos;
public Quaternion StartingRot;
public Rigidbody Rb;
}
/// <summary>
/// The area bounds.
/// </summary>
[HideInInspector]
public Bounds areaBounds;
/// <summary>
/// The ground. The bounds are used to spawn the elements.
/// </summary>
public GameObject ground;
public GameObject area;
Material m_GroundMaterial; //cached on Awake()
/// <summary>
/// We will be changing the ground material based on success/failue
/// </summary>
Renderer m_GroundRenderer;
public List<AgentInfo> AgentsList = new List<AgentInfo>();
public List<BlockInfo> BlocksList = new List<BlockInfo>();
public bool UseRandomAgentRotation = true;
public bool UseRandomAgentPosition = true;
public bool UseRandomBlockRotation = true;
public bool UseRandomBlockPosition = true;
PushBlockSettings m_PushBlockSettings;
private int m_NumberOfRemainingBlocks;
void Awake()
{
m_NumberOfRemainingBlocks = BlocksList.Count;
// Get the ground's bounds
areaBounds = ground.GetComponent<Collider>().bounds;
// Get the ground renderer so we can change the material when a goal is scored
m_GroundRenderer = ground.GetComponent<Renderer>();
// Starting material
m_GroundMaterial = m_GroundRenderer.material;
m_PushBlockSettings = FindObjectOfType<PushBlockSettings>();
foreach (var item in BlocksList)
{
item.StartingPos = item.T.transform.position;
item.StartingRot = item.T.transform.rotation;
item.Rb = item.T.GetComponent<Rigidbody>();
}
foreach (var item in AgentsList)
{
item.StartingPos = item.Agent.transform.position;
item.StartingRot = item.Agent.transform.rotation;
item.Rb = item.Agent.GetComponent<Rigidbody>();
}
ResetScene();
}
// Update is called once per frame
void Update()
{
}
/// <summary>
/// Use the ground's bounds to pick a random spawn position.
/// </summary>
public Vector3 GetRandomSpawnPos()
{
var foundNewSpawnLocation = false;
var randomSpawnPos = Vector3.zero;
while (foundNewSpawnLocation == false)
{
var randomPosX = Random.Range(-areaBounds.extents.x * m_PushBlockSettings.spawnAreaMarginMultiplier,
areaBounds.extents.x * m_PushBlockSettings.spawnAreaMarginMultiplier);
var randomPosZ = Random.Range(-areaBounds.extents.z * m_PushBlockSettings.spawnAreaMarginMultiplier,
areaBounds.extents.z * m_PushBlockSettings.spawnAreaMarginMultiplier);
randomSpawnPos = ground.transform.position + new Vector3(randomPosX, 1f, randomPosZ);
if (Physics.CheckBox(randomSpawnPos, new Vector3(2.5f, 0.01f, 2.5f)) == false)
{
foundNewSpawnLocation = true;
}
}
return randomSpawnPos;
}
/// <summary>
/// Resets the block position and velocities.
/// </summary>
void ResetBlock(BlockInfo block)
{
// Get a random position for the block.
block.T.position = GetRandomSpawnPos();
// Reset block velocity back to zero.
block.Rb.velocity = Vector3.zero;
// Reset block angularVelocity back to zero.
block.Rb.angularVelocity = Vector3.zero;
}
/// <summary>
/// Swap ground material, wait time seconds, then swap back to the regular material.
/// </summary>
IEnumerator GoalScoredSwapGroundMaterial(Material mat, float time)
{
m_GroundRenderer.material = mat;
yield return new WaitForSeconds(time); // Wait for 2 sec
m_GroundRenderer.material = m_GroundMaterial;
}
/// <summary>
/// Called when the agent moves the block into the goal.
/// </summary>
public void ScoredAGoal(float f)
{
m_NumberOfRemainingBlocks--;
bool done = m_NumberOfRemainingBlocks == 0;
if (m_NumberOfRemainingBlocks == 0) //done
{
}
else
{
}
//Give Agent Rewards
foreach (var item in AgentsList)
{
item.Agent.AddReward(5f);
item.Agent.EndEpisode();
}
// Swap ground material for a bit to indicate we scored.
StartCoroutine(GoalScoredSwapGroundMaterial(m_PushBlockSettings.goalScoredMaterial, 0.5f));
//Reset assets
ResetScene();
}
Quaternion GetRandomRot()
{
return Quaternion.Euler(0, Random.Range(0.0f, 360.0f), 0);
}
void ResetScene()
{
//Random platform rot
area.transform.rotation = GetRandomRot();
//Reset Agents
foreach (var item in AgentsList)
{
var pos = UseRandomAgentPosition ? GetRandomSpawnPos() : item.StartingPos;
var rot = UseRandomAgentRotation ? GetRandomRot() : item.StartingRot;
item.Agent.transform.SetPositionAndRotation(pos, rot);
item.Rb.velocity = Vector3.zero;
item.Rb.angularVelocity = Vector3.zero;
}
//Reset Blocks
foreach (var item in BlocksList)
{
var pos = UseRandomBlockPosition ? GetRandomSpawnPos() : item.StartingPos;
var rot = UseRandomBlockRotation ? GetRandomRot() : item.StartingRot;
item.T.transform.SetPositionAndRotation(pos, rot);
item.Rb.velocity = Vector3.zero;
item.Rb.angularVelocity = Vector3.zero;
}
}
}

11
Project/Assets/ML-Agents/Examples/PushBlock/Scripts/PushBlockEnvController.cs.meta


fileFormatVersion: 2
guid: 4bb2bb36bb51d452ab58d30a868dfab3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

143
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/CollisionCallbacks.cs


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>
{
}
[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);
// if (respawnIfTouched)
// {
// MoveTargetToRandomPosition();
// }
}
}
private void OnCollisionStay(Collision col)
{
if (col.transform.CompareTag(tagToDetect))
{
onCollisionStayEvent.Invoke(col);
}
}
private void OnCollisionExit(Collision col)
{
if (col.transform.CompareTag(tagToDetect))
{
onCollisionExitEvent.Invoke(col);
}
}
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);
// }
// }
}
}

11
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/CollisionCallbacks.cs.meta


fileFormatVersion: 2
guid: 506de7b261f374159a5cdfdb0ff48c0c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存