Example #1
0
    def computeMean(self, grid, alpha, newGridPoints):
        gs = grid.getStorage()
        opEval = createOperationEval(grid)

        newGs = grid.getStorage()
        newNodalValues = dehierarchize(grid, alpha)
        p = DataVector(gs.dim())

        for newGp in newGridPoints:
            # run over all grid points of current level
            newGp.getCoords(p)
            pp = DataVector(p)
            i = newGs.seq(newGp)
            newNodalValues[i] = 0.
            for d in xrange(gs.dim()):
                # get current index
                index = newGp.getIndex(d)
                level = newGp.getLevel(d)
                # bounds
                xlow, xhigh = getBoundsOfSupport(level, index)
                # compute function values at bounds
                pp[d] = xlow
                fxlow = opEval.eval(alpha, pp)
                pp[d] = xhigh
                fxhigh = opEval.eval(alpha, pp)
                # interpolate linearly
                a = (fxhigh - fxlow) / (xlow - xhigh)
                newNodalValues[i] += a * (p[d] - xlow) + fxhigh
                # reset pp and set sumWeights
                pp[d] = p[d]
            newNodalValues[i] /= gs.dim()

        return newNodalValues
    def computeMean(self, grid, alpha, newGridPoints):
        gs = grid.getStorage()
        opEval = createOperationEval(grid)

        newGs = grid.getStorage()
        newNodalValues = dehierarchize(grid, alpha)
        p = DataVector(gs.dim())

        for newGp in newGridPoints:
            # run over all grid points of current level
            newGp.getCoords(p)
            pp = DataVector(p)
            i = newGs.seq(newGp)
            newNodalValues[i] = 0.
            for d in xrange(gs.dim()):
                # get current index
                index = newGp.getIndex(d)
                level = newGp.getLevel(d)
                # bounds
                xlow, xhigh = getBoundsOfSupport(level, index)
                # compute function values at bounds
                pp[d] = xlow
                fxlow = opEval.eval(alpha, pp)
                pp[d] = xhigh
                fxhigh = opEval.eval(alpha, pp)
                # interpolate linearly
                a = (fxhigh - fxlow) / (xlow - xhigh)
                newNodalValues[i] += a * (p[d] - xlow) + fxhigh
                # reset pp and set sumWeights
                pp[d] = p[d]
            newNodalValues[i] /= gs.dim()

        return newNodalValues
    def general_test(self, d, l, bb, x):

        test_desc = "dim=%d, level=%d, bb=%s, x=%s" % (d, l, bb, x)
        print(test_desc)

        self.grid = Grid.createLinearGrid(d)
        self.grid_gen = self.grid.getGenerator()
        self.grid_gen.regular(l)

        alpha = DataVector([1] * self.grid.getSize())

        bb_ = BoundingBox(d)

        for d_k in range(d):
            dimbb = BoundingBox1D()
            dimbb.leftBoundary = bb[d_k][0]
            dimbb.rightBoundary = bb[d_k][1]
            bb_.setBoundary(d_k, dimbb)

        # Calculate the expected value without the bounding box

        expected = 0.0

        inside = True

        x_trans = DataVector(d)
        opEval = createOperationEval(self.grid)

        for d_k in range(d):
            if not (bb[d_k][0] <= x[d_k] and x[d_k] <= bb[d_k][1]):
                inside = False
                break
            else:
                x_trans[d_k] = (x[d_k] - bb[d_k][0]) / (bb[d_k][1] -
                                                        bb[d_k][0])

        if inside:
            p = DataVector(x_trans)
            expected = opEval.eval(alpha, p)
        else:
            expected = 0.0

        # Now set the bounding box

        self.grid.getStorage().setBoundingBox(bb_)

        p = DataVector(x)
        actual = opEval.eval(alpha, p)

        self.assertAlmostEqual(actual, expected)

        del self.grid
Example #4
0
    def findCandidates(self, grid, alpha, addedGridPoints):
        fullGridStorage = self.fullGrid.getStorage()
        gs = grid.getStorage()

        if self.iteration == 0:
            self.costs += fullGridStorage.getSize()
        elif len(addedGridPoints) == 0:
            return

        opEval = createOperationEval(grid)
        for i in range(fullGridStorage.getSize()):
            gp = fullGridStorage.getPoint(i)
            if not gs.isContaining(gp):
                self.candidates.append(HashGridPoint(gp))
