/// <inheritdoc /> public RemotePolicy( BrainParameters brainParameters, string behaviorName) { m_BehaviorName = behaviorName; var aca = Object.FindObjectOfType <Academy>(); aca.LazyInitialization(); m_Communicator = aca.Communicator; aca.Communicator.SubscribeBrain(m_BehaviorName, brainParameters); }
/// <summary> /// Convert a BrainParametersProto to a BrainParameters struct. /// </summary> /// <param name="bpp">An instance of a brain parameters protobuf object.</param> /// <returns>A BrainParameters struct.</returns> public static BrainParameters ToBrainParameters(this BrainParametersProto bpp) { var bp = new BrainParameters { vectorActionSize = bpp.VectorActionSize.ToArray(), vectorActionDescriptions = bpp.VectorActionDescriptions.ToArray(), vectorActionSpaceType = (SpaceType)bpp.VectorActionSpaceType }; return(bp); }
/// <inheritdoc /> public BarracudaPolicy( BrainParameters brainParameters, NNModel model, InferenceDevice inferenceDevice) { var aca = Object.FindObjectOfType <Academy>(); aca.LazyInitialization(); var modelRunner = aca.GetOrCreateModelRunner(model, brainParameters, inferenceDevice); m_ModelRunner = modelRunner; }
/// <summary> /// Creates or retrieves an existing ModelRunner that uses the same /// NNModel and the InferenceDevice as provided. /// </summary> /// <param name="model"> The NNModel the ModelRunner must use </param> /// <param name="brainParameters"> The brainParameters used to create /// the ModelRunner </param> /// <param name="inferenceDevice"> The inference device (CPU or GPU) /// the ModelRunner will use </param> /// <returns> The ModelRunner compatible with the input settings</returns> internal ModelRunner GetOrCreateModelRunner( NNModel model, BrainParameters brainParameters, InferenceDevice inferenceDevice) { var modelRunner = m_ModelRunners.Find(x => x.HasModel(model, inferenceDevice)); if (modelRunner == null) { modelRunner = new ModelRunner( model, brainParameters, inferenceDevice); m_ModelRunners.Add(modelRunner); } return(modelRunner); }
/// <summary> /// Writes brain parameters to file. /// </summary> void WriteBrainParameters(string brainName, BrainParameters brainParameters) { if (m_Writer == null) { // Already closed return; } // Writes BrainParameters to file. m_Writer.Seek(MetaDataBytes + 1, 0); var brainProto = brainParameters.ToProto(brainName, false); brainProto.WriteDelimitedTo(m_Writer); }
/// <summary> /// Converts a Brain into to a Protobuf BrainInfoProto so it can be sent /// </summary> /// <returns>The BrainInfoProto generated.</returns> /// <param name="bp">The instance of BrainParameter to extend.</param> /// <param name="name">The name of the brain.</param> /// <param name="isTraining">Whether or not the Brain is training.</param> public static BrainParametersProto ToProto(this BrainParameters bp, string name, bool isTraining) { var brainParametersProto = new BrainParametersProto { VectorActionSize = { bp.vectorActionSize }, VectorActionSpaceType = (SpaceTypeProto)bp.vectorActionSpaceType, BrainName = name, IsTraining = isTraining }; brainParametersProto.VectorActionDescriptions.AddRange(bp.vectorActionDescriptions); return(brainParametersProto); }
/// Helper function that resets all the data structures associated with /// the agent. Typically used when the agent is being initialized or reset /// at the end of an episode. void ResetData() { if (brain == null) { return; } BrainParameters param = brain.brainParameters; if (param.vectorActionSpaceType == SpaceType.continuous) { action.vectorActions = new float[param.vectorActionSize]; info.storedVectorActions = new float[param.vectorActionSize]; } else { action.vectorActions = new float[1]; info.storedVectorActions = new float[1]; } if (info.textObservation == null) { info.textObservation = ""; } action.textActions = ""; info.memories = new List <float>(); action.memories = new List <float>(); if (param.vectorObservationSpaceType == SpaceType.continuous) { info.vectorObservation = new List <float>(param.vectorObservationSize); info.stackedVectorObservation = new List <float>(param.vectorObservationSize * brain.brainParameters.numStackedVectorObservations); info.stackedVectorObservation.AddRange( new float[param.vectorObservationSize * param.numStackedVectorObservations]); } else { info.vectorObservation = new List <float>(1); info.stackedVectorObservation = new List <float>(param.numStackedVectorObservations); info.stackedVectorObservation.AddRange( new float[param.numStackedVectorObservations]); } info.visualObservations = new List <Texture2D>(); }
/// Helper function that resets all the data structures associated with /// the agent. Typically used when the agent is being initialized or reset /// at the end of an episode. void ResetData() { if (brain == null) { return; } BrainParameters param = brain.brainParameters; actionMasker = new ActionMasker(param); if (param.vectorActionSpaceType == SpaceType.continuous) { action.vectorActions = new float[param.vectorActionSize[0]]; info.storedVectorActions = new float[param.vectorActionSize[0]]; } else { action.vectorActions = new float[param.vectorActionSize.Length]; info.storedVectorActions = new float[param.vectorActionSize.Length]; } if (info.textObservation == null) { info.textObservation = ""; } action.textActions = ""; info.memories = new List <float>(); action.memories = new List <float>(); info.vectorObservation = new List <float>(param.vectorObservationSize); info.stackedVectorObservation = new List <float>(param.vectorObservationSize * brain.brainParameters.numStackedVectorObservations); info.stackedVectorObservation.AddRange( new float[param.vectorObservationSize * param.numStackedVectorObservations]); info.visualObservations = new List <Texture2D>(); /// Added by M.Baske to enable frame-stacking. /// visualObservations = brain.gameObject.GetComponent <VisualObservations>(); if (visualObservations) { visualObservations.OnAgentResetData(this); } }
/// Helper function that resets all the data structures associated with /// the agent. Typically used when the agent is being initialized or reset /// at the end of an episode. void ResetData() { if (brain == null) { return; } BrainParameters param = brain.brainParameters; actionMasker = new ActionMasker(param); // If we haven't initialized vectorActions, initialize to 0. This should only // happen during the creation of the Agent. In subsequent episodes, vectorAction // should stay the previous action before the Done(), so that it is properly recorded. if (action.vectorActions == null) { if (param.vectorActionSpaceType == SpaceType.continuous) { action.vectorActions = new float[param.vectorActionSize[0]]; info.storedVectorActions = new float[param.vectorActionSize[0]]; } else { action.vectorActions = new float[param.vectorActionSize.Length]; info.storedVectorActions = new float[param.vectorActionSize.Length]; } } if (info.textObservation == null) { info.textObservation = ""; } action.textActions = ""; info.memories = new List <float>(); action.memories = new List <float>(); info.vectorObservation = new List <float>(param.vectorObservationSize); info.stackedVectorObservation = new List <float>(param.vectorObservationSize * brain.brainParameters.numStackedVectorObservations); info.stackedVectorObservation.AddRange( new float[param.vectorObservationSize * param.numStackedVectorObservations]); info.visualObservations = new List <Texture2D>(); info.customObservation = null; }
/// <summary> /// Writes the initial data to the stream. /// </summary> public void Initialize( string demonstrationName, BrainParameters brainParameters, string brainName) { if (m_Writer == null) { // Already closed return; } m_MetaData = new DemonstrationMetaData { demonstrationName = demonstrationName }; var metaProto = m_MetaData.ToProto(); metaProto.WriteDelimitedTo(m_Writer); WriteBrainParameters(brainName, brainParameters); }
public override void OnImportAsset(AssetImportContext ctx) { var inputType = Path.GetExtension(ctx.assetPath); if (inputType == null) { throw new Exception("Demonstration import error."); } try { // Read first two proto objects containing metadata and brain parameters. Stream reader = File.OpenRead(ctx.assetPath); var metaDataProto = DemonstrationMetaProto.Parser.ParseDelimitedFrom(reader); var metaData = new DemonstrationMetaData(metaDataProto); reader.Seek(DemonstrationStore.MetaDataBytes + 1, 0); var brainParamsProto = BrainParametersProto.Parser.ParseDelimitedFrom(reader); var brainParameters = new BrainParameters(brainParamsProto); reader.Close(); var demonstration = ScriptableObject.CreateInstance <Demonstration>(); demonstration.Initialize(brainParameters, metaData); userData = demonstration.ToString(); Texture2D texture = (Texture2D) AssetDatabase.LoadAssetAtPath(IconPath, typeof(Texture2D)); #if UNITY_2017_3_OR_NEWER ctx.AddObjectToAsset(ctx.assetPath, demonstration, texture); ctx.SetMainObject(demonstration); #else ctx.SetMainAsset(ctx.assetPath, demonstration); #endif } catch { return; } }
internal DiscreteActionMasker(BrainParameters brainParameters) { m_BrainParameters = brainParameters; }
public ActionMasker(BrainParameters brainParameters) { m_BrainParameters = brainParameters; }
public override void OnInspectorGUI() { Brain myBrain = (Brain)target; SerializedObject serializedBrain = serializedObject; if (myBrain.transform.parent == null) { EditorGUILayout.HelpBox( "A Brain GameObject must be a child of an Academy GameObject!", MessageType.Error); } else if (myBrain.transform.parent.GetComponent <Academy>() == null) { EditorGUILayout.HelpBox( "The Parent of a Brain must have an Academy Component attached to it!", MessageType.Error); } BrainParameters parameters = myBrain.brainParameters; if (parameters.vectorActionDescriptions == null || parameters.vectorActionDescriptions.Length != parameters.vectorActionSize) { parameters.vectorActionDescriptions = new string[parameters.vectorActionSize]; } serializedBrain.Update(); _Foldout = EditorGUILayout.Foldout(_Foldout, "Brain Parameters"); int indentLevel = EditorGUI.indentLevel; if (_Foldout) { EditorGUI.indentLevel++; EditorGUILayout.LabelField("Vector Observation"); EditorGUI.indentLevel++; SerializedProperty bpVectorObsSize = serializedBrain.FindProperty("brainParameters.vectorObservationSize"); EditorGUILayout.PropertyField(bpVectorObsSize, new GUIContent("Space Size", "Length of state " + "vector for brain (In Continuous state space)." + "Or number of possible values (in Discrete state space).")); SerializedProperty bpNumStackedVectorObs = serializedBrain.FindProperty("brainParameters.numStackedVectorObservations"); EditorGUILayout.PropertyField(bpNumStackedVectorObs, new GUIContent( "Stacked Vectors", "Number of states that" + " will be stacked before beeing fed to the neural network.")); EditorGUI.indentLevel--; SerializedProperty bpCamResol = serializedBrain.FindProperty("brainParameters.cameraResolutions"); EditorGUILayout.PropertyField(bpCamResol, new GUIContent("Visual Observation", "Describes height, " + "width, and whether to greyscale visual observations for the Brain."), true); EditorGUILayout.LabelField("Vector Action"); EditorGUI.indentLevel++; SerializedProperty bpVectorActionType = serializedBrain.FindProperty("brainParameters.vectorActionSpaceType"); EditorGUILayout.PropertyField(bpVectorActionType, new GUIContent("Space Type", "Corresponds to whether state" + " vector contains a single integer (Discrete) " + "or a series of real-valued floats (Continuous).")); SerializedProperty bpVectorActionSize = serializedBrain.FindProperty("brainParameters.vectorActionSize"); EditorGUILayout.PropertyField(bpVectorActionSize, new GUIContent("Space Size", "Length of action vector " + "for brain (In Continuous state space)." + "Or number of possible values (In Discrete action space).")); SerializedProperty bpVectorActionDescription = serializedBrain.FindProperty("brainParameters.vectorActionDescriptions"); EditorGUILayout.PropertyField(bpVectorActionDescription, new GUIContent( "Action Descriptions", "A list of strings used to name" + " the available actions for the Brain."), true); } EditorGUI.indentLevel = indentLevel; SerializedProperty bt = serializedBrain.FindProperty("brainType"); EditorGUILayout.PropertyField(bt); if (bt.enumValueIndex < 0) { bt.enumValueIndex = (int)BrainType.Player; } serializedBrain.ApplyModifiedProperties(); myBrain.UpdateCoreBrains(); myBrain.coreBrain.OnInspector(); #if !NET_4_6 && ENABLE_TENSORFLOW EditorGUILayout.HelpBox("You cannot have ENABLE_TENSORFLOW without NET_4_6", MessageType.Error); #endif }
public void Initialize(BrainParameters brainParameters, DemonstrationMetaData demonstrationMetaData) { this.brainParameters = brainParameters; metaData = demonstrationMetaData; }
/// <summary> /// Sends the Agent info to the linked Brain. /// </summary> void SendInfoToBrain() { if (brain == null) { return; } info.memories = action.memories; info.storedVectorActions = action.vectorActions; info.storedTextActions = action.textActions; info.vectorObservation.Clear(); actionMasker.ResetMask(); CollectObservations(); info.actionMasks = actionMasker.GetMask(); BrainParameters param = brain.brainParameters; if (info.vectorObservation.Count != param.vectorObservationSize) { throw new UnityAgentsException(string.Format( "Vector Observation size mismatch between continuous " + "agent {0} and brain {1}. " + "Was Expecting {2} but received {3}. ", gameObject.name, brain.name, brain.brainParameters.vectorObservationSize, info.vectorObservation.Count)); } info.stackedVectorObservation.RemoveRange( 0, param.vectorObservationSize); info.stackedVectorObservation.AddRange(info.vectorObservation); info.visualObservations.Clear(); var visualObservationCount = agentParameters.agentCameras.Count + agentParameters.agentRenderTextures.Count; if (param.cameraResolutions.Length > visualObservationCount) { throw new UnityAgentsException(string.Format( "Not enough cameras/renderTextures for agent {0} : Brain {1} expecting at " + "least {2} cameras/renderTextures but only {3} were present.", gameObject.name, brain.name, brain.brainParameters.cameraResolutions.Length, visualObservationCount)); } //First add all cameras for (int i = 0; i < agentParameters.agentCameras.Count; i++) { ObservationToTexture( agentParameters.agentCameras[i], param.cameraResolutions[i].width, param.cameraResolutions[i].height, ref textureArray[i]); info.visualObservations.Add(textureArray[i]); } //Then add all renderTextures var camCount = agentParameters.agentCameras.Count; for (int i = 0; i < agentParameters.agentRenderTextures.Count; i++) { ObservationToTexture( agentParameters.agentRenderTextures[i], param.cameraResolutions[camCount + i].width, param.cameraResolutions[camCount + i].height, ref textureArray[i]); info.visualObservations.Add(textureArray[i]); } info.reward = reward; info.done = done; info.maxStepReached = maxStepReached; info.id = id; brain.SendState(this, info); if (recorder != null && recorder.record && Application.isEditor) { recorder.WriteExperience(info); } info.textObservation = ""; }
public ActionMasker(BrainParameters brainParameters) { this._brainParameters = brainParameters; }
/// <summary> /// Sends the Agent info to the linked Brain. /// </summary> void SendInfoToBrain() { if (brain == null) { return; } info.memories = action.memories; info.storedVectorActions = action.vectorActions; info.storedTextActions = action.textActions; info.vectorObservation.Clear(); CollectObservations(); BrainParameters param = brain.brainParameters; if (param.vectorObservationSpaceType == SpaceType.continuous) { if (info.vectorObservation.Count != param.vectorObservationSize) { throw new UnityAgentsException(string.Format( "Vector Observation size mismatch between continuous " + "agent {0} and brain {1}. " + "Was Expecting {2} but received {3}. ", gameObject.name, brain.gameObject.name, brain.brainParameters.vectorObservationSize, info.vectorObservation.Count)); } info.stackedVectorObservation.RemoveRange( 0, param.vectorObservationSize); info.stackedVectorObservation.AddRange(info.vectorObservation); } else { if (info.vectorObservation.Count != 1) { throw new UnityAgentsException(string.Format( "Vector Observation size mismatch between discrete agent" + " {0} and brain {1}. Was Expecting {2} but received {3}. ", gameObject.name, brain.gameObject.name, 1, info.vectorObservation.Count)); } info.stackedVectorObservation.RemoveRange(0, 1); info.stackedVectorObservation.AddRange(info.vectorObservation); } info.visualObservations.Clear(); if (param.cameraResolutions.Length > agentParameters.agentCameras.Count) { throw new UnityAgentsException(string.Format( "Not enough cameras for agent {0} : Bain {1} expecting at " + "least {2} cameras but only {3} were present.", gameObject.name, brain.gameObject.name, brain.brainParameters.cameraResolutions.Length, agentParameters.agentCameras.Count)); } for (int i = 0; i < brain.brainParameters.cameraResolutions.Length; i++) { info.visualObservations.Add(ObservationToTexture( agentParameters.agentCameras[i], param.cameraResolutions[i].width, param.cameraResolutions[i].height)); } info.reward = reward; info.done = done; info.maxStepReached = maxStepReached; info.id = id; brain.SendState(this, info); info.textObservation = ""; }
/// <summary> /// Sends the Agent info to the linked Brain. /// </summary> void SendInfoToBrain() { if (brain == null) { return; } info.memories = action.memories; info.storedVectorActions = action.vectorActions; info.storedTextActions = action.textActions; info.vectorObservation.Clear(); actionMasker.ResetMask(); CollectObservations(); info.actionMasks = actionMasker.GetMask(); BrainParameters param = brain.brainParameters; if (info.vectorObservation.Count != param.vectorObservationSize) { throw new UnityAgentsException(string.Format( "Vector Observation size mismatch between continuous " + "agent {0} and brain {1}. " + "Was Expecting {2} but received {3}. ", gameObject.name, brain.gameObject.name, brain.brainParameters.vectorObservationSize, info.vectorObservation.Count)); } info.stackedVectorObservation.RemoveRange( 0, param.vectorObservationSize); info.stackedVectorObservation.AddRange(info.vectorObservation); info.visualObservations.Clear(); /// Modified by M.Baske to enable frame-stacking. /// if (visualObservations) { visualObservations.ApplyObservations(this, info.visualObservations); } /// Original code: /* * if (param.cameraResolutions.Length > agentParameters.agentCameras.Count) * { * throw new UnityAgentsException(string.Format( * "Not enough cameras for agent {0} : Bain {1} expecting at " + * "least {2} cameras but only {3} were present.", * gameObject.name, brain.gameObject.name, * brain.brainParameters.cameraResolutions.Length, * agentParameters.agentCameras.Count)); * } * * for (int i = 0; i < brain.brainParameters.cameraResolutions.Length; i++) * { * ObservationToTexture( * agentParameters.agentCameras[i], * param.cameraResolutions[i].width, * param.cameraResolutions[i].height, * ref textureArray[i]); * info.visualObservations.Add(textureArray[i]); * } */ info.reward = reward; info.done = done; info.maxStepReached = maxStepReached; info.id = id; brain.SendState(this, info); info.textObservation = ""; }
public void Awake() { brainParameters = gameObject.GetComponent <Brain>().brainParameters; actionSpaceType = brainParameters.vectorActionSpaceType; }