def test_init_curriculum_happy_path(mock_file, location,
                                    default_reset_parameters):
    curriculum = Curriculum(location, default_reset_parameters)

    assert curriculum._brain_name == "TestBrain"
    assert curriculum.lesson_num == 0
    assert curriculum.measure == "reward"
Exemple #2
0
def test_get_config(mock_file):
    curriculum = Curriculum("TestBrain.json")
    assert curriculum.get_config() == {"param1": 0.7, "param2": 100, "param3": 0.2}

    curriculum.lesson_num = 2
    assert curriculum.get_config() == {"param1": 0.3, "param2": 20, "param3": 0.7}
    assert curriculum.get_config(0) == {"param1": 0.7, "param2": 100, "param3": 0.2}
    def from_directory(folder_path: str) -> "MetaCurriculum":
        """
        Creates a MetaCurriculum given a folder full of curriculum config files.

        :param folder_path: The path to the folder which holds the curriculum configs
                for this environment. The folder should contain JSON files whose names
                are the brains that the curricula belong to.
        """
        try:
            curricula = {}
            for curriculum_filename in os.listdir(folder_path):
                # This process requires JSON files
                brain_name, extension = os.path.splitext(curriculum_filename)
                if extension.lower() != ".json":
                    continue
                curriculum_filepath = os.path.join(folder_path,
                                                   curriculum_filename)
                curriculum_config = Curriculum.load_curriculum_file(
                    curriculum_filepath)
                curricula[brain_name] = Curriculum(brain_name,
                                                   curriculum_config)
            return MetaCurriculum(curricula)
        except NotADirectoryError:
            raise MetaCurriculumError(
                f"{folder_path} is not a directory. Refer to the ML-Agents "
                "curriculum learning docs.")
Exemple #4
0
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)
def test_increment_lesson(mock_file, location, default_reset_parameters):
    curriculum = Curriculum(location, default_reset_parameters)
    assert curriculum.lesson_num == 0

    curriculum.lesson_num = 1
    assert curriculum.lesson_num == 1

    assert not curriculum.increment_lesson(10)
    assert curriculum.lesson_num == 1

    assert curriculum.increment_lesson(30)
    assert curriculum.lesson_num == 2

    assert not curriculum.increment_lesson(30)
    assert curriculum.lesson_num == 2

    assert curriculum.increment_lesson(10000)
    assert curriculum.lesson_num == 3
Exemple #6
0
def test_increment_lesson():
    curriculum = Curriculum("TestBrain", dummy_curriculum_config)
    assert curriculum.lesson_num == 0

    curriculum.lesson_num = 1
    assert curriculum.lesson_num == 1

    assert not curriculum.increment_lesson(10)
    assert curriculum.lesson_num == 1

    assert curriculum.increment_lesson(30)
    assert curriculum.lesson_num == 2

    assert not curriculum.increment_lesson(30)
    assert curriculum.lesson_num == 2

    assert curriculum.increment_lesson(10000)
    assert curriculum.lesson_num == 3
