示例#1
0
def discretize(grid,
               alpha,
               f,
               epsilon=0.,
               refnums=0,
               pointsNum=10,
               level=0,
               deg=1,
               useDiscreteL2Error=True):
    """
    discretize f with a sparse grid

    @param grid: Grid
    @param alpha: surplus vector
    @param f: function
    @param epsilon: float, error tolerance
    @param refnums: int, number of refinment steps
    @param pointsNum: int, number of points to be refined per step
    @param level: int, initial grid level
    @param deg: int, degree of lagrange basis
    """
    # copy grid
    jgrid = copyGrid(grid, level=level, deg=deg)
    jgs = jgrid.getStorage()
    jgn = jgrid.createGridGenerator()
    basis_alpha = DataVector(alpha)

    # compute joined sg function
    jalpha = computeCoefficients(jgrid, grid, alpha, f)

    # compute errors
    maxdrift = None
    accMiseL2 = None
    l2error_grid = alpha.l2Norm()
    if useDiscreteL2Error:
        maxdrift, accMiseL2 = computeErrors(jgrid, jalpha, grid, alpha, f)
    else:
        accMiseL2 = l2error_grid

#     print "iteration 0/%i (%i, %i, %g): %g, %g, %s" % \
#         (refnums, jgs.size(), len(jalpha),
#          epsilon, accMiseL2, l2error_grid, maxdrift)

    ref = 0
    errs = [jgs.size(), accMiseL2, l2error_grid, maxdrift]
    bestGrid, bestAlpha, bestL2Error = copyGrid(jgrid), DataVector(
        jalpha), accMiseL2

    # repeat refinement as long as there are iterations and the
    # minimum error epsilon is reached
    while ref < refnums and bestL2Error > epsilon:
        oldgrid = copyGrid(jgrid)
        rp = jgn.getNumberOfRefinablePoints(
        )  # max(1, min(pointsNum, jgn.getNumberOfRefinablePoints()))
        jgn.refine(SurplusRefinementFunctor(jalpha, rp, epsilon))

        # if grid point has been added in the last iteration step
        if len(basis_alpha) == jgs.size():
            break

        # extend alpha vector...
        basis_alpha.resizeZero(jgs.size())

        # ------------------------------
        # compute joined sg function
        jalpha = computeCoefficients(jgrid, grid, basis_alpha, f)
        # compute useDiscreteL2Error
        l2error_grid = estimateL2error(oldgrid, jgrid, jalpha)

        # do Monte Carlo integration for obtaining the accMiseL2
        if useDiscreteL2Error:
            maxdrift, accMiseL2 = computeErrors(jgrid, jalpha, grid, alpha, f)
        # ------------------------------
        print "iteration %i/%i (%i, %i, %i, %i, %g): %g, %g, %s -> current best %g" % \
            (ref + 1, refnums,
             jgs.size(), len(jalpha),
             bestGrid.getSize(), len(bestAlpha),
             epsilon,
             accMiseL2, l2error_grid, maxdrift, bestL2Error)

        # check whether the new grid is better than the current best one
        # using the discrete l2 error. If no MC integration is done,
        # use the l2 error approximation via the sparse grid surpluses
        if (not useDiscreteL2Error and l2error_grid < bestL2Error) or \
                (useDiscreteL2Error and accMiseL2 < bestL2Error):
            bestGrid = copyGrid(jgrid)
            bestAlpha = DataVector(jalpha)
            if useDiscreteL2Error:
                bestL2Error = accMiseL2
            else:
                bestL2Error = l2error_grid
            errs = [jgs.size(), accMiseL2, l2error_grid, maxdrift]

        ref += 1

    return bestGrid, bestAlpha, errs
示例#2
0
def discretize(grid, alpha, f, epsilon=0.,
               refnums=0, pointsNum=10,
               level=0, deg=1,
               useDiscreteL2Error=True):
    """
    discretize f with a sparse grid

    @param grid: Grid
    @param alpha: surplus vector
    @param f: function
    @param epsilon: float, error tolerance
    @param refnums: int, number of refinment steps
    @param pointsNum: int, number of points to be refined per step
    @param level: int, initial grid level
    @param deg: int, degree of lagrange basis
    """
    # copy grid
    jgrid = copyGrid(grid, level=level, deg=deg)
    jgs = jgrid.getStorage()
    jgn = jgrid.createGridGenerator()
    basis_alpha = DataVector(alpha)

    # compute joined sg function
    jalpha = computeCoefficients(jgrid, grid, alpha, f)

    # compute errors
    maxdrift = None
    accMiseL2 = None
    l2error_grid = alpha.l2Norm()
    if useDiscreteL2Error:
        maxdrift, accMiseL2 = computeErrors(jgrid, jalpha, grid, alpha, f)
    else:
        accMiseL2 = l2error_grid

