Skip to content

Commit

Permalink
Merge pull request #27 from issp-center-dev/add_examples
Browse files Browse the repository at this point in the history
  • Loading branch information
yomichi authored May 20, 2021
2 parents 55db876 + bf1b484 commit ba1e1aa
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
52 changes: 52 additions & 0 deletions examples/multiple.py
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()
64 changes: 64 additions & 0 deletions examples/multiple_score.py
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)
41 changes: 41 additions & 0 deletions examples/simple_score.py
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)

0 comments on commit ba1e1aa

Please sign in to comment.