浏览代码
Namespacification (#814)
Namespacification (#814)
* [Namespace created] Added the namespace MLAgents on the C# scripts/develop-generalizationTraining-TrainerController
Arthur Juliani
7 年前
当前提交
d4a2df66
共有 76 个文件被更改,包括 2966 次插入 和 2532 次删除
-
130unity-environment/Assets/ML-Agents/Editor/AgentEditor.cs
-
159unity-environment/Assets/ML-Agents/Editor/BrainEditor.cs
-
2unity-environment/Assets/ML-Agents/Editor/MLAgentsEditModeTest.cs
-
199unity-environment/Assets/ML-Agents/Editor/ResetParameterDrawer.cs
-
1unity-environment/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DAcademy.cs
-
1unity-environment/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DAgent.cs
-
1unity-environment/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DDecision.cs
-
1unity-environment/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DHardAgent.cs
-
1unity-environment/Assets/ML-Agents/Examples/BananaCollectors/Scripts/BananaAcademy.cs
-
1unity-environment/Assets/ML-Agents/Examples/BananaCollectors/Scripts/BananaAgent.cs
-
1unity-environment/Assets/ML-Agents/Examples/BananaCollectors/Scripts/BananaArea.cs
-
1unity-environment/Assets/ML-Agents/Examples/Basic/Scripts/BasicAcademy.cs
-
1unity-environment/Assets/ML-Agents/Examples/Basic/Scripts/BasicAgent.cs
-
1unity-environment/Assets/ML-Agents/Examples/Basic/Scripts/BasicDecision.cs
-
1unity-environment/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAcademy.cs
-
1unity-environment/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAgent.cs
-
1unity-environment/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerBanana.cs
-
1unity-environment/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAcademy.cs
-
1unity-environment/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAgentConfigurable.cs
-
1unity-environment/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAcademy.cs
-
1unity-environment/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAgent.cs
-
1unity-environment/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayAcademy.cs
-
1unity-environment/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayAgent.cs
-
1unity-environment/Assets/ML-Agents/Examples/PushBlock/Scripts/PushAgentBasic.cs
-
1unity-environment/Assets/ML-Agents/Examples/PushBlock/Scripts/PushBlockAcademy.cs
-
1unity-environment/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidAcademy.cs
-
1unity-environment/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidAgent.cs
-
1unity-environment/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidArea.cs
-
1unity-environment/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherAcademy.cs
-
1unity-environment/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherAgent.cs
-
1unity-environment/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherDecision.cs
-
32unity-environment/Assets/ML-Agents/Examples/SharedAssets/Scripts/Area.cs
-
34unity-environment/Assets/ML-Agents/Examples/SharedAssets/Scripts/CameraFollow.cs
-
183unity-environment/Assets/ML-Agents/Examples/SharedAssets/Scripts/FlyCamera.cs
-
70unity-environment/Assets/ML-Agents/Examples/SharedAssets/Scripts/GroundContact.cs
-
71unity-environment/Assets/ML-Agents/Examples/SharedAssets/Scripts/RandomDecision.cs
-
130unity-environment/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception.cs
-
1unity-environment/Assets/ML-Agents/Examples/Soccer/Scripts/AgentSoccer.cs
-
1unity-environment/Assets/ML-Agents/Examples/Soccer/Scripts/SoccerAcademy.cs
-
1unity-environment/Assets/ML-Agents/Examples/Template/Scripts/TemplateAcademy.cs
-
1unity-environment/Assets/ML-Agents/Examples/Template/Scripts/TemplateAgent.cs
-
1unity-environment/Assets/ML-Agents/Examples/Template/Scripts/TemplateDecision.cs
-
1unity-environment/Assets/ML-Agents/Examples/Tennis/Scripts/TennisAcademy.cs
-
1unity-environment/Assets/ML-Agents/Examples/Tennis/Scripts/TennisAgent.cs
-
1unity-environment/Assets/ML-Agents/Examples/Walker/Scripts/WalkerAcademy.cs
-
1unity-environment/Assets/ML-Agents/Examples/Walker/Scripts/WalkerAgent.cs
-
1unity-environment/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpAcademy.cs
-
1unity-environment/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpAgent.cs
-
992unity-environment/Assets/ML-Agents/Scripts/Academy.cs
-
998unity-environment/Assets/ML-Agents/Scripts/Agent.cs
-
85unity-environment/Assets/ML-Agents/Scripts/BCTeacherHelper.cs
-
339unity-environment/Assets/ML-Agents/Scripts/Brain.cs
-
26unity-environment/Assets/ML-Agents/Scripts/CoreBrain.cs
-
79unity-environment/Assets/ML-Agents/Scripts/CoreBrainExternal.cs
-
133unity-environment/Assets/ML-Agents/Scripts/CoreBrainHeuristic.cs
-
322unity-environment/Assets/ML-Agents/Scripts/CoreBrainInternal.cs
-
228unity-environment/Assets/ML-Agents/Scripts/CoreBrainPlayer.cs
-
87unity-environment/Assets/ML-Agents/Scripts/Decision.cs
-
835unity-environment/Assets/ML-Agents/Scripts/Monitor.cs
-
66unity-environment/Assets/ML-Agents/Scripts/ResetParameters.cs
-
30unity-environment/Assets/ML-Agents/Scripts/UnityAgentsException.cs
-
54unity-environment/Assets/PlayGround/Communicator/Communicatror_Test.cs
-
54unity-environment/Assets/PlayGround/Docker/DockerTest.cs
-
18unity-environment/Assets/PlayGround/PersistentSingleton/Editor/SingletonWindow.cs
-
18unity-environment/Assets/PlayGround/PersistentSingleton/SingletonTest.cs
-
18unity-environment/Assets/PlayGround/PersistentStatic/Editor/StaticWindow.cs
-
31unity-environment/Assets/PlayGround/PersistentStatic/StaticTest.cs
-
37unity-environment/Assets/PlayGround/TensorflowTest/TensorflowTester.cs
|
|||
using UnityEngine; |
|||
using UnityEditor; |
|||
|
|||
namespace MLAgents |
|||
{ |
|||
[CustomEditor(typeof(Agent), true)] |
|||
[CanEditMultipleObjects] |
|||
public class AgentEditor : Editor |
|||
{ |
|||
|
|||
public override void OnInspectorGUI() |
|||
[CustomEditor(typeof(Agent), true)] |
|||
[CanEditMultipleObjects] |
|||
public class AgentEditor : Editor |
|||
SerializedObject serializedAgent = serializedObject; |
|||
serializedAgent.Update(); |
|||
SerializedProperty brain = serializedAgent.FindProperty("brain"); |
|||
SerializedProperty actionsPerDecision = serializedAgent.FindProperty( |
|||
"agentParameters.numberOfActionsBetweenDecisions"); |
|||
SerializedProperty maxSteps = serializedAgent.FindProperty( |
|||
"agentParameters.maxStep"); |
|||
SerializedProperty isResetOnDone = serializedAgent.FindProperty( |
|||
"agentParameters.resetOnDone"); |
|||
SerializedProperty isODD = serializedAgent.FindProperty( |
|||
"agentParameters.onDemandDecision"); |
|||
SerializedProperty cameras = serializedAgent.FindProperty( |
|||
"agentParameters.agentCameras"); |
|||
public override void OnInspectorGUI() |
|||
{ |
|||
SerializedObject serializedAgent = serializedObject; |
|||
serializedAgent.Update(); |
|||
EditorGUILayout.PropertyField(brain); |
|||
SerializedProperty brain = serializedAgent.FindProperty("brain"); |
|||
SerializedProperty actionsPerDecision = serializedAgent.FindProperty( |
|||
"agentParameters.numberOfActionsBetweenDecisions"); |
|||
SerializedProperty maxSteps = serializedAgent.FindProperty( |
|||
"agentParameters.maxStep"); |
|||
SerializedProperty isResetOnDone = serializedAgent.FindProperty( |
|||
"agentParameters.resetOnDone"); |
|||
SerializedProperty isODD = serializedAgent.FindProperty( |
|||
"agentParameters.onDemandDecision"); |
|||
SerializedProperty cameras = serializedAgent.FindProperty( |
|||
"agentParameters.agentCameras"); |
|||
EditorGUILayout.LabelField("Agent Cameras"); |
|||
for (int i = 0; i < cameras.arraySize; i++) |
|||
{ |
|||
EditorGUILayout.PropertyField( |
|||
cameras.GetArrayElementAtIndex(i), |
|||
new GUIContent("Camera " + (i + 1).ToString() + ": ")); |
|||
} |
|||
EditorGUILayout.BeginHorizontal(); |
|||
if (GUILayout.Button("Add Camera", EditorStyles.miniButton)) |
|||
{ |
|||
cameras.arraySize++; |
|||
} |
|||
if (GUILayout.Button("Remove Camera", EditorStyles.miniButton)) |
|||
{ |
|||
cameras.arraySize--; |
|||
} |
|||
EditorGUILayout.EndHorizontal(); |
|||
EditorGUILayout.PropertyField(brain); |
|||
EditorGUILayout.PropertyField( |
|||
maxSteps, |
|||
new GUIContent( |
|||
"Max Step", "The per-agent maximum number of steps.")); |
|||
EditorGUILayout.PropertyField( |
|||
isResetOnDone, |
|||
new GUIContent( |
|||
"Reset On Done", |
|||
"If checked, the agent will reset on done. Else, AgentOnDone() will be called.")); |
|||
EditorGUILayout.PropertyField( |
|||
isODD, |
|||
new GUIContent( |
|||
"On Demand Decisions", |
|||
"If checked, you must manually request decisions.")); |
|||
if (!isODD.boolValue) |
|||
{ |
|||
EditorGUILayout.LabelField("Agent Cameras"); |
|||
for (int i = 0; i < cameras.arraySize; i++) |
|||
{ |
|||
EditorGUILayout.PropertyField( |
|||
cameras.GetArrayElementAtIndex(i), |
|||
new GUIContent("Camera " + (i + 1).ToString() + ": ")); |
|||
} |
|||
|
|||
EditorGUILayout.BeginHorizontal(); |
|||
if (GUILayout.Button("Add Camera", EditorStyles.miniButton)) |
|||
{ |
|||
cameras.arraySize++; |
|||
} |
|||
|
|||
if (GUILayout.Button("Remove Camera", EditorStyles.miniButton)) |
|||
{ |
|||
cameras.arraySize--; |
|||
} |
|||
|
|||
EditorGUILayout.EndHorizontal(); |
|||
|
|||
EditorGUILayout.PropertyField( |
|||
maxSteps, |
|||
new GUIContent( |
|||
"Max Step", "The per-agent maximum number of steps.")); |
|||
EditorGUILayout.PropertyField( |
|||
isResetOnDone, |
|||
new GUIContent( |
|||
"Reset On Done", |
|||
"If checked, the agent will reset on done. Else, AgentOnDone() will be called.")); |
|||
actionsPerDecision, |
|||
isODD, |
|||
"Decision Frequency", |
|||
"The agent will automatically request a decision every X" + |
|||
" steps and perform an action at every step.")); |
|||
actionsPerDecision.intValue = Mathf.Max(1, actionsPerDecision.intValue); |
|||
} |
|||
"On Demand Decisions", |
|||
"If checked, you must manually request decisions.")); |
|||
if (!isODD.boolValue) |
|||
{ |
|||
EditorGUILayout.PropertyField( |
|||
actionsPerDecision, |
|||
new GUIContent( |
|||
"Decision Frequency", |
|||
"The agent will automatically request a decision every X" + |
|||
" steps and perform an action at every step.")); |
|||
actionsPerDecision.intValue = Mathf.Max(1, actionsPerDecision.intValue); |
|||
} |
|||
serializedAgent.ApplyModifiedProperties(); |
|||
serializedAgent.ApplyModifiedProperties(); |
|||
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider); |
|||
base.OnInspectorGUI(); |
|||
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider); |
|||
base.OnInspectorGUI(); |
|||
} |
|||
} |
|||
} |
|
|||
using UnityEngine; |
|||
using System.Collections; |
|||
|
|||
public class FlyCamera : MonoBehaviour |
|||
namespace MLAgents |
|||
/* |
|||
wasd : basic movement |
|||
shift : Makes camera accelerate |
|||
space : Moves camera on X and Z axis only. So camera doesn't gain any height*/ |
|||
public class FlyCamera : MonoBehaviour |
|||
{ |
|||
/* |
|||
wasd : basic movement |
|||
shift : Makes camera accelerate |
|||
space : Moves camera on X and Z axis only. So camera doesn't gain any height*/ |
|||
public float mainSpeed = 100.0f; // regular speed
|
|||
public float shiftAdd = 250.0f; // multiplied by how long shift is held. Basically running
|
|||
public float maxShift = 1000.0f; // Maximum speed when holdin gshift
|
|||
public float camSens = 0.25f; // How sensitive it with mouse
|
|||
public bool rotateOnlyIfMousedown = true; |
|||
public bool movementStaysFlat = true; |
|||
private Vector3 lastMouse = new Vector3(255, 255, 255); // kind of in the middle of the screen, rather than at the top (play)
|
|||
private float totalRun = 1.0f; |
|||
|
|||
void Awake() |
|||
{ |
|||
Debug.Log("FlyCamera Awake() - RESETTING CAMERA POSITION"); // nop?
|
|||
// nop:
|
|||
// transform.position.Set(0,8,-32);
|
|||
// transform.rotation.Set(15,0,0,1);
|
|||
transform.position = new Vector3(0, 8, -32); |
|||
transform.rotation = Quaternion.Euler(25, 0, 0); |
|||
} |
|||
public float mainSpeed = 100.0f; // regular speed
|
|||
public float shiftAdd = 250.0f; // multiplied by how long shift is held. Basically running
|
|||
public float maxShift = 1000.0f; // Maximum speed when holdin gshift
|
|||
public float camSens = 0.25f; // How sensitive it with mouse
|
|||
public bool rotateOnlyIfMousedown = true; |
|||
public bool movementStaysFlat = true; |
|||
private Vector3 |
|||
lastMouse = |
|||
new Vector3(255, 255, |
|||
255); // kind of in the middle of the screen, rather than at the top (play)
|
|||
void Update() |
|||
{ |
|||
private float totalRun = 1.0f; |
|||
if (Input.GetMouseButtonDown(1)) |
|||
void Awake() |
|||
lastMouse = Input.mousePosition; // $CTK reset when we begin
|
|||
Debug.Log("FlyCamera Awake() - RESETTING CAMERA POSITION"); // nop?
|
|||
// nop:
|
|||
// transform.position.Set(0,8,-32);
|
|||
// transform.rotation.Set(15,0,0,1);
|
|||
transform.position = new Vector3(0, 8, -32); |
|||
transform.rotation = Quaternion.Euler(25, 0, 0); |
|||
if (!rotateOnlyIfMousedown || |
|||
(rotateOnlyIfMousedown && Input.GetMouseButton(1))) |
|||
|
|||
void Update() |
|||
lastMouse = Input.mousePosition - lastMouse; |
|||
lastMouse = new Vector3(-lastMouse.y * camSens, lastMouse.x * camSens, 0); |
|||
lastMouse = new Vector3(transform.eulerAngles.x + lastMouse.x, transform.eulerAngles.y + lastMouse.y, 0); |
|||
transform.eulerAngles = lastMouse; |
|||
lastMouse = Input.mousePosition; |
|||
// Mouse camera angle done.
|
|||
} |
|||
|
|||
if (Input.GetMouseButtonDown(1)) |
|||
{ |
|||
lastMouse = Input.mousePosition; // $CTK reset when we begin
|
|||
} |
|||
|
|||
if (!rotateOnlyIfMousedown || |
|||
(rotateOnlyIfMousedown && Input.GetMouseButton(1))) |
|||
{ |
|||
lastMouse = Input.mousePosition - lastMouse; |
|||
lastMouse = new Vector3(-lastMouse.y * camSens, lastMouse.x * camSens, 0); |
|||
lastMouse = new Vector3(transform.eulerAngles.x + lastMouse.x, |
|||
transform.eulerAngles.y + lastMouse.y, 0); |
|||
transform.eulerAngles = lastMouse; |
|||
lastMouse = Input.mousePosition; |
|||
// Mouse camera angle done.
|
|||
} |
|||
|
|||
// Keyboard commands
|
|||
Vector3 p = GetBaseInput(); |
|||
if (Input.GetKey(KeyCode.LeftShift)) |
|||
{ |
|||
totalRun += Time.deltaTime; |
|||
p = p * totalRun * shiftAdd; |
|||
p.x = Mathf.Clamp(p.x, -maxShift, maxShift); |
|||
p.y = Mathf.Clamp(p.y, -maxShift, maxShift); |
|||
p.z = Mathf.Clamp(p.z, -maxShift, maxShift); |
|||
} |
|||
else |
|||
{ |
|||
totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f); |
|||
p = p * mainSpeed; |
|||
} |
|||
|
|||
p = p * Time.deltaTime; |
|||
Vector3 newPosition = transform.position; |
|||
if (Input.GetKey(KeyCode.Space) |
|||
|| (movementStaysFlat && !(rotateOnlyIfMousedown && Input.GetMouseButton(1)))) |
|||
{ |
|||
// If player wants to move on X and Z axis only
|
|||
transform.Translate(p); |
|||
newPosition.x = transform.position.x; |
|||
newPosition.z = transform.position.z; |
|||
transform.position = newPosition; |
|||
} |
|||
else |
|||
{ |
|||
transform.Translate(p); |
|||
} |
|||
// Keyboard commands
|
|||
Vector3 p = GetBaseInput(); |
|||
if (Input.GetKey(KeyCode.LeftShift)) |
|||
{ |
|||
totalRun += Time.deltaTime; |
|||
p = p * totalRun * shiftAdd; |
|||
p.x = Mathf.Clamp(p.x, -maxShift, maxShift); |
|||
p.y = Mathf.Clamp(p.y, -maxShift, maxShift); |
|||
p.z = Mathf.Clamp(p.z, -maxShift, maxShift); |
|||
else |
|||
|
|||
private Vector3 GetBaseInput() |
|||
totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f); |
|||
p = p * mainSpeed; |
|||
} |
|||
// returns the basic values, if it's 0 than it's not active.
|
|||
Vector3 p_Velocity = new Vector3(); |
|||
if (Input.GetKey(KeyCode.W)) |
|||
{ |
|||
p_Velocity += new Vector3(0, 0, 1); |
|||
} |
|||
|
|||
if (Input.GetKey(KeyCode.S)) |
|||
{ |
|||
p_Velocity += new Vector3(0, 0, -1); |
|||
} |
|||
p = p * Time.deltaTime; |
|||
Vector3 newPosition = transform.position; |
|||
if (Input.GetKey(KeyCode.Space) |
|||
|| (movementStaysFlat && !(rotateOnlyIfMousedown && Input.GetMouseButton(1)))) |
|||
{ // If player wants to move on X and Z axis only
|
|||
transform.Translate(p); |
|||
newPosition.x = transform.position.x; |
|||
newPosition.z = transform.position.z; |
|||
transform.position = newPosition; |
|||
} |
|||
else |
|||
{ |
|||
transform.Translate(p); |
|||
} |
|||
if (Input.GetKey(KeyCode.A)) |
|||
{ |
|||
p_Velocity += new Vector3(-1, 0, 0); |
|||
} |
|||
} |
|||
if (Input.GetKey(KeyCode.D)) |
|||
{ |
|||
p_Velocity += new Vector3(1, 0, 0); |
|||
} |
|||
private Vector3 GetBaseInput() |
|||
{ // returns the basic values, if it's 0 than it's not active.
|
|||
Vector3 p_Velocity = new Vector3(); |
|||
if (Input.GetKey(KeyCode.W)) |
|||
{ |
|||
p_Velocity += new Vector3(0, 0, 1); |
|||
} |
|||
if (Input.GetKey(KeyCode.S)) |
|||
{ |
|||
p_Velocity += new Vector3(0, 0, -1); |
|||
} |
|||
if (Input.GetKey(KeyCode.A)) |
|||
{ |
|||
p_Velocity += new Vector3(-1, 0, 0); |
|||
return p_Velocity; |
|||
if (Input.GetKey(KeyCode.D)) |
|||
{ |
|||
p_Velocity += new Vector3(1, 0, 0); |
|||
} |
|||
return p_Velocity; |
|||
} |
|||
} |
|
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
using MLAgents; |
|||
public class RandomDecision : MonoBehaviour, Decision |
|||
namespace MLAgents |
|||
BrainParameters brainParameters; |
|||
SpaceType actionSpaceType; |
|||
int actionSpaceSize; |
|||
public void Awake() |
|||
public class RandomDecision : MonoBehaviour, Decision |
|||
brainParameters = |
|||
gameObject.GetComponent<Brain>().brainParameters; |
|||
actionSpaceType = brainParameters.vectorActionSpaceType; |
|||
actionSpaceSize = brainParameters.vectorActionSize; |
|||
} |
|||
BrainParameters brainParameters; |
|||
SpaceType actionSpaceType; |
|||
int actionSpaceSize; |
|||
public float[] Decide( |
|||
List<float> vectorObs, |
|||
List<Texture2D> visualObs, |
|||
float reward, |
|||
bool done, |
|||
List<float> memory) |
|||
{ |
|||
if (actionSpaceType == SpaceType.continuous) |
|||
public void Awake() |
|||
List<float> act = new List<float>(); |
|||
brainParameters = |
|||
gameObject.GetComponent<Brain>().brainParameters; |
|||
actionSpaceType = brainParameters.vectorActionSpaceType; |
|||
actionSpaceSize = brainParameters.vectorActionSize; |
|||
} |
|||
for (int i = 0; i < actionSpaceSize; i++) |
|||
public float[] Decide( |
|||
List<float> vectorObs, |
|||
List<Texture2D> visualObs, |
|||
float reward, |
|||
bool done, |
|||
List<float> memory) |
|||
{ |
|||
if (actionSpaceType == SpaceType.continuous) |
|||
act.Add(2 * Random.value - 1); |
|||
List<float> act = new List<float>(); |
|||
|
|||
for (int i = 0; i < actionSpaceSize; i++) |
|||
{ |
|||
act.Add(2 * Random.value - 1); |
|||
} |
|||
|
|||
return act.ToArray(); |
|||
return act.ToArray(); |
|||
return new float[1] {Random.Range(0, actionSpaceSize)}; |
|||
return new float[1] { Random.Range(0, actionSpaceSize) }; |
|||
} |
|||
|
|||
public List<float> MakeMemory( |
|||
List<float> vectorObs, |
|||
List<Texture2D> visualObs, |
|||
float reward, |
|||
bool done, |
|||
List<float> memory) |
|||
{ |
|||
return new List<float>(); |
|||
public List<float> MakeMemory( |
|||
List<float> vectorObs, |
|||
List<Texture2D> visualObs, |
|||
float reward, |
|||
bool done, |
|||
List<float> memory) |
|||
{ |
|||
return new List<float>(); |
|||
} |
|||
} |
|||
} |
|
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
|
|||
/// <summary>
|
|||
/// Ray perception component. Attach this to agents to enable "local perception"
|
|||
/// via the use of ray casts directed outward from the agent.
|
|||
/// </summary>
|
|||
public class RayPerception : MonoBehaviour |
|||
namespace MLAgents |
|||
List<float> perceptionBuffer = new List<float>(); |
|||
Vector3 endPosition; |
|||
RaycastHit hit; |
|||
|
|||
/// Creates perception vector to be used as part of an observation of an agent.
|
|||
/// Ray perception component. Attach this to agents to enable "local perception"
|
|||
/// via the use of ray casts directed outward from the agent.
|
|||
/// <returns>The partial vector observation corresponding to the set of rays</returns>
|
|||
/// <param name="rayDistance">Radius of rays</param>
|
|||
/// <param name="rayAngles">Anlges of rays (starting from (1,0) on unit circle).</param>
|
|||
/// <param name="detectableObjects">List of tags which correspond to object types agent can see</param>
|
|||
/// <param name="startOffset">Starting heigh offset of ray from center of agent.</param>
|
|||
/// <param name="endOffset">Ending height offset of ray from center of agent.</param>
|
|||
public List<float> Perceive(float rayDistance, |
|||
float[] rayAngles, string[] detectableObjects, |
|||
float startOffset, float endOffset) |
|||
public class RayPerception : MonoBehaviour |
|||
perceptionBuffer.Clear(); |
|||
// For each ray sublist stores categorial information on detected object
|
|||
// along with object distance.
|
|||
foreach (float angle in rayAngles) |
|||
List<float> perceptionBuffer = new List<float>(); |
|||
Vector3 endPosition; |
|||
RaycastHit hit; |
|||
|
|||
/// <summary>
|
|||
/// Creates perception vector to be used as part of an observation of an agent.
|
|||
/// </summary>
|
|||
/// <returns>The partial vector observation corresponding to the set of rays</returns>
|
|||
/// <param name="rayDistance">Radius of rays</param>
|
|||
/// <param name="rayAngles">Anlges of rays (starting from (1,0) on unit circle).</param>
|
|||
/// <param name="detectableObjects">List of tags which correspond to object types agent can see</param>
|
|||
/// <param name="startOffset">Starting heigh offset of ray from center of agent.</param>
|
|||
/// <param name="endOffset">Ending height offset of ray from center of agent.</param>
|
|||
public List<float> Perceive(float rayDistance, |
|||
float[] rayAngles, string[] detectableObjects, |
|||
float startOffset, float endOffset) |
|||
endPosition = transform.TransformDirection( |
|||
PolarToCartesian(rayDistance, angle)); |
|||
endPosition.y = endOffset; |
|||
if (Application.isEditor) |
|||
{ |
|||
Debug.DrawRay(transform.position + new Vector3(0f, startOffset, 0f), |
|||
endPosition, Color.black, 0.01f, true); |
|||
} |
|||
float[] subList = new float[detectableObjects.Length + 2]; |
|||
if (Physics.SphereCast(transform.position + |
|||
new Vector3(0f, startOffset, 0f), 0.5f, |
|||
endPosition, out hit, rayDistance)) |
|||
perceptionBuffer.Clear(); |
|||
// For each ray sublist stores categorial information on detected object
|
|||
// along with object distance.
|
|||
foreach (float angle in rayAngles) |
|||
for (int i = 0; i < detectableObjects.Length; i++) |
|||
endPosition = transform.TransformDirection( |
|||
PolarToCartesian(rayDistance, angle)); |
|||
endPosition.y = endOffset; |
|||
if (Application.isEditor) |
|||
if (hit.collider.gameObject.CompareTag(detectableObjects[i])) |
|||
Debug.DrawRay(transform.position + new Vector3(0f, startOffset, 0f), |
|||
endPosition, Color.black, 0.01f, true); |
|||
} |
|||
|
|||
float[] subList = new float[detectableObjects.Length + 2]; |
|||
if (Physics.SphereCast(transform.position + |
|||
new Vector3(0f, startOffset, 0f), 0.5f, |
|||
endPosition, out hit, rayDistance)) |
|||
{ |
|||
for (int i = 0; i < detectableObjects.Length; i++) |
|||
subList[i] = 1; |
|||
subList[detectableObjects.Length + 1] = hit.distance / rayDistance; |
|||
break; |
|||
if (hit.collider.gameObject.CompareTag(detectableObjects[i])) |
|||
{ |
|||
subList[i] = 1; |
|||
subList[detectableObjects.Length + 1] = hit.distance / rayDistance; |
|||
break; |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
subList[detectableObjects.Length] = 1f; |
|||
else |
|||
{ |
|||
subList[detectableObjects.Length] = 1f; |
|||
} |
|||
|
|||
perceptionBuffer.AddRange(subList); |
|||
perceptionBuffer.AddRange(subList); |
|||
|
|||
return perceptionBuffer; |
|||
return perceptionBuffer; |
|||
} |
|||
/// <summary>
|
|||
/// Converts polar coordinate to cartesian coordinate.
|
|||
/// </summary>
|
|||
public static Vector3 PolarToCartesian(float radius, float angle) |
|||
{ |
|||
float x = radius * Mathf.Cos(DegreeToRadian(angle)); |
|||
float z = radius * Mathf.Sin(DegreeToRadian(angle)); |
|||
return new Vector3(x, 0f, z); |
|||
} |
|||
/// <summary>
|
|||
/// Converts polar coordinate to cartesian coordinate.
|
|||
/// </summary>
|
|||
public static Vector3 PolarToCartesian(float radius, float angle) |
|||
{ |
|||
float x = radius * Mathf.Cos(DegreeToRadian(angle)); |
|||
float z = radius * Mathf.Sin(DegreeToRadian(angle)); |
|||
return new Vector3(x, 0f, z); |
|||
} |
|||
/// <summary>
|
|||
/// Converts degrees to radians.
|
|||
/// </summary>
|
|||
public static float DegreeToRadian(float degree) |
|||
{ |
|||
return degree * Mathf.PI / 180f; |
|||
/// <summary>
|
|||
/// Converts degrees to radians.
|
|||
/// </summary>
|
|||
public static float DegreeToRadian(float degree) |
|||
{ |
|||
return degree * Mathf.PI / 180f; |
|||
} |
|||
} |
|||
} |
992
unity-environment/Assets/ML-Agents/Scripts/Academy.cs
文件差异内容过多而无法显示
查看文件
文件差异内容过多而无法显示
查看文件
998
unity-environment/Assets/ML-Agents/Scripts/Agent.cs
文件差异内容过多而无法显示
查看文件
文件差异内容过多而无法显示
查看文件
|
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
|
|||
/// <summary>
|
|||
/// Interface for implementing the behavior of an Agent that uses a Heuristic
|
|||
/// Brain. The behavior of an Agent in this case is fully decided using the
|
|||
/// implementation of these methods and no training or inference takes place.
|
|||
/// Currently, the Heuristic Brain does not support text observations and actions.
|
|||
/// </summary>
|
|||
public interface Decision |
|||
namespace MLAgents |
|||
/// Defines the decision-making logic of the agent. Given the information
|
|||
/// about the agent, returns a vector of actions.
|
|||
/// Interface for implementing the behavior of an Agent that uses a Heuristic
|
|||
/// Brain. The behavior of an Agent in this case is fully decided using the
|
|||
/// implementation of these methods and no training or inference takes place.
|
|||
/// Currently, the Heuristic Brain does not support text observations and actions.
|
|||
/// <returns>Vector action vector.</returns>
|
|||
/// <param name="vectorObs">The vector observations of the agent.</param>
|
|||
/// <param name="visualObs">The cameras the agent uses for visual observations.</param>
|
|||
/// <param name="reward">The reward the agent received at the previous step.</param>
|
|||
/// <param name="done">Whether or not the agent is done.</param>
|
|||
/// <param name="memory">
|
|||
/// The memories stored from the previous step with
|
|||
/// <see cref="MakeMemory(List{float}, List{Texture2D}, float, bool, List{float})"/>
|
|||
/// </param>
|
|||
float[] Decide( |
|||
List<float> |
|||
vectorObs, |
|||
List<Texture2D> visualObs, |
|||
float reward, |
|||
bool done, |
|||
List<float> memory); |
|||
public interface Decision |
|||
{ |
|||
/// <summary>
|
|||
/// Defines the decision-making logic of the agent. Given the information
|
|||
/// about the agent, returns a vector of actions.
|
|||
/// </summary>
|
|||
/// <returns>Vector action vector.</returns>
|
|||
/// <param name="vectorObs">The vector observations of the agent.</param>
|
|||
/// <param name="visualObs">The cameras the agent uses for visual observations.</param>
|
|||
/// <param name="reward">The reward the agent received at the previous step.</param>
|
|||
/// <param name="done">Whether or not the agent is done.</param>
|
|||
/// <param name="memory">
|
|||
/// The memories stored from the previous step with
|
|||
/// <see cref="MakeMemory(List{float}, List{Texture2D}, float, bool, List{float})"/>
|
|||
/// </param>
|
|||
float[] Decide( |
|||
List<float> |
|||
vectorObs, |
|||
List<Texture2D> visualObs, |
|||
float reward, |
|||
bool done, |
|||
List<float> memory); |
|||
/// <summary>
|
|||
/// Defines the logic for creating the memory vector for the Agent.
|
|||
/// </summary>
|
|||
/// <returns>The vector of memories the agent will use at the next step.</returns>
|
|||
/// <param name="vectorObs">The vector observations of the agent.</param>
|
|||
/// <param name="visualObs">The cameras the agent uses for visual observations.</param>
|
|||
/// <param name="reward">The reward the agent received at the previous step.</param>
|
|||
/// <param name="done">Whether or not the agent is done.</param>
|
|||
/// <param name="memory">
|
|||
/// The memories stored from the previous call to this method.
|
|||
/// </param>
|
|||
List<float> MakeMemory( |
|||
List<float> vectorObs, |
|||
List<Texture2D> visualObs, |
|||
float reward, |
|||
bool done, |
|||
List<float> memory); |
|||
/// <summary>
|
|||
/// Defines the logic for creating the memory vector for the Agent.
|
|||
/// </summary>
|
|||
/// <returns>The vector of memories the agent will use at the next step.</returns>
|
|||
/// <param name="vectorObs">The vector observations of the agent.</param>
|
|||
/// <param name="visualObs">The cameras the agent uses for visual observations.</param>
|
|||
/// <param name="reward">The reward the agent received at the previous step.</param>
|
|||
/// <param name="done">Whether or not the agent is done.</param>
|
|||
/// <param name="memory">
|
|||
/// The memories stored from the previous call to this method.
|
|||
/// </param>
|
|||
List<float> MakeMemory( |
|||
List<float> vectorObs, |
|||
List<Texture2D> visualObs, |
|||
float reward, |
|||
bool done, |
|||
List<float> memory); |
|||
} |
|||
} |
|
|||
//using System.Collections;
|
|||
//using System.Collections.Generic;
|
|||
//using MLAgents;
|
|||
//using MLAgents.CommunicatorObjects;
|
|||
//using UnityEngine;
|
|||
//
|
|||
//public class Communicatror_Test : MonoBehaviour
|
|||
//{
|
|||
// private Communicator _comm;
|
|||
// private bool _communicationIsActive;
|
|||
// private int _lastInput;
|
|||
//
|
|||
// // Use this for initialization
|
|||
// private void Start () {
|
|||
// _comm = new RPCCommunicator(
|
|||
// new CommunicatorParameters
|
|||
// {
|
|||
// port = 5005
|
|||
// });
|
|||
// var initOut = new UnityOutput();
|
|||
// initOut.CustomDataOutput = "First Unity Output";
|
|||
// UnityInput firstInput;
|
|||
// UnityInput secondInput;
|
|||
// firstInput = _comm.Initialize(initOut, out secondInput);
|
|||
// Debug.Log(firstInput.CustomDataInput);
|
|||
// Debug.Log(secondInput.CustomDataInput);
|
|||
// _communicationIsActive = true;
|
|||
// _lastInput = secondInput.CustomDataInput;
|
|||
// }
|
|||
//
|
|||
// // Update is called once per frame
|
|||
// private void Update()
|
|||
// {
|
|||
// if (!_communicationIsActive)
|
|||
// {
|
|||
// return;
|
|||
// }
|
|||
//
|
|||
// UnityInput input = _comm.Exchange(new UnityOutput
|
|||
// {
|
|||
// CustomDataOutput = "Ok, received " + _lastInput.ToString()
|
|||
// });
|
|||
// if (input == null)
|
|||
// {
|
|||
// Debug.Log("The communication is now over.");
|
|||
// _communicationIsActive = false;
|
|||
// }
|
|||
// else
|
|||
// {
|
|||
// Debug.Log(input.CustomDataInput);
|
|||
// _lastInput = input.CustomDataInput;
|
|||
// }
|
|||
// }
|
|||
//}
|
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
using Docker.DotNet; |
|||
using System.Buffers; |
|||
using System; |
|||
using Docker.DotNet.Models; |
|||
using System.IO; |
|||
|
|||
public class DockerTest : MonoBehaviour { |
|||
|
|||
// Use this for initialization
|
|||
void Start () |
|||
{ |
|||
Test(); |
|||
} |
|||
|
|||
private async void Test() |
|||
{ |
|||
DockerClient client = new DockerClientConfiguration( |
|||
// new Uri("http://ubuntu-docker.cloudapp.net:4243"))
|
|||
new Uri("localhost:5004")) |
|||
.CreateClient(); |
|||
// client.Containers.StartContainerAsync("39e3317fd25", new ContainerStartParameters(){});
|
|||
using (FileStream stream = File.Open(Path.GetFullPath("..") + "/Dockerfile", FileMode.Open)) |
|||
{ |
|||
await client.Images.BuildImageFromDockerfileAsync(stream, new ImageBuildParameters |
|||
{ |
|||
Tags = new List<string>{"TestFromUnity"} |
|||
}); |
|||
} |
|||
await client.Containers.StartContainerAsync("TestFromUnity", new ContainerStartParameters() |
|||
{ |
|||
|
|||
}); |
|||
Debug.Log("Finished"); |
|||
|
|||
IList<ContainerListResponse> containers = await client.Containers.ListContainersAsync( |
|||
new ContainersListParameters(){ |
|||
Limit = 10, |
|||
}); |
|||
foreach (ContainerListResponse clr in containers) |
|||
{ |
|||
Debug.Log(clr.ID); |
|||
} |
|||
|
|||
} |
|||
|
|||
// Update is called once per frame
|
|||
void Update () { |
|||
// var client = new DockerClientConfiguration()
|
|||
|
|||
} |
|||
} |
|
|||
using UnityEngine; |
|||
using UnityEditor; |
|||
|
|||
class SingletonTestWindow : EditorWindow |
|||
{ |
|||
[MenuItem("Window/SingletonTest")] |
|||
|
|||
public static void ShowWindow() |
|||
{ |
|||
EditorWindow.GetWindow(typeof(SingletonTestWindow)); |
|||
} |
|||
|
|||
void OnGUI() |
|||
{ |
|||
SingletonTest.something = GUILayout.HorizontalSlider(SingletonTest.something, 1, 5); |
|||
GUILayout.Label("Current value of something is " + SingletonTest.something); |
|||
} |
|||
} |
|
|||
using UnityEngine; |
|||
|
|||
public class SingletonTest |
|||
{ |
|||
public static float something; |
|||
|
|||
protected SingletonTest() { } |
|||
|
|||
private static SingletonTest _instance = null; |
|||
|
|||
public static SingletonTest Instance |
|||
{ |
|||
get |
|||
{ |
|||
return SingletonTest._instance == null ? new SingletonTest() : SingletonTest._instance; |
|||
} |
|||
} |
|||
} |
|
|||
using UnityEngine; |
|||
using UnityEditor; |
|||
|
|||
class StaticTestWindow : EditorWindow |
|||
{ |
|||
[MenuItem("Window/StaticTest")] |
|||
|
|||
public static void ShowWindow() |
|||
{ |
|||
EditorWindow.GetWindow(typeof(StaticTestWindow)); |
|||
} |
|||
|
|||
void OnGUI() |
|||
{ |
|||
StaticTest.something = GUILayout.HorizontalSlider(StaticTest.something, 1, 5); |
|||
GUILayout.Label("Current value of something is " + StaticTest.something); |
|||
} |
|||
} |
|
|||
using UnityEngine; |
|||
|
|||
public static class StaticTest |
|||
{ |
|||
public static float something; |
|||
|
|||
//
|
|||
// {"State":
|
|||
// {"Predicates":[
|
|||
// {"Name":"AT","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"9", "Color":"White"}}]},
|
|||
// {"Name":"START","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"0", "Color":"White"}}]},
|
|||
// {"Name":"LOCKED","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"9", "Color":"White"}}]},
|
|||
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"0", "Color":"White"}},{"Type":"KeyDomain.Color","Value":"White"}]},
|
|||
// {"Name":"KEY_COLOR","Args":[{"Type":"KeyDomain.Color","Value":"White"}]},
|
|||
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"1", "Color":"Black"}},{"Type":"KeyDomain.Color","Value":"Black"}]},
|
|||
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"2", "Color":"Black"}},{"Type":"KeyDomain.Color","Value":"Black"}]},
|
|||
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"3", "Color":"White"}},{"Type":"KeyDomain.Color","Value":"White"}]},
|
|||
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"4", "Color":"White"}},{"Type":"KeyDomain.Color","Value":"White"}]},
|
|||
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"5", "Color":"White"}},{"Type":"KeyDomain.Color","Value":"White"}]},
|
|||
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"6", "Color":"Black"}},{"Type":"KeyDomain.Color","Value":"Black"}]},
|
|||
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"7", "Color":"White"}},{"Type":"KeyDomain.Color","Value":"White"}]},
|
|||
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"8", "Color":"White"}},{"Type":"KeyDomain.Color","Value":"White"}]},
|
|||
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"9", "Color":"White"}},{"Type":"KeyDomain.Color","Value":"White"}]},
|
|||
// {"Name":"END","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"9", "Color":"White"}}]}]
|
|||
//
|
|||
// },
|
|||
// "Action":{"Name":"UNLOCK_ROOM","Parameters":[]}
|
|||
// ,"Value":-0.5804855227470398}
|
|||
// {"State":{"Predicates":[{"Name":"AT","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"0", "Color":"White"}}]},{"Name":"START","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"0", "Color":"White"}}]},{"Name":"LOCKED","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"0", "Color":"White"}}]},{"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"0", "Color":"White"}},{"Type":"KeyDomain.Color","Value":"White"}]},{"Name":"KEY_COLOR","Args":[{"Type":"KeyDomain.Color","Value":"White"}]}]},"Action":{"Name":"PICKUP_WHITE_KEY","Parameters":[{"Type":"KeyDomain.Room","Value":{"ID":"0", "Color":"White"}},{"Type":"KeyDomain.Color","Value":"Black"},{"Type":"KeyDomain.Color","Value":"White"}]},"Value":-1.9235280752182007}
|
|||
|
|||
} |
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
#if ENABLE_TENSORFLOW
|
|||
using TensorFlow; |
|||
#endif
|
|||
|
|||
public class TensorflowTester : MonoBehaviour { |
|||
#if ENABLE_TENSORFLOW
|
|||
TFGraph graph; |
|||
TFSession session; |
|||
public TextAsset graphModelGrid; |
|||
|
|||
// Use this for initialization
|
|||
void Start () { |
|||
|
|||
graph = new TFGraph(); |
|||
graph.Import(graphModelGrid.bytes); |
|||
session = new TFSession(graph); |
|||
|
|||
} |
|||
|
|||
// Update is called once per frame
|
|||
void Update () { |
|||
var runner = session.GetRunner(); |
|||
float[,,,] visual = new float[1,84,84,3]; |
|||
runner.Fetch(graph["action"][0]); |
|||
runner.AddInput(graph["visual_observation_0"][0], visual ); |
|||
// runner.AddInput(graph["batch_size"][0], new int[] { 0 });
|
|||
TFTensor[] networkOutput; |
|||
networkOutput = runner.Run(); |
|||
long[,] output = networkOutput[0].GetValue() as long[,]; |
|||
Debug.Log(output[0,0]); |
|||
} |
|||
|
|||
#endif
|
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue