[docs]classDomainRandomization(Curriculum):"""A simple but strong baseline for curriculum learning that uniformly samples a task from the task space."""def_sample_distribution(self)->List[float]:""" Returns a sample distribution over the task space. """ifself.num_tasks<=0:return[]# Uniform distributionreturn[1.0/self.num_tasksfor_inrange(self.num_tasks)]
[docs]classBatchedDomainRandomization(Curriculum):"""A simple but strong baseline for curriculum learning that uniformly samples a task from the task space."""def__init__(self,batch_size:int,task_space,warmup_batches:int=5,**kwargs):super().__init__(task_space,**kwargs)self.batch_size=batch_sizeself.current_task=Noneself._batch_steps=batch_size# Start by sampling new taskself._batch_count=0self.warmup_batches=warmup_batchesself.distribution=[1.0/self.num_tasksfor_inrange(self.num_tasks)]# Uniform distributiondef_sample_distribution(self)->List[float]:""" Returns a sample distribution over the task space. """returnself.distribution
[docs]classSyncedBatchedDomainRandomization(Curriculum):"""A simple but strong baseline for curriculum learning that uniformly samples a task from the task space."""def__init__(self,batch_size:int,task_space,warmup_batches:int=1,uniform_chance:float=0.05,**kwargs):super().__init__(task_space,**kwargs)self.batch_size=batch_sizeself.warmup_batches=warmup_batchesself.uniform_chance=uniform_chanceself.current_task=Noneself._batch_count=0self._should_update=Trueself.distribution=[1.0/self.num_tasksfor_inrange(self.num_tasks)]# Uniform distributiondef_sample_distribution(self)->List[float]:""" Returns a sample distribution over the task space. """returnself.distribution
[docs]defsample(self,k:int=1)->Any:""" Sample k tasks from the curriculum."""tasks=Noneifself._batch_count<self.warmup_batches:tasks=super().sample(k=k)ifself._should_update:self.current_task=super().sample(k=1)[0]self._should_update=FalseiftasksisNone:tasks=[]for_inrange(k):ifself.uniform_chance<np.random.rand():tasks.append(self.current_task)else:tasks.append(np.random.choice(self.num_tasks))returntasks
[docs]defupdate_batch(self):""" Update the current batch."""self._should_update=Trueself._batch_count+=1