Example #5
0
    def lookupFullGridPoints(self, grid, alpha, candidates):
        acc = []
        gs = grid.getStorage()
        p = DataVector(gs.dim())
        opEval = createOperationEval(grid)
        # TODO: find local max level for adaptively refined grids
        maxLevel = gs.getMaxLevel()
        if grid.getType() in [LinearBoundary, PolyBoundary]:
            maxLevel += 1

        for gp in candidates:
            for d in xrange(gs.dim()):
                if 0 < gp.getLevel(d) < maxLevel:
                    self.lookupFullGridPointsRec1d(grid, alpha, gp, d, p,
                                                   opEval, maxLevel, acc)
        return acc
def testFG(obj, grid, level, function):
    node_values = None
    node_values_back = None
    alpha = None
    points = None
    p = None
    l_user = 2
    # generate a regular test grid
    generator = grid.createGridGenerator()
    generator.truncated(level, l_user)

    storage = grid.getStorage()
    dim = storage.dim()

    # generate the node_values vector
    fgs = FullGridSet(dim, level, l_user)
    node_values = DataVector(storage.size())
    for i in xrange(fgs.getSize()):
        fg = fgs.at(i)
        m = fg.getSize()
        for j in xrange(m):
            points = fg.getCoordsString(j).split()
            d = evalFunction(function, points)
            fg.set(j, d)
    fgs.reCompose(storage, node_values)
    createOperationHierarchisation(grid).doHierarchisation(node_values)
    evalOp = createOperationEval(grid)
    p = DataVector(dim)
    # Extensions in C and C++ -- Extension modules and extension types can be written by hand. There are also tools that help with this, for example, SWIG, sip, Pyrex. perationEval()
    for m in range(10):
        points = []
        for k in range(dim):
            p[k] = random.random()
            points.append(str(p[k]))
        for j in range(fgs.getSize()):
            fg = fgs.at(j)
            fg.eval(p)
        if (abs(evalOp.eval(node_values, p) - evalFunction(function, points)) >
                0.01):
            print points
            print evalOp.eval(node_values, p)
            print evalFunction(function, points)
            obj.fail()
        obj.failUnlessAlmostEqual(evalOp.eval(node_values, p),
                                  fgs.combinedResult())
def testFG(obj, grid, level, function):
    node_values = None
    node_values_back = None
    alpha = None
    points = None
    p = None
    l_user=2;
    # generate a regular test grid
    generator = grid.createGridGenerator()
    generator.truncated(level,l_user)

    storage = grid.getStorage()
    dim = storage.dim()

    # generate the node_values vector
    fgs = FullGridSet(dim,level, l_user)
    node_values = DataVector(storage.size())
    for i in xrange(fgs.getSize()):
        fg=fgs.at(i)  
        m=fg.getSize()
        for j in xrange(m):
             points=fg.getCoordsString(j).split()        
             d=evalFunction(function, points)          
             fg.set(j,d) 
    fgs.reCompose(storage,node_values)     
    createOperationHierarchisation(grid).doHierarchisation(node_values);
    evalOp = createOperationEval(grid)
    p=DataVector(dim)
# Extensions in C and C++ -- Extension modules and extension types can be written by hand. There are also tools that help with this, for example, SWIG, sip, Pyrex. perationEval()
    for m in range(10):
        points = []
        for k in range(dim):
	    p[k]=random.random()
            points.append(str(p[k]))
        for j in range(fgs.getSize()):
            fg=fgs.at(j)  
            fg.eval(p)
        if (abs(evalOp.eval(node_values, p)-evalFunction(function,points))>0.01):  
            print points
            print evalOp.eval(node_values,p)
            print evalFunction(function,points)
            obj.fail()
        obj.failUnlessAlmostEqual(evalOp.eval(node_values,p),fgs.combinedResult())
