docstr: Tests the refactored (Categorical-less) version of `sample_action`
docstr_tok: ["Tests","the","refactored","(","Categorical-less",")","version","of","`","sample_action","`"]
code: def test_sample_action(self):
      """
      Tests the refactored (Categorical-less) version of `sample_action`
      """
  
      """Test with single observation modality, single hidden state factor and single timestep"""
  
      num_obs = [3]
      num_states = [3]
      num_controls = [3]
  
      qs = utils.obj_array_uniform(num_states)
      A = utils.random_A_matrix(num_obs, num_states)
      pA = utils.obj_array_ones([A_m.shape for A_m in A])
  
      B = utils.random_B_matrix(num_states, num_controls)
      pB = utils.obj_array_ones([B_f.shape for B_f in B])
  
      C = utils.obj_array_zeros(num_obs)
      C[0][0] = 1.0
      C[0][1] = -2.0
  
      policies = control.construct_policies(
          num_states, num_controls, policy_len=1
      )
  
      q_pi, efe = control.update_posterior_policies(
          qs,
          A,
          B,
          C,
          policies,
          use_utility=True,
          use_states_info_gain=True,
          use_param_info_gain=True,
          pA=pA,
          pB=pB,
          gamma=16.0,
      )
  
      factor_idx = 0
      modality_idx = 0
      t_idx = 0
  
      chosen_action = control.sample_action(
          q_pi, policies, num_controls, action_selection="deterministic"
      )
      sampled_action = control.sample_action(
          q_pi,
          policies,
          num_controls,
          action_selection="stochastic",
          alpha=1.0,
      )
  
      self.assertEqual(chosen_action.shape, sampled_action.shape)
  
      """Test with multiple observation modalities, multiple hidden state factors and single timestep"""
  
      num_obs = [3, 4]
      num_states = [3, 4]
      num_controls = [3, 3]
  
      qs = utils.random_single_categorical(num_states)
      A = utils.random_A_matrix(num_obs, num_states)
      pA = utils.obj_array_ones([A_m.shape for A_m in A])
  
      B = utils.random_B_matrix(num_states, num_controls)
      pB = utils.obj_array_ones([B_f.shape for B_f in B])
  
      C = utils.obj_array_zeros(num_obs)
      C[0][0] = 1.0
      C[0][1] = -2.0
      C[1][3] = 3.0
  
      policies = control.construct_policies(
          num_states, num_controls, policy_len=1
      )
  
      q_pi, efe = control.update_posterior_policies(
          qs,
          A,
          B,
          C,
          policies,
          use_utility=True,
          use_states_info_gain=True,
          use_param_info_gain=True,
          pA=pA,
          pB=pB,
          gamma=16.0,
      )
  
      chosen_action = control.sample_action(
          q_pi, policies, num_controls, action_selection="deterministic"
      )
      sampled_action = control.sample_action(
          q_pi,
          policies,
          num_controls,
          action_selection="stochastic",
          alpha=1.0,
      )
  
      self.assertEqual(chosen_action.shape, sampled_action.shape)
  
      """Test with multiple observation modalities, multiple hidden state factors and multiple timesteps"""
  
      num_obs = [3, 4]
      num_states = [3, 4]
      num_controls = [3, 3]
  
      qs = utils.random_single_categorical(num_states)
      A = utils.random_A_matrix(num_obs, num_states)
      pA = utils.obj_array_ones([A_m.shape for A_m in A])
  
      B = utils.random_B_matrix(num_states, num_controls)
      pB = utils.obj_array_ones([B_f.shape for B_f in B])
  
      C = utils.obj_array_zeros(num_obs)
      C[0][0] = 1.0
      C[0][1] = -2.0
      C[1][3] = 3.0
  
      policies = control.construct_policies(
          num_states, num_controls, policy_len=3
      )
  
      q_pi, efe = control.update_posterior_policies(
          qs,
          A,
          B,
          C,
          policies,
          use_utility=True,
          use_states_info_gain=True,
          use_param_info_gain=True,
          pA=pA,
          pB=pB,
          gamma=16.0,
      )
  
      chosen_action = control.sample_action(
          q_pi, policies, num_controls, action_selection="deterministic"
      )
      sampled_action = control.sample_action(
          q_pi,
          policies,
          num_controls,
          action_selection="stochastic",
          alpha=1.0,
      )
  
      self.assertEqual(chosen_action.shape, sampled_action.shape)
  
      """Single observation modality, single (controllable) hidden state factor, 3-step policies. Using utility only"""
      # One policy entails going to state 0 two times in a row, and then state 2 at the end
      # Another policy entails going to state 1 three times in a row
  
      num_obs = [3]
      num_states = [3]
      num_controls = [3]
  
      qs = utils.random_single_categorical(num_states)
      B = utils.construct_controllable_B(num_states, num_controls)
  
      policies = [
          np.array([0, 0, 2]).reshape(-1, 1),
          np.array([1, 1, 1]).reshape(-1, 1),
      ]
  
      # create noiseless identity A matrix
      A = utils.to_obj_array(np.eye(num_obs[0]))
  
      # create imbalance in preferences for observations
      # This test is designed to illustrate the emergence of planning by
      # using the time-integral of the expected free energy.
      # Even though the first observation (index 0) is the most preferred, the policy
      # that frequents this observation the most is actually not optimal, because that policy
      # terminates in a less preferred state by timestep 3.
      C = utils.to_obj_array(np.array([1.2, 1.0, 0.55]))
  
      q_pi, efe = control.update_posterior_policies(
          qs,
          A,
          B,
          C,
          policies,
          use_utility=True,
          use_states_info_gain=False,
          use_param_info_gain=False,
          pA=None,
          pB=None,
          gamma=16.0,
      )
  
      chosen_action = control.sample_action(
          q_pi, policies, num_controls, action_selection="deterministic"
      )
      self.assertEqual(int(chosen_action[0]), 1)
  
imports: import os
  import unittest
  import numpy
  from pymdp import utils, maths
  from pymdp import control
label_desc: Use image node_id 12 for calling the TestControl obj's underlying member method code with example usage: obj.test_sample_action() without return types