Skip to content

Commit

Permalink
format by black
Browse files Browse the repository at this point in the history
yomichi committed Apr 6, 2023
1 parent c89d595 commit ed48b47
Showing 25 changed files with 116 additions and 71 deletions.
2 changes: 1 addition & 1 deletion examples/multiple_score.py
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
# Make a set of candidates, test_X
D = 2 # The number of params (the dimension of parameter space)
Nx = 11 # The number of candidates
N = Nx*Nx
N = Nx * Nx

# score = "HVPI"
score = "EHVI"
2 changes: 1 addition & 1 deletion examples/simple.py
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@


def simulator(actions: np.ndarray) -> np.ndarray:
""" Objective function
"""Objective function
Quadratic function, -Σ_i x_i^2
Receives an array of actions (indices of candidates) and returns the corresponding results as an array
2 changes: 1 addition & 1 deletion examples/simple_time.py
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@


def simulator(actions: np.ndarray) -> np.ndarray:
""" Objective function
"""Objective function
Quadratic function, -Σ_i x_i^2
Receives an array of actions (indices of candidates) and returns the corresponding results as an array
3 changes: 2 additions & 1 deletion physbo/gp/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .prior import prior
from .model import model
#from predictor import predictor

# from predictor import predictor
4 changes: 2 additions & 2 deletions physbo/gp/core/learning.py
Original file line number Diff line number Diff line change
@@ -309,7 +309,7 @@ def get_one_update(self, params, X, t):


class adam(online):
""" default """
"""default"""

def __init__(self, gp, config):
"""
@@ -351,7 +351,7 @@ def get_one_update(self, params, X, t):
"""
grad = self.gp.get_grad_marlik(params, X, t)
self.m = self.m * self.beta + grad * (1 - self.beta)
self.v = self.v * self.gamma + grad ** 2 * (1 - self.gamma)
self.v = self.v * self.gamma + grad**2 * (1 - self.gamma)
hat_m = self.m / (1 - self.beta ** (self.num_iter))
hat_v = self.v / (1 - self.gamma ** (self.num_iter))
return -self.alpha * hat_m / (np.sqrt(hat_v) + self.epsilon)
2 changes: 1 addition & 1 deletion physbo/gp/core/model.py
Original file line number Diff line number Diff line change
@@ -301,7 +301,7 @@ def post_sampling(self, X, Z, params=None, N=1, alpha=1):

fmean = self.get_post_fmean(X, Z, params=None)
fcov = self.get_post_fcov(X, Z, params=None, diag=False)
return np.random.multivariate_normal(fmean, fcov * alpha ** 2, N)
return np.random.multivariate_normal(fmean, fcov * alpha**2, N)

def predict_sampling(self, X, Z, params=None, N=1):
"""
2 changes: 1 addition & 1 deletion physbo/gp/core/prior.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@


class prior:
""" prior of gaussian process """
"""prior of gaussian process"""

def __init__(self, mean, cov):
"""
10 changes: 5 additions & 5 deletions physbo/gp/cov/gauss.py
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@


class gauss:
""" gaussian kernel """
"""gaussian kernel"""

