예제 #1
0
def test_laplace(grid, lmax):
    resolution = 100000
    grid.getGenerator().regular(lmax)
    gridStorage = grid.getStorage()
    size = gridStorage.getSize()
    b = getBasis(grid)
    op = pysgpp.createOperationLaplace(grid)
    alpha = pysgpp.DataVector(size)
    result = pysgpp.DataVector(size)

    for point_i in range(size):
        for point_j in range(size):
            gp_i = gridStorage.getPoint(point_i)
            gp_j = gridStorage.getPoint(point_j)
            print("--------")
            for i in range(0, size):
                alpha[i] = 0
            alpha[point_i] = 1
            op.mult(alpha, result)
            xs = np.linspace(0, 1, resolution)
            approx = sum([
                b.evalDx(gp_i.getLevel(0), gp_i.getIndex(0), x) *
                b.evalDx(gp_j.getLevel(0), gp_j.getIndex(0), x) for x in xs
            ]) / resolution
            print("i,j: {},{} result: {} approx:{}".format(
                point_i, point_j, result[point_j], approx))
            if (abs(result.get(point_j) - approx) > 1e-1):
                print("--------")
                print("points: {},{} ".format(point_i, point_j))
                print("approx:{}".format(approx))
                print("result:{}".format(result.get(point_j)))
                # print result
                print("--------")
예제 #2
0
    def loadAll(self, iteration):
        self.grid = self.loadGrid(iteration)
        self.knowledge = self.loadLearnedKnowledge(iteration)
        self.learner = self.__loadLearner(iteration)
        self.learner.setGrid(self.grid)
        self.learner.setLearnedKnowledge(self.knowledge)
        self.learner.attachEventController(self)
        # setting C operator here, since the knowledge about grid is needed
        # recreation of B operator is not necessary since it will be set in learnData()
        cOperatorType = self.__getCOperatorType(iteration)
        if cOperatorType == 'laplace':
            self.learner.specification.setCOperator(
                createOperationLaplace(self.learner.grid))
            self.learner.specification.setCOperatorType('laplace')

        elif cOperatorType == 'identity':
            self.learner.specification.setCOperator(
                createOperationIdentity(self.learner.grid))
            self.learner.specification.setCOperatorType('identity')

        else:
            raise Exception('C Operator type is unknown')

        for controller in self.__getLearnerEventControllers(iteration):
            if controller['module'] == 'bin.controller.InfoToScreenRegressor':
                self.learner.attachEventController(InfoToScreenRegressor())
            elif controller['module'] == 'bin.controller.InfoToScreen':
                self.learner.attachEventController(InfoToScreen())
            elif controller['module'] == 'bin.controller.InfoToFile':
                self.learner.attachEventController(
                    InfoToFile(controller['filename']))
            elif controller['module'] == 'bin.controller.InfoToGraph':
                self.learner.attachEventController(
                    InfoToGraph(controller['filename']))

        for controller in self.__getSolverEventControllers(iteration):
            if controller['module'] == 'bin.controller.InfoToScreenRegressor':
                self.learner.solver.attachEventController(
                    InfoToScreenRegressor())
            elif controller['module'] == 'bin.controller.InfoToScreen':
                self.learner.solver.attachEventController(InfoToScreen())
            elif controller['module'] == 'bin.controller.InfoToFile':
                self.learner.solver.attachEventController(
                    InfoToFile(controller['filename']))
            elif controller['module'] == 'bin.controller.InfoToGraph':
                self.learner.solver.attachEventController(
                    InfoToGraph(controller['filename']))

        return self.learner
    def loadAll(self, iteration):
        self.grid = self.loadGrid(iteration)
        self.knowledge = self.loadLearnedKnowledge(iteration)
        self.learner = self.__loadLearner(iteration)
        self.learner.setGrid(self.grid)
        self.learner.setLearnedKnowledge(self.knowledge)
        self.learner.attachEventController(self)
        # setting C operator here, since the knowledge about grid is needed
        # recreation of B operator is not necessary since it will be set in learnData()
        cOperatorType = self.__getCOperatorType(iteration)
        if cOperatorType == 'laplace':
            self.learner.specification.setCOperator(createOperationLaplace(self.learner.grid))
            self.learner.specification.setCOperatorType('laplace')

        elif cOperatorType == 'identity':
            self.learner.specification.setCOperator(createOperationIdentity(self.learner.grid))
            self.learner.specification.setCOperatorType('identity')

        else:
            raise Exception('C Operator type is unknown')

        for controller in self.__getLearnerEventControllers(iteration):
            if controller['module'] == 'bin.controller.InfoToScreenRegressor':
                self.learner.attachEventController(InfoToScreenRegressor())
            elif controller['module'] == 'bin.controller.InfoToScreen':
                self.learner.attachEventController(InfoToScreen())
            elif controller['module'] == 'bin.controller.InfoToFile':
                self.learner.attachEventController(InfoToFile(controller['filename']))
            elif controller['module'] == 'bin.controller.InfoToGraph':
                self.learner.attachEventController(InfoToGraph(controller['filename']))

        for controller in self.__getSolverEventControllers(iteration):
            if controller['module'] == 'bin.controller.InfoToScreenRegressor':
                self.learner.solver.attachEventController(InfoToScreenRegressor())
            elif controller['module'] == 'bin.controller.InfoToScreen':
                self.learner.solver.attachEventController(InfoToScreen())
            elif controller['module'] == 'bin.controller.InfoToFile':
                self.learner.solver.attachEventController(InfoToFile(controller['filename']))
            elif controller['module'] == 'bin.controller.InfoToGraph':
                self.learner.solver.attachEventController(InfoToGraph(controller['filename']))


        return self.learner