#     print "iteration 0/%i (%i, %i, %g): %g, %g, %s" % \
#         (refnums, jgs.size(), len(jalpha),
#          epsilon, accMiseL2, l2error_grid, maxdrift)

    ref = 0
    errs = [jgs.size(), accMiseL2, l2error_grid, maxdrift]
    bestGrid, bestAlpha, bestL2Error = copyGrid(jgrid), DataVector(jalpha), accMiseL2

    # repeat refinement as long as there are iterations and the
    # minimum error epsilon is reached
    while ref < refnums and bestL2Error > epsilon:
        oldgrid = copyGrid(jgrid)
        rp = jgn.getNumberOfRefinablePoints()  # max(1, min(pointsNum, jgn.getNumberOfRefinablePoints()))
        jgn.refine(SurplusRefinementFunctor(jalpha, rp, epsilon))

        # if grid point has been added in the last iteration step
        if len(basis_alpha) == jgs.size():
            break

        # extend alpha vector...
        basis_alpha.resizeZero(jgs.size())

        # ------------------------------
        # compute joined sg function
        jalpha = computeCoefficients(jgrid, grid, basis_alpha, f)
        # compute useDiscreteL2Error
        l2error_grid = estimateL2error(oldgrid, jgrid, jalpha)

        # do Monte Carlo integration for obtaining the accMiseL2
        if useDiscreteL2Error:
            maxdrift, accMiseL2 = computeErrors(jgrid, jalpha, grid, alpha, f)
        # ------------------------------
        print "iteration %i/%i (%i, %i, %i, %i, %g): %g, %g, %s -> current best %g" % \
            (ref + 1, refnums,
             jgs.size(), len(jalpha),
             bestGrid.getSize(), len(bestAlpha),
             epsilon,
             accMiseL2, l2error_grid, maxdrift, bestL2Error)

        # check whether the new grid is better than the current best one
        # using the discrete l2 error. If no MC integration is done,
        # use the l2 error approximation via the sparse grid surpluses
        if (not useDiscreteL2Error and l2error_grid < bestL2Error) or \
                (useDiscreteL2Error and accMiseL2 < bestL2Error):
            bestGrid = copyGrid(jgrid)
            bestAlpha = DataVector(jalpha)
            if useDiscreteL2Error:
                bestL2Error = accMiseL2
            else:
                bestL2Error = l2error_grid
            errs = [jgs.size(), accMiseL2, l2error_grid, maxdrift]

        ref += 1

    return bestGrid, bestAlpha, errs
示例#3
0
    def makePositive(self, alpha):
        """
        insert recursively all grid points such that the function is positive
        defined. Interpolate the function values for the new grid points using
        the registered algorithm.
        @param alpha: DataVector hierarchical coefficients
        """
        # make sure that the function is positive at every existing grid point
        grid = self.grid
        alpha = self.makeCurrentGridPositive(grid, alpha)
        # start adding points
        newGridPoints = []
        iteration = 1
        while True:
            # copy the old grid
            newGrid = copyGrid(grid)
            # add all those grid points which have an ancestor with negative
            # coefficient
            if self.verbose:
                print "-" * 60
                print "%i:" % iteration
                print "adding full grid points"
            addedGridPoints = self.addFullGridPoints(newGrid, alpha)

            if len(addedGridPoints) == 0:
                newAlpha = alpha
                break

            if self.verbose:
                print "learning the new density"

            newGridPoints += addedGridPoints
            # set the function value at the new grid points to zero
            newNodalValues = dehierarchize(grid, alpha)
            newNodalValues.resizeZero(newGrid.getSize())
            newAlpha = DataVector(alpha)
            newAlpha.resizeZero(newGrid.getSize())
            # compute now the hierarchical coefficients for the newly
            # added points
            newAlpha = self.algorithm.computeHierarchicalCoefficients(newGrid,
                                                                      newAlpha,
                                                                      addedGridPoints)
            # the function does not have to be positive now -> check it again
            if self.verbose:
                print "force function to be positive"
            # check manually if all nodal points are positive
            newNodalValues = dehierarchize(newGrid, newAlpha)
            # collect all negative function values
            forceToBePositive = []
            newGs = newGrid.getStorage()
            for i in xrange(newGs.size()):
                gp = newGs.get(i)
                if newNodalValues[newGs.seq(gp)] < 0.:
                    forceToBePositive.append(gp)

            if len(forceToBePositive) > 0:
                # if not, interpolate the function values for the negative
                # grid points
                if self.verbose:
                    warnings.warn("manually forcing the the function to be positive")
                newAlpha = self.makeCurrentGridPositive(newGrid, newAlpha)
                # newAlpha = SetGridPointsToZero().computeHierarchicalCoefficients(grid, newAlpha, forceToBePositive)
                # newAlpha = InterpolateParents().computeHierarchicalCoefficients(grid, newAlpha, forceToBePositive)

            if newGrid.getSize() == grid.getSize():
                break

            if self.verbose:
                fig = plt.figure()
                plotSG2d(newGrid, newAlpha)
                plt.title("iteration = %i" % iteration)
                fig.show()

                print "%i + %i = %i for discretization" % (grid.getSize(),
                                                           newGrid.getSize() - grid.getSize(),
                                                           newGrid.getSize(),)

            iteration += 1
            grid, alpha = newGrid, newAlpha

        # coarsening: remove all new grid points with negative surplus
        coarsedGrid, coarsedAlpha, _ = self.coarsening(newGrid, newAlpha, newGridPoints)
        if self.verbose:
            print "%i - %i = %i grid size after coarsening" % (newGrid.getSize(),
                                                               newGrid.getSize() - coarsedGrid.getSize(),
                                                               coarsedGrid.getSize(),)

        # scale the result such that the integral over it is one
#         c = createOperationQuadrature(coarsedGrid).doQuadrature(coarsedAlpha)
#         coarsedAlpha.mult(1. / c)

        # security check for positiveness
        checkPositivity(coarsedGrid, coarsedAlpha)

        return coarsedGrid, coarsedAlpha