def testSerializationLinearBoundingBox(self):
        """Uses Linear grid for tests"""
        from pysgpp import Grid
        
        factory = Grid.createLinearGrid(2)
        self.failIfEqual(factory, None)

        gen = factory.createGridGenerator()
        gen.regular(3)
        
        boundingBox = factory.getBoundingBox()
        tempBound = boundingBox.getBoundary(0)
        tempBound.leftBoundary = 0.0
        tempBound.rightBoundary = 100.0
        tempBound.bDirichletLeft = False;
        tempBound.bDirichletRight = False;
        boundingBox.setBoundary(0, tempBound)
        
        str = factory.serialize()
        self.assert_(len(str) > 0)
        
        newfac = Grid.unserialize(str)
        self.failIfEqual(newfac, None)
        
        self.assertEqual(factory.getStorage().size(), newfac.getStorage().size())
        
        boundingBox = newfac.getBoundingBox()    
        tempBound = boundingBox.getBoundary(0)
        self.assertEqual(0.0, tempBound.leftBoundary)
        self.assertEqual(100.0, tempBound.rightBoundary)
        self.assertEqual(False, tempBound.bDirichletLeft)
        self.assertEqual(False, tempBound.bDirichletRight)
Beispiel #2
0
def createGrid(grid, dim, deg=1, addTruncatedBorder=False):
    # create new grid
    gridType = grid.getType()

    if gridType in [Poly, PolyBoundary]:
        deg = max(deg, grid.getDegree())

    # print gridType, deg
    if deg > 1:
        if gridType in [LinearBoundary, PolyBoundary]:
            return Grid.createPolyBoundaryGrid(dim, deg)
        elif gridType == LinearL0Boundary:
            raise NotImplementedError("there is no full boundary polynomial grid")
        elif gridType in [Linear, Poly]:
            return Grid.createPolyGrid(dim, deg)
        else:
            raise Exception('unknown grid type %s' % gridType)
    else:
        if gridType == Linear:
            return Grid.createLinearGrid(dim)
        elif gridType == LinearBoundary:
            return Grid.createLinearBoundaryGrid(dim)
        elif gridType == LinearL0Boundary:
            return Grid.createLinearBoundaryGrid(dim, 0)
        else:
            raise Exception('unknown grid type %s' % gridType)
 def testOperationB(self):
     from pysgpp import Grid, DataVector, DataMatrix
     factory = Grid.createLinearGrid(1)
     gen = factory.createGridGenerator()
     gen.regular(2)
     
     alpha = DataVector(factory.getStorage().size())
     p = DataMatrix(1,1)
     beta = DataVector(1)
     
     
     alpha.setAll(0.0)
     p.set(0,0,0.25)
     beta[0] = 1.0
     
     opb = factory.createOperationB()
     opb.mult(beta, p, alpha)
     
     self.failUnlessAlmostEqual(alpha[0], 0.5)
     self.failUnlessAlmostEqual(alpha[1], 1.0)
     self.failUnlessAlmostEqual(alpha[2], 0.0)
     
     alpha.setAll(0.0)
     alpha[0] = 1.0
     
     p.set(0,0, 0.25)
     
     beta[0] = 0.0
     
     opb.multTranspose(alpha, p, beta)
     self.failUnlessAlmostEqual(beta[0], 0.5)
