浏览代码

Can now switch inference configuration on/off in the editor. Reintroduced the broadcast feature for the non-External brains. Introduced the API number to check the compatibility between Unity and Python.

/tag-0.2.0
vincentpierre 7 年前
当前提交
6e950cd3
共有 8 个文件被更改,包括 46 次插入26 次删除
  1. 2
      python/ppo.py
  2. 9
      python/unityagents/environment.py
  3. 33
      unity-environment/Assets/ML-Agents/Scripts/Academy.cs
  4. 2
      unity-environment/Assets/ML-Agents/Scripts/Communicator.cs
  5. 6
      unity-environment/Assets/ML-Agents/Scripts/CoreBrainHeuristic.cs
  6. 7
      unity-environment/Assets/ML-Agents/Scripts/CoreBrainInternal.cs
  7. 6
      unity-environment/Assets/ML-Agents/Scripts/CoreBrainPlayer.cs
  8. 7
      unity-environment/Assets/ML-Agents/Scripts/ExternalCommunicator.cs

2
python/ppo.py


env = UnityEnvironment(file_name=env_name, worker_id=worker_id, curriculum=curriculum_file)
print(str(env))
brain_name = env.brain_names[0]
brain_name = env.external_brain_names[0]
tf.reset_default_graph()

9
python/unityagents/environment.py


atexit.register(self.close)
self.port = base_port + worker_id
self._buffer_size = 12000
self._python_api = "API-2"
self._loaded = False
self._open_socket = False

"and that the Academy and the external Brain(s) are attached to objects in the Scene.".format(
str(file_name)))
if "apiNumber" not in p:
self._unity_api = "API-1"
else:
self._unity_api = p["apiNumber"]
if self._unity_api != self._python_api:
raise UnityEnvironmentException(
"The API number is not compatible between Unity and python. Python API : {0}, Unity API : "
"{1}.".format(self._python_api, self._unity_api))
self._data = {}
self._global_done = None

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


private int frameToSkip;
[SerializeField]
private float waitTime;
public bool isInference = true;
/**< \brief Do not modify : If true, the Academy will use inference
* settings. */
private bool _isCurrentlyInference;
[SerializeField]
private ScreenConfiguration trainingConfiguration = new ScreenConfiguration(80, 80, 1, 100.0f, 60);
[SerializeField]

public Communicator communicator;
/**< \brief Do not modify : pointer to the communicator currently in
* use by the Academy. */
[HideInInspector]
public bool isInference;
/**< \brief Do not modify : If true, the Academy will use inference
* settings. */
[HideInInspector]
public bool windowResize;
/**< \brief Do not modify : Used to determine if the application window
* should be resized at reset. */
* use by the Academy. */
private float timeAtStep;

externalCommand = communicator.GetCommand();
}
windowResize = true;
isInference = (communicator == null);
_isCurrentlyInference = !isInference;
done = true;
acceptingSteps = true;
}

private void ConfigureEngine()
{
if ((communicator != null) && (!isInference))
if ((!isInference))
{
Screen.SetResolution(trainingConfiguration.width, trainingConfiguration.height, false);
QualitySettings.SetQualityLevel(trainingConfiguration.qualityLevel, true);

// Called before AcademyReset().
internal void Reset()
{
if (windowResize)
{
ConfigureEngine();
windowResize = false;
}
currentStep = 0;
episodeCount++;
done = false;

*/
void RunMdp()
{
if (((communicator == null) || isInference) && (timeAtStep + waitTime > Time.time))
if (isInference != _isCurrentlyInference)
{
ConfigureEngine();
_isCurrentlyInference = isInference;
}
if ((isInference) && (timeAtStep + waitTime > Time.time))
{
return;
}

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


public string AcademyName;
/**< \brief The name of the Academy. If the communicator is External,
* it will be the name of the Academy GameObject */
public string apiNumber;
/**< \brief The API number for the communicator. */
public Dictionary<string, float> resetParameters;
/**< \brief The default reset parameters are sent via socket*/
public List<string> brainNames;

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


/// CoreBrain which decides actions using developer-provided Decision.cs script.
public class CoreBrainHeuristic : ScriptableObject, CoreBrain
{
[SerializeField]
private bool broadcast = true;
public Brain brain;
/**< Reference to the brain that uses this CoreBrainHeuristic */

{
decision = brain.gameObject.GetComponent<Decision>();
if (brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator == null)
if ((brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator == null)
|| (!broadcast))
{
coord = null;
}

{
#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);

7
unity-environment/Assets/ML-Agents/Scripts/CoreBrainInternal.cs


public class CoreBrainInternal : ScriptableObject, CoreBrain
{
[SerializeField]
private bool broadcast = true;
[System.Serializable]
private struct TensorFlowAgentPlaceholder
{

}
#endif
if (brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator == null)
if ((brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator == null)
|| (!broadcast))
{
coord = null;
}

{
#if ENABLE_TENSORFLOW && UNITY_EDITOR
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
broadcast = EditorGUILayout.Toggle("Broadcast", broadcast);
SerializedObject serializedBrain = new SerializedObject(this);
GUILayout.Label("Edit the Tensorflow graph parameters here");
SerializedProperty tfGraphModel = serializedBrain.FindProperty("graphModel");

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


/// CoreBrain which decides actions using Player input.
public class CoreBrainPlayer : ScriptableObject, CoreBrain
{
[SerializeField]
private bool broadcast = true;
[System.Serializable]
private struct DiscretePlayerAction

/// Nothing to implement
public void InitializeCoreBrain()
{
if (brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator == null)
if ((brain.gameObject.transform.parent.gameObject.GetComponent<Academy>().communicator == null)
|| (!broadcast))
{
coord = null;
}

{
#if UNITY_EDITOR
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
broadcast = EditorGUILayout.Toggle("Broadcast", broadcast);
SerializedObject serializedBrain = new SerializedObject(this);
if (brain.brainParameters.actionSpaceType == StateType.continuous)
{

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


const int messageLength = 12000;
const string api = "API-2";
private class StepMessage
{
public string brain_name { get; set; }

accParamerters.brainParameters = new List<BrainParameters>();
accParamerters.brainNames = new List<string>();
accParamerters.externalBrainNames = new List<string>();
accParamerters.apiNumber = api;
foreach (Brain b in brains)
{
accParamerters.brainParameters.Add(b.brainParameters);

{
sender.Send(Encoding.ASCII.GetBytes("CONFIG_REQUEST"));
ResetParametersMessage resetParams = JsonConvert.DeserializeObject<ResetParametersMessage>(Receive());
if (academy.isInference != !resetParams.train_model)
{
academy.windowResize = true;
}
academy.isInference = !resetParams.train_model;
return resetParams.parameters;
}

正在加载...
取消
保存