浏览代码

fixed other tabs and spaces

/develop-generalizationTraining-TrainerController
Vincent Gao 7 年前
当前提交
ba0ecf24
共有 22 个文件被更改,包括 135 次插入134 次删除
  1. 6
      unity-environment/Assets/ML-Agents/Editor/MLAgentsEditModeTest.cs
  2. 3
      unity-environment/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DDecision.cs
  3. 18
      unity-environment/Assets/ML-Agents/Examples/Area/Scripts/Area.cs
  4. 38
      unity-environment/Assets/ML-Agents/Examples/Area/Scripts/AreaAgent.cs
  5. 14
      unity-environment/Assets/ML-Agents/Examples/Area/Scripts/AreaDecision.cs
  6. 2
      unity-environment/Assets/ML-Agents/Examples/Area/Scripts/GoalInteract.cs
  7. 4
      unity-environment/Assets/ML-Agents/Examples/Banana/Scripts/BananaAgent.cs
  8. 2
      unity-environment/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAgent.cs
  9. 2
      unity-environment/Assets/ML-Agents/Examples/Crawler/Scripts/CameraFollow.cs
  10. 6
      unity-environment/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayAcademy.cs
  11. 22
      unity-environment/Assets/ML-Agents/Examples/Reacher/Scripts/FlyCamera.cs
  12. 20
      unity-environment/Assets/ML-Agents/Scripts/Academy.cs
  13. 2
      unity-environment/Assets/ML-Agents/Scripts/Agent.cs
  14. 8
      unity-environment/Assets/ML-Agents/Scripts/Brain.cs
  15. 2
      unity-environment/Assets/ML-Agents/Scripts/Communicator.cs
  16. 24
      unity-environment/Assets/ML-Agents/Scripts/CoreBrainHeuristic.cs
  17. 10
      unity-environment/Assets/ML-Agents/Scripts/CoreBrainPlayer.cs
  18. 28
      unity-environment/Assets/ML-Agents/Scripts/Decision.cs
  19. 8
      unity-environment/Assets/ML-Agents/Scripts/ExternalCommunicator.cs
  20. 14
      unity-environment/Assets/ML-Agents/Scripts/TeacherHelper.cs
  21. 12
      unity-environment/Assets/ML-Agents/Template/Scripts/TemplateAcademy.cs
  22. 24
      unity-environment/Assets/ML-Agents/Template/Scripts/TemplateAgent.cs

6
unity-environment/Assets/ML-Agents/Editor/MLAgentsEditModeTest.cs


Assert.AreEqual(0, aca.stepsSinceReset);
Assert.AreEqual(0, aca.episodeCount);
Assert.AreEqual(false, aca.IsDone());
//This will call the method even though it is private
// This will call the method even though it is private
MethodInfo AcademyInitializeMethod = typeof(Academy).GetMethod("_InitializeAcademy",
BindingFlags.Instance | BindingFlags.NonPublic);
AcademyInitializeMethod.Invoke(aca, new object[] { });

Assert.AreEqual(false, agent1.IsDone());
Assert.AreEqual(false, agent2.IsDone());
//agent1 was not enabled when the academy started
// agent1 was not enabled when the academy started
Assert.AreEqual(0, agent1.agentResetCalls);
Assert.AreEqual(1, agent2.agentResetCalls);
Assert.AreEqual(1, agent1.initializeAgentCalls);

// agent2 will request decisions only when RequestDecision is called
agent1.agentParameters.maxStep = 20;
agent1.agentParameters.resetOnDone = false;
agent2.agentParameters.resetOnDone = false; //Here we specify that the agent does not reset when done
agent2.agentParameters.resetOnDone = false; // Here we specify that the agent does not reset when done
brain.brainParameters.vectorObservationSize = 0;
brain.brainParameters.cameraResolutions = new resolution[0];
agent1.GiveBrain(brain);

3
unity-environment/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DDecision.cs


