docstr: Test random actions vs. a LQR controller. LQR controller should perform
  much better than random actions in the linear dynamics environment.
docstr_tok: ["Test","random","actions","vs.","a","LQR","controller",".","LQR","controller","should","perform","much","better","than","random","actions","in","the","linear","dynamics","environment","."]
code: def test_random_vs_lqr(self):
      """
      Test random actions vs. a LQR controller. LQR controller should perform
      much better than random actions in the linear dynamics environment.
      """
      env = Gym(env_name="LinearDynamics-v0")
      num_test_episodes = 500
  
      def random_policy(env, state):
          return np.random.uniform(
              env.action_space.low,
              env.action_space.high,
              env.action_dim,
          )
  
      def lqr_policy(env, state):
          # Four matrices that characterize the environment
          A, B, Q, R = env.A, env.B, env.Q, env.R
          # Solve discrete algebraic Riccati equation:
          M = linalg.solve_discrete_are(A, B, Q, R)
          K = np.dot(
              linalg.inv(np.dot(np.dot(B.T, M), B) + R),
              (np.dot(np.dot(B.T, M), A)),
          )
          state = state.reshape((-1, 1))
          action = -K.dot(state).squeeze()
          return action
  
      mean_acc_rws_random = self.run_n_episodes(
          env, num_test_episodes, random_policy
      )
      mean_acc_rws_lqr = self.run_n_episodes(
          env, num_test_episodes, lqr_policy
      )
      logger.info(
          f"Mean acc. reward of random policy: {mean_acc_rws_random}"
      )
      logger.info(f"Mean acc. reward of LQR policy: {mean_acc_rws_lqr}")
      assert mean_acc_rws_lqr > mean_acc_rws_random
  
file_path: ReAgent/reagent/gym/tests/test_linear_dynamics.py
imports: import logging
  import time
  import unittest
  import numpy
  import scipy.linalg
  from reagent.gym.envs import Gym
label_desc: Use image node_id 2 for calling the TestLinearDynamicsEnvironment obj's underlying member method code with example usage: obj.test_random_vs_lqr() and returns: np, action