/// <summary> /// Cleanup function /// </summary> protected virtual void OnDestroy() { Physics.gravity = originalGravity; Time.fixedDeltaTime = originalFixedDeltaTime; Time.maximumDeltaTime = originalMaximumDeltaTime; broadcastHub.Clear(); broadcastHub = null; agentSpawner = null; // Signal to listeners that the academy is being destroyed now DestroyAction(); }
/// <summary> /// Initializes the environment, configures it and initialized the Academy. /// </summary> private void InitializeEnvironment() { originalGravity = Physics.gravity; originalFixedDeltaTime = Time.fixedDeltaTime; originalMaximumDeltaTime = Time.maximumDeltaTime; InitializeAcademy(); Communicator communicator = null; var exposedBrains = broadcastHub.broadcastingBrains.Where(x => x != null).ToList(); var controlledBrains = broadcastHub.broadcastingBrains.Where( x => x != null && x is LearningBrain && broadcastHub.IsControlled(x)); foreach (LearningBrain brain in controlledBrains) { brain.SetToControlledExternally(); } // Try to launch the communicator by usig the arguments passed at launch try { communicator = new RPCCommunicator( new CommunicatorParameters { port = ReadArgs() }); } // If it fails, we check if there are any external brains in the scene // If there are : Launch the communicator on the default port // If there arn't, there is no need for a communicator and it is set // to null catch { communicator = null; if (controlledBrains.ToList().Count > 0) { communicator = new RPCCommunicator( new CommunicatorParameters { port = 5005 }); } } brainBatcher = new Batcher(communicator); foreach (var trainingBrain in exposedBrains) { trainingBrain.SetBatcher(brainBatcher); } if (communicator != null) { isCommunicatorOn = true; var academyParameters = new CommunicatorObjects.UnityRLInitializationOutput(); academyParameters.Name = gameObject.name; academyParameters.Version = kApiVersion; foreach (var brain in exposedBrains) { var bp = brain.brainParameters; academyParameters.BrainParameters.Add( bp.ToProto(brain.name, broadcastHub.IsControlled(brain))); } var pythonParameters = brainBatcher.SendAcademyParameters(academyParameters); Random.InitState(pythonParameters.Seed); Application.logMessageReceived += HandleLog; logPath = Path.GetFullPath(".") + "/UnitySDK.log"; logWriter = new StreamWriter(logPath, false); logWriter.WriteLine(System.DateTime.Now.ToString()); logWriter.WriteLine(" "); logWriter.Close(); UpdateResetParameters(); if (playerMode && !externalInferenceMode) { broadcastHub.Clear(); isCommunicatorOn = false; } } // If a communicator is enabled/provided, then we assume we are in // training mode. In the absence of a communicator, we assume we are // in inference mode. isInference = !isCommunicatorOn; BrainDecideAction += () => { }; AgentSetStatus += (m, d, i) => { }; AgentResetIfDone += () => { }; AgentSendState += () => { }; AgentAct += () => { }; AgentForceReset += () => { }; // Configure the environment using the configurations provided by // the developer in the Editor. SetIsInference(!brainBatcher.GetIsTraining()); ConfigureEnvironment(); AcademyStep(); }