def __init__(self, dimensions: int, function: Optional[Callable[[np.ndarray], Sequence[L]]] = None, domain: Optional[Sequence[Tuple[float, float]]] = None, **kwargs): """Initialize vector space data. If no function is specified, data are unlabeled. If a domain is specified, samples must be within that domain. Parameters: dimensions: dimensionality of vector space; positive finite integer function: a function that accepts a real matrix (vectors are rows) and returns a corresponding sequence of labels. If not specified, Data are unlabeled. domain: domain in the form of a hypercube, if specified; given as a sequence of intervals [a,b], where a <= b. If only a single interval is specified it is used for all dimensions. Raises: InvalidParameterError for invalid arguments. """ self._dimensions = params.integer(dimensions, above=0) self._function = params.optional_( function, lambda arg: params.callable(arg, num_pos_or_kw=1)) self._domain = params.optional_( domain, lambda arg: params.hypercube_domain(arg, self._dimensions)) super().__init__(*kwargs)
def __init__(self, **kwargs): """Initialize state.""" dimensions = 2 domain = params.hypercube_domain((-3, 3), dimensions=dimensions) super().__init__( dimensions=dimensions, function=self.__class__.doubleWell, domain=domain, **kwargs )
def __init__(self, dimensions=6, **kwargs): """Initialize state. Parameters: dimensions: dimensionality; at least 5; 6 in original paper; higher dimensions do not change function Raises: InvalidParameterError: on invalid parameter values """ dimensions = params.integer(dimensions, from_=5) domain = params.hypercube_domain((0, 1), dimensions=dimensions) super().__init__( dimensions=dimensions, function=self.__class__.friedman1979, domain=domain, **kwargs )
def __init__(self, size, domain: Optional[Any] = None, rng=None, **kwargs): """Initialize sampler. Parameters: size: number of vector samples to draw domain: (sub)domain to sample from; default is to use the data's domain if available, or the unit hypercube otherwise rng: pseudo-random number generator used Returns: IndexedFiniteData of vectors """ super().__init__(rng=rng, **kwargs) self._size = params.integer(size, from_=0) # no upper bound on number of vectors to draw self._domain = params.optional_(domain, lambda arg: params.hypercube_domain(arg))
def __init__(self, dimensions: int, **kwargs): """Initialize Schwefel 26 test function. Parameters: dimensions: dimensionality of the problem Raises: InvalidParameterError: on invalid parameter values Examples: __init__(dimension=2) """ dimensions = params.integer(dimensions, above=0) domain = params.hypercube_domain((-500, 500), dimensions=dimensions) super().__init__(dimensions=dimensions, function=self.__class__.schwefel26_1981, domain=domain, **kwargs)
def apply(self, data: Data, **kwargs) -> Data: """Draw random vectors. Parameters: data: Data to draw from Returns: TabularData of vectors """ data = params.instance(data, Data) if self._domain is None: if data.domain is None: domain = np.asarray([[0, 1]] * data.dimensions) else: domain = data.domain else: domain = params.hypercube_domain( self._domain, dimensions=data.dimensions ) # checks dimensionality (see __init__) for low, high in domain: if low == -np.inf or high == np.inf: raise BenchmarkError("can not sample from infinite domain") # vectors = np.transpose( # np.asfarray( # [ # self.random.uniform(low=low, high=high, size=self._size) # for (low, high) in self._domain # ] # ) # ) # this version avoids the python loop for efficiency in high dimensions vectors = ( self.random.uniform(size=(self._size, data.dimensions)) * (domain[:, 1] - domain[:, 0]) + domain[:, 0] # noqa W503 ) return data.subset(vectors)
def full_grid(self, data: VectorSpaceData, samples_per_dim: int, domain=None): """Full multi-dimensional evenly-spaced grid. For one sample per dimension, the result is a single vector, the mean of the domain. Parameters: data: sampled dataset samples_per_dim: number of evenly-spaced samples to take in each dimension domain: (sub)domain to sample from; by default, data's domain is used Returns: two-dimensional NumPy array where samples are rows """ data = params.instance(data, VectorSpaceData) k = params.integer(samples_per_dim, above=0) # positive integer domain = data.domain if domain is None else domain domain = params.hypercube_domain(domain, data.dimensions) if k == 1: return np.mean(domain, axis=1).reshape((1, -1)) locs = (np.linspace(xfrom, xto, k) for xfrom, xto in domain) return np.asfarray(list(itertools.product(*locs)))
def test_hypercube_domain_2(): """Accumulated test cases.""" assert (params.hypercube_domain([(0, np.inf)]) == np.asfarray([[0, np.inf]])).all() assert (params.hypercube_domain((0, np.inf), dimensions=1) == np.asfarray([[0, np.inf]])).all()
def test_hypercube_domain_1(): """Tests hypercube vector space domains.""" # without dimensionality assert (params.hypercube_domain([[1, 2]]) == np.asfarray([[1, 2]])).all() assert (params.hypercube_domain([[1, 2], [3, 4]]) == np.asfarray([[1, 2], [3, 4]])).all() with pytest.raises(InvalidParameterError): params.hypercube_domain([1, 2]) with pytest.raises(InvalidParameterError): params.hypercube_domain([[2, 1]]) # with dimensionality assert (params.hypercube_domain([[1, 2]], dimensions=1) == np.asfarray([[1, 2]])).all() assert ( params.hypercube_domain([[1, 2], [3, 4]], dimensions=2) == np.asfarray([[1, 2], [3, 4]]) ).all() assert ( params.hypercube_domain([1, 2], dimensions=3) == np.asfarray([[1, 2], [1, 2], [1, 2]]) ).all() with pytest.raises(InvalidParameterError): params.hypercube_domain([[1, 2], [2, 1]], dimensions=2)