-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathacquisition.py
109 lines (85 loc) · 2.97 KB
/
acquisition.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 13 14:47:04 2020
@author: Ryan Roussel
"""
import logging
logging.basicConfig(level=logging.INFO)
import numpy as np
from scipy.optimize import minimize
from scipy.stats import norm
import matplotlib.pyplot as plt
def expected_improvement(X, gpr, xi=0.01):
'''
Computes the EI at points X based on existing samples X_sample
and Y_sample using a Gaussian process surrogate model.
Args:
X: Points at which EI shall be computed (m x d).
X_sample: Sample locations (n x d).
Y_sample: Sample values (n x 1).
gpr: A GaussianProcessRegressor fitted to samples.
xi: Exploitation-exploration trade-off parameter.
Returns:
Expected improvements at points X.
'''
mu, sigma = gpr.predict(X, return_std=True)
mu_sample = gpr.predict(gpr.X_train_)
sigma = sigma.reshape(-1, 1)
# Needed for noise-based model,
# otherwise use np.max(Y_sample).
# See also section 2.4 in [...]
mu_sample_opt = np.max(mu_sample)
#logging.info(f'{mu_sample_opt} {sigma} {mu}')
with np.errstate(divide='warn'):
imp = mu - mu_sample_opt - xi
Z = imp / sigma
ei = imp * norm.cdf(Z) + sigma * norm.pdf(Z)
ei[sigma == 0.0] = 0.0
return ei
def mean(X,gpr):
'''
returns mean of gpr prediction
'''
return gpr.predict(X)
def upper_confidence_bound(X, gpr, kappa = 0.1):
'''
Upper confidence bound acquisition function
Args:
X (TYPE): Points at which the acquisition function is evaluated at.
gpr (TYPE): A GaussianProcessRegressor fitted to samples.
kappa (TYPE, optional): Exploitation vs. exploration function.
Defaults to 0.1.
Returns:
UBI at points X.
'''
mu, sigma = gpr.predict(X, return_std=True)
f = mu + kappa*sigma
#normalize to between 0 - 1
#f = f - np.min(f)
return f
def conditional_acquisition(X, X_sample, C_sample, length_scale = 1):
'''
Returns the conditional acquisition function
Args:
X (TYPE): Query Point.
X_sample (TYPE): collection of sample locations.
C_sample (TYPE): collection of sample feasibilities 0 or 1.
Returns:
Donditional value between 0 and 1 (1 == ok region).
'''
val = 1
for loc,C in zip(X_sample,C_sample):
if not C:
val = val * norm.cdf(np.linalg.norm(X - loc),scale=length_scale)
return val
def probable_improvement(X, X_sample, gpr,xi=0.0):
#set xi to non-zero if probable improvement is too greedy
mu, sigma = gpr.predict(X, return_std=True)
mu_sample = gpr.predict(X_sample)
mu_max = np.max(mu_sample)
with np.errstate(divide='warn'):
imp = mu - mu_max - xi
Z = imp / sigma
ei = norm.cdf(Z)
ei[sigma == 0.0] = 0.0
return ei