public class Ball3DDecision : MonoBehaviour, Decision
{
public float rotationSpeed = 2f;
public float[] Decide(List<float> state, List<Texture2D> observation, float reward, bool done, List<float> memory)
{
if (gameObject.GetComponent<Brain>().brainParameters.vectorActionSpaceType == StateType.continuous)

act.Add(-state[7] * rotationSpeed);
return act.ToArray();
}
//If the vector action space type is discrete, then we don't do anything.
// If the vector action space type is discrete, then we don't do anything.
else
{
return new float[1]{ 1f };

18
unity-environment/Assets/ML-Agents/Examples/Area/Scripts/Area.cs


public class Area : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public virtual void ResetArea() {

38
unity-environment/Assets/ML-Agents/Examples/Area/Scripts/AreaAgent.cs


public override void CollectObservations()
{
Vector3 velocity = GetComponent<Rigidbody>().velocity;
AddVectorObs((transform.position.x - area.transform.position.x));
AddVectorObs((transform.position.y - area.transform.position.y));
AddVectorObs((transform.position.z + 5 - area.transform.position.z));
AddVectorObs(velocity.x);
AddVectorObs(velocity.y);
AddVectorObs(velocity.z);
}
AddVectorObs((transform.position.x - area.transform.position.x));
AddVectorObs((transform.position.y - area.transform.position.y));
AddVectorObs((transform.position.z + 5 - area.transform.position.z));
AddVectorObs(velocity.x);
AddVectorObs(velocity.y);
AddVectorObs(velocity.z);
}
public void MoveAgent(float[] act) {
float directionX = 0;

}
}
public override void AgentAction(float[] act)
{
public override void AgentAction(float[] act)
{
if (gameObject.transform.position.y < 0.0f || Mathf.Abs(gameObject.transform.position.x - area.transform.position.x) > 8f ||
if (gameObject.transform.position.y < 0.0f || Mathf.Abs(gameObject.transform.position.x - area.transform.position.x) > 8f ||
{
{
}
}
}
}
public override void AgentReset()
{
transform.position = new Vector3(Random.Range(-3.5f, 3.5f), 1.1f, -8f) + area.transform.position;
GetComponent<Rigidbody>().velocity = new Vector3(0f, 0f, 0f);
public override void AgentReset()
{
transform.position = new Vector3(Random.Range(-3.5f, 3.5f), 1.1f, -8f) + area.transform.position;
GetComponent<Rigidbody>().velocity = new Vector3(0f, 0f, 0f);
area.GetComponent<Area>().ResetArea();
}
area.GetComponent<Area>().ResetArea();
}
}

14
unity-environment/Assets/ML-Agents/Examples/Area/Scripts/AreaDecision.cs


public class AreaDecision : MonoBehaviour, Decision {
public float[] Decide (List<float> state, List<Texture2D> observation, float reward, bool done, List<float> memory)
{
{
}
}
public List<float> MakeMemory (List<float> state, List<Texture2D> observation, float reward, bool done, List<float> memory)
{
return new List<float>();
}
public List<float> MakeMemory (List<float> state, List<Texture2D> observation, float reward, bool done, List<float> memory)
{
return new List<float>();
}
}

2
unity-environment/Assets/ML-Agents/Examples/Area/Scripts/GoalInteract.cs


agent.Done();
agent.AddReward(1f);
}
}
}
}

4
unity-environment/Assets/ML-Agents/Examples/Banana/Scripts/BananaAgent.cs


public override void InitializeAgent()
{
base.InitializeAgent();
agentRB = GetComponent<Rigidbody>(); //cache the RB
agentRB = GetComponent<Rigidbody>(); // cache the RB
Monitor.verticalOffset = 1f;
myArea = area.GetComponent<BananaArea>();
myAcademy = myAcademyObj.GetComponent<BananaAcademy>();

transform.Rotate(rotateDir, Time.deltaTime * turnSpeed);
}
if (agentRB.velocity.sqrMagnitude > 25f) //slow it down
if (agentRB.velocity.sqrMagnitude > 25f) // slow it down
{
agentRB.velocity *= 0.95f;
}

2
unity-environment/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAgent.cs


}
else
{
//AddReward(0.05f);
// AddReward(0.05f);
}
}

2
unity-environment/Assets/ML-Agents/Examples/Crawler/Scripts/CameraFollow.cs


// Update is called once per frame
void Update () {
//gameObject.transform.position = target.position + offset;
// gameObject.transform.position = target.position + offset;
Vector3 newPosition = new Vector3(target.position.x + offset.x, transform.position.y, target.position.z + offset.z);
gameObject.transform.position = newPosition;
}

6
unity-environment/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayAcademy.cs


public float agentRunSpeed;
public float agentRotationSpeed;
public Material goalScoredMaterial; //when a goal is scored the ground will use this material for a few seconds.
public Material failMaterial; //when fail, the ground will use this material for a few seconds.
public float gravityMultiplier; //use ~3 to make things less floaty
public Material goalScoredMaterial; // when a goal is scored the ground will use this material for a few seconds.
public Material failMaterial; // when fail, the ground will use this material for a few seconds.
public float gravityMultiplier; // use ~3 to make things less floaty
public override void InitializeAcademy()
{

22
unity-environment/Assets/ML-Agents/Examples/Reacher/Scripts/FlyCamera.cs


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 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
private Vector3 lastMouse = new Vector3(255, 255, 255); //kind of in the middle of the screen, rather than at the top (play)
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()

//transform.position.Set(0,8,-32);
//transform.rotation.Set(15,0,0,1);
// 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);
}

