def test_env_altitude_controller_temporal_consistency(self):
        """ Tests that env.altitude_controller.compute_thrust() has no random component
        """
        for pol in self.reset_policy:
            self.training_config['training_params']['quadcopter']['reset_policy'][pol]['pdf'] = 'none'
        self.training_config['model']['name'] = 'ddpg'
        self.args = training_args_from_configs(base_config=self.base_config, config=self.training_config, debug_info=False)

        env_desc = EnvDict(env_dict=self.args.env)
        tp_desc = TrainingParamsDict(tp_dict=self.args.training_params)
        self.assertEqual(env_desc.get_env_id(), self.training_config['env']['value'])
        self.assertEqual(self.args.model, self.training_config['model'])
        self.assertEqual(self.args.n_steps, self.training_config['n_steps'])
        self.assertEqual(self.args.training_params, self.training_config['training_params'])

        supported_envs = [0]
        for i in supported_envs:
            env_id = f'gym_quadcopter:quadcopter-v{i}'
            print(f"Checking EnvID={env_id}")
            env = f_fwgym_get_env(
                env_id=env_id, used_states = ['e_p', 'e_q', 'e_r'],
                instance_index=0, query_class='something',
                query_classes={}, params=self.args.training_params
            )
            self.assertEqual(env.params, self.args.training_params)

            self.assertEqual(env.altitude_controller.p, 3000)
            self.assertEqual(env.altitude_controller.i, 300)
            self.assertEqual(env.altitude_controller.d, 500)

            obs_trace = np.zeros(10)
            exp_trace = np.zeros(10)

            env.reset()
            env.set_target_z(1.0)
            self.assertEqual(env.quadcopter.z, 0.0)
            self.assertEqual(env.target_z, 1.0)

            self.assertEqual(env.altitude_controller.z_integral, 0.0)
            self.assertEqual(env.previous_z_error, 0.0)

            for i in range(obs_trace.shape[0]):
                self.assertEqual(env.quadcopter.z, 0.0)
                exp_trace[i] = env.altitude_controller.compute_thrust(target_z=env.target_z, current_z=env.quadcopter.z)

            env.reset()
            env.set_target_z(1.0)
            self.assertEqual(env.quadcopter.z, 0.0)
            self.assertEqual(env.target_z, 1.0)

            self.assertEqual(env.altitude_controller.z_integral, 0.0)
            self.assertEqual(env.previous_z_error, 0.0)

            for i in range(obs_trace.shape[0]):
                self.assertEqual(env.quadcopter.z, 0.0)
                obs_trace[i] = env.altitude_controller.compute_thrust(target_z=env.target_z, current_z=env.quadcopter.z)

            self.assertTrue(np.allclose(obs_trace, exp_trace, atol=1e-5), msg=f"Temporal Consistency Check: EnvID={env_id} ObsTrace={obs_trace}, ExpTrace={exp_trace}, Delta={obs_trace-exp_trace}")
    def test_env_controller_temporal_consistency_on_altitude(self):
        """ Tests if given the same initial conditions the and target_z the selected altitude controller leads to the same z after the same number of iterations

        WARNING
        - This test is currently failing due to some precision issues hence it has been disabled but it revealed an issue that needs to be deeply investigated hence it needs to be commited in the repo
        """
        for pol in self.reset_policy:
            self.training_config['training_params']['quadcopter']['reset_policy'][pol]['pdf'] = 'none'
        self.training_config['model']['name'] = 'ddpg'
        self.args = training_args_from_configs(base_config=self.base_config, config=self.training_config, debug_info=False)

        env_desc = EnvDict(env_dict=self.args.env)
        tp_desc = TrainingParamsDict(tp_dict=self.args.training_params)
        self.assertEqual(env_desc.get_env_id(), self.training_config['env']['value'])
        self.assertEqual(self.args.model, self.training_config['model'])
        self.assertEqual(self.args.n_steps, self.training_config['n_steps'])
        self.assertEqual(self.args.training_params, self.training_config['training_params'])

        for i in self.supported_envs:
            env_id = f'gym_quadcopter:quadcopter-v{i}'
            print(f"Checking EnvID={env_id}")
            env = f_fwgym_get_env(
                env_id=env_id, used_states = ['e_p', 'e_q', 'e_r'],
                instance_index=0, query_class='something',
                query_classes={}, params=self.args.training_params
            )
            self.assertEqual(env.params, self.args.training_params)

            self.assertEqual(env.altitude_controller.p, 3000)
            self.assertEqual(env.altitude_controller.i, 300)
            self.assertEqual(env.altitude_controller.d, 500)

            obs_trace = np.zeros(10)
            exp_trace = np.zeros(10)

            env.reset()
            env.set_target_z(1.0)
            self.assertEqual(env.quadcopter.z, 0.0)
            self.assertEqual(env.target_z, 1.0)
            self.assertEqual(env.z_integral, 0.0)
            self.assertEqual(env.previous_z_error, 0.0)
            for i in range(obs_trace.shape[0]):
                env.step(action=np.zeros(3))
                exp_trace[i] = env.quadcopter.z

            env.reset()
            env.set_target_z(1.0)
            self.assertEqual(env.quadcopter.z, 0.0)
            self.assertEqual(env.target_z, 1.0)
            self.assertEqual(env.z_integral, 0.0)
            self.assertEqual(env.previous_z_error, 0.0)
            for i in range(obs_trace.shape[0]):
                env.step(action=np.zeros(3))
                obs_trace[i] = env.quadcopter.z

            self.assertTrue(np.allclose(obs_trace, exp_trace, atol=1e-5), msg=f"Temporal Consistency Check: EnvID={env_id} ObsTrace={obs_trace}, ExpTrace={exp_trace}, Delta={obs_trace-exp_trace}")
    def test_logging_base(self):
        """ Runs a full training only few iterations long as a sort of integration test
        """
        self.supported_models = ['ddpg']    #TODO find out why the test times out if we don't limit it to ddpg
        for model_name in self.supported_models:
            for env_id in self.supported_envs:
                self.training_config['model']['name'] = model_name
                self.training_config['env']['value'] = env_id
                self.training_config['n_steps'] = 0

                self.training_config['logging']['framework']['log_filename']['active'] = True
                args = training_args_from_configs(base_config=self.base_config, config=self.training_config, debug_info=False)
                job = Training()
                res = job.run_training(args)

                self.training_config['logging']['framework']['log_filename']['active'] = False
                args = training_args_from_configs(base_config=self.base_config, config=self.training_config, debug_info=False)
                job = Training()
                res = job.run_training(args)
 def setUp(self):
     """Prepare the test environment
     """
     with open('run/config/default.yaml', 'r') as f:
         self.base_config = yaml.safe_load(f)
     with open(self.base_config['filenames']['config_training'], 'r') as f:
         self.training_config = yaml.safe_load(f)['training']
     self.args = training_args_from_configs(base_config=self.base_config,
                                            config=self.training_config,
                                            debug_info=False)
 def setUp(self):
     """Prepare the test environment
     """
     with open('run/config/default.yaml', 'r') as f:
         self.base_config = yaml.safe_load(f)
     with open(self.base_config['filenames']['config_training'], 'r') as f:
         self.training_config = yaml.safe_load(f)['training']
         # Overwrite some values for the unit tests environment
         self.training_config['n_steps'] = 10
         self.training_config['iterations_checkpoint'] = 10
     self.args = training_args_from_configs(base_config=self.base_config, config=self.training_config, debug_info=False)
     self.supported_envs = ['0']
     self.supported_models = ['ddpg', 'ppo', 'trpo', 'td3', 'sac']
     self.reset_policy = 'abs_z velocity_x velocity_y velocity_z abs_roll abs_pitch abs_yaw rate_roll rate_pitch rate_yaw'.split(' ')
 def test_training_reset_policy_none(self):
     """ Runs a full training only few iterations long as a sort of integration test to show T6469163 is fixed
     """
     for model_name in self.supported_models:
         for env_id in self.supported_envs:
             self.training_config['model']['name'] = model_name
             self.training_config['env']['value'] = env_id
             self.training_config['n_steps'] = 10
             self.training_config['iterations_checkpoint'] = 10
             print(f"-------- TESTING Model={model_name} in EnvID={env_id} ------------")
             args = training_args_from_configs(base_config=self.base_config, config=self.training_config, debug_info=False)
             job = Training()
             res = job.run_training(args)
             self.assertTrue(res)
             print(f"--------- SUCCESS Model={model_name} in EnvID={env_id} ---------")
 def test_gamma_default_value(self):
     """ This tests the gamma default value for the dppg, ppo and trpo is 0.99 by default
     """
     for model_name in self.supported_models:
         for env_id in self.supported_envs:
             print(f"-------- TESTING Model={model_name} in EnvID={env_id} ------------")
             self.training_config['model']['name'] = model_name
             args = training_args_from_configs(
                 base_config=self.base_config,
                 config=self.training_config,
                 debug_info=False
             )
             job = Training()
             res = job.run_training(args)
             self.assertEqual(job.model.gamma, 0.99)
             print(f"--------- SUCCESS Model={model_name} in EnvID={env_id} ---------")
예제 #8
0
import sys
import yaml
import logging
from pathlib import Path
from training import train, parsers
import doe

if __name__ == "__main__":
    config_path = sys.argv[1] if len(
        sys.argv) > 1 else "run/config/default.yaml"

    with open(config_path) as file_base_config:
        base_config = yaml.safe_load(file_base_config)

    with open(base_config['filenames']['config_training']) as config_file:
        config = yaml.safe_load(config_file)['training']

    args = parsers.training_args_from_configs(base_config, config)
    logging.basicConfig(level=logging.INFO,
                        format="%(asctime)s [%(levelname)s] %(message)s",
                        handlers=[
                            logging.FileHandler(f"{args.log_dir}/log.txt"),
                            logging.StreamHandler()
                        ])
    training = train.Training()
    training.run_training(args)
    doe.IO.done(base_config['filenames']['results_base_dir'])