public void setInitAgent(MLAgents.Brain brain) { if (this.isScriptValid()) { AgentScript.GiveBrain(brain); //AgentScript.AgentReset(); } }
/// <summary> /// Updates the Brain for the agent. Any brain currently assigned to the /// agent will be replaced with the provided one. /// </summary> /// <remarks> /// The agent unsubscribes from its current brain (if it has one) and /// subscribes to the provided brain. This enables contextual brains, that /// is, updating the behaviour (hence brain) of the agent depending on /// the context of the game. For example, we may utilize one (wandering) /// brain when an agent is randomly exploring an open world, but switch /// to another (fighting) brain when it comes into contact with an enemy. /// </remarks> /// <param name="brain">New brain to subscribe this agent to</param> public void GiveBrain(Brain brain) { this.brain = brain; ResetData(); }
/// Create the reference to the brain public void SetBrain(Brain b) { brain = b; }
//TODO add support for multiple public static void CreateNewSquad(GameObject squad, GameObject unitPrefab, HashSet <Vector2Int> tiles, Vector2Int minMax, System.Random random, MLAgents.Brain Brain, Room room, Vector2Int offset, GameObject player) { if (unitPrefab == null) { Debug.LogError("create squad need unit prefab"); return; } CommanderAgent prefabCommander = squad.GetComponent <CommanderAgent>(); if (prefabCommander == null) { Debug.LogError("create squad need squad prefab"); return; } if (prefabCommander.squad.units.Length > 0) { Debug.LogError("squad prefab needs to contain no units"); return; } if (player == null) { Debug.LogError("Squad needs to be initialized with player reference"); return; } GameObject newSquadObject = Object.Instantiate(squad, Vector3.zero, Quaternion.identity); CommanderAgent commanderAgent = newSquadObject.GetComponent <CommanderAgent>(); commanderAgent.squad.currentRoom = room; commanderAgent.GiveBrain(Brain); commanderAgent.squadColor = Random.ColorHSV(0, 1, 0.25f, 1f); int squadSize = random.NextInclusive(minMax.x, minMax.y); int tilesPerUnit = 6; int maxUnits = tiles.Count / tilesPerUnit; if (squadSize > maxUnits) { squadSize = maxUnits; } if (squadSize > CommanderAgent.MAX_SQUAD_SIZE) { squadSize = CommanderAgent.MAX_SQUAD_SIZE; } for (int i = 0; i < squadSize; i++) { Vector2 tile = tiles.GetRandom(random); Vector3 position = new Vector3((tile.x * 2) + offset.x, 1.055f, (tile.y * 2) + offset.y); var unit = Object.Instantiate(unitPrefab, position, Quaternion.identity, newSquadObject.transform); SquadUnit newUnit = unit.GetComponent <SquadUnit>(); var unitSensor = newUnit.GetComponent <UnitSensor>(); unitSensor.player = player; unitSensor.headColliderTransform = player.transform; SetMaterialColor(newUnit, commanderAgent.squadColor); commanderAgent.squad.AddUnit(newUnit); } return; }
/// <summary> /// Updates the Brain for the agent. Any brain currently assigned to the /// agent will be replaced with the provided one. /// </summary> /// <remarks> /// The agent unsubscribes from its current brain (if it has one) and /// subscribes to the provided brain. This enables contextual brains, that /// is, updating the behaviour (hence brain) of the agent depending on /// the context of the game. For example, we may utilize one (wandering) /// brain when an agent is randomly exploring an open world, but switch /// to another (fighting) brain when it comes into contact with an enemy. /// </remarks> /// <param name="givenBrain">New brain to subscribe this agent to</param> public void GiveBrain(Brain givenBrain) { brain = givenBrain; ResetData(); }
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); } 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).")); if (bpVectorActionType.enumValueIndex == 1) { //Continuous case : SerializedProperty bpVectorActionSize = serializedBrain.FindProperty("brainParameters.vectorActionSize"); bpVectorActionSize.arraySize = 1; SerializedProperty continuousActionSize = bpVectorActionSize.GetArrayElementAtIndex(0); EditorGUILayout.PropertyField(continuousActionSize, new GUIContent( "Space Size", "Length of continuous action vector.")); } else { // Discrete case : SerializedProperty bpVectorActionSize = serializedBrain.FindProperty("brainParameters.vectorActionSize"); bpVectorActionSize.arraySize = EditorGUILayout.IntField( "Branches Size", bpVectorActionSize.arraySize); EditorGUI.indentLevel++; for (int branchIndex = 0; branchIndex < bpVectorActionSize.arraySize; branchIndex++) { SerializedProperty branchActionSize = bpVectorActionSize.GetArrayElementAtIndex(branchIndex); EditorGUILayout.PropertyField(branchActionSize, new GUIContent( "Branch " + branchIndex + " Size", "Number of possible actions for the branch number " + branchIndex + ".")); } EditorGUI.indentLevel--; } try { BrainParameters parameters = myBrain.brainParameters; int numberOfDescriptions = 0; if (parameters.vectorActionSpaceType == SpaceType.continuous) { numberOfDescriptions = parameters.vectorActionSize[0]; } else { numberOfDescriptions = parameters.vectorActionSize.Length; } if (parameters.vectorActionDescriptions == null || parameters.vectorActionDescriptions.Length != numberOfDescriptions) { parameters.vectorActionDescriptions = new string[numberOfDescriptions]; } } catch { } if (bpVectorActionType.enumValueIndex == 1) { //Continuous case : 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); } else { // Discrete case : SerializedProperty bpVectorActionDescription = serializedBrain.FindProperty("brainParameters.vectorActionDescriptions"); EditorGUILayout.PropertyField(bpVectorActionDescription, new GUIContent( "Branch Descriptions", "A list of strings used to name" + " the available branches 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 }
GiveBrain(Brain brain) { this.brain = brain; ResetData(); }