logger = logging . getLogger ( " mlagents.trainers " )
class MetaCurriculum ( object ) :
""" A MetaCurriculum holds curriculums. Each curriculum is associated to a
class MetaCurriculum :
""" A MetaCurriculum holds curricula. Each curriculum is associated to a
def __init__ ( self , curriculum_folder : str ) :
def __init__ ( self , curricula : Dict [ str , Curriculum ] ) :
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 .
: param curriculum_folder : Dictionary of brain_name to the
Curriculum for each brain .
self . _brains_to_curriculums : Dict [ str , Curriculum ] = { }
self . _brains_to_curricula : Dict [ str , Curriculum ] = { }
for brain_name , curriculum in curricula . items ( ) :
self . _brains_to_curricula [ brain_name ] = curriculum
config_keys : Set [ str ] = set ( curriculum . get_config ( ) . keys ( ) )
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
# 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. "
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. "
)
used_reset_parameters . update ( config_keys )
def brains_to_curriculums ( self ) :
def brains_to_curricula ( self ) :
return self . _brains_to_curriculums
return self . _brains_to_curricula
for brain_name , curriculum in self . brains_to_curriculums . items ( ) :
for brain_name , curriculum in self . brains_to_curricula . items ( ) :
lesson_nums [ brain_name ] = curriculum . lesson_num
return lesson_nums
for brain_name , lesson in lesson_nums . items ( ) :
self . brains_to_curriculums [ brain_name ] . lesson_num = lesson
self . brains_to_curricula [ brain_name ] . lesson_num = lesson
def _lesson_ready_to_increment (
self , brain_name : str , reward_buff_size : int
Whether the curriculum of the specified brain should attempt to
increment its lesson .
"""
if brain_name not in self . brains_to_curriculums :
if brain_name not in self . brains_to_curricula :
self . brains_to_curriculums [ brain_name ] . min_lesson_length
self . brains_to_curricula [ brain_name ] . min_lesson_length
""" Attempts to increments all the lessons of all the curriculums in this
""" Attempts to increments all the lessons of all the curricula in this
MetaCurriculum . Note that calling this method does not guarantee the
lesson of a curriculum will increment . The lesson of a curriculum will
only increment if the specified measure threshold defined in the
for brain_name , buff_size in reward_buff_sizes . items ( ) :
if self . _lesson_ready_to_increment ( brain_name , buff_size ) :
measure_val = measure_vals [ brain_name ]
ret [ brain_name ] = self . brains_to_curriculums [
ret [ brain_name ] = self . brains_to_curricula [
ret [ brain_name ] = self . brains_to_curriculums [
brain_name
] . increment_lesson ( measure_val )
ret [ brain_name ] = self . brains_to_curricula [ brain_name ] . increment_lesson (
measure_val
)
def set_all_curriculums_to_lesson_num ( self , lesson_num ) :
""" Sets all the curriculums in this meta curriculum to a specified
def set_all_curricula_to_lesson_num ( self , lesson_num ) :
""" Sets all the curricula in this meta curriculum to a specified
lesson_num ( int ) : The lesson number which all the curriculums will
lesson_num ( int ) : The lesson number which all the curricula will
for _ , curriculum in self . brains_to_curriculums . items ( ) :
for _ , curriculum in self . brains_to_curricula . items ( ) :
""" Get the combined configuration of all curriculums in this
""" Get the combined configuration of all curricula in this
Returns :
A dict from parameter to value.
: return : A dict from parameter to value.
for _ , curriculum in self . brains_to_curriculums . items ( ) :
for _ , curriculum in self . brains_to_curricula . items ( ) :
@staticmethod
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. "
)