-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
executable file
·55 lines (41 loc) · 1.96 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
###############################################################################
# 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
import random
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."""
"""this assumes x in [0, 1]"""
np.random.seed(seed=int(np.random.randint(int(1e6))))
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
def convert_dtype(x):
y = []
for i in range(len(x)):
y.append(x[i])
return np.array(y)
#x = latin_hypercube(10, 2)
#print(x.shape)