lastMouse = new Vector3(transform.eulerAngles.x + lastMouse.x, transform.eulerAngles.y + lastMouse.y, 0);
transform.eulerAngles = lastMouse;
lastMouse = Input.mousePosition;
//Mouse camera angle done.
// Mouse camera angle done.
//Keyboard commands
// Keyboard commands
Vector3 p = GetBaseInput();
if (Input.GetKey(KeyCode.LeftShift))
{

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
{ // If player wants to move on X and Z axis only
transform.Translate(p);
newPosition.x = transform.position.x;
newPosition.z = transform.position.z;

}
private Vector3 GetBaseInput()
{ //returns the basic values, if it's 0 than it's not active.
{ // returns the basic values, if it's 0 than it's not active.
Vector3 p_Velocity = new Vector3();
if (Input.GetKey(KeyCode.W))
{

20
unity-environment/Assets/ML-Agents/Scripts/Academy.cs


/// Environment specific initialization.
/**
* Implemented in environment-specific child class.
* This method is called once when the environment is loaded.
*/
* Implemented in environment-specific child class.
* This method is called once when the environment is loaded.
*/
public virtual void InitializeAcademy()
{

/// Environment specific step logic.
/**
* Implemented in environment-specific child class.
* This method is called at every step.
*/
* Implemented in environment-specific child class.
* This method is called at every step.
*/
public virtual void AcademyStep()
{

/**
* Implemented in environment-specific child class.
* This method is called everytime the Academy resets (when the global done
* flag is set to true).
*/
* Implemented in environment-specific child class.
* This method is called everytime the Academy resets (when the global done
* flag is set to true).
*/
public virtual void AcademyReset()
{

2
unity-environment/Assets/ML-Agents/Scripts/Agent.cs


{
if (!hasAlreadyReset)
{
//If event based, the agent can reset as soon
// If event based, the agent can reset as soon
// as it is done
_AgentReset();
hasAlreadyReset = true;

8
unity-environment/Assets/ML-Agents/Scripts/Brain.cs


//[HideInInspector]
///**< \brief Keeps track of the agents which subscribe to this brain*/
//public Dictionary<int, Agent> agents = new Dictionary<int, Agent>();
// public Dictionary<int, Agent> agents = new Dictionary<int, Agent>();
[SerializeField]
ScriptableObject[] CoreBrains;

//Ensures the coreBrains are not dupplicated with the brains
// Ensures the coreBrains are not dupplicated with the brains
* If the brain gameObject was just created, it generates a list of
* coreBrains (one for each brainType) */
* If the brain gameObject was just created, it generates a list of
* coreBrains (one for each brainType) */
public void UpdateCoreBrains()
{

2
unity-environment/Assets/ML-Agents/Scripts/Communicator.cs


public string logPath;
/**< \brief The default reset parameters are sent via socket*/
public Dictionary<string, float> resetParameters;
public Dictionary<string, float> resetParameters;
/**< \brief A list of the all the brains names sent via socket*/
public List<string> brainNames;

24
unity-environment/Assets/ML-Agents/Scripts/CoreBrainHeuristic.cs


/// Uses the Decision Component to decide that action to take
public void DecideAction(Dictionary<Agent, AgentInfo> agentInfo)
{
if (coord!=null)
{
if (coord!=null)
{
}
if (decision == null)
}
if (decision == null)
{
throw new UnityAgentsException("The Brain is set to Heuristic, but no decision script attached to it");
}

{
agent.UpdateVectorAction(decision.Decide(
agent.UpdateVectorAction(decision.Decide(
agentInfo[agent].stackedVectorObservation,
agentInfo[agent].visualObservations,
agentInfo[agent].reward,

foreach (Agent agent in agentInfo.Keys)
{
agent.UpdateMemoriesAction(decision.MakeMemory(
agentInfo[agent].stackedVectorObservation,
agentInfo[agent].visualObservations,
agentInfo[agent].reward,
agentInfo[agent].done,
agentInfo[agent].memories));
agentInfo[agent].stackedVectorObservation,
agentInfo[agent].visualObservations,
agentInfo[agent].reward,
agentInfo[agent].done,
agentInfo[agent].memories));
}
}

#if UNITY_EDITOR
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
broadcast = EditorGUILayout.Toggle(new GUIContent("Broadcast",
"If checked, the brain will broadcast states and actions to Python."), broadcast);
broadcast = EditorGUILayout.Toggle(new GUIContent("Broadcast",
"If checked, the brain will broadcast states and actions to Python."), broadcast);
if (brain.gameObject.GetComponent<Decision>() == null)
{
EditorGUILayout.HelpBox("You need to add a 'Decision' component to this gameObject", MessageType.Error);

10
unity-environment/Assets/ML-Agents/Scripts/CoreBrainPlayer.cs


/// decide action
public void DecideAction(Dictionary<Agent, AgentInfo> agentInfo)
{
if (coord != null)
{
coord.GiveBrainInfo(brain, agentInfo);
}
if (coord != null)
{
coord.GiveBrainInfo(brain, agentInfo);
}
if (brain.brainParameters.vectorActionSpaceType == StateType.continuous)
{
foreach (Agent agent in agentInfo.Keys)

else
{
foreach (Agent agent in agentInfo.Keys)
{
{
var action = new float[1] { defaultAction };
foreach (DiscretePlayerAction dha in discretePlayerActions)
{

28
unity-environment/Assets/ML-Agents/Scripts/Decision.cs


/// \brief Implement this method to define the logic of decision making
/// for the CoreBrainHeuristic
/** Given the information about the agent, return a vector of actions.
* @param state The state of the agent
* @param observation The cameras the agent uses
* @param reward The reward the agent had at the previous step
* @param done Whether or not the agent is done
* @param memory The memories stored from the previous step with MakeMemory()
* @return The vector of actions the agent will take at the next step
*/
* @param state The state of the agent
* @param observation The cameras the agent uses
* @param reward The reward the agent had at the previous step
* @param done Whether or not the agent is done
* @param memory The memories stored from the previous step with MakeMemory()
* @return The vector of actions the agent will take at the next step
*/
* @param state The state of the agent
* @param observation The cameras the agent uses
* @param reward The reward the agent had at the previous step
* @param done Weather or not the agent is done
* @param memory The memories stored from the previous step with MakeMemory()
* @return The vector of memories the agent will use at the next step
*/
* @param state The state of the agent
* @param observation The cameras the agent uses
* @param reward The reward the agent had at the previous step
* @param done Weather or not the agent is done
* @param memory The memories stored from the previous step with MakeMemory()
* @return The vector of memories the agent will use at the next step
*/
List<float> MakeMemory(List<float> state, List<Texture2D> observation, float reward, bool done, List<float> memory);
}

8
unity-environment/Assets/ML-Agents/Scripts/ExternalCommunicator.cs


sMessage.maxes= new List<bool>(defaultNumAgents);
sMessage.textObservations = new List<string>(defaultNumAgents);
//Initialize the list of brains the Communicator must listen to
// Initialize the list of brains the Communicator must listen to
// Issue : This assumes all brains are broadcasting.
foreach(string k in accParamerters.brainNames){
current_agents[k] = new List<Agent>(defaultNumAgents);

return triedSendState;
}
public Dictionary<string, bool> GetSent()
{
public Dictionary<string, bool> GetSent()
{
}
}
/// Listens for actions, memories, and values and sends them
/// to the corrensponding brains.

14
unity-environment/Assets/ML-Agents/Scripts/TeacherHelper.cs


Agent myAgent;
float bufferResetTime;
// Use this for initialization
void Start () {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.R))
{
recordExperiences = !recordExperiences;

Monitor.Log("Recording experiences", recordExperiences.ToString());
float timeSinceBufferReset = Time.time - bufferResetTime;
Monitor.Log("Seconds since buffer reset", Mathf.FloorToInt(timeSinceBufferReset));
}
}
void FixedUpdate()
{

12
unity-environment/Assets/ML-Agents/Template/Scripts/TemplateAcademy.cs


public class TemplateAcademy : Academy {
public override void AcademyReset()
{
public override void AcademyReset()
{
}
}
public override void AcademyStep()
{
public override void AcademyStep()
{
}
}
}

24
unity-environment/Assets/ML-Agents/Template/Scripts/TemplateAgent.cs


public override void CollectObservations()
{
public override void CollectObservations()
{
}
}
public override void AgentAction(float[] act)
{
public override void AgentAction(float[] act)
{
}
}
public override void AgentReset()
{
public override void AgentReset()
{
}
}
public override void AgentOnDone()
{
public override void AgentOnDone()
{
}
}
}
正在加载...
取消
保存