|
|
|
|
|
|
import pytest |
|
|
|
from unittest.mock import patch, call |
|
|
|
from unittest.mock import patch, call, mock_open |
|
|
|
from mlagents.trainers.curriculum import Curriculum |
|
|
|
|
|
|
|
from mlagents.trainers.tests.test_simple_rl import ( |
|
|
|
Simple1DEnvironment, |
|
|
|
_check_environment_trains, |
|
|
|
BRAIN_NAME, |
|
|
|
) |
|
|
|
from mlagents.trainers.tests.test_curriculum import dummy_curriculum_json_str |
|
|
|
|
|
|
|
|
|
|
|
class MetaCurriculumTest(MetaCurriculum): |
|
|
|
|
|
|
|
|
|
|
@patch("mlagents.trainers.curriculum.Curriculum.get_config", return_value={}) |
|
|
|
@patch("mlagents.trainers.curriculum.Curriculum.__init__", return_value=None) |
|
|
|
@patch("os.listdir", return_value=["Brain1.json", "Brain2.json"]) |
|
|
|
@patch("os.listdir", return_value=["Brain1.json", "Brain2.test.json"]) |
|
|
|
def test_init_meta_curriculum_happy_path( |
|
|
|
listdir, mock_curriculum_init, mock_curriculum_get_config, default_reset_parameters |
|
|
|
): |
|
|
|
|
|
|
|
|
|
|
assert "Brain1" in meta_curriculum.brains_to_curriculums |
|
|
|
assert "Brain2" in meta_curriculum.brains_to_curriculums |
|
|
|
assert "Brain2.test" in meta_curriculum.brains_to_curriculums |
|
|
|
calls = [call("test/Brain1.json"), call("test/Brain2.json")] |
|
|
|
calls = [call("test/Brain1.json"), call("test/Brain2.test.json")] |
|
|
|
|
|
|
|
mock_curriculum_init.assert_has_calls(calls) |
|
|
|
|
|
|
|
|
|
|
new_reset_parameters.update(more_reset_parameters) |
|
|
|
|
|
|
|
assert meta_curriculum.get_config() == new_reset_parameters |
|
|
|
|
|
|
|
|
|
|
|
META_CURRICULUM_CONFIG = """ |
|
|
|
default: |
|
|
|
trainer: ppo |
|
|
|
batch_size: 16 |
|
|
|
beta: 5.0e-3 |
|
|
|
buffer_size: 64 |
|
|
|
epsilon: 0.2 |
|
|
|
hidden_units: 128 |
|
|
|
lambd: 0.95 |
|
|
|
learning_rate: 5.0e-3 |
|
|
|
max_steps: 100 |
|
|
|
memory_size: 256 |
|
|
|
normalize: false |
|
|
|
num_epoch: 3 |
|
|
|
num_layers: 2 |
|
|
|
time_horizon: 64 |
|
|
|
sequence_length: 64 |
|
|
|
summary_freq: 50 |
|
|
|
use_recurrent: false |
|
|
|
reward_signals: |
|
|
|
extrinsic: |
|
|
|
strength: 1.0 |
|
|
|
gamma: 0.99 |
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("curriculum_brain_name", [BRAIN_NAME, "WrongBrainName"]) |
|
|
|
def test_simple_metacurriculum(curriculum_brain_name): |
|
|
|
env = Simple1DEnvironment(use_discrete=False) |
|
|
|
with patch( |
|
|
|
"builtins.open", new_callable=mock_open, read_data=dummy_curriculum_json_str |
|
|
|
): |
|
|
|
curriculum = Curriculum("TestBrain.json") |
|
|
|
mc = MetaCurriculumTest({curriculum_brain_name: curriculum}) |
|
|
|
_check_environment_trains(env, META_CURRICULUM_CONFIG, mc, -100.0) |