Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added examples #27

Merged
merged 2 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)