import pysgpp
'''
In this example we create a B-spline based surrogate for a function with
vectorial output. We do this through the convenience class
'SplineResponseSurface'. For functions with vectorial output the analogous
class 'SplineResponseSurfaceVector' can be used.
The advantage of these classes is that only few lines of code suffice to create
the surrogate and to calculate various quantities of interest.
'''
class objFuncSGpp(pysgpp.ScalarFunction):
def __init__(self, dim):
super(objFuncSGpp, self).__init__(dim)
def eval(self, x):
return x[0]*x[1]+x[1]
dim = 2
objFunc = objFuncSGpp(dim)
lb = pysgpp.DataVector([0, 0])
ub = pysgpp.DataVector([1, 1])
degree = 3
gridType = 'nakBsplineBoundary'
reSurf = pysgpp.SplineResponseSurface(
objFunc, lb, ub, pysgpp.Grid.stringToGridType(gridType), degree)
numPoints = 30
initialLevel = 1
numRefine = 5
verbose = False
reSurf.surplusAdaptive(numPoints, initialLevel, numRefine, verbose)
evalPoint = pysgpp.DataVector([0.3, 0.6])
print(f'reSurf: {reSurf.eval(evalPoint)} truth: {objFunc.eval(evalPoint)}')
gradient = pysgpp.DataVector(dim, 0)
reSurf.evalGradient(evalPoint, gradient)
print(f'gradient: {gradient.toString()}')
print(f'integral: {reSurf.getIntegral()}')
pdfs = pysgpp.DistributionsVector()
pdfs.push_back(pysgpp.DistributionNormal(0.5, 0.1))
pdfs.push_back(pysgpp.DistributionUniform(0, 1))
quadOrder = 15
print(f'mean: {reSurf.getMean(pdfs,quadOrder)}')
v = reSurf.getVariance(pdfs, quadOrder)
print(f'variance: {v[0]}')
print(f'Optimum: {reSurf.optimize()}')