Beispiel #4
0
def checkPositivity(grid, alpha):
    # define a full grid of maxlevel of the grid
    gs = grid.getStorage()
    fullGrid = Grid.createLinearGrid(gs.dim())
    fullGrid.createGridGenerator().full(gs.getMaxLevel())
    fullHashGridStorage = fullGrid.getStorage()
    A = DataMatrix(fullHashGridStorage.size(), fullHashGridStorage.dim())
    p = DataVector(gs.dim())
    for i in xrange(fullHashGridStorage.size()):
        fullHashGridStorage.get(i).getCoords(p)
        A.setRow(i, p)

    res = evalSGFunctionMulti(grid, alpha, A)
    ymin, ymax, cnt = 0, -1e10, 0
    for i, yi in enumerate(res.array()):
        if yi < 0. and abs(yi) > 1e-13:
            cnt += 1
            ymin = min(ymin, yi)
            ymax = max(ymax, yi)
            A.getRow(i, p)
            print "  %s = %g" % (p, yi)
    if cnt > 0:
        print "warning: function is not positive"
        print "%i/%i: [%g, %g]" % (cnt, fullHashGridStorage.size(), ymin, ymax)
    return cnt == 0
 def testHierarchisationD(self):
     from pysgpp import Grid
     
     dim = 3
     level = 5
     function = buildParable(dim)
     grid = Grid.createLinearGrid(dim)
     testHierarchisationDehierarchisation(self, grid, level, function)
 def testCreation(self):
     """Uses Linear grid for tests"""
     from pysgpp import Grid
     
     factory = Grid.createLinearGrid(2)
     self.failIfEqual(factory, None)
     
     storage = factory.getStorage()
     self.failIfEqual(storage, None)
 def setUp(self):
     self.grid = Grid.createLinearGrid(2)  # a simple 2D grid
     self.grid.createGridGenerator().regular(3)  # max level 3 => 17 points
     self.HashGridStorage = self.grid.getStorage()
     alpha = DataVector(self.grid.getSize())
     alpha.setAll(1.0)
     for i in [9, 10, 11, 12]:
         alpha[i] = 0.0
     coarseningFunctor = SurplusCoarseningFunctor(alpha, 4, 0.5)
     self.grid.createGridGenerator().coarsen(coarseningFunctor, alpha)
    def testHatRegulardD(self):
        
        from pysgpp import Grid
        
        factory = Grid.createLinearGrid(3)

        m = generateLaplaceMatrix(factory, 3)
        m_ref = readReferenceMatrix(self, factory.getStorage(), 'data/C_laplace_phi_li_hut_dim_3_nopsgrid_31_float.dat.gz')

        # compare
        compareStiffnessMatrices(self, m, m_ref)
    def general_test(self, d, l, bb, xs):

        test_desc = "dim=%d, level=%d, len(x)=%s" % (d, l, len(xs))

        print test_desc

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

        alpha = DataVector([self.get_random_alpha() for i in xrange(self.grid.getSize())])

        bb_ = BoundingBox(d)

        for d_k in xrange(d):
            dimbb = DimensionBoundary()
            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_normal = [self.calc_exp_value_normal(x, d, bb, alpha) for x in xs]
        #expected_transposed = [self.calc_exp_value_transposed(x, d, bb, alpha) for x in xs]

        # Now set the bounding box

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

        dm = DataMatrix(len(xs), d)
        for k, x in enumerate(xs):
            dv = DataVector(x)
            dm.setRow(k, dv)

        multEval = createOperationMultipleEval(self.grid, dm)

        actual_normal = DataVector(len(xs))
        #actual_transposed = DataVector(len(xs))

        multEval.mult(alpha, actual_normal)
        #multEval.mult(alpha, actual_transposed)

        actual_normal_list = []
        for k in xrange(len(xs)):
            actual_normal_list.append(actual_normal.__getitem__(k))

        #actual_transposed_list = []
        #for k in xrange(len(xs)):
        #    actual_transposed_list.append(actual_transposed.__getitem__(k))

        self.assertAlmostEqual(actual_normal_list, expected_normal)
        #self.assertAlmostEqual(actual_tranposed_list, expected_tranposed)

        del self.grid
    def setUp(self):

        #
        # Grid
        #

        DIM = 2
        LEVEL = 2

        self.grid = Grid.createLinearGrid(DIM)
        self.grid_gen = self.grid.createGridGenerator()
        self.grid_gen.regular(LEVEL)

        #
        # trainData, classes, errors
        #

        xs = []
        DELTA = 0.05
        DELTA_RECI = int(1/DELTA)

        for i in xrange(DELTA_RECI):
            for j in xrange(DELTA_RECI):
                xs.append([DELTA*i, DELTA*j])

        random.seed(1208813)
        ys = [ random.randint(-10, 10) for i in xrange(DELTA_RECI**2)]

        # print xs
        # print ys

        self.trainData = DataMatrix(xs)
        self.classes = DataVector(ys)
        self.alpha = DataVector([3, 6, 7, 9, -1])

        self.errors = DataVector(DELTA_RECI**2)
        coord = DataVector(DIM)

        for i in xrange(self.trainData.getNrows()):
            self.trainData.getRow(i, coord)
            self.errors.__setitem__ (i, self.classes[i] - self.grid.eval(self.alpha, coord))

        #print "Errors:"
        #print self.errors

        #
        # Functor
        #

        self.functor = WeightedErrorRefinementFunctor(self.alpha, self.grid)
        self.functor.setTrainDataset(self.trainData)
        self.functor.setClasses(self.classes)
        self.functor.setErrors(self.errors)
def makePositive(grid, alpha):
    """
    insert full grid points if they are negative and the father
    node is part of the sparse grid
    @param grid:
    @param alpha:
    """
    # copy old sg function
    jgrid = copyGrid(grid)

    # evaluate the sparse grid function at all full grid points
    level = grid.getStorage().getMaxLevel()
    fg = Grid.createLinearGrid(grid.getStorage().dim())
    fg.createGridGenerator().full(level)

    # copy the old grid and use it as reference
    jgs = jgrid.getStorage()
    fgs = fg.getStorage()

    # run over all results and check where the function value
    # is lower than zero
    cnt = 1
    while True:
        print "run %i: full grid size = %i" % (cnt, fgs.size())
        gps = []

        # insert those fg points, which are not yet positive
        values = computeNodalValues(fg, grid, alpha)
        for i in xrange(len(values)):
            gp = fgs.get(i)
            if values[i] < 0 and not jgs.has_key(gp):
                gps += insertPoint(jgrid, gp)
                gps += insertHierarchicalAncestors(jgrid, gp)

        jgrid.getStorage().recalcLeafProperty()

        # 1. compute nodal values for new grid points
        jnodalValues = computeNodalValues(jgrid, grid, alpha)
        # 2. set the new ones to zero
        jgs = jgrid.getStorage()
        for gp in gps:
            jnodalValues[jgs.seq(gp)] = 0.
        # 3. hierarchize
        jalpha = hierarchize(jgrid, jnodalValues)
        # stop loop if no points have been added
        if len(gps) == 0:
            break
        # 4. reset values for next loop
        grid = copyGrid(jgrid)
        alpha = DataVector(jalpha)
        cnt += 1

    return jgrid, jalpha
    def setUp(self,):
        self.__gridFormatter = None
        self.filename = pathlocal + "/datasets/grid.gz"
        self.savefile = pathlocal + "/datasets/savetest.grid.gz"
        self.correct_str = ""
        self.grid = None

        self.__gridFormatter = GridFormatter()
        dim = 3
        self.grid = Grid.createLinearGrid(dim)
        self.grid.createGridGenerator().regular(3)
        self.correct_str = self.grid.serialize()
