Skip to content
Open
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
53 changes: 2 additions & 51 deletions blackbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import multiprocessing as mp
import numpy as np
import scipy.optimize as op
from scipy.interpolate import Rbf as rbf

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import Rbf() function from scipy as rbf().

import datetime


Expand Down Expand Up @@ -116,7 +117,7 @@ def cubetobox(x):
str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) + ' ...')

# sampling next batch of points
fit = rbf(points)
fit = rbf(*np.transpose(points), function='cubic')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New rbf() function requires another input format. The input list is transposed and then unpacked with *.

Old: [ [ x1, x2, ... , xd, val ] , [ ... ] , ... ]
New: [ x1, ... ] , [ x2, ... ] , ... , [ xd, ... ] , [ val, ... ]

Original function included cubic basis function, so now it's called manually in arguments.

points = np.append(points, np.zeros((batch, d+1)), axis=0)

for j in range(batch):
Expand Down Expand Up @@ -168,53 +169,3 @@ def rseq(n, d):
points = np.array([(0.5 + alpha*(i+1)) % 1 for i in range(n)])

return points


def rbf(points):

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Old rbf() function is completely removed.

"""
Build RBF-fit for given points (see Holmstrom, 2008 for details).

Parameters
----------
points : ndarray
Array of multi-d points with corresponding values [[x1, x2, .., xd, val], ...].

Returns
-------
fit : callable
Function that returns the value of the RBF-fit at a given point.
"""
n = len(points)
d = len(points[0])-1

def phi(r):
return r*r*r

Phi = [[phi(np.linalg.norm(np.subtract(points[i, 0:-1], points[j, 0:-1]))) for j in range(n)] for i in range(n)]

P = np.ones((n, d+1))
P[:, 0:-1] = points[:, 0:-1]

F = points[:, -1]

M = np.zeros((n+d+1, n+d+1))
M[0:n, 0:n] = Phi
M[0:n, n:n+d+1] = P
M[n:n+d+1, 0:n] = np.transpose(P)

v = np.zeros(n+d+1)
v[0:n] = F

try:
sol = np.linalg.solve(M, v)
except:
# might help with singular matrices
print('Singular matrix occurred during RBF-fit construction. RBF-fit might be inaccurate!')
sol = np.linalg.lstsq(M, v)[0]

lam, b, a = sol[0:n], sol[n:n+d], sol[n+d]

def fit(x):
return sum(lam[i]*phi(np.linalg.norm(np.subtract(x, points[i, 0:-1]))) for i in range(n)) + np.dot(b, x) + a

return fit