@pytest.fixture
def basic_trainer_controller ( brain_info ) :
def basic_trainer_controller ( ) :
return TrainerController (
model_path = " test_model_path " ,
summaries_dir = " test_summaries_dir " ,
train = True ,
keep_checkpoints = False ,
lesson = None ,
external_brains = { " testbrain " : brain_info } ,
training_seed = 99 ,
fast_simulation = True ,
)
@patch ( " tensorflow.set_random_seed " )
def test_initialization_seed ( numpy_random_seed , tensorflow_set_seed ) :
seed = 27
TrainerController ( " " , " " , " 1 " , 1 , None , True , False , False , None , { } , seed , True )
TrainerController ( " " , " " , " 1 " , 1 , None , True , False , False , None , seed , True )
trainer_cls , input_config , tc , expected_brain_info , expected_config
trainer_cls , input_config , tc , expected_brain_params , expected_config
external_brains = { " testbrain " : expected_brain_params }
assert brain == expected_brain_info
assert brain == expected_brain_params
assert trainer_params == expected_config
assert training == tc . train_model
assert load == tc . load_model
with patch . object ( trainer_cls , " __init__ " , mock_constructor ) :
tc . initialize_trainers ( input_config )
tc . initialize_trainers ( input_config , external_brains )
input_config , tc , expected_brain_info , expected_config , expected_reward_buff_cap = 0
input_config , tc , expected_brain_params , expected_config , expected_reward_buff_cap = 0
external_brains = { " testbrain " : expected_brain_params }
assert brain == expected_brain_info
assert brain == expected_brain_params
assert trainer_parameters == expected_config
assert reward_buff_cap == expected_reward_buff_cap
assert training == tc . train_model
with patch . object ( PPOTrainer , " __init__ " , mock_constructor ) :
tc . initialize_trainers ( input_config )
tc . initialize_trainers ( input_config , external_brains )
@patch ( " mlagents.envs.BrainInfo " )
def test_initialize_trainer_parameters_uses_defaults ( BrainInfo Mock ) :
brain_info_mock = BrainInfo Mock ( )
tc = basic_trainer_controller ( brain_info_mock )
@patch ( " mlagents.envs.BrainParameters " )
def test_initialize_trainer_parameters_uses_defaults ( BrainParameters Mock ) :
brain_params_mock = BrainParameters Mock ( )
tc = basic_trainer_controller ( )
full_config = dummy_offline_bc_config ( )
expected_config = full_config [ " default " ]
assert_bc_trainer_constructed (
OfflineBCTrainer , full_config , tc , brain_info _mock , expected_config
OfflineBCTrainer , full_config , tc , brain_params _mock , expected_config
@patch ( " mlagents.envs.BrainInfo " )
def test_initialize_trainer_parameters_override_defaults ( BrainInfo Mock ) :
brain_info_mock = BrainInfo Mock ( )
tc = basic_trainer_controller ( brain_info_mock )
@patch ( " mlagents.envs.BrainParameters " )
def test_initialize_trainer_parameters_override_defaults ( BrainParameters Mock ) :
brain_params_mock = BrainParameters Mock ( )
tc = basic_trainer_controller ( )
full_config = dummy_offline_bc_config_with_override ( )
expected_config = full_config [ " default " ]
expected_config [ " normalize " ] = False
assert_bc_trainer_constructed (
OfflineBCTrainer , full_config , tc , brain_info _mock , expected_config
OfflineBCTrainer , full_config , tc , brain_params _mock , expected_config
@patch ( " mlagents.envs.BrainInfo " )
def test_initialize_online_bc_trainer ( BrainInfo Mock ) :
brain_info_mock = BrainInfo Mock ( )
tc = basic_trainer_controller ( brain_info_mock )
@patch ( " mlagents.envs.BrainParameters " )
def test_initialize_online_bc_trainer ( BrainParameters Mock ) :
brain_params_mock = BrainParameters Mock ( )
tc = basic_trainer_controller ( )
full_config = dummy_online_bc_config ( )
expected_config = full_config [ " default " ]
assert_bc_trainer_constructed (
OnlineBCTrainer , full_config , tc , brain_info _mock , expected_config
OnlineBCTrainer , full_config , tc , brain_params _mock , expected_config
@patch ( " mlagents.envs.BrainInfo " )
def test_initialize_ppo_trainer ( BrainInfo Mock ) :
brain_info_mock = BrainInfo Mock ( )
tc = basic_trainer_controller ( brain_info_mock )
@patch ( " mlagents.envs.BrainParameters " )
def test_initialize_ppo_trainer ( BrainParameters Mock ) :
brain_params_mock = BrainParameters Mock ( )
tc = basic_trainer_controller ( )
full_config = dummy_config ( )
expected_config = full_config [ " default " ]
assert_ppo_trainer_constructed ( full_config , tc , brain_info _mock , expected_config )
assert_ppo_trainer_constructed ( full_config , tc , brain_params _mock , expected_config )
@patch ( " mlagents.envs.BrainInfo " )
def test_initialize_invalid_trainer_raises_exception ( BrainInfo Mock ) :
brain_info_mock = BrainInfoMock ( )
tc = basic_trainer_controller ( brain_info_mock )
@patch ( " mlagents.envs.BrainParameters " )
def test_initialize_invalid_trainer_raises_exception ( BrainParameters Mock ) :
tc = basic_trainer_controller ( )
external_brains = { " testbrain " : BrainParametersMock ( ) }
tc . initialize_trainers ( bad_config )
tc . initialize_trainers ( bad_config , external_brains )
def trainer_controller_with_start_learning_mocks ( ) :
trainer_mock . parameters = { " some " : " parameter " }
trainer_mock . write_tensorboard_text = MagicMock ( )
brain_info_mock = MagicMock ( )
tc = basic_trainer_controller ( brain_info_mock )
tc = basic_trainer_controller ( )
tc . initialize_trainers = MagicMock ( )
tc . trainers = { " testbrain " : trainer_mock }
tc . advance = MagicMock ( )
env_mock = MagicMock ( )
env_mock . close = MagicMock ( )
env_mock . reset = MagicMock ( )
env_mock . external_brains = MagicMock ( )
tc . initialize_trainers . assert_called_once_with ( trainer_config )
tc . initialize_trainers . assert_called_once_with (
trainer_config , env_mock . external_brains
)
env_mock . reset . assert_called_once ( )
assert tc . advance . call_count == 11
tc . _export_graph . assert_not_called ( )
env_mock = MagicMock ( )
env_mock . close = MagicMock ( )
env_mock . reset = MagicMock ( return_value = brain_info_mock )
env_mock . external_brains = MagicMock ( )
tc . initialize_trainers . assert_called_once_with ( trainer_config )
tc . initialize_trainers . assert_called_once_with (
trainer_config , env_mock . external_brains
)
env_mock . reset . assert_called_once ( )
assert tc . advance . call_count == trainer_mock . get_max_steps + 1
env_mock . close . assert_called_once ( )
trainer_mock . parameters = { " some " : " parameter " }
trainer_mock . write_tensorboard_text = MagicMock ( )
brain_info_mock = MagicMock ( )
tc = basic_trainer_controller ( brain_info_mock )
tc = basic_trainer_controller ( )
tc . trainers = { " testbrain " : trainer_mock }
return tc , trainer_mock