コード例 #1
0
ファイル: utils.py プロジェクト: Basvanstein/OWCK
def get_design_sites(dim, n_sample, x_lb, x_ub, sampling_method="lhs"):

    x_lb = atleast_2d(x_lb)
    x_ub = atleast_2d(x_ub)

    x_lb = x_lb.T if size(x_lb, 0) != 1 else x_lb
    x_ub = x_ub.T if size(x_ub, 0) != 1 else x_ub

    if sampling_method == "lhs":
        # Latin Hyper Cube Sampling: Get evenly distributed sampling in R^dim
        samples = lhs(dim, samples=n_sample) * (x_ub - x_lb) + x_lb

    elif sampling_method == "uniform":
        samples = np.random.rand(n_sample, dim) * (x_ub - x_lb) + x_lb

    elif sampling_method == "sobol":
        seed = mod(int(time.time()) + os.getpid(), int(1e6))
        samples = np.zeros((n_sample, dim))
        for i in range(n_sample):
            samples[i, :], seed = i4_sobol(dim, seed)
        samples = samples * (x_ub - x_lb) + x_lb

    elif sampling_method == "halton":
        sequencer = Halton(dim)
        samples = sequencer.get(n_sample) * (x_ub - x_lb) + x_lb

    return samples
コード例 #2
0
def get_design_sites(dim, n_sample, x_lb, x_ub, sampling_method='lhs'):

    x_lb = atleast_2d(x_lb)
    x_ub = atleast_2d(x_ub)

    x_lb = x_lb.T if size(x_lb, 0) != 1 else x_lb
    x_ub = x_ub.T if size(x_ub, 0) != 1 else x_ub

    if sampling_method == 'lhs':
        # Latin Hyper Cube Sampling: Get evenly distributed sampling in R^dim
        samples = lhs(dim, samples=n_sample) * (x_ub - x_lb) + x_lb

    elif sampling_method == 'uniform':
        samples = np.random.rand(n_sample, dim) * (x_ub - x_lb) + x_lb

    elif sampling_method == 'sobol':
        seed = mod(int(time.time()) + os.getpid(), int(1e6))
        samples = np.zeros((n_sample, dim))
        for i in range(n_sample):
            samples[i, :], seed = i4_sobol(dim, seed)
        samples = samples * (x_ub - x_lb) + x_lb

    elif sampling_method == 'halton':
        sequencer = Halton(dim)
        samples = sequencer.get(n_sample) * (x_ub - x_lb) + x_lb

    return samples
コード例 #3
0
    def __init__(self, n, shape='col'):

        if not halton_available:
            raise ImportError("Package 'ghalton' not found, QuasiGaussianHaltonSampling not available.")
        self.n = n
        self.shape = (n,1) if shape == 'col' else (1,n)
        self.halton = Halton(n)
コード例 #4
0
 def __init__(self, mode):
     self.mode = mode
     if self.mode == "random":
         pass
     elif self.mode == "sobol":
         self.rand_gen = SobolGenerator(2)
     elif self.mode == "halton":
         self.rand_gen = Halton(2)
         self.rand_gen_1 = Halton(1)
コード例 #5
0
    def __init__(self,
                 param_distributions,
                 n_iter,
                 random_state=None,
                 method='Halton'):
        self.param_distributions = param_distributions
        self.n_iter = n_iter
        self.random_state = random_state
        self.method = method

        if method == 'Halton':
            self.Halton = Halton(len(self.param_distributions.keys()))
コード例 #6
0
class QuasiGaussianHaltonSampling(object):
    """
        A quasi-Gaussian sampler based on a Halton sequence

        :param n:       Dimensionality of the vectors to be sampled
        :param shape:   String to select between whether column (``'col'``) or row (``'row'``) vectors should be
                        returned. Defaults to column vectors
    """
    def __init__(self, n, shape='col'):

        if not halton_available:
            raise ImportError(
                "Package 'ghalton' not found, QuasiGaussianHaltonSampling not available."
            )
        self.n = n
        self.shape = (n, 1) if shape == 'col' else (1, n)
        self.halton = Halton(n)

    def next(self):
        """
            Draw the next sample from the Sampler

            :return:    A new vector sampled from a Halton sequence with mean 0 and standard deviation 1
        """
        vec = self.halton.get(1)[0]

        vec = array(norm_dist.ppf(vec))
        vec = vec.reshape(self.shape)
        return vec
