private void Update() { // Check UI buttons if (resetButtonPressed) { ResetEnvironment(); resetButtonPressed = false; } if (exitButtonPressed) { agentController.CloseAgent(); Close(); return; } // Update state UpdateState(); onStateChange?.Invoke(state); // Calculate reward float reward = 1f; if (state[5] > 0.5f) { reward -= (state[3] - state[0]) * (state[3] - state[0]); } else { // new method float rotationDelta = state[4] - state[1]; reward = HillFunction(rotationDelta, 0, 5) - 0.01f * Mathf.Abs(lastAction); // old method //reward -= Mathf.Abs((state[4] - state[1])/45); //reward -= 0.05f * lastAction * lastAction; } // Check done if (Mathf.Abs(state[1]) > 135f) { reward = -100; done = true; } // Get Action from agent AgentController.ActionResponse response = agentController.GetActionResponse(0, state, reward, done); // Perform configurations received from custom message string in agent response if (response.msg.Length > 0) { BoxLogUI.instance.Log("Received message from agent: "); foreach (string s in response.msg.Split(messageArgumentSeparator, System.StringSplitOptions.None)) { BoxLogUI.instance.Log(s); HandleMessage(s); } } // Perform Exit or Reset on environment if told so by agent if (response.symbol == AgentController.AgentActionSymbol.Exit) { Close(); } else if (response.symbol == AgentController.AgentActionSymbol.Reset) { ResetEnvironment(); lastAction = 0; return; } else { // Execute physics boxMechanics.rotationForce = response.actions[0]; boxMechanics.ManualFixedUpdate(); Physics.Simulate(0.02f); onActionValueChange?.Invoke(response.actions[0]); lastAction = response.actions[0]; } }
// Update is called once per frame void Update() { // Check UI buttons if (initialRun || resetOrdered) { ResetEnvironment(); resetOrdered = false; initialRun = false; return; } if (exitOrdered) { agentController.CloseAgent(); Close(); return; } // Execute Physics ExecutePhysics(); // Update state analytics.UpdateAnalytics(); UpdateState(); onStateChange?.Invoke(state); // Calculate reward currentReward += CalculateReward(); // Check done if (touchedDown && !done) { onEpisodeEnded?.Invoke(landingReport); done = true; } if (airplane.position.z < 0) { done = true; } // Get action from agent if (actionSkipCounter == actionFrequency || done) { for (int i = 0; i < actionsOld.Length; i++) { actionsOld[i] = response.actions[i]; } response = agentController.GetActionResponse(0, state, currentReward, done); responseCount++; aircraftScript.SetControlInput(response.actions); onActionValueChange?.Invoke(response.actions); actionSkipCounter = 1; currentReward = 0; } else { actionSkipCounter++; } // Perform configurations received from custom message string in agent response if (response.msg.Length > 0) { //BoxLogUI.instance.Log("Received message from agent: "); foreach (string s in response.msg.Split(messageArgumentSeparator, System.StringSplitOptions.None)) { //BoxLogUI.instance.Log(s); HandleMessage(s); } } // Perform exit or reset on environment if told so by agent, else run physics if (response.symbol == AgentController.AgentActionSymbol.Exit) { Close(); } else if (response.symbol == AgentController.AgentActionSymbol.Reset) { resetOrdered = true; } }