Exemplo n.º 1
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])
    def test_1(self):

        storage = self.grid.getStorage()
        gridSize = self.grid.getSize()
        numDim = storage.getDimension()

        print("######")
        print("Expected result:")
        print("######")

        expected = {}

        for j in range(gridSize):

            HashGridPoint = storage.getPoint(j)
            HashGridPoint.setLeaf(False)

            print("Point: ", j, " (", HashGridPoint.toString(), ")")

            for d in range(numDim):

                #
                # Get left and right child
                #

                leftChild = HashGridPoint(HashGridPoint)
                rightChild = HashGridPoint(HashGridPoint)

                storage.left_child(leftChild, d)
                storage.right_child(rightChild, d)

                #
                # Check if point is refinable
                #

                if storage.isContaining(leftChild) or storage.isContaining(
                        rightChild):
                    continue

                #
                # Insert children temporarily
                #

                storage.insert(leftChild)
                storage.insert(rightChild)

                val1 = self.calc_indicator_value(leftChild)
                val2 = self.calc_indicator_value(rightChild)

                storage.deleteLast()
                storage.deleteLast()

                print("Dimension: ", d)
                print("Left Child: ", val1)
                print("Right Child: ", val2)
                print("")

                expected[(j, d)] = val1 + val2

            print("")

        for k, v in list(expected.items()):
            print((k, v))

        print("######")
        print("Actual result:")
        print("######")

        actual = refinement_map({})
        self.strategy.collectRefinablePoints(storage, 10, actual)

        for k, v in list(actual.items()):
            print((k, v))

        #
        # Assertions
        #

        for k, v in list(expected.items()):
            self.assertEqual(actual[k], v)
Exemplo n.º 3
0
print "(1, 0): 4.04"
print "#" * 10

d = 2
l = 2

xs = [[0.1, 0.9], [0.9, 0.2], [0.3, 0.5], [0.3, 0.0], [0.9, 0.0]]
errs = [-2, -0.1, -0.2, -0.2, -1.8]

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

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

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

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

for k, v in online_result.iteritems():
    print k, v
Exemplo n.º 4
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])
Exemplo n.º 5
0
print "(1, 0): 4.04"
print "#" * 10

d = 2
l = 2

xs = [[0.1, 0.9], [0.9, 0.2], [0.3, 0.5], [0.3, 0.0], [0.9, 0.0]]
errs = [-2, -0.1, -0.2, -0.2, -1.8]

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

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

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

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

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

        storage = self.grid.getStorage()
        gridSize = self.grid.getSize()
        numDim = storage.dim()

        print "######"
        print "Expected result:"
        print "######"

        expected = {}

        for j in xrange(gridSize):

            HashGridIndex = storage.get(j)
            HashGridIndex.setLeaf(False)
                
            print "Point: ", j, " (", HashGridIndex.toString(), ")"

            for d in xrange(numDim):

                #
                # Get left and right child
                #

                leftChild = HashGridIndex(HashGridIndex)
                rightChild = HashGridIndex(HashGridIndex)

                storage.left_child(leftChild, d)
                storage.right_child(rightChild, d)

                #
                # Check if point is refinable
                #

                if storage.has_key(leftChild) or storage.has_key(rightChild):
                    continue

                #
                # Insert children temporarily
                #

                storage.insert(leftChild) 
                storage.insert(rightChild) 

                val1 = self.calc_indicator_value(leftChild)
                val2 = self.calc_indicator_value(rightChild)
                
                storage.deleteLast()
                storage.deleteLast()

                print "Dimension: ", d
                print "Left Child: ", val1
                print "Right Child: ", val2
                print ""

                expected[(j, d)] = val1 + val2
            
            print ""

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

        print "######"
        print "Actual result:"
        print "######"

        actual = refinement_map({})
        self.strategy.collectRefinablePoints(storage, 10, actual)
        
        for k, v in actual.iteritems():
            print(k, v)

        #
        # Assertions
        #

        for k, v in expected.iteritems():
            self.assertEqual(actual[k], v)
Exemplo n.º 7
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
    def test_1(self):

        storage = self.grid.getStorage()
        gridSize = self.grid.getSize()
        numDim = storage.dim()

        print "######"
        print "Expected result:"
        print "######"

        expected = {}

        for j in xrange(gridSize):

            HashGridIndex = storage.get(j)
            HashGridIndex.setLeaf(False)

            print "Point: ", j, " (", HashGridIndex.toString(), ")"

            for d in xrange(numDim):

                #
                # Get left and right child
                #

                leftChild = HashGridIndex(HashGridIndex)
                rightChild = HashGridIndex(HashGridIndex)

                storage.left_child(leftChild, d)
                storage.right_child(rightChild, d)

                #
                # Check if point is refinable
                #

                if storage.has_key(leftChild) or storage.has_key(rightChild):
                    continue

                #
                # Insert children temporarily
                #

                storage.insert(leftChild)
                storage.insert(rightChild)

                val1 = self.calc_indicator_value(leftChild)
                val2 = self.calc_indicator_value(rightChild)

                storage.deleteLast()
                storage.deleteLast()

                print "Dimension: ", d
                print "Left Child: ", val1
                print "Right Child: ", val2
                print ""

                expected[(j, d)] = val1 + val2

            print ""

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

        print "######"
        print "Actual result:"
        print "######"

        actual = refinement_map({})
        self.strategy.collectRefinablePoints(storage, 10, actual)

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

        #
        # Assertions
        #

        for k, v in expected.iteritems():
            self.assertEqual(actual[k], v)