|
|
|
|
|
|
from unittest.mock import MagicMock, Mock, patch |
|
|
|
import pytest |
|
|
|
|
|
|
|
import yaml |
|
|
|
import pytest |
|
|
|
|
|
|
|
from mlagents.trainers.trainer_controller import TrainerController |
|
|
|
from mlagents.trainers.subprocess_env_manager import EnvironmentStep |
|
|
|
from mlagents.trainers.sampler_class import SamplerManager |
|
|
|
|
|
|
def dummy_config(): |
|
|
|
return yaml.safe_load( |
|
|
|
""" |
|
|
|
default: |
|
|
|
trainer: ppo |
|
|
|
batch_size: 32 |
|
|
|
beta: 5.0e-3 |
|
|
|
buffer_size: 512 |
|
|
|
epsilon: 0.2 |
|
|
|
gamma: 0.99 |
|
|
|
hidden_units: 128 |
|
|
|
lambd: 0.95 |
|
|
|
learning_rate: 3.0e-4 |
|
|
|
max_steps: 5.0e4 |
|
|
|
normalize: true |
|
|
|
num_epoch: 5 |
|
|
|
num_layers: 2 |
|
|
|
time_horizon: 64 |
|
|
|
sequence_length: 64 |
|
|
|
summary_freq: 1000 |
|
|
|
use_recurrent: false |
|
|
|
memory_size: 8 |
|
|
|
use_curiosity: false |
|
|
|
curiosity_strength: 0.0 |
|
|
|
curiosity_enc_size: 1 |
|
|
|
""" |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
|
def basic_trainer_controller(): |
|
|
|
return TrainerController( |
|
|
|
trainer_factory=None, |
|
|
|
|
|
|
tensorflow_set_seed.assert_called_with(seed) |
|
|
|
|
|
|
|
|
|
|
|
def trainer_controller_with_start_learning_mocks(): |
|
|
|
@pytest.fixture |
|
|
|
def trainer_controller_with_start_learning_mocks(basic_trainer_controller): |
|
|
|
trainer_mock = MagicMock() |
|
|
|
trainer_mock.get_step = 0 |
|
|
|
trainer_mock.get_max_steps = 5 |
|
|
|
|
|
|
tc = basic_trainer_controller() |
|
|
|
tc = basic_trainer_controller |
|
|
|
tc.initialize_trainers = MagicMock() |
|
|
|
tc.trainers = {"testbrain": trainer_mock} |
|
|
|
tc.advance = MagicMock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch.object(tf, "reset_default_graph") |
|
|
|
def test_start_learning_trains_forever_if_no_train_model(tf_reset_graph): |
|
|
|
tc, trainer_mock = trainer_controller_with_start_learning_mocks() |
|
|
|
def test_start_learning_trains_forever_if_no_train_model( |
|
|
|
tf_reset_graph, trainer_controller_with_start_learning_mocks |
|
|
|
): |
|
|
|
tc, trainer_mock = trainer_controller_with_start_learning_mocks |
|
|
|
tc.train_model = False |
|
|
|
|
|
|
|
tf_reset_graph.return_value = None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch.object(tf, "reset_default_graph") |
|
|
|
def test_start_learning_trains_until_max_steps_then_saves(tf_reset_graph): |
|
|
|
tc, trainer_mock = trainer_controller_with_start_learning_mocks() |
|
|
|
def test_start_learning_trains_until_max_steps_then_saves( |
|
|
|
tf_reset_graph, trainer_controller_with_start_learning_mocks |
|
|
|
): |
|
|
|
tc, trainer_mock = trainer_controller_with_start_learning_mocks |
|
|
|
tf_reset_graph.return_value = None |
|
|
|
|
|
|
|
brain_info_mock = MagicMock() |
|
|
|
|
|
|
tc._save_model.assert_called_once() |
|
|
|
|
|
|
|
|
|
|
|
def trainer_controller_with_take_step_mocks(): |
|
|
|
@pytest.fixture |
|
|
|
def trainer_controller_with_take_step_mocks(basic_trainer_controller): |
|
|
|
trainer_mock = MagicMock() |
|
|
|
trainer_mock.get_step = 0 |
|
|
|
trainer_mock.get_max_steps = 5 |
|
|
|
|
|
|
tc = basic_trainer_controller() |
|
|
|
tc = basic_trainer_controller |
|
|
|
def test_take_step_adds_experiences_to_trainer_and_trains(): |
|
|
|
tc, trainer_mock = trainer_controller_with_take_step_mocks() |
|
|
|
def test_take_step_adds_experiences_to_trainer_and_trains( |
|
|
|
trainer_controller_with_take_step_mocks |
|
|
|
): |
|
|
|
tc, trainer_mock = trainer_controller_with_take_step_mocks |
|
|
|
|
|
|
|
brain_name = "testbrain" |
|
|
|
action_info_dict = {brain_name: MagicMock()} |
|
|
|
|
|
|
trainer_mock.increment_step.assert_called_once() |
|
|
|
|
|
|
|
|
|
|
|
def test_take_step_if_not_training(): |
|
|
|
tc, trainer_mock = trainer_controller_with_take_step_mocks() |
|
|
|
def test_take_step_if_not_training(trainer_controller_with_take_step_mocks): |
|
|
|
tc, trainer_mock = trainer_controller_with_take_step_mocks |
|
|
|
tc.train_model = False |
|
|
|
|
|
|
|
brain_name = "testbrain" |
|
|
|