Пример #1
0
    def addFullGridPoints(self, grid, alpha):
        """
        Add all those full grid points with |accLevel|_1 <= n, where n is the
        maximun level of the sparse grid
        @param grid: Grid sparse grid to be discretized
        @param alpha: numpy array hierarchical coefficients
        """

        # make grid isotropic by adding all missing hierarchical ancestors
        newGridPoints = []
        # 1. locate candidates to be refined
        candidates = self.findCandidates(grid, alpha)
        # 2. do full grid search locally
        gridPoinsToBeAdded = self.lookupFullGridPoints(grid, alpha, candidates)
        # 3. insert them in a new grid
        for gp in gridPoinsToBeAdded:
            newGridPoints += insertPoint(grid, gp)
            newGridPoints += insertHierarchicalAncestors(grid, gp)
            if grid.getType() in [
                    GridType_LinearBoundary, GridType_PolyBoundary
            ]:
                newGridPoints += insertTruncatedBorder(grid, gp)

        # recompute the leaf property and return the result
        grid.getStorage().recalcLeafProperty()
        return newGridPoints
 def refine(self, grid, gp):
     ans = insertPoint(grid, gp)
     ans += insertHierarchicalAncestors(grid, gp)
     if hasBorder(grid):
         ans += insertTruncatedBorder(grid, gp)
     grid.getStorage().recalcLeafProperty()
     return ans
Пример #3
0
 def refine(self, grid, gp):
     ans = insertPoint(grid, gp)
     ans += insertHierarchicalAncestors(grid, gp)
     if hasBorder(grid):
         ans += insertTruncatedBorder(grid, gp)
     grid.getStorage().recalcLeafProperty()
     return ans
Пример #4
0
    def refine(self, grid, gp):
        ans = []
        gs = grid.getStorage()
        for d in range(gs.getDimension()):
            gpl = HashGridPoint(gp)
            gpl.getLeftChild(d)
            if isValid(grid, gpl):
                ans += insertPoint(grid, gpl)
                if hasBorder(grid.getType()):
                    ans += insertTruncatedBorder(grid, gpl)

            gpr = HashGridPoint(gp)
            gpr.getRightChild(d)
            if isValid(grid, gpr):
                ans += insertPoint(grid, gpr)
                if hasBorder(grid.getType()):
                    ans += insertTruncatedBorder(grid, gpr)

        gs.recalcLeafProperty()
        return ans
    def refine(self, grid, gp):
        ans = []
        gs = grid.getStorage()
        for d in xrange(gs.dim()):
            gpl = HashGridIndex(gp)
            gs.left_child(gpl, d)
            ans += insertPoint(grid, gpl)
            ans += insertHierarchicalAncestors(grid, gpl)
            if hasBorder(grid):
                ans += insertTruncatedBorder(grid, gpl)

            gpr = HashGridIndex(gp)
            gs.right_child(gpr, d)
            ans += insertPoint(grid, gpr)
            ans += insertHierarchicalAncestors(grid, gpr)
            if hasBorder(grid):
                ans += insertTruncatedBorder(grid, gpr)

        gs.recalcLeafProperty()
        return ans
Пример #6
0
    def refine(self, grid, gp):
        ans = []
        gs = grid.getStorage()
        for d in xrange(gs.dim()):
            gpl = HashGridIndex(gp)
            gs.left_child(gpl, d)
            ans += insertPoint(grid, gpl)
            ans += insertHierarchicalAncestors(grid, gpl)
            if hasBorder(grid):
                ans += insertTruncatedBorder(grid, gpl)

            gpr = HashGridIndex(gp)
            gs.right_child(gpr, d)
            ans += insertPoint(grid, gpr)
            ans += insertHierarchicalAncestors(grid, gpr)
            if hasBorder(grid):
                ans += insertTruncatedBorder(grid, gpr)

        gs.recalcLeafProperty()
        return ans
    def addFullGridPoints(self, grid, alpha):
        """
        Add all those full grid points with |accLevel|_1 <= n, where n is the
        maximun level of the sparse grid
        @param grid: Grid sparse grid to be discretized
        @param alpha: DataVector hierarchical coefficients
        """

        # make grid isotropic by adding all missing hierarchical ancestors
        newGridPoints = []
        # 1. locate candidates to be refined
        candidates = self.findCandidates(grid, alpha)
        # 2. do full grid search locally
        gridPoinsToBeAdded = self.lookupFullGridPoints(grid, alpha, candidates)
        # 3. insert them in a new grid
        for gp in gridPoinsToBeAdded:
            newGridPoints += insertPoint(grid, gp)
            newGridPoints += insertHierarchicalAncestors(grid, gp)
            if grid.getType() in [LinearBoundary, PolyBoundary]:
                newGridPoints += insertTruncatedBorder(grid, gp)

        # recompute the leaf property and return the result
        grid.getStorage().recalcLeafProperty()
        return newGridPoints
Пример #8
0
    def addFullGridPoints(self, grid, alpha, candidates, tol=-1e-14):
        """
        Add all those full grid points with |accLevel|_1 <= n, where n is the
        maximun level of the sparse grid
        @param grid: Grid sparse grid to be discretized
        @param candidates:
        @param tol:
        """
        # remove all the already existing candidates
        gs = grid.getStorage()
        nonExistingCandidates = [
            gp for gp in candidates if not gs.isContaining(gp)
        ]

        # sort the non existing grid points by level
        finalCandidates = self.sortCandidatesByLevelSum(nonExistingCandidates)

        levelSums = sorted(finalCandidates.keys())
        if self.lastMinimumCandidateLevelSum is None:
            ix = 0
        else:
            ixs = np.where(
                np.array(levelSums) == self.lastMinimumCandidateLevelSum)[0]
            if len(ixs) == 0:
                ix = 0
            elif len(ixs) == 1:
                ix = ixs[0] + 1
            else:
                raise AttributeError("this should never happen")

        done = False
        addedGridPoints = []
        minLevelSum = -1
        nextLevelCosts = 0

        while not done and ix < len(levelSums):
            minLevelSum = levelSums[ix]
            self.lastMinimumCandidateLevelSum = minLevelSum

            currentCandidates = finalCandidates[minLevelSum]
            if self.verbose:
                print(("# check candidates    : %i/%i (at |l|_1 = %i <= %i)" %
                       (len(currentCandidates), len(candidates), minLevelSum,
                        np.max(levelSums)), ))

            # evaluate the remaining candidates
            samples = np.ndarray((len(currentCandidates), self.numDims))
            p = DataVector(self.numDims)
            for j, gp in enumerate(currentCandidates):
                gp.getStandardCoordinates(p)
                samples[j, :] = p.array()
            eval = evalSGFunctionMulti(grid, alpha, samples)

            negativeNonExistingCandidates = [
                gp for j, gp in enumerate(currentCandidates) if eval[j] < tol
            ]

            if self.verbose:
                print("-> %i : considered candidates" %
                      len(negativeNonExistingCandidates))

            for gp in negativeNonExistingCandidates:
                addedGridPoints += insertPoint(grid, gp)
                addedGridPoints += insertHierarchicalAncestors(grid, gp)

                if not self.addAllGridPointsOnNextLevel and len(
                        addedGridPoints) > self.maxNewGridPoints:
                    done = True
                    break

            if self.addAllGridPointsOnNextLevel and len(addedGridPoints) > 0:
                nextLevelCosts = len(finalCandidates[minLevelSum])
                done = True

            ix += 1

        # recompute the leaf property and return the result
        grid.getStorage().recalcLeafProperty()

        return addedGridPoints, minLevelSum, nextLevelCosts