コード例 #7
0
class AdvancedSampler(object):
    """Generator on parameters sampled from given distributions using
    numerical sequences. Based on the sklearn ParameterSampler.

    Non-deterministic iterable over random candidate combinations for hyper-
    parameter search. If all parameters are presented as a list,
    sampling without replacement is performed. If at least one parameter
    is given as a distribution, sampling with replacement is used.
    It is highly recommended to use continuous distributions for continuous
    parameters.

    Note that before SciPy 0.16, the ``scipy.stats.distributions`` do not
    accept a custom RNG instance and always use the singleton RNG from
    ``numpy.random``. Hence setting ``random_state`` will not guarantee a
    deterministic iteration whenever ``scipy.stats`` distributions are used to
    define the parameter search space. Deterministic behavior is however
    guaranteed from SciPy 0.16 onwards.

    Read more in the :ref:`User Guide <search>`.

    Parameters
    ----------
    param_distributions : dict
        Dictionary where the keys are parameters and values
        are distributions from which a parameter is to be sampled.
        Distributions either have to provide a ``rvs`` function
        to sample from them, or can be given as a list of values,
        where a uniform distribution is assumed.

    n_iter : integer
        Number of parameter settings that are produced.

    random_state : int or RandomState
        Pseudo random number generator state used for random uniform sampling
        from lists of possible values instead of scipy.stats distributions.

    Returns
    -------
    params : dict of string to any
        **Yields** dictionaries mapping each estimator parameter to
        as sampled value.

    Examples
    --------
    >>> from WORC.classification.AdvancedSampler import HaltonSampler
    >>> from scipy.stats.distributions import expon
    >>> import numpy as np
    >>> np.random.seed(0)
    >>> param_grid = {'a':[1, 2], 'b': expon()}
    >>> param_list = list(HaltonSampler(param_grid, n_iter=4))
    >>> rounded_list = [dict((k, round(v, 6)) for (k, v) in d.items())
    ...                 for d in param_list]
    >>> rounded_list == [{'b': 0.89856, 'a': 1},
    ...                  {'b': 0.923223, 'a': 1},
    ...                  {'b': 1.878964, 'a': 2},
    ...                  {'b': 1.038159, 'a': 2}]
    True
    """
    def __init__(self,
                 param_distributions,
                 n_iter,
                 random_state=None,
                 method='Halton'):
        self.param_distributions = param_distributions
        self.n_iter = n_iter
        self.random_state = random_state
        self.method = method

        if method == 'Halton':
            self.Halton = Halton(len(self.param_distributions.keys()))

    def __iter__(self):
        # Create a random state to be used
        rnd = check_random_state(self.random_state)

        # Generate the sequence generator
        if self.method == 'Halton':
            sequence = self.Halton.get(self.n_iter)
        elif self.method == 'Sobol':
            sequence = Sobol(len(self.param_distributions.keys()), self.n_iter)

        # Always sort the keys of a dictionary, for reproducibility
        items = sorted(self.param_distributions.items())
        for i in six.moves.range(self.n_iter):
            sample = sequence[i]
            params = dict()
            for ind, (k, v) in enumerate(items):
                point = sample[ind]
                # Check if the parameter space is a distribution or a list
                if hasattr(v, "rvs"):
                    print(point)
                    # Parameter space is a distribution, hence sample
                    params[k] = v.ppf(point)
                else:
                    # Parameter space is a list, so select an index
                    point = int(round(point * float(len(v) - 1)))
                    print(point)
                    params[k] = v[point]
            yield params

        # For reproducibility, reset sampler if needed
        if self.method == 'Halton':
            self.Halton.reset()

    def __len__(self):
        """Number of points that will be sampled."""
        return self.n_iter
コード例 #8
0
class Random_Gen(object):
    def __init__(self, mode):
        self.mode = mode
        if self.mode == "random":
            pass
        elif self.mode == "sobol":
            self.rand_gen = SobolGenerator(2)
        elif self.mode == "halton":
            self.rand_gen = Halton(2)
            self.rand_gen_1 = Halton(1)

    def generate_random(self, n, x1, x2, y1, y2):
        if self.mode == "random":
            x = np.random.randint(x1, x2 - 1, size=n, dtype='int')
            y = np.random.randint(y1, y2 - 1, size=n, dtype='int')
        elif self.mode == "sobol":
            xy = self.rand_gen.generate(n)
            x = np.rint(xy[:,0] * (x2 - x1) + x1 - 1).astype(np.int32)
            y = np.rint(xy[:,1] * (y2 - y1) + y1 - 1).astype(np.int32)
        elif self.mode == "halton":
            xy = np.array(self.rand_gen.get(n))
            x = np.rint(xy[:,0] * (x2 - x1) + x1 - 1).astype(np.int32)
            y = np.rint(xy[:,1] * (y2 - y1) + y1 - 1).astype(np.int32)

        return x, y

    # def generate_random_by_mask(self, n, x1, x2, y1, y2, mask):
    #     if mask is None:
    #         return self.generate_random(n, x1, x2, y1, y2)
    #
    #     # coordinate_scale和seed_scale是相同的,是GLOBAL_SCALE
    #     if self.mode == "halton":
    #         result_x = []
    #         result_y = []
    #         while len(result_x) < n:
    #             xy = self.rand_gen.get(1)[0]
    #             x = np.rint(xy[0] * (x2 - x1) - 1).astype(np.int32)
    #             y = np.rint(xy[1] * (y2 - y1) - 1).astype(np.int32)
    #             if mask[y, x]:
    #                 result_x.append(x + x1)
    #                 result_y.append(y + y1)
    #
    #     return np.array(result_x), np.array(result_y)

    def generate_random_by_mask(self, n, x1, x2, y1, y2, mask):
        if mask is None:
            return self.generate_random(n, x1, x2, y1, y2)

        # coordinate_scale和seed_scale是相同的,是GLOBAL_SCALE
        if self.mode == "halton":

            pos = np.nonzero(mask)

            y = np.array(pos[0]).astype(np.int32)
            x = np.array(pos[1]).astype(np.int32)

            count = len(x)
            if count > n:
                index = np.rint(np.array(self.rand_gen_1.get(n)) * (count - 1)).astype(np.int32).flatten()
                x = x[index] + x1
                y = y[index] + y1
            elif count > 0:
                space = 4
                y = (np.rint(pos[0] / space) * space)  # row
                x = (np.rint(pos[1] / space) * space)  # col

                result = set()
                for xx, yy in zip(x, y):
                    result.add((xx, yy))

                result = np.array(list(result)).astype(np.int32)
                x = result[:,0]
                y = result[:,1]

                x = x + x1
                y = y + y1
            else:
                x, y = None, None

        return x, y