Example #8
0
def evalSGFunction(grid, alpha, p):
    try:
        # raise Exception()
        return createOperationEval(grid).eval(alpha, p)
    except Exception:
        # import ipdb; ipdb.set_trace()
        # alternative
        basis = getBasis(grid)
        gs = grid.getStorage()

        res = 0.0
        for i in xrange(gs.size()):
            gp = gs.get(i)
            val = 1.0
            for d in xrange(gs.dim()):
                x = max(0.0, basis.eval(gp.getLevel(d), gp.getIndex(d), p[d]))
                val *= x
            res += alpha[i] * val
        return res
Example #9
0
def evalSGFunction(grid, alpha, p):
    try:
        # raise Exception()
        return createOperationEval(grid).eval(alpha, p)
    except Exception:
        # import ipdb; ipdb.set_trace()
        # alternative
        basis = getBasis(grid)
        gs = grid.getStorage()

        res = 0.0
        for i in xrange(gs.size()):
            gp = gs.get(i)
            val = 1.0
            for d in xrange(gs.dim()):
                x = max(0.0, basis.eval(gp.getLevel(d), gp.getIndex(d), p[d]))
                val *= x
            res += alpha[i] * val
        return res
Example #10
0
def evalSGFunction(grid, alpha, p, isConsistent=True):
    if grid.getSize() != len(alpha):
        raise AttributeError(
            "grid size differs from length of coefficient vector")
    if len(p.shape) == 1:
        if grid.getStorage().getDimension() != p.shape[0]:
            raise AttributeError(
                "grid dimension differs from dimension of sample")

        evalNaiveGridTypes = [
            GridType_Bspline, GridType_BsplineClenshawCurtis,
            GridType_BsplineBoundary, GridType_ModBsplineClenshawCurtis,
            GridType_ModBspline, GridType_LinearClenshawCurtis,
            GridType_LinearClenshawCurtisBoundary,
            GridType_ModLinearClenshawCurtis, GridType_PolyClenshawCurtis,
            GridType_PolyClenshawCurtisBoundary, GridType_ModPolyClenshawCurtis
        ]

        p_vec = DataVector(p)
        alpha_vec = DataVector(alpha)
        if isConsistent:
            if grid.getType() in evalNaiveGridTypes:
                opEval = createOperationEvalNaive(grid)
            else:
                opEval = createOperationEval(grid)
        else:
            opEval = createOperationEvalNaive(grid)

        ans = opEval.eval(alpha_vec, p_vec)

        del opEval
        del alpha_vec
        del p_vec

        return ans
    else:
        if grid.getStorage().getDimension() != p.shape[1]:
            raise AttributeError(
                "grid dimension differs from dimension of samples")
        return evalSGFunctionMulti(grid, alpha, p, isConsistent)
Example #11
0
    def computeMin(self, i, grid, alpha, nodalValues):
        gs = grid.getStorage()
        numDims = gs.getDimension()

        opEval = createOperationEval(grid)
        alphaVec = DataVector(alpha)

        gp = gs.getPoint(i)
        p = DataVector(numDims)
        gp.getStandardCoordinates(p)
        value = float("inf")

        level, index = getLevelIndex(gp)
        for idim in range(numDims):
            left, right = getGridPointsOnBoundary(level[idim], index[idim])

            if left is not None:
                llevel, lindex = left
                p[idim] = 2**-llevel * lindex
                leftValue = opEval.eval(alphaVec, p)
            else:
                leftValue = 0.0

            if right is not None:
                rlevel, rindex = right
                p[idim] = 2**-rlevel * rindex
                rightValue = opEval.eval(alphaVec, p)
            else:
                rightValue = 0.0

            interpolatedValue = abs(leftValue - rightValue) / 2

            if interpolatedValue < value:
                value = interpolatedValue

            # reset p
            p[idim] = 2**-level[idim] * index[idim]

        return value
Example #12
0
## function value at the grid point's coordinates which are obtained by
## \c getStandardCoordinate(dim).
## The current coefficient vector is then printed.
for i in range(gridStorage.getSize()):
    gp = gridStorage.getPoint(i)
    alpha[i] = f(gp.getStandardCoordinate(0), gp.getStandardCoordinate(1))