def __init__(
self,
@@ -68,7 +68,7 @@ def print_params(self):
print(" Parameters of Gaussian kernel \n ")
print(" width = ", +self.width)
print(" scale = ", +self.scale)
print(" scale2 = ", +self.scale ** 2)
print(" scale2 = ", +self.scale**2)
print(" \n")

def prepare(self, params=None):
@@ -122,7 +122,7 @@ def get_grad(self, X, params=None):
grad[0 : self.num_params - 1, :, :] = grad_width64(X, width, G)
else:
pairwise_dists = spatial.distance.pdist(X / width, "euclidean")
grad[0, :, :] = G * spatial.distance.squareform(pairwise_dists ** 2)
grad[0, :, :] = G * spatial.distance.squareform(pairwise_dists**2)

grad[-1, :, :] = 2 * G
return grad
@@ -150,7 +150,7 @@ def get_cov(self, X, Z=None, params=None, diag=False):
covariant matrix
"""
params, width, scale = self.prepare(params)
scale2 = scale ** 2
scale2 = scale**2

if Z is None:
if diag:
@@ -332,7 +332,7 @@ def rand_expans(self, num_basis, params=None):
tupple (W, b, amp)
"""
params, width, scale = self.prepare(params)
scale2 = scale ** 2
scale2 = scale**2
amp = np.sqrt((2 * scale2) / num_basis)
W = np.random.randn(num_basis, self.num_dim) / width
b = np.random.rand(num_basis) * 2 * np.pi
6 changes: 3 additions & 3 deletions physbo/gp/lik/gauss.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@


class gauss:
""" Gaussian likelihood function """
"""Gaussian likelihood function"""

def __init__(self, std=1, max_params=1e6, min_params=1e-6):
"""
@@ -99,7 +99,7 @@ def get_cov(self, num_data, params=None):
Diagonal element matrix of exp(2.0*params)
"""
std = self.trans_params(params)
var = std ** 2
var = std**2
return var * np.identity(num_data)

def get_grad(self, num_data, params=None):
@@ -119,7 +119,7 @@ def get_grad(self, num_data, params=None):
Diagonal element matrix of 2.0 * exp(2.0*params)
"""
std = self.trans_params(params)
var = std ** 2
var = std**2
return var * np.identity(num_data) * 2

def set_params(self, params):
2 changes: 1 addition & 1 deletion physbo/gp/mean/const.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@


class const:
""" constant """
"""constant"""

def __init__(self, params=None, max_params=1e12, min_params=-1e12):
"""
2 changes: 1 addition & 1 deletion physbo/gp/mean/zero.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@


class zero:
""" zero """
"""zero"""

def __init__(self):
self.num_params = 0
2 changes: 1 addition & 1 deletion physbo/gp/predictor.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@


class predictor(physbo.predictor.base_predictor):
""" predictor """
"""predictor"""

def __init__(self, config, model=None):
"""
2 changes: 1 addition & 1 deletion physbo/misc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .centering import centering
from .centering import centering
from .gauss_elim import gauss_elim
from .set_config import set_config
from ._src.diagAB import diagAB_64 as diagAB
2 changes: 1 addition & 1 deletion physbo/opt/adam.py
Original file line number Diff line number Diff line change
@@ -72,7 +72,7 @@ def update(self, params, *args, **kwargs):
"""
g = self.grad(params, *args, **kwargs)
self.m = self.m * self.beta + g * (1 - self.beta)
self.v = self.v * self.gamma + g ** 2 * (1 - self.gamma)
self.v = self.v * self.gamma + g**2 * (1 - self.gamma)
hat_m = self.m / (1 - self.beta ** (self.epoch + 1))
hat_v = self.v / (1 - self.gamma ** (self.epoch + 1))
self.epoch += 1
13 changes: 9 additions & 4 deletions physbo/search/discrete/policy.py
Original file line number Diff line number Diff line change
@@ -60,7 +60,9 @@ def __init__(self, test_X, config=None, initial_data=None, comm=None):
self.mpisize = comm.size
self.mpirank = comm.rank
self.actions = np.array_split(self.actions, self.mpisize)[self.mpirank]
self.config.learning.is_disp = (self.config.learning.is_disp and self.mpirank == 0)
self.config.learning.is_disp = (
self.config.learning.is_disp and self.mpirank == 0
)

def set_seed(self, seed):
"""
@@ -130,7 +132,9 @@ def write(
)
self.training.add(X=X, t=t, Z=Z)
local_index = np.searchsorted(self.actions, action)
local_index = local_index[np.take(self.actions, local_index, mode='clip') == action]
local_index = local_index[
np.take(self.actions, local_index, mode="clip") == action
]
self.actions = self._delete_actions(local_index)
if self.new_data is None:
self.new_data = variable(X=X, t=t, Z=Z)
@@ -168,7 +172,6 @@ def random_search(
utility.show_interactive_mode(simulator, self.history)

for n in range(0, max_num_probes):

time_total = time.time()
if is_disp and N > 1:
utility.show_start_message_multi_search(self.history.num_runs)
@@ -479,7 +482,9 @@ def _get_marginal_score(self, mode, chosen_actions, K, alpha):

# draw K samples of the values of objective function of chosen actions
new_test_local = self.test.get_subset(chosen_actions)
virtual_t_local = self.predictor.get_predict_samples(self.training, new_test_local, K)
virtual_t_local = self.predictor.get_predict_samples(
self.training, new_test_local, K
)
if self.mpisize == 1:
new_test = new_test_local
virtual_t = virtual_t_local
8 changes: 4 additions & 4 deletions physbo/search/discrete/results.py
Original file line number Diff line number Diff line change
@@ -22,19 +22,19 @@ def __init__(self):

@property
def time_total(self):
return copy.copy(self.time_total_[0:self.num_runs])
return copy.copy(self.time_total_[0 : self.num_runs])

@property
def time_update_predictor(self):
return copy.copy(self.time_update_predictor_[0:self.num_runs])
return copy.copy(self.time_update_predictor_[0 : self.num_runs])

@property
def time_get_action(self):
return copy.copy(self.time_get_action_[0:self.num_runs])
return copy.copy(self.time_get_action_[0 : self.num_runs])

@property
def time_run_simulator(self):
return copy.copy(self.time_run_simulator_[0:self.num_runs])
return copy.copy(self.time_run_simulator_[0 : self.num_runs])

def write(
self,
24 changes: 16 additions & 8 deletions physbo/search/discrete_multi/policy.py
Original file line number Diff line number Diff line change
@@ -100,7 +100,9 @@ def write(
self.new_data_list[i].add(X=X, t=t[:, i], Z=Z)
self.training_list[i].add(X=X, t=t[:, i], Z=Z)
local_index = np.searchsorted(self.actions, action)
local_index = local_index[np.take(self.actions, local_index, mode='clip') == action]
local_index = local_index[
np.take(self.actions, local_index, mode="clip") == action
]
self.actions = self._delete_actions(local_index)

def _model(self, i):
@@ -123,7 +125,6 @@ def random_search(
is_disp=True,
disp_pareto_set=False,
):

if self.mpirank != 0:
is_disp = False

@@ -133,7 +134,6 @@ def random_search(
utility.show_interactive_mode(simulator, self.history)

for n in range(0, max_num_probes):

time_total = time.time()
if is_disp and N > 1:
utility.show_start_message_multi_search(
@@ -181,7 +181,6 @@ def bayes_search(
interval=0,
num_rand_basis=0,
):

if self.mpirank != 0:
is_disp = False

@@ -418,14 +417,18 @@ def _get_marginal_score(self, mode, chosen_actions, K, alpha):
virtual_t_list = [np.zeros((K, 0)) for _ in range(self.num_objectives)]
for i in range(self.num_objectives):
new_test_local = self.test_list[i].get_subset(chosen_actions)
virtual_t_local = self.predictor_list[i].get_predict_samples(self.training_list[i], new_test_local, K)
virtual_t_local = self.predictor_list[i].get_predict_samples(
self.training_list[i], new_test_local, K
)
if self.mpisize == 1:
new_test_list[i] = new_test_local
virtual_t_list[i] = virtual_t_local
else:
for nt in self.mpicomm.allgather(new_test_local):
new_test_list[i].add(X=nt.X, t=nt.t, Z=nt.Z)
virtual_t_list[i] = np.concatenate(self.mpicomm.allgather(virtual_t_local), axis=1)
virtual_t_list[i] = np.concatenate(
self.mpicomm.allgather(virtual_t_local), axis=1
)

for k in range(K):
predictor_list = [copy.deepcopy(p) for p in self.predictor_list]
@@ -438,12 +441,17 @@ def _get_marginal_score(self, mode, chosen_actions, K, alpha):
if virtual_train.Z is None:
training_list[i].add(virtual_train.X, virtual_train.t)
else:
training_list[i].add(virtual_train.X, virtual_train.t, virtual_train.Z)
training_list[i].add(
virtual_train.X, virtual_train.t, virtual_train.Z
)

predictor_list[i].update(training_list[i], virtual_train)

f[k, :] = self.get_score(
mode, predictor_list=predictor_list, training_list=training_list, parallel=False
mode,
predictor_list=predictor_list,
training_list=training_list,
parallel=False,
)
return np.mean(f, axis=0)

8 changes: 4 additions & 4 deletions physbo/search/discrete_multi/results.py
Original file line number Diff line number Diff line change
@@ -25,19 +25,19 @@ def __init__(self, num_objectives):

@property
def time_total(self):
return copy.copy(self._time_total[0:self.num_runs])
return copy.copy(self._time_total[0 : self.num_runs])

@property
def time_update_predictor(self):
return copy.copy(self._time_update_predictor[0:self.num_runs])
return copy.copy(self._time_update_predictor[0 : self.num_runs])

@property
def time_get_action(self):
return copy.copy(self._time_get_action[0:self.num_runs])
return copy.copy(self._time_get_action[0 : self.num_runs])

@property
def time_run_simulator(self):
return copy.copy(self._time_run_simulator[0:self.num_runs])
return copy.copy(self._time_run_simulator[0 : self.num_runs])

def write(
self,
1 change: 1 addition & 0 deletions physbo/search/score_multi.py
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ def score(mode, predictor_list, test, training_list, **kwargs):
raise NotImplementedError("mode must be EHVI, HVPI or TS.")
return f


def HVPI(fmean, fstd, pareto):
"""
Calculate Hypervolume-based Probability of Improvement (HVPI).
2 changes: 1 addition & 1 deletion tests/integrated/test_gp.py
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ def __init__(self, noise=1.0e-5):
np.random.seed(12345)
self.nslice = 11
self.dim = 2
self.N = self.nslice ** self.dim
self.N = self.nslice**self.dim
self.X = np.zeros((self.N, self.dim))
self.t = np.zeros(self.N)
for i, x in enumerate(
14 changes: 10 additions & 4 deletions tests/integrated/test_interactive_example.py
Original file line number Diff line number Diff line change
@@ -16,9 +16,11 @@ class simulator:
def __init__(self):
self.nslice = 11
self.dim = 2
self.N = self.nslice ** self.dim
self.N = self.nslice**self.dim
self.X = np.zeros((self.N, self.dim))
for i, x in enumerate(product(np.linspace(0.0, 1.0, self.nslice), repeat=self.dim)):
for i, x in enumerate(
product(np.linspace(0.0, 1.0, self.nslice), repeat=self.dim)
):
self.X[i, :] = list(x)

def __call__(self, actions):
@@ -32,14 +34,18 @@ def test_interactive():
policy = physbo.search.discrete.policy(test_X=sim.X)
policy.set_seed(12345)

actions = policy.random_search(max_num_probes=1, num_search_each_probe=nrand, simulator=None)
actions = policy.random_search(
max_num_probes=1, num_search_each_probe=nrand, simulator=None
)
targets = sim(actions)
print(actions)
print(targets)
policy.write(actions, targets)
physbo.search.utility.show_search_results(policy.history, nrand)

actions = policy.bayes_search(max_num_probes=1, num_search_each_probe=nsearch, simulator=None, score='TS')
actions = policy.bayes_search(
max_num_probes=1, num_search_each_probe=nsearch, simulator=None, score="TS"
)
targets = sim(actions)
print(actions)
print(targets)
Loading

0 comments on commit ed48b47

Please sign in to comment.