예제 #4
0
    def create(self):
        grid = self._builder.getSimulationLearner().getGrid()
        if grid is None:
            raise AttributeError('No grid defined. Needed for creating\
                                C-Operator')

        if self.__specification.getCOperatorType() == 'identity':
            self.__specification.setCOperator(createOperationIdentity(grid))
        else:
            # use laplace as default
            self.__specification.setCOperator(createOperationLaplace(grid))
            self.__specification.setCOperatorType('laplace')

        # make sure that there is a stop policy
        if self.__stopPolicyDescriptor is None:
            self.withStopPolicy()

        # create folding polcy if specified
        if self.__foldingPolicyDescriptor is not None:
            level = self.__foldingPolicyDescriptor.level
            seed = self.__foldingPolicyDescriptor.seed
            dtype = self.__foldingPolicyDescriptor.dtype
            if level is None:
                raise Exception("Folding level has to be defined")
            if dtype == FoldingStrategy.SEQUENTIAL:
                policy = SequentialFoldingPolicy(level)
            elif self.dtype == FoldingStrategy.RANDOM:
                policy = RandomFoldingPolicy(level, seed)
            elif self.dtype == FoldingStrategy.STRATIFIED:
                policy = StratifiedFoldingPolicy(level, seed)
            elif self.dtype == FoldingStrategy.FILES:
                policy = FilesFoldingPolicy(level)
            else:
                raise Exception("Folding dtype is not defined or is unproper")

            # set the folding policy in the learner
            self._builder.getLearner().setFoldingPolicy(policy)
    def create(self):
        grid = self._builder.getSimulationLearner().getGrid()
        if grid is None:
            raise AttributeError('No grid defined. Needed for creating\
                                C-Operator')

        if self.__specification.getCOperatorType() == 'identity':
            self.__specification.setCOperator(createOperationIdentity(grid))
        else:
            # use laplace as default
            self.__specification.setCOperator(createOperationLaplace(grid))
            self.__specification.setCOperatorType('laplace')

        # make sure that there is a stop policy
        if self.__stopPolicyDescriptor is None:
            self.withStopPolicy()

        # create folding polcy if specified
        if self.__foldingPolicyDescriptor is not None:
            level = self.__foldingPolicyDescriptor.level
            seed = self.__foldingPolicyDescriptor.seed
            dtype = self.__foldingPolicyDescriptor.dtype
            if level is None:
                raise Exception("Folding level has to be defined")
            if dtype == FoldingStrategy.SEQUENTIAL:
                policy = SequentialFoldingPolicy(level)
            elif self.dtype == FoldingStrategy.RANDOM:
                policy = RandomFoldingPolicy(level, seed)
            elif self.dtype == FoldingStrategy.STRATIFIED:
                policy = StratifiedFoldingPolicy(level, seed)
            elif self.dtype == FoldingStrategy.FILES:
                policy = FilesFoldingPolicy(level)
            else:
                raise Exception("Folding dtype is not defined or is unproper")

            # set the folding policy in the learner
            self._builder.getLearner().setFoldingPolicy(policy)