print("alpha before hierarchization: {}".format(alpha))

## An object of sgpp::base::OperationHierarchisation is created and used to
## hierarchize the coefficient vector, which we print.
pysgpp.createOperationHierarchisation(grid).doHierarchisation(alpha)
print("alpha after hierarchization:  {}".format(alpha))

## Finally, a second DataVector is created which is used as a point to
## evaluate the sparse grid function at. An object is obtained which
## provides an evaluation operation (of type sgpp::base::OperationEvaluation),
## and the sparse grid interpolant is evaluated at \f$\vec{p}\f$,
## which is close to (but not exactly at) a grid point.
p = pysgpp.DataVector(dim)
p[0] = 0.52
p[1] = 0.73
opEval = pysgpp.createOperationEval(grid)
print("u(0.52, 0.73) = {}".format(opEval.eval(alpha, p)))

## The example results in the following output:
## \verbinclude tutorial.output.txt
## It can be clearly seen that the surpluses decay with a factor of 1/4:
## On the first level, we obtain 1, on the second 1/4, and on the third
## 1/16 as surpluses.
 def predict_next_value(self, test_vector):
     opEval = pysgpp.createOperationEval(self._learner.grid)
     vector = DataVector(len(test_vector))
     for i in xrange(len(test_vector)):
         vector[i] = test_vector[i]
     return opEval.eval(self._learner.alpha, vector)
Example #14
0
print "dimensionality:         {}".format(gridStorage.dim())

# create regular grid, level 3
level = 3
gridGen = grid.createGridGenerator()
gridGen.regular(level)
print "number of grid points:  {}".format(gridStorage.size())

# create coefficient vector
alpha = DataVector(gridStorage.size())
alpha.setAll(0.0)
print "length of alpha vector: {}".format(len(alpha))

# set function values in alpha
f = lambda x0, x1: 16.0 * (x0-1.0)*x0 * (x1-1.0)*x1
for i in xrange(gridStorage.size()):
    gp = gridStorage.get(i)
    alpha[i] = f(gp.getCoord(0), gp.getCoord(1))
print "alpha before hierarchization: {}".format(alpha)

# hierarchize
createOperationHierarchisation(grid).doHierarchisation(alpha)
print "alpha after hierarchization:  {}".format(alpha)

# evaluate
p = DataVector(dim)
p[0] = 0.52
p[1] = 0.73
opEval = createOperationEval(grid)
print "u(0.52, 0.73) = {}".format(opEval.eval(alpha, p))
Example #15
0
print "dimensionality:         {}".format(gridStorage.dim())

# create regular grid, level 3
level = 3
gridGen = grid.createGridGenerator()
gridGen.regular(level)
print "number of grid points:  {}".format(gridStorage.size())

# create coefficient vector
alpha = DataVector(gridStorage.size())
alpha.setAll(0.0)
print "length of alpha vector: {}".format(len(alpha))

# set function values in alpha
f = lambda x0, x1: 16.0 * (x0 - 1.0) * x0 * (x1 - 1.0) * x1
for i in xrange(gridStorage.size()):
    gp = gridStorage.get(i)
    alpha[i] = f(gp.getCoord(0), gp.getCoord(1))
print "alpha before hierarchization: {}".format(alpha)

# hierarchize
createOperationHierarchisation(grid).doHierarchisation(alpha)
print "alpha after hierarchization:  {}".format(alpha)