Beispiel #13
0
    def testHatRegulardD_two(self):
        from pysgpp import Grid

        factory = Grid.createLinearGrid(3)
        training = buildTrainingVector(readDataVector('data/data_dim_3_nops_512_float.arff.gz'))
        level = 4
        gen = factory.createGridGenerator()
        gen.regular(level)

        m = generateBBTMatrix(factory, training)
        m_ref = readReferenceMatrix(self, factory.getStorage(), 'data/BBT_phi_li_hut_dim_3_nopsgrid_111_float.dat.gz')

        # compare
        compareBBTMatrices(self, m, m_ref)
    def setUp(self):

        #
        # Grid
        #

        DIM = 2
        LEVEL = 2

        self.grid = Grid.createLinearGrid(DIM)
        self.grid_gen = self.grid.createGridGenerator()
        self.grid_gen.regular(LEVEL)


        #
        # trainData, classes, errors
        #

        xs = []
        DELTA = 0.05
        DELTA_RECI = int(1/DELTA)

        for i in xrange(DELTA_RECI):
            for j in xrange(DELTA_RECI):
                xs.append([DELTA*i, DELTA*j])

        random.seed(1208813)
        ys = [ random.randint(-10, 10) for i in xrange(DELTA_RECI**2)]

        self.trainData = DataMatrix(xs)
        self.classes = DataVector(ys)
        self.alpha = DataVector([3, 6, 7, 9, -1])
        self.multEval = createOperationMultipleEval(self.grid, self.trainData)

        self.errors = DataVector(DELTA_RECI**2)
        coord = DataVector(DIM)

        for i in xrange(self.trainData.getNrows()):
            self.trainData.getRow(i, coord)
            self.errors.__setitem__ (i, abs(self.classes[i] - self.grid.eval(self.alpha, coord)))

        #
        # OnlinePredictiveRefinementDimension
        #

        hash_refinement = HashRefinement();
        self.strategy = OnlinePredictiveRefinementDimension(hash_refinement)
        self.strategy.setTrainDataset(self.trainData)
        self.strategy.setClasses(self.classes)
        self.strategy.setErrors(self.errors)
 def testGeneration(self):
     from pysgpp import Grid, DataVector
     factory = Grid.createLinearGrid(2)
     storage = factory.getStorage()
     
     gen = factory.createGridGenerator()
     self.failIfEqual(gen, None)
     
     self.failUnlessEqual(storage.size(), 0)
     gen.regular(3)
     self.failUnlessEqual(storage.size(), 17)
     
     #This should fail
     self.failUnlessRaises(Exception, gen.regular, 3)
    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.createGridGenerator()
        self.grid_gen.regular(l)

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

        bb_ = BoundingBox(d)

        for d_k in xrange(d):
            dimbb = DimensionBoundary()
            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)

        for d_k in xrange(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 = self.grid.eval(alpha, p)
        else:
            expected = 0.0

        # Now set the bounding box

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

        p = DataVector(x)
        actual = self.grid.eval(alpha, p)

        self.assertAlmostEqual(actual, expected)

        del self.grid
 def testRefinement(self):
     from pysgpp import Grid, DataVector, SurplusRefinementFunctor
     factory = Grid.createLinearGrid(2)
     storage = factory.getStorage()
     
     gen = factory.createGridGenerator()
     gen.regular(1)
     
     self.failUnlessEqual(storage.size(), 1)
     alpha = DataVector(1)
     alpha[0] = 1.0
     func = SurplusRefinementFunctor(alpha)
     
     gen.refine(func)
     self.failUnlessEqual(storage.size(), 5)
Beispiel #18
0
def eval_fullGrid(level, dim, border=True):
    if border:
        grid = Grid.createLinearBoundaryGrid(dim)
    else:
        grid = Grid.createLinearGrid(dim)

    grid.createGridGenerator().full(level)
    gs = grid.getStorage()
    ans = DataMatrix(gs.size(), dim)
    p = DataVector(dim)

    for i in xrange(gs.size()):
        gs.get(i).getCoords(p)
        ans.setRow(i, p)

    return ans
    def testLoadGrid(self):
        dim = 2
        level = 2
        grid = Grid.createLinearGrid(dim)
        generator = grid.createGridGenerator()
        generator.regular(level)

        controller = CheckpointController("sample", pathlocal)
        sampleGrid = controller.loadGrid(0)

        # check dimension and size
        self.assertEqual(dim, sampleGrid.getStorage().dim())
        self.assertEqual(grid.getStorage().size(), sampleGrid.getStorage().size())

        # if string representations are equal, then grids are equal
        self.assertEqual(grid.serialize(), sampleGrid.serialize())
    def testOperationEval_eval(self):
        from pysgpp import Grid, DataVector

        factory = Grid.createLinearGrid(1)
        gen = factory.createGridGenerator()
        gen.regular(1)
        
        alpha = DataVector(factory.getStorage().size())        
        alpha.setAll(1.0)

        p = DataVector(1)
        p.setAll(0.25)
        
        eval = factory.createOperationEval()

        self.failUnlessAlmostEqual(eval.eval(alpha, p), 0.5)
    def testSerializationLinear(self):
        """Uses Linear grid for tests"""
        from pysgpp import Grid
        
        factory = Grid.createLinearGrid(2)
        self.failIfEqual(factory, None)

        gen = factory.createGridGenerator()
        gen.regular(3)

        str = factory.serialize()
        self.assert_(len(str) > 0)
        
        newfac = Grid.unserialize(str)
        self.failIfEqual(newfac, None)
        
        self.assertEqual(factory.getStorage().size(), newfac.getStorage().size())
    def testSaveGrid(self):
        dim = 2
        level = 2
        grid = Grid.createLinearGrid(dim)
        generator = grid.createGridGenerator()
        generator.regular(level)

        controller = CheckpointController("savegrid", pathlocal)
        controller.setGrid(grid)
        controller.saveGrid(0)

        f = gzip.open(pathlocal + "/savegrid.0.grid.gz", "r")
        try:
            sampleString = f.read()
        finally:
            f.close()

        self.assertEqual(grid.serialize(), sampleString)
Beispiel #23
0
def createGrid(dim, level, borderType, isFull=False):
    from pysgpp.extensions.datadriven.learner.Types import BorderTypes
    if borderType == BorderTypes.NONE:
        grid = Grid.createLinearGrid(dim)
    elif borderType == BorderTypes.TRAPEZOIDBOUNDARY:
        grid = Grid.createLinearTrapezoidBoundaryGrid(dim)
    elif borderType == BorderTypes.COMPLETEBOUNDARY:
        grid = Grid.createLinearBoundaryGrid(dim, 0)
    else:
        raise Exception('Unknown border type')

    # create regular grid of level accLevel
    gridGen = grid.createGridGenerator()
    if isFull:
        gridGen.full(level)
    else:
        gridGen.regular(level)

    return grid
Beispiel #24
0
 def testHatRegular1D(self):
     from pysgpp import Grid, DataVector
     
     factory = Grid.createLinearGrid(1)
     storage = factory.getStorage()
     
     gen = factory.createGridGenerator()
     gen.regular(7)
     
     laplace = factory.createOperationLaplace()
   
     
     alpha = DataVector(storage.size())
     result = DataVector(storage.size())
     
     alpha.setAll(1.0)
     
     laplace.mult(alpha, result)
     
     for seq in xrange(storage.size()):
         index = storage.get(seq)
         level, _ = index.get(0)
         self.failUnlessAlmostEqual(result[seq], pow(2.0, level+1))
    def testOperationTest_test(self):
        from pysgpp import Grid, DataVector, DataMatrix

        factory = Grid.createLinearGrid(1)
        gen = factory.createGridGenerator()
        gen.regular(1)
        
        alpha = DataVector(factory.getStorage().size())        
        
        data = DataMatrix(1,1)
        data.setAll(0.25)
        classes = DataVector(1)
        classes.setAll(1.0)

        testOP = factory.createOperationTest()

        alpha.setAll(1.0)
        c = testOP.test(alpha, data, classes)
        self.failUnless(c > 0.0)
        
        alpha.setAll(-1.0)
        c = testOP.test(alpha, data, classes)
        self.failUnless(c == 0.0)
def discretizeFunction(f, bounds, level=2, hasBorder=False, *args, **kws):
    # define linear transformation to the unit hyper cube
    T = JointTransformation()
    for xlim in bounds:
        T.add(LinearTransformation(xlim[0], xlim[1]))

    # create grid
    dim = len(bounds)

    # create adequate grid
    if hasBorder:
        grid = Grid.createLinearBoundaryGrid(dim)
    else:
        grid = Grid.createLinearGrid(dim)

    # init storage
    grid.createGridGenerator().regular(level)
    gs = grid.getStorage()

    # discretize on given level
    p = DataVector(dim)
    nodalValues = DataVector(gs.size())
    for i in xrange(gs.size()):
        gs.get(i).getCoords(p)
        # transform to the right space
        q = T.unitToProbabilistic(p.array())
        # apply the given function
        nodalValues[i] = float(f(q))

    # hierarchize
    alpha = hierarchize(grid, nodalValues)

    # estimate the l2 error
    err = estimateDiscreteL2Error(grid, alpha, f)

    # TODO: adaptive refinement
    return grid, alpha, err
    def testSerializationLinearWithLeaf(self):
        """Uses Linear grid for tests"""
        from pysgpp import Grid
        
        srcLeaf = []
        factory = Grid.createLinearGrid(2)
        self.failIfEqual(factory, None)

        gen = factory.createGridGenerator()
        gen.regular(3)

        for i in xrange(factory.getStorage().size()):
            srcLeaf.append(factory.getStorage().get(i).isLeaf())

        str = factory.serialize()
        self.assert_(len(str) > 0)
        
        newfac = Grid.unserialize(str)
        self.failIfEqual(newfac, None)
        
        self.assertEqual(factory.getStorage().size(), newfac.getStorage().size())
        
        for i in xrange(factory.getStorage().size()):
            self.failUnlessEqual(newfac.getStorage().get(i).isLeaf(), srcLeaf[i])
Beispiel #28
0
        return [[0, 1], [0, 1]]


dist = myDist()

# plot analytic density
fig = plt.figure()
plotDensity2d(dist)
plt.title("analytic, KL = 0")
plt.xlim(0, 1)
plt.ylim(0, 1)
fig.show()

# get a sprse grid approximation
level = 6
grid = Grid.createLinearGrid(2)
grid.getGenerator().regular(level)
gs = grid.getStorage()

nodalValues = DataVector(grid.getSize())
p = DataVector(gs.getDimension())
for i in range(gs.getSize()):
    gs.getCoordinates(gs.getPoint(i), p)
    nodalValues[i] = dist.pdf(p.array())

alpha = hierarchize(grid, nodalValues)

# plot the result
fig = plt.figure()
plotSG2d(grid, alpha)
plt.title("plotSG: vol = %g" % (doQuadrature(grid, alpha)))
Beispiel #29
0
 def setUp(self):
     self.grid = Grid.createLinearGrid(2)  # a simple 2D grid
     self.grid.getGenerator().regular(3)  # max level 3 => 17 points
     self.HashGridStorage = self.grid.getStorage()
Beispiel #30
0
    def testBilinearForms(self):
        # define parameter set
        builder = ParameterBuilder().defineUncertainParameters()
        # builder.new().isCalled('x').withTNormalDistribution(0.5, 0.1, 0, 1)
        # builder.new().isCalled('y').withTNormalDistribution(0.5, 0.1, 0, 1)
        builder.new().isCalled('x').withUniformDistribution(0, 1)
        builder.new().isCalled('y').withUniformDistribution(0, 1)
        params = builder.andGetResult()

        U = params.getIndependentJointDistribution()
        T = params.getJointTransformation()

        def f(x):
            """
            Function to be interpolated
            """
            # return 2.
            # return float(np.sin(4 * x[0]) * np.cos(4 * x[1]))
            return np.prod([4 * xi * (1 - xi) for xi in x])

        # define sparse grid function
        grid = Grid.createLinearGrid(params.getStochasticDim())
        grid.getGenerator().regular(2)
        gs = grid.getStorage()

        nodalValues = DataVector(gs.getSize())
        p = DataVector(gs.getDimension())

        for i in range(gs.getSize()):
            gs.getCoordinates(gs.getPoint(i), p)
            nodalValues[i] = f(p.array())

        v = hierarchize(grid, nodalValues)
        res = DataVector(gs.getSize())

        # -------------------------------------------------------------------------------
        # compute admissible set
        admissibleSet = RefinableNodesSet()
        admissibleSet.create(grid)

        # -------------------------------------------------------------------------------
        # define the strategies
        s0 = UniformQuadratureStrategy()
        s1 = PiecewiseConstantQuadratureStrategy(params=params)
        s2 = BilinearGaussQuadratureStrategy(U=U.getDistributions(),
                                             T=T.getTransformations())
        s3 = SparseGridQuadratureStrategy(U=U.getDistributions())

        A = s0.computeBilinearForm(grid)
        C, _ = s2.computeBilinearForm(grid)
        D, _ = s3.computeBilinearForm(grid)

        # -------------------------------------------------------------------------------
        # compute bilinear form for lists of grid points
        gpsi, basisi = project(grid, [0, 1])
        gpsj, basisj = project(grid, [0, 1])

        C_list, _ = s2.computeBilinearFormByList(gs, gpsi, basisi, gpsj,
                                                 basisj)
        D_list, _ = s3.computeBilinearFormByList(gs, gpsi, basisi, gpsj,
                                                 basisj)

        assert np.all(np.abs(C_list - A) < 1e-13)
        assert np.all(np.abs(C_list - C) < 1e-13)
        assert np.all(np.abs(D_list - D) < 1e-13)
        plt.title("analytic")
        fig.show()
    elif numDims == 2:
        fig = plt.figure()
        plotFunction2d(fun)
        plt.title("analytic")
        fig.show()

        fig, ax, _ = plotFunction3d(fun)
        ax.set_title("analytic")
        ax.set_zlim(-2, 2)
        fig.show()
        plt.savefig("sin_analytic.pdf")

# get a sparse grid approximation
grid = Grid.createLinearGrid(numDims)
grid.getGenerator().regular(level)
gs = grid.getStorage()

alpha = hierarchizeFun(fun)
print("l=%i: (gs=%i)" % (level, grid.getSize()))
print("-" * 80)

# plot the result
if plot and numDims < 3:
    fig = plt.figure()
    if numDims == 1:
        plotSG1d(grid, alpha)
    elif numDims == 2:
        plotSG2d(grid, alpha, show_negative=False, show_grid_points=True)
Beispiel #32
0
    def general_test(self, d, l, num):

        # print "#"*20
        # print 

        xs = [self.get_random_x(d) for i in xrange(num)]

        dupl = True
        while dupl:
            dupl_tmp = False
            for x in xs:
                for y in xs:
                    if x == y:
                        dupl = True
                        break
                if dupl:
                    break
            dupl = dupl_tmp
            xs = [self.get_random_x(d) for i in xrange(num)]

        errs = [self.get_random_err() for i in xrange(num)]

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

        self.trainData = DataMatrix(xs)
        self.errors = DataVector(errs)
        self.multEval = createOperationMultipleEval(self.grid, self.trainData)
        self.dim = d
        self.storage = self.grid.getStorage()
        self.gridSize = self.grid.getSize()
        
        #
        # OnlinePredictiveRefinementDimension
        #

        # print "OnlineRefinementDim"

        hash_refinement = HashRefinement();
        online = OnlinePredictiveRefinementDimension(hash_refinement)
        online.setTrainDataset(self.trainData)
        online.setErrors(self.errors)

        online_result = refinement_map({})
        online.collectRefinablePoints(self.storage, 5, online_result)

        # for k,v in online_result.iteritems():
            # print k, v

        #
        # Naive
        #

        # print 
        # print "Naive"

        naive_result = self.naive_calc()
        
        # for k,v in naive_result.iteritems():
            # print k, v

        #
        # OnlinePredictiveRefinementDimensionOld
        #

        hash_refinement = HashRefinement();
        online_old = OnlinePredictiveRefinementDimensionOld(hash_refinement)

        #
        # Assertions
        #

        for k,v in online_result.iteritems():
            if abs(online_result[k] - naive_result[k]) >= 0.1:
                #print "Error in:", k
                #print online_result[k]
                #print naive_result[k]

                #print naive_result

                #print "Datapoints"
                #print xs
                #print "Errors"
                #print errs

                #print "All values:"
                #print "Key: Online result, naive result"
                #for k,v in online_result.iteritems():
                #    print("{} ({}): {}, {}".format(k, self.storage.get(k[0]).toString(), v, naive_result[k]))

                self.assertTrue(False)

            # self.assertAlmostEqual(online_result[k], naive_result[k])

        del self.grid
        del self.grid_gen
        del self.trainData
        del self.errors
        del self.multEval
        del self.storage
Beispiel #33
0
    def test_manual(self):

        result = {(1, 0): 0, (2, 0): 0}

        #
        # Grid
        #

        DIM = 1
        LEVEL = 2

        self.grid = Grid.createLinearGrid(DIM)
        self.grid_gen = self.grid.createGridGenerator()
        self.grid_gen.regular(LEVEL)

        #
        # trainData, classes, errors
        #

        xs = [[0.0], [1.0]]
        errs = [1, 2]

        self.trainData = DataMatrix(xs)
        self.errors = DataVector(errs)
        self.multEval = createOperationMultipleEval(self.grid, self.trainData)
        self.dim = DIM
        self.storage = self.grid.getStorage()
        self.gridSize = self.grid.getSize()

        #
        # OnlinePredictiveRefinementDimension
        #

        print "#"*20
        print "OnlineRefinementDim"

        hash_refinement = HashRefinement();
        online = OnlinePredictiveRefinementDimension(hash_refinement)
        online.setTrainDataset(self.trainData)
        online.setErrors(self.errors)

        online_result = refinement_map({})
        online.collectRefinablePoints(self.grid.getStorage(), 10, online_result)

        for k,v in online_result.iteritems():
            print k, v

        for k,v in online_result.iteritems():
            self.assertAlmostEqual(online_result[k], result[k])

        #
        # Naive
        #

        print "#"*20
        print "Naive"

        naive_result = self.naive_calc()

        #for k,v in naive_result.iteritems():
            #print k, v

        for k,v in naive_result.iteritems():
            self.assertAlmostEqual(naive_result[k], result[k])
 def setUp(self):
     self.grid = Grid.createLinearGrid(2)  # a simple 2D grid
     self.grid.createGridGenerator().regular(3)  # max level 3 => 17 points
     self.HashGridStorage = self.grid.getStorage()
# sgpp.sparsegrids.org

# import modules
from pysgpp import DataVector, Grid, createOperationHierarchisation, \
                   createOperationQuadrature, OperationQuadratureMC

# the standard parabola (arbitrary-dimensional)
def f(x):
     res = 1.0
     for i in range(len(x)):
          res *= 4.0*x[i]*(1.0-x[i])
     return res

# create a two-dimensional piecewise bi-linear grid
dim = 2
grid = Grid.createLinearGrid(dim)
gridStorage = grid.getStorage()
print "dimensionality:        {}".format(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())
for i in xrange(gridStorage.size()):
    gp = gridStorage.get(i)
    alpha[i] = f((gp.getCoord(0), gp.getCoord(1)))
createOperationHierarchisation(grid).doHierarchisation(alpha)
Beispiel #36
0
    def test_manual(self):

        print "#"*20

        result = {(1, 0): 5, (2, 0): 25}

        #
        # Grid
        #

        DIM = 1
        LEVEL = 2

        self.grid = Grid.createLinearGrid(DIM)
        self.grid_gen = self.grid.createGridGenerator()
        self.grid_gen.regular(LEVEL)

        #
        # trainData, classes, errors
        #

        xs = [[0.1], [0.4], [0.6], [0.8]]
        errs = [1, 2, 3, 4]

        self.trainData = DataMatrix(xs)
        self.errors = DataVector(errs)
        self.multEval = createOperationMultipleEval(self.grid, self.trainData)
        self.dim = DIM
        self.storage = self.grid.getStorage()
        self.gridSize = self.grid.getSize()

        #
        # OnlinePredictiveRefinementDimension
        #

        print "OnlineRefinementDim"

        hash_refinement = HashRefinement();
        online = OnlinePredictiveRefinementDimension(hash_refinement)
        online.setTrainDataset(self.trainData)
        online.setErrors(self.errors)

        online_result = refinement_map({})
        online.collectRefinablePoints(self.grid.getStorage(), 10, online_result)

        for k,v in online_result.iteritems():
            print k, v

        for k,v in online_result.iteritems():
            self.assertAlmostEqual(online_result[k], result[k])

        #
        # Naive
        #

        print 
        print "Naive"

        naive_result = self.naive_calc()

        for k,v in naive_result.iteritems():
            print k, v

        for k,v in naive_result.iteritems():
            self.assertAlmostEqual(naive_result[k], result[k])
Beispiel #37
0
def adaptiveGridToRegularGrid(numDims,
                              level,
                              refnums,
                              f,
                              numSamples=1000,
                              plot=False,
                              verbose=False):
    """
    Converts a regular sparse grid function to a sparse grid in the
    combination technique and back.

    Arguments:
    numDims -- int number of dimensions
    level -- level of the sparse grid
    refnums -- int number of refinement steps
    f -- function to be interpolated
    numSamples -- int number of random samples on which we evaluate the different sparse grid
                  functions to validate the grid conversion
    plot -- bool whether the sparse grid functions are plotted or not (just for numDims=1)
    verbose -- bool verbosity
    """
    ## We generate a iid of uniform samples, which we are going to use to
    ## validate the grid conversion
    x = np.random.rand(numSamples, numDims)
    parameters = DataMatrix(x)

    ## We create a regular sparse grid as usual and...
    grid = Grid.createLinearGrid(numDims)
    grid.getGenerator().regular(level)
    alpha = interpolate(grid, f)

    ## ... refine it adaptively
    grid_adaptive = grid.clone()
    alpha_adaptive = DataVector(alpha)
    refineGrid(grid_adaptive, alpha_adaptive, f, refnums)

    ## We apply now both methods of the grid conversion on the
    ## adaptively refined grid. The first conversion considers all
    ## levels where at least one sparse grid point exists, while the
    ## second one considers just complete subspaces.
    treeStorage_all = convertHierarchicalSparseGridToCombigrid(
        grid_adaptive.getStorage(), GridConversionTypes_ALLSUBSPACES)
    treeStorage_complete = convertHierarchicalSparseGridToCombigrid(
        grid_adaptive.getStorage(), GridConversionTypes_COMPLETESUBSPACES)

    ## We initialize the CombigridOperation on a grid that spans the
    ## same function space as the original hierarchical sparse grid:
    ## hat basis on an equidistant grids without boundary points.
    func = multiFunc(f)
    opt_all = CombigridMultiOperation.createExpUniformLinearInterpolation(
        numDims, func)
    opt_complete = CombigridMultiOperation.createExpUniformLinearInterpolation(
        numDims, func)

    ## The CombigridOperation expects the points at which you want to
    ## evaluate the interpolant as DataMatrix with the shape (numDims
    ## x numSamples). We, therefore, need to transpose the samples and
    ## initialize the multi operation with them. To set the level
    ## structure we initialize the level manager of the operation with
    ## the storage we have obtained after the conversion.
    parameters.transpose()
    opt_all.setParameters(parameters)
    opt_all.getLevelManager().addLevelsFromStructure(treeStorage_all)
    opt_complete.setParameters(parameters)
    opt_complete.getLevelManager().addLevelsFromStructure(treeStorage_complete)
    parameters.transpose()

    ## If you want you can examine the levels of the combination
    ## technique...
    if verbose:
        print("-" * 80)
        print("just full levels:")
        print(opt_complete.getLevelManager().getSerializedLevelStructure())
        print("-" * 80)
        print("all levels:")
        print(opt_all.getLevelManager().getSerializedLevelStructure())
        print("-" * 80)

    ## We start to transform the grids from the combination technique
    ## back to their hierarchical formulation. We, again, create a
    ## grid with a piecewise d-linear basis and initialize the grid
    ## points in its storage by the ones available in the levels of
    ## the combination technique. We do it first for the combination
    ## grids that just contain just those levels where the original
    ## sparse grid had complete subpsaces...
    grid_complete = Grid.createLinearGrid(numDims)
    treeStorage_complete = opt_complete.getLevelManager().getLevelStructure()
    convertCombigridToHierarchicalSparseGrid(treeStorage_complete,
                                             grid_complete.getStorage())

    ## ... and do the same for the version where we considered all
    ## subspaces where at least one grid point was located.
    grid_all = Grid.createLinearGrid(numDims)
    treeStorage_all = opt_all.getLevelManager().getLevelStructure()
    convertCombigridToHierarchicalSparseGrid(treeStorage_all,
                                             grid_all.getStorage())

    ## we interpolate now f on the new grids and...
    alpha_complete = interpolate(grid_complete, f)
    alpha_all = interpolate(grid_all, f)

    ## ... evaluate all the surrogate functions we have so far
    y_sg_regular = DataVector(numSamples)
    createOperationMultipleEval(grid, parameters).eval(alpha, y_sg_regular)

    y_sg_adaptive = DataVector(numSamples)
    createOperationMultipleEval(grid_adaptive,
                                parameters).eval(alpha_adaptive, y_sg_adaptive)

    y_sg_all = DataVector(numSamples)
    createOperationMultipleEval(grid_all, parameters).eval(alpha_all, y_sg_all)

    y_sg_complete = DataVector(numSamples)
    createOperationMultipleEval(grid_complete,
                                parameters).eval(alpha_complete, y_sg_complete)

    y_ct_all = opt_all.getResult()
    y_ct_complete = opt_complete.getResult()

    ## For convenience we use flattened numpy arrays to test if the
    ## function values at each point are the same.
    y_sg_regular = y_sg_regular.array().flatten()
    y_sg_adaptive = y_sg_adaptive.array().flatten()
    y_ct_all = y_ct_all.array().flatten()
    y_ct_complete = y_ct_complete.array().flatten()
    y_sg_all = y_sg_all.array().flatten()
    y_sg_complete = y_sg_complete.array().flatten()

    ## If you want, you can plot the results if the problem is one dimensional
    if plot and numDims == 1:
        x = x.flatten()
        ixs = np.argsort(x)
        plt.figure()
        plt.plot(x[ixs], y_sg_regular[ixs], label="sg regular")
        plt.plot(x[ixs], y_sg_adaptive[ixs], label="sg adaptive")
        plt.plot(x[ixs], y_ct_complete[ixs], label="ct full")
        plt.plot(x[ixs], y_ct_all[ixs], label="ct all")
        plt.plot(x[ixs], y_sg_complete[ixs], label="sg full")
        plt.plot(x[ixs], y_sg_all[ixs], label="sg all")
        plt.legend()
        plt.show()

    ## All the function values should not be equivalent if...
    if grid_complete.getSize() < grid_all.getSize():
        assert np.sum((y_ct_complete - y_ct_all)**2) > 1e-14
        assert np.sum((y_sg_regular - y_sg_all)**2) > 1e-14

    ## and should be equivalent if...
    if grid_complete.getSize() == grid.getSize():
        assert np.sum((y_ct_complete - y_sg_regular)**2) < 1e-14
        assert np.sum((y_sg_complete - y_sg_regular)**2) < 1e-14

    ## For the grid sizes it must hold that
    assert grid_adaptive.getSize() > grid.getSize()
    assert grid_complete.getSize() <= grid_adaptive.getSize()
    assert grid_all.getSize() >= grid.getSize()
Beispiel #38
0

def f(x):
    """
    Function to be interpolated
    """
    # return float(x ** 3 + 2 * x ** 2 - x + 1)
    # return float(np.exp(2 * -x))
    # return float(np.sin(8 * x))
    return np.prod([4 * xi * (1 - xi) for xi in x])


# -------------------------------------------------------
# define sparse grid function - control
# -------------------------------------------------------
grid_control = Grid.createLinearGrid(1)
grid_control.getGenerator().regular(4)
gsc = grid_control.getStorage()

nodalValues = DataVector(gsc.getSize())
p = DataVector(gsc.getDimension())

for i in range(gsc.getSize()):
    gsc.getCoordinates(gsc.getPoint(i), p)
    nodalValues[i] = f(p.array())

w = hierarchize(grid_control, nodalValues)

# -------------------------------------------------------
# define sparse grid function
# -------------------------------------------------------
Beispiel #39
0
# import modules
from pysgpp import DataVector, Grid, createOperationHierarchisation, \
                   createOperationQuadrature, OperationQuadratureMC


# the standard parabola (arbitrary-dimensional)
def f(x):
    res = 1.0
    for i in range(len(x)):
        res *= 4.0 * x[i] * (1.0 - x[i])
    return res


# create a two-dimensional piecewise bi-linear grid
dim = 2
grid = Grid.createLinearGrid(dim)
gridStorage = grid.getStorage()
print "dimensionality:        {}".format(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())
for i in xrange(gridStorage.size()):
    gp = gridStorage.get(i)
    alpha[i] = f((gp.getCoord(0), gp.getCoord(1)))
createOperationHierarchisation(grid).doHierarchisation(alpha)