浏览代码

removed the broadcast checkbox

Added a Handshake method for the communicator
The academy will try to handshake regardless of the brains present
Player and Heuristic brains will send their information through the communicator but will not receive commands
/develop-generalizationTraining-TrainerController
vincentpierre 7 年前
当前提交
5db042c6
共有 6 个文件被更改,包括 54 次插入60 次删除
  1. 8
      unity-environment/Assets/ML-Agents/Scripts/Academy.cs
  2. 3
      unity-environment/Assets/ML-Agents/Scripts/Communicator.cs
  3. 32
      unity-environment/Assets/ML-Agents/Scripts/CoreBrainExternal.cs
  4. 27
      unity-environment/Assets/ML-Agents/Scripts/CoreBrainHeuristic.cs
  5. 29
      unity-environment/Assets/ML-Agents/Scripts/CoreBrainPlayer.cs
  6. 15
      unity-environment/Assets/ML-Agents/Scripts/ExternalCommunicator.cs

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


brains = gameObject.GetComponentsInChildren<Brain>();
InitializeAcademy();
communicator = new ExternalCommunicator(this);
if (!communicator.CommunicatorHandShake())
{
communicator = null;
}
windowResize = true;
done = true;
acceptingSteps = true;

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


/// decisions made outside of Unity
void SubscribeBrain(Brain brain);
/// First contact between Communicator and external process
bool CommunicatorHandShake();
/// Implement this method to initialize the communicator
void InitializeCommunicator();

32
unity-environment/Assets/ML-Agents/Scripts/CoreBrainExternal.cs


{
if (brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator == null)
{
coord = new ExternalCommunicator(brain.gameObject.transform.parent.gameObject.GetComponent<Academy>());
brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator = coord;
coord.SubscribeBrain(brain);
throw new UnityAgentsException(string.Format("The brain {0} was set to" +
" External mode" +
" but Unity was unable to read the" +
" arguments passed at launch.", brain.gameObject.name));
coord = null;
else
else if (brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator is ExternalCommunicator)
if (brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator is ExternalCommunicator)
{
coord = (ExternalCommunicator)brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator;
coord.SubscribeBrain(brain);
}
coord = (ExternalCommunicator)brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator;
coord.SubscribeBrain(brain);
}
/// Uses the communicator to retrieve the actions, memories and values and

brain.SendActions(coord.GetDecidedAction(brain.gameObject.name));
brain.SendMemories(coord.GetMemories(brain.gameObject.name));
brain.SendValues(coord.GetValues(brain.gameObject.name));
if (coord != null)
{
brain.SendActions(coord.GetDecidedAction(brain.gameObject.name));
brain.SendMemories(coord.GetMemories(brain.gameObject.name));
brain.SendValues(coord.GetValues(brain.gameObject.name));
}
}
/// Uses the communicator to send the states, observations, rewards and

coord.giveBrainInfo(brain);
if (coord != null)
{
coord.giveBrainInfo(brain);
}
}
/// Nothing needs to appear in the inspector

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


public Brain brain;
/**< Reference to the brain that uses this CoreBrainHeuristic */
public bool broadcast;
/**< If true, the brain will send states / actions / rewards through the communicator */
ExternalCommunicator coord;
public Decision decision;

{
decision = brain.gameObject.GetComponent<Decision>();
if (broadcast)
if (brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator == null)
if (brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator == null)
{
coord = new ExternalCommunicator(brain.gameObject.transform.parent.gameObject.GetComponent<Academy>());
brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator = coord;
coord.SubscribeBrain(brain);
}
else
{
if (brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator is ExternalCommunicator)
{
coord = (ExternalCommunicator)brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator;
coord.SubscribeBrain(brain);
}
}
coord = null;
}
else if (brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator is ExternalCommunicator)
{
coord = (ExternalCommunicator)brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator;
coord.SubscribeBrain(brain);
}
}

/// Nothing needs to be implemented, the states are collected in DecideAction
public void SendState()
{
if (broadcast)
if (coord!=null)
{
coord.giveBrainInfo(brain);
}

{
#if UNITY_EDITOR
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
broadcast = EditorGUILayout.Toggle("Broadcast", broadcast);
if (brain.gameObject.GetComponent<Decision>() == null)
{
EditorGUILayout.HelpBox("You need to add a 'Decision' component to this gameObject", MessageType.Error);

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


public int index;
public float value;
}
public bool broadcast;
/**< If true, the brain will send states / actions / rewards through the communicator */
ExternalCommunicator coord;
[SerializeField]

/// Nothing to implement
public void InitializeCoreBrain()
{
if (broadcast)
if (brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator == null)
{
coord = null;
}
else if (brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator is ExternalCommunicator)
if (brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator == null)
{
coord = new ExternalCommunicator(brain.gameObject.transform.parent.gameObject.GetComponent<Academy>());
brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator = coord;
coord.SubscribeBrain(brain);
}
else
{
if (brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator is ExternalCommunicator)
{
coord = (ExternalCommunicator)brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator;
coord.SubscribeBrain(brain);
}
}
coord = (ExternalCommunicator)brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator;
coord.SubscribeBrain(brain);
}
}

/// decisions
public void SendState()
{
if (broadcast)
if (coord!=null)
{
coord.giveBrainInfo(brain);
}

#if UNITY_EDITOR
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
SerializedObject serializedBrain = new SerializedObject(this);
broadcast = EditorGUILayout.Toggle("Broadcast", broadcast);
if (brain.brainParameters.actionSpaceType == StateType.continuous)
{
GUILayout.Label("Edit the continuous inputs for you actions", EditorStyles.boldLabel);

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


hasSentState[brain.gameObject.name] = false;
}
/// Contains the logic for the initializtation of the socket.
public void InitializeCommunicator()
{
public bool CommunicatorHandShake(){
try
{
ReadArgs();

throw new UnityAgentsException("One of the brains was set to " +
"External mode or broadcast" +
" but Unity was unable to read the" +
" arguments passed at launch");
return false;
return true;
}
/// Contains the logic for the initializtation of the socket.
public void InitializeCommunicator()
{
messageHolder = new byte[messageLength];
// Create a TCP/IP socket.

正在加载...
取消
保存