Exemple #7
0
def test_get_parameters():
    curriculum = Curriculum("TestBrain", dummy_curriculum_config)
    assert curriculum.get_config() == {
        "param1": 0.7,
        "param2": 100,
        "param3": 0.2
    }

    curriculum.lesson_num = 2
    assert curriculum.get_config() == {
        "param1": 0.3,
        "param2": 20,
        "param3": 0.7
    }
    assert curriculum.get_config(0) == {
        "param1": 0.7,
        "param2": 100,
        "param3": 0.2
    }
    def __init__(self, curriculum_folder: str):
        """Initializes a MetaCurriculum object.

        Args:
            curriculum_folder (str): The relative or absolute path of the
                folder which holds the curriculums for this environment.
                The folder should contain JSON files whose names are the
                brains that the curriculums belong to.
            default_reset_parameters (dict): The default reset parameters
                of the environment.
        """
        used_reset_parameters: Set[str] = set()
        self._brains_to_curriculums: Dict[str, Curriculum] = {}

        try:
            for curriculum_filename in os.listdir(curriculum_folder):
                # This process requires JSON files
                brain_name, extension = os.path.splitext(curriculum_filename)
                if extension.lower() != ".json":
                    continue
                curriculum_filepath = os.path.join(
                    curriculum_folder, curriculum_filename
                )
                curriculum = Curriculum(curriculum_filepath)
                config_keys: Set[str] = set(curriculum.get_config().keys())

                # Check if any two curriculums use the same reset params.
                if config_keys & used_reset_parameters:
                    logger.warning(
                        "Two or more curriculums will "
                        "attempt to change the same reset "
                        "parameter. The result will be "
                        "non-deterministic."
                    )

                used_reset_parameters.update(config_keys)
                self._brains_to_curriculums[brain_name] = curriculum
        except NotADirectoryError:
            raise MetaCurriculumError(
                curriculum_folder + " is not a "
                "directory. Refer to the ML-Agents "
                "curriculum learning docs."
            )
    def __init__(self, curriculum_configs: Dict[str, CurriculumSettings]):
        """Initializes a MetaCurriculum object.

        :param curriculum_folder: Dictionary of brain_name to the
          Curriculum for each brain.
        """
        self._brains_to_curricula: Dict[str, Curriculum] = {}
        used_reset_parameters: Set[str] = set()
        for brain_name, curriculum_settings in curriculum_configs.items():
            self._brains_to_curricula[brain_name] = Curriculum(
                brain_name, curriculum_settings)
            config_keys: Set[str] = set(
                self._brains_to_curricula[brain_name].get_config().keys())

            # Check if any two curricula use the same reset params.
            if config_keys & used_reset_parameters:
                logger.warning("Two or more curricula will "
                               "attempt to change the same reset "
                               "parameter. The result will be "
                               "non-deterministic.")

            used_reset_parameters.update(config_keys)
Exemple #10
0
    def __init__(self, curriculum_folder, default_reset_parameters):
        """Initializes a MetaCurriculum object.

        Args:
            curriculum_folder (str): The relative or absolute path of the
                folder which holds the curriculums for this environment.
                The folder should contain JSON files whose names are the
                brains that the curriculums belong to.
            default_reset_parameters (dict): The default reset parameters
                of the environment.
        """
        used_reset_parameters = set()
        self._brains_to_curriculums = {}

        try:
            for curriculum_filename in os.listdir(curriculum_folder):
                brain_name = curriculum_filename.split('.')[0]
                curriculum_filepath = \
                    os.path.join(curriculum_folder, curriculum_filename)
                curriculum = Curriculum(curriculum_filepath,
                                        default_reset_parameters)

                # Check if any two curriculums use the same reset params.
                if any([(parameter in curriculum.get_config().keys())
                        for parameter in used_reset_parameters]):
                    logger.warning('Two or more curriculums will '
                                   'attempt to change the same reset '
                                   'parameter. The result will be '
                                   'non-deterministic.')

                used_reset_parameters.update(curriculum.get_config().keys())
                self._brains_to_curriculums[brain_name] = curriculum
        except NotADirectoryError:
            raise MetaCurriculumError(curriculum_folder + ' is not a '
                                      'directory. Refer to the ML-Agents '
                                      'curriculum learning docs.')
def test_init_curriculum_bad_curriculum_raises_error(mock_file, location,
                                                     default_reset_parameters):
    with pytest.raises(CurriculumConfigError):
        Curriculum(location, default_reset_parameters)
Exemple #12
0
def test_load_bad_curriculum_file_raises_error():
    with pytest.raises(CurriculumConfigError):
        Curriculum("TestBrain", bad_curriculum_config)
Exemple #13
0
def test_init_curriculum_happy_path():
    curriculum = Curriculum("TestBrain", dummy_curriculum_config)

    assert curriculum.brain_name == "TestBrain"
    assert curriculum.lesson_num == 0
    assert curriculum.measure == "reward"
Exemple #14
0
def test_load_bad_curriculum_file_raises_error(mock_file):
    with pytest.raises(CurriculumConfigError):
        Curriculum(
            "TestBrain",
            Curriculum.load_curriculum_file(dummy_curriculum_config_path))