-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
39 lines (31 loc) · 1.71 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
###############################################################################
# Copyright (c) 2019 Uber Technologies, Inc. #
# #
# Licensed under the Uber Non-Commercial License (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at the root directory of this project. #
# #
# See the License for the specific language governing permissions and #
# limitations under the License. #
###############################################################################
import numpy as np
def to_unit_cube(x, lb, ub):
"""Project to [0, 1]^d from hypercube with bounds lb and ub"""
assert np.all(lb < ub) and lb.ndim == 1 and ub.ndim == 1 and x.ndim == 2
xx = (x - lb) / (ub - lb)
return xx
def from_unit_cube(x, lb, ub):
"""Project from [0, 1]^d to hypercube with bounds lb and ub"""
assert np.all(lb < ub) and lb.ndim == 1 and ub.ndim == 1 and x.ndim == 2
xx = x * (ub - lb) + lb
return xx
def latin_hypercube(n_pts, dim):
"""Basic Latin hypercube implementation with center perturbation."""
X = np.zeros((n_pts, dim))
centers = (1.0 + 2.0 * np.arange(0.0, n_pts)) / float(2 * n_pts)
for i in range(dim): # Shuffle the center locataions for each dimension.
X[:, i] = centers[np.random.permutation(n_pts)]
# Add some perturbations within each box
pert = np.random.uniform(-1.0, 1.0, (n_pts, dim)) / float(2 * n_pts)
X += pert
return X