Exemple #1
0
    def __init__(self,
                 mu0,
                 std0,
                 popsize,
                 std_decay=0.999,
                 min_std=0.01,
                 lr=1e-3,
                 lr_decay=0.9999,
                 min_lr=1e-2,
                 antithetic=False,
                 rank_transform=True):
        r"""Initialize OpenAI-ES. 
        
        Args:
            mu0 (ndarray): initial mean
            std0 (float): initial standard deviation
            popsize (int): population size
            std_decay (float): standard deviation decay
            min_std (float): minimum of standard deviation
            lr (float): learning rate
            lr_decay (float): learning rate decay
            min_lr (float): minumum of learning rate
            antithetic (bool): If True, then use antithetic sampling to generate population.
            rank_transform (bool): If True, then use rank transformation of fitness (combat with outliers). 
        """
        self.mu0 = np.array(mu0)
        self.std0 = std0
        self.popsize = popsize
        self.std_decay = std_decay
        self.min_std = min_std
        self.lr = lr
        self.lr_decay = lr_decay
        self.min_lr = min_lr
        self.antithetic = antithetic
        if self.antithetic:
            assert self.popsize % 2 == 0, 'popsize must be even for antithetic sampling. '
        self.rank_transform = rank_transform
        if self.rank_transform:
            self.rank_transformer = RankTransform()

        # Some other settings
        self.num_params = self.mu0.size
        self.mu = torch.from_numpy(self.mu0).float()
        self.mu.requires_grad = True  # requires gradient for optimizer to update
        self.std = self.std0
        self.optimizer = optim.Adam([self.mu], lr=self.lr)
        self.lr_scheduler = optim.lr_scheduler.ExponentialLR(
            optimizer=self.optimizer, gamma=self.lr_decay)

        self.solutions = None
        self.best_param = None
        self.best_f_val = None
        self.hist_best_param = None
        self.hist_best_f_val = None
Exemple #2
0
    def test_rank_transform(self):
        rank_transform = RankTransform()

        # scalar not allowed
        with pytest.raises(AssertionError):
            rank_transform(3)

        # multidimensional array not allowed
        with pytest.raises(AssertionError):
            rank_transform([[1, 2, 3]])

        # List
        a = [3, 14, 1]
        assert np.allclose(rank_transform(a, centered=False), [1, 2, 0])
        assert np.allclose(rank_transform(a), [0, 0.5, -0.5])

        # ndarray
        b = np.array([3, 14, 1])
        assert np.allclose(rank_transform(b, centered=False), [1, 2, 0])
        assert np.allclose(rank_transform(b), [0, 0.5, -0.5])
Exemple #3
0
 def test_rank_transform(self):
     rank_transform = RankTransform()
     
     # List
     a = [3, 14, 1]
     assert np.allclose(rank_transform(a, centered=False), [1, 2, 0])
     assert np.allclose(rank_transform(a), [0, 0.5, -0.5])
     
     # ndarray
     b = np.array([3, 14, 1])
     assert np.allclose(rank_transform(b, centered=False), [1, 2, 0])
     assert np.allclose(rank_transform(b), [0, 0.5, -0.5])
     
     #
     # Test exceptions
     #
     # Scalar is not allowed
     with pytest.raises(AssertionError):
         rank_transform(5)
         
     # ndarray more than 1-dim is not allowed
     c = np.array([[3, 14, 1]])
     with pytest.raises(ValueError):
         rank_transform(c)