-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from issp-center-dev/add_examples
- Loading branch information
Showing
3 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import itertools | ||
|
||
import numpy as np | ||
import physbo | ||
|
||
# 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 | ||
|
||
# score = "HVPI" | ||
score = "EHVI" | ||
|
||
a = np.linspace(-2, 2, Nx) | ||
test_X = np.array(list(itertools.product(a, a))) | ||
|
||
|
||
def vlmop2_minus(x): | ||
n = x.shape[1] | ||
y1 = 1 - np.exp(-1 * np.sum((x - 1 / np.sqrt(n)) ** 2, axis=1)) | ||
y2 = 1 - np.exp(-1 * np.sum((x + 1 / np.sqrt(n)) ** 2, axis=1)) | ||
|
||
return np.c_[-y1, -y2] | ||
|
||
|
||
class simulator(object): | ||
def __init__(self, X): | ||
self.t = vlmop2_minus(X) | ||
|
||
def __call__(self, action): | ||
return self.t[action] | ||
|
||
|
||
sim = simulator(test_X) | ||
|
||
policy = physbo.search.discrete_multi.policy(test_X, num_objectives=2) | ||
policy.set_seed(0) | ||
# Random search (10 times) | ||
policy.random_search(max_num_probes=10, simulator=sim) | ||
|
||
# Bayesian search (100 times) | ||
# score function (acquition function): expectation of improvement (EI) | ||
policy.bayes_search(max_num_probes=0, simulator=sim, score=score, interval=0) | ||
|
||
print("Pareto fronts:") | ||
res = policy.history | ||
front, front_index = res.export_pareto_front() | ||
for fr, ifr in zip(front, front_index): | ||
print(" action: ", ifr) | ||
print(" X: ", test_X[ifr, :]) | ||
print(" f: ", fr) | ||
print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import itertools | ||
|
||
import numpy as np | ||
import physbo | ||
|
||
# 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 | ||
|
||
# score = "HVPI" | ||
score = "EHVI" | ||
|
||
a = np.linspace(-2, 2, Nx) | ||
test_X = np.array(list(itertools.product(a, a))) | ||
|
||
|
||
def vlmop2_minus(x): | ||
n = x.shape[1] | ||
y1 = 1 - np.exp(-1 * np.sum((x - 1 / np.sqrt(n)) ** 2, axis=1)) | ||
y2 = 1 - np.exp(-1 * np.sum((x + 1 / np.sqrt(n)) ** 2, axis=1)) | ||
|
||
return np.c_[-y1, -y2] | ||
|
||
|
||
class simulator(object): | ||
def __init__(self, X): | ||
self.t = vlmop2_minus(X) | ||
|
||
def __call__(self, action): | ||
return self.t[action] | ||
|
||
|
||
sim = simulator(test_X) | ||
|
||
# actions = np.arange(N) | ||
# np.random.shuffle(actions) | ||
# n = 10 | ||
# actions = actions[0:n] | ||
# data = sim(actions) | ||
# policy = physbo.search.discrete_multi.policy(test_X, num_objectives=2, initial_data=[actions, data]) | ||
|
||
policy = physbo.search.discrete_multi.policy(test_X, num_objectives=2) | ||
policy.set_seed(0) | ||
# Random search (10 times) | ||
policy.random_search(max_num_probes=10, simulator=sim) | ||
|
||
# Bayesian search (100 times) | ||
# score function (acquisition function): expectation of improvement (EI) | ||
policy.bayes_search(max_num_probes=0, simulator=sim, score=score, interval=0) | ||
|
||
print("Mean values of prediction") | ||
scores = policy.get_post_fmean(xs=test_X) | ||
print(scores) | ||
print() | ||
|
||
print("Standard derivations of prediction") | ||
scores = policy.get_post_fcov(xs=test_X) | ||
print(np.sqrt(scores)) | ||
print() | ||
|
||
print("Acquisition function") | ||
scores = policy.get_score(mode=score, xs=test_X) | ||
print(scores) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import numpy as np | ||
import physbo | ||
|
||
# Make a set of candidates, test_X | ||
D = 3 # The number of params (the dimension of parameter space) | ||
N = 100 # The number of candidates | ||
test_X = np.random.randn(N, D) # Generated from Gaussian | ||
score = "EI" | ||
|
||
|
||
def simulator(actions: np.ndarray) -> np.ndarray: | ||
"""Objective function | ||
Quadratic function, -Σ_i x_i^2 | ||
Recieves an array of actions (indices of candidates) and returns the corresponding results as an array | ||
""" | ||
return -np.sum(test_X[actions, :] ** 2, axis=1) | ||
|
||
|
||
policy = physbo.search.discrete.policy(test_X) | ||
|
||
# Random search (10 times) | ||
policy.random_search(max_num_probes=10, simulator=simulator) | ||
|
||
# Bayesian search (100 times) | ||
# score function (acquisition function): expectation of improvement (EI) | ||
policy.bayes_search(max_num_probes=0, simulator=simulator, score=score) | ||
|
||
print("Mean values of prediction") | ||
scores = policy.get_post_fmean(xs=test_X) | ||
print(scores) | ||
print() | ||
|
||
print("Standard derivations of prediction") | ||
scores = policy.get_post_fcov(xs=test_X) | ||
print(np.sqrt(scores)) | ||
print() | ||
|
||
print("Acquisition function") | ||
scores = policy.get_score(mode=score, xs=test_X) | ||
print(scores) |