# evaluate
p = DataVector(dim)
p[0] = 0.52
p[1] = 0.73
opEval = createOperationEval(grid)
print "u(0.52, 0.73) = {}".format(opEval.eval(alpha, p))
Example #16
0
def plotGrid(grid, alpha, admissibleSet, params, refined=None):
    gs = grid.getStorage()
    x = [0.0] * gs.size()
    y = [0.0] * gs.size()

    for i in xrange(gs.size()):
        x[i] = gs.get(i).getCoord(0)
        y[i] = gs.get(i).getCoord(1)

    xa = [0.0] * len(admissibleSet)
    ya = [0.0] * len(admissibleSet)
    for i, gp in enumerate(admissibleSet):
        xa[i] = gp.getCoord(0)
        ya[i] = gp.getCoord(1)

    xr = []
    yr = []
    if refined:
        xr = [0.0] * len(refined)
        yr = [0.0] * len(refined)

        for i, ix in enumerate(refined):
            xr[i] = gs.get(ix).getCoord(0)
            yr[i] = gs.get(ix).getCoord(1)

    n = 50
    A = np.ones(n * n).reshape(n, n)
    B = np.ones(n * n).reshape(n, n)
    U = params.getIndependentJointDistribution()
    bounds = U.getBounds()
    opEval = createOperationEval(grid)
    for i, xi in enumerate(np.linspace(bounds[0][0], bounds[0][1], n)):
        for j, yj in enumerate(np.linspace(bounds[1][0], bounds[1][1], n)):
            A[i, j] = U.pdf([yj, 1 - xi])
            B[i, j] = opEval.eval(alpha, DataVector([xi, yj]))

    fig = plt.figure()
    plt.imshow(A, interpolation='bicubic', extent=[0,1,0,1])

    plt.jet()
    plt.colorbar()

    plt.plot(x, y, linestyle=' ', marker='o', color='g', markersize=20)     # grid
    # plt.plot(xa, ya, linestyle=' ', marker='^', color = 'y', markersize=20) # admissible set
    plt.plot(xr, yr, linestyle=' ', marker='v', color = 'r', markersize=20) # refined points
    plt.title("size = %i" % gs.size())
    # plt.xlim(0, 1)
    # plt.ylim(0, 1)

    # fig = plt.figure()
    # plt.imshow(B, interpolation='bicubic', extent=[0,1,0,1])

    # plt.jet()
    # plt.colorbar()
    # fig.show()
    global myid
    # plt.savefig("out_%i.jpg" % (myid))
    # plt.close()
    myid += 1

    return fig
Example #17
0
def plotGrid(grid, alpha, admissibleSet, params, refined=None):
    gs = grid.getStorage()
    x = [0.0] * gs.size()
    y = [0.0] * gs.size()

    for i in xrange(gs.size()):
        x[i] = gs.get(i).getCoord(0)
        y[i] = gs.get(i).getCoord(1)

    xa = [0.0] * len(admissibleSet)
    ya = [0.0] * len(admissibleSet)
    for i, gp in enumerate(admissibleSet):
        xa[i] = gp.getCoord(0)
        ya[i] = gp.getCoord(1)

    xr = []
    yr = []
    if refined:
        xr = [0.0] * len(refined)
        yr = [0.0] * len(refined)

        for i, ix in enumerate(refined):
            xr[i] = gs.get(ix).getCoord(0)
            yr[i] = gs.get(ix).getCoord(1)

    n = 50
    A = np.ones(n * n).reshape(n, n)
    B = np.ones(n * n).reshape(n, n)
    U = params.getIndependentJointDistribution()
    bounds = U.getBounds()
    opEval = createOperationEval(grid)
    for i, xi in enumerate(np.linspace(bounds[0][0], bounds[0][1], n)):
        for j, yj in enumerate(np.linspace(bounds[1][0], bounds[1][1], n)):
            A[i, j] = U.pdf([yj, 1 - xi])
            B[i, j] = opEval.eval(alpha, DataVector([xi, yj]))

    fig = plt.figure()
    plt.imshow(A, interpolation='bicubic', extent=[0, 1, 0, 1])

    plt.jet()
    plt.colorbar()

    plt.plot(x, y, linestyle=' ', marker='o', color='g', markersize=20)  # grid
    # plt.plot(xa, ya, linestyle=' ', marker='^', color = 'y', markersize=20) # admissible set
    plt.plot(xr, yr, linestyle=' ', marker='v', color='r',
             markersize=20)  # refined points
    plt.title("size = %i" % gs.size())
    # plt.xlim(0, 1)
    # plt.ylim(0, 1)

    # fig = plt.figure()
    # plt.imshow(B, interpolation='bicubic', extent=[0,1,0,1])

    # plt.jet()
    # plt.colorbar()
    # fig.show()
    global myid
    # plt.savefig("out_%i.jpg" % (myid))
    # plt.close()
    myid += 1

    return fig