Exemplo n.º 1
0
    def test_mergeDataFrontNoSubmanager(self):
        dataManager = DataManager("manager")
        dataManager.addDataEntry("entry1", 2)

        data1 = dataManager.createDataObject([10, 10])
        data1.setDataEntry("entry1", ..., np.ones((10, 2)))

        data2 = dataManager.createDataObject([5, 5])
        data2.setDataEntry("entry1", ..., np.zeros((5, 2)))

        data1.mergeData(data2, True)

        self.assertEqual(data1.getNumElements("entry1"), 15)
        self.assertTrue((data1.getDataEntry("entry1", ...) == np.vstack(
            (np.zeros((5, 2)), np.ones((10, 2))))).all())
Exemplo n.º 2
0
    def test_nextAliases(self):
        dataManager = DataManager("manager", isTimeSeries=True)
        dataManager.addDataEntry("entry1", 1)
        dataManager.addDataEntry("entry2", 3)
        dataManager.addDataAlias("entries", [('entry1', ...), ('entry2', ...)])

        data1 = dataManager.createDataObject([10])
        dataMat = np.array(range(0, 11))
        dataMat.resize(11, 1)

        dataMatNext = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
        dataMatLast = np.array([0, 0, 1, 2, 3, 4, 5, 6, 7, 8])
        dataMatNext.resize(10, 1)
        dataMatLast.resize(10, 1)

        data1.setDataEntry("allEntry1", ..., dataMat)

        nextEntry1 = data1.getDataEntry("nextEntry1", ...)
        allDataEntry1 = data1.getDataEntry("allEntry1", ...)
        lastEntry1 = data1.getDataEntry("lastEntry1", ...)

        self.assertTrue((nextEntry1 == dataMatNext).all())
        self.assertTrue((allDataEntry1 == dataMat).all())
        self.assertTrue((lastEntry1 == dataMatLast).all())

        allDataEntries = data1.getDataEntry('allEntries', slice(0, 3))
        allDataEntriesMat = np.array([[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0,
                                                                   0]])
        self.assertTrue((allDataEntries == allDataEntriesMat).all())
Exemplo n.º 3
0
    def test_generator(self):

        dataManager = DataManager('values')
        dataManager.addDataEntry('X', 2)
        dataManager.addDataEntry('Y', 2)
        f = DummyMapping(dataManager)

        dataManager.addFeatureMapping(f)

        data = dataManager.createDataObject(10)

        # features mapping gets called every time now when we access data.Y
        data[...].X = np.random.normal(0, 1, (10, 2))
        print('Features: ', data[...].Y)
        print('Features: ', data[...].Y)

        f.setLazyEvaluation(True)
        # mapping gets called only once
        print('Features: ', data[...].Y)
        print('Features: ', data[...].Y)
        print('Features: ', data[...].Y)

        # reset valid Tags
        data[...].Y_validFlag = np.vstack((np.ones(
            (5, 1), dtype=bool), np.zeros((5, 1), dtype=bool)))
        #Now mapping is only called for the last 5 elements
        print('Features: ', data[...].Y)
Exemplo n.º 4
0
    def test_dataSuffix(self):

        settings = SettingsManager.getDefaultSettings()
        settings.clean()
        dataManager = DataManager("manager")
        settings.pushSuffixStack('Agent1')

        dataManager.addDataEntry('parameters1', 1)
        dataManager.addDataEntry('parameters2', 1)

        dataManager.addDataAlias('parameters', [('parameters1', ...),
                                                ('parameters2', ...)])

        self.assertTrue(dataManager.isDataEntry("parameters1Agent1"))
        self.assertFalse(dataManager.isDataEntry("parameters1"))

        data = dataManager.createDataObject(10)
        data.setDataEntry('parameters1Agent1', ..., np.ones((10, 1)) * 5)

        dummyManipulator = TestSuffixManipulator(dataManager)
        data[...] >> dummyManipulator.dummyFunction >> data

        self.assertTrue(
            np.all(
                data.getDataEntry("parameters2Agent1") ==
                data.getDataEntry("parameters1Agent1") + 10))
        self.assertTrue(
            np.all(
                data.getDataEntry("parametersAgent1") == np.hstack((
                    data.getDataEntry("parameters1Agent1"),
                    data.getDataEntry("parameters2Agent1")))))

        settings.popSuffixStack()
Exemplo n.º 5
0
    def test_reserveStorage(self):
        dataManager = DataManager('episodes')
        dataManager.addDataEntry('parameters', 5)
        dataManager.addDataEntry('contexts', 2)

        subDataManager = DataManager('steps')
        subDataManager.addDataEntry('states', 2)
        subDataManager.addDataEntry('actions', 2)

        dataManager.subDataManager = subDataManager

        data = dataManager.createDataObject([100, 20])

        data.reserveStorage([20, 20])

        self.assertEqual(data.dataStructure['contexts'].shape[0], 20)
        self.assertEqual(data.dataStructure['parameters'].shape[0], 20)

        for i in range(0, 20):
            self.assertEqual(data.dataStructure['steps'][i]['states'].shape[0],
                             20)
            self.assertEqual(
                data.dataStructure['steps'][i]['actions'].shape[0], 20)

        data.reserveStorage([50, 100])

        self.assertEqual(data.dataStructure['contexts'].shape[0], 50)
        self.assertEqual(data.dataStructure['parameters'].shape[0], 50)

        for i in range(0, 50):
            self.assertEqual(data.dataStructure['steps'][i]['states'].shape[0],
                             100)
            self.assertEqual(
                data.dataStructure['steps'][i]['actions'].shape[0], 100)

        data.reserveStorage([50, 0])

        self.assertEqual(data.dataStructure['contexts'].shape[0], 50)
        self.assertEqual(data.dataStructure['parameters'].shape[0], 50)

        for i in range(0, 50):
            self.assertEqual(data.dataStructure['steps'][i]['states'].shape[0],
                             0)
            self.assertEqual(
                data.dataStructure['steps'][i]['actions'].shape[0], 0)

        data.reserveStorage(15, ...)

        self.assertEqual(data.dataStructure['contexts'].shape[0], 50)
        self.assertEqual(data.dataStructure['parameters'].shape[0], 50)

        for i in range(0, 50):
            self.assertEqual(data.dataStructure['steps'][i]['states'].shape[0],
                             15)
            self.assertEqual(
                data.dataStructure['steps'][i]['actions'].shape[0], 15)
Exemplo n.º 6
0
    def testMappingData(self):
        dataManager = DataManager('values')
        dataManager.addDataEntry('X', 1)
        dataManager.addDataEntry('Y', 1)

        mapping = DummyMapping(dataManager)
        data = dataManager.createDataObject(10)

        data >> mapping

        self.assertTrue((data.getDataEntry('Y') == np.ones((10, 1))).all())
Exemplo n.º 7
0
    def test_mergeDataFront(self):
        dataManager = DataManager("manager")
        subDataManager = DataManager("subDataManager")
        dataManager.subDataManager = subDataManager
        dataManager.addDataEntry("entry1", 2)
        subDataManager.addDataEntry("entry2", 3)

        data1 = dataManager.createDataObject([10, 10])
        data1.setDataEntry("entry1", ..., np.ones((10, 2)))
        data1.setDataEntry("entry2", ..., np.ones((100, 3)))

        data2 = dataManager.createDataObject([5, 5])
        data2.setDataEntry("entry1", ..., np.zeros((5, 2)))
        data2.setDataEntry("entry2", ..., np.zeros((25, 3)))

        data1.mergeData(data2, True)

        self.assertEqual(data1.getNumElements("entry1"), 15)
        self.assertEqual(len(data1.dataStructure["subDataManager"]), 15)
        self.assertTrue((data1.getDataEntry("entry1", ...) == np.vstack(
            (np.zeros((5, 2)), np.ones((10, 2))))).all())
Exemplo n.º 8
0
    def testMatrixEntries(self):
        manager = DataManager('episodes')
        manager.addDataEntry('matrixEntry', (10, 10))

        data = manager.createDataObject(5)

        data.setDataEntry('matrixEntry', slice(0, 2), np.ones((2, 10, 10)))
        testMatrix = np.zeros((5, 10, 10))
        testMatrix[slice(0, 2)] = np.ones((2, 10, 10))

        testMatrix = testMatrix - data.getDataEntry('matrixEntry')
        self.assertTrue((testMatrix == 0).all())
Exemplo n.º 9
0
    def test_getDataEntryDeepCopy(self):
        dataManager = DataManager('episodes')
        subDataManager = DataManager('steps')
        subSubDataManager = DataManager('subSteps')

        dataManager.subDataManager = subDataManager
        subDataManager.subDataManager = subSubDataManager

        dataManager.addDataEntry('parameters', 5)
        subDataManager.addDataEntry('states', 1)
        subSubDataManager.addDataEntry('subActions', 2, -10, 10)

        myData = dataManager.createDataObject([1, 1, 1])

        # set the data for all subActions of all episodes, steps and subSteps
        myData.setDataEntry('subActions', [], np.ones((1, 2)))

        self.assertTrue((myData.dataStructure['steps'][0]['subSteps'][0]
                         ['subActions'][0] == np.array([1, 1])).all())

        data1 = myData.getDataEntry('subActions', [])
        data2 = myData.getDataEntry('subActions', [], True)
        data3 = myData.getDataEntry('subActions', [], False)
        data4 = myData.getDataEntry('subActions', [], False)

        self.assertTrue((data1 == data2).all())
        self.assertTrue((data2 == data3).all())
        self.assertTrue((data3 == data4).all())

        data2[0, 1] = 7  # This should NOT have any impact on any other data

        self.assertTrue((data2 != data1).any())
        self.assertTrue((data2 != data3).any())
        self.assertTrue((data2 != data4).any())

        data5 = myData.getDataEntry('subActions', [])

        data3[0, 0] = 8  # This MAY impact the data in the data structure,
        # but should not impact data1 or data2

        self.assertTrue((data5 == data1).all())
        self.assertTrue((data3 != data1).any())
        self.assertTrue((data3 == data4).any())

        myData.setDataEntry('subActions', [],
                            data2)  # still no impact on data1
        self.assertTrue((data5 == data1).all())
        self.assertTrue(((myData.getDataEntry('subActions', [])) == [1,
                                                                     7]).all())
Exemplo n.º 10
0
    def test_setgetDataEntryLocalLayer(self):
        dataManager = DataManager('episodes')
        dataManager.addDataEntry('parameters', 5, -5, 5)

        myData = dataManager.createDataObject([10])

        # set the data for the parameters of all episodes
        myData.setDataEntry(['parameters'], [], np.ones((10, 5)))
        myData.setDataEntry(['parameters'], [slice(0, 5)], np.ones((5, 5)))

        # the first episode should have different parameters
        myData.setDataEntry(['parameters'], [0], np.array([1, 2, 3, 4, 5]))

        self.assertTrue(
            (myData.dataStructure['parameters'][0] == np.array([1, 2, 3, 4,
                                                                5])).all())
        self.assertTrue(
            (myData.dataStructure['parameters'][1] == np.array([1, 1, 1, 1,
                                                                1])).all())
        self.assertTrue(
            (myData.dataStructure['parameters'][9] == np.array([1, 1, 1, 1,
                                                                1])).all())

        # this should not change anything
        myData.setDataEntry(['parameters'], [], np.ones((10, 5)))
        myData.setDataEntry(['parameters'], [0], np.array([1, 2, 3, 4, 5]))

        self.assertTrue(
            (myData.dataStructure['parameters'][0] == np.array([1, 2, 3, 4,
                                                                5])).all())
        self.assertTrue(
            (myData.dataStructure['parameters'][1] == np.array([1, 1, 1, 1,
                                                                1])).all())
        self.assertTrue(
            (myData.dataStructure['parameters'][9] == np.array([1, 1, 1, 1,
                                                                1])).all())

        # tests for getDataEntry
        self.assertTrue((myData.getDataEntry('parameters') ==
                         myData.dataStructure['parameters']).all())

        self.assertTrue((myData.getDataEntry(
            'parameters', ...) == myData.dataStructure['parameters']).all())

        self.assertTrue((myData.getDataEntry('parameters')[4] ==
                         myData.dataStructure['parameters'][4]).all())

        self.assertTrue((myData.getDataEntry(
            'parameters', [4]) == myData.dataStructure['parameters'][4]).all())
Exemplo n.º 11
0
    def test_get_alias_data(self):
        dataManager = DataManager('episodes')
        dataManager.addDataEntry('parameters', 5)
        dataManager.addDataEntry('contexts', 5)
        dataManager.addDataAlias('parameterAlias',
                                 [('parameters', slice(0, 2))])

        dataManager.addDataAlias('twoAlias', [('parameters', slice(0, 2)),
                                              ('contexts', slice(2, 5))])

        myData = dataManager.createDataObject([10, 5, 1])

        myData.dataStructure['parameters'] = np.ones((10, 5))
        myData.dataStructure['contexts'] = np.ones((10, 5)) * 2

        paramAlias = myData.dataStructure['parameterAlias']
        paramAlias[:] = np.ones(2) * 3
        paramAlias[0][1] = 10
        myData.dataStructure['parameterAlias'] = paramAlias

        self.assertEqual(myData.dataStructure['parameters'][0][1], 10)
        self.assertEqual(myData.dataStructure['parameters'][0][2], 1)
        self.assertEqual(myData.dataStructure['parameters'][3][1], 3)
        self.assertEqual(myData.dataStructure['parameters'][5][3], 1)

        twoAlias = myData.dataStructure['twoAlias']
        twoAlias[4] = np.ones(5) * 4
        twoAlias[5] = np.ones(5) * 5
        twoAlias[6] = np.ones(5) * 6
        twoAlias[-1] = np.ones(5) * 9

        myData.dataStructure['twoAlias'] = twoAlias

        self.assertEqual(myData.dataStructure['twoAlias'][0][3], 2)
        self.assertEqual(myData.dataStructure['twoAlias'][-1][3], 9)
        self.assertEqual(myData.dataStructure['twoAlias'][4][2], 4)
        self.assertEqual(myData.dataStructure['twoAlias'][5][2], 5)
        self.assertEqual(myData.dataStructure['parameters'][0][0], 3)
        self.assertEqual(myData.dataStructure['parameters'][0][1], 10)
        self.assertEqual(myData.dataStructure['parameters'][0][2], 1)
        self.assertEqual(myData.dataStructure['parameters'][4][2], 1)
        self.assertEqual(myData.dataStructure['parameters'][5][2], 1)
        self.assertEqual(myData.dataStructure['contexts'][1][3], 2)
        self.assertEqual(myData.dataStructure['contexts'][6][3], 6)
        self.assertEqual(myData.dataStructure['contexts'][-1][3], 9)
        self.assertEqual(myData.dataStructure['contexts'][4][0], 2)
        self.assertEqual(myData.dataStructure['contexts'][5][1], 2)
        self.assertEqual(myData.dataStructure['contexts'][4][2], 4)
        self.assertEqual(myData.dataStructure['contexts'][5][2], 5)
Exemplo n.º 12
0
    def testLinearGaussianMLLearner(self):

        dimInput = 3
        dimOutput = 2
        dataManager = DataManager('data')
        dataManager.addDataEntry('inputs', dimInput)
        dataManager.addDataEntry('outputs', dimOutput)
        dataManager.addDataEntry('weights', 1)

        numSamples = 1000000
        data = dataManager.createDataObject([numSamples])

        gaussianDist = GaussianLinearInFeatures(dataManager, ['inputs'], 'outputs', 'testFunction')
        gaussianDistLearner = LinearGaussianMLLearner(dataManager, gaussianDist)

        meanVec = normal(0, 1,(dimOutput,1))
        betaVec = normal(0, 1, (dimOutput, dimInput))

        covMat = normal(0, 1, (dimOutput, dimOutput))
        covMat = np.dot(covMat.transpose(), covMat)

        gaussianDist.setCovariance(covMat)
        gaussianDist.setWeightsAndBias(betaVec, meanVec)


        inputVectors = normal(0, 1, (numSamples, dimInput))

        outputVectors = gaussianDist.sampleFromDistribution(inputVectors)
        weights = uniform(0,1, (numSamples,1))

        data.setDataEntry('inputs', [], inputVectors)
        data.setDataEntry('outputs', [], outputVectors)
        data.setDataEntry('weights', [], weights)

        gaussianDistLearner.setWeightName('weights')
        data >> gaussianDistLearner

        data >> gaussianDist >> data

        meanVecLearned = gaussianDist.getMean()
        betaVecLearned = gaussianDist.getBeta()

        covMatLearned = gaussianDist.getCovariance()

        self.assertTrue(np.sum(np.square(meanVec - meanVecLearned)) < 0.01)
        self.assertTrue(np.sum(np.square(betaVec - betaVecLearned)) < 0.01)

        self.assertTrue(np.sum(np.square(covMat - covMatLearned)) < 0.05)
Exemplo n.º 13
0
    def testSparseEntries(self):
        manager = DataManager('episodes')
        manager.addDataEntry('matrixEntry', 10, dataType=DataType.sparse)

        data = manager.createDataObject(5)

        sparseMatrix = csr_matrix((2, 10))
        sparseMatrix[0, 4] = 4
        sparseMatrix[1, 2] = 1

        data.setDataEntry('matrixEntry', slice(0, 2), sparseMatrix)
        testMatrix = csr_matrix((5, 10))
        testMatrix[slice(0, 2)] = sparseMatrix

        testMatrix = testMatrix - data.getDataEntry('matrixEntry')
        self.assertTrue(np.abs(testMatrix).sum() == 0)
Exemplo n.º 14
0
    def test_restrictedEntry(self):
        dataManager = DataManager("manager", isTimeSeries=False)
        dataManager.addDataEntry("entry1", 1)
        dataManager.addDataEntry("entry2", 3, isPeriodic=[True, False, True])
        dataManager.addDataAlias("entries", [('entry1', ...), ('entry2', ...)])

        data = dataManager.createDataObject(2)
        data.setDataEntry('entries', ...,
                          np.array([[0, 8, 0, 2], [0, 2, 0, -8]]))

        entryRestricted = data.getDataEntry('entries_restricted')
        self.assertTrue(
            sum(
                sum(
                    np.abs(entryRestricted -
                           np.array([[0, 1, 0, 1], [0, 1, 0, -1]])))) < 0.001)
Exemplo n.º 15
0
    def test_getNumElementsForIndex(self):
        manager = DataUtil.createTestManager()
        data = manager.createDataObject([3, 4, 5])

        self.assertEqual(data.getNumElementsForIndex(0), 3)
        self.assertEqual(data.getNumElementsForIndex(0, [...]), 3)
        self.assertEqual(data.getNumElementsForIndex(1), 12)
        self.assertEqual(data.getNumElementsForIndex(1, [...]), 12)
        self.assertEqual(data.getNumElementsForIndex(1, [..., ...]), 12)
        self.assertEqual(data.getNumElementsForIndex(1, [..., slice(0, 2)]), 6)
        self.assertEqual(
            data.getNumElementsForIndex(1,
                                        [slice(2, 3), slice(0, 2)]), 2)
        self.assertEqual(data.getNumElementsForIndex(2), 60)
        self.assertEqual(
            data.getNumElementsForIndex(2, ...),
            60)  # test implicit cast to array of second argument (deprecated)
        self.assertEqual(data.getNumElementsForIndex(2, [...]),
                         60)  # this should be the standart way
        self.assertEqual(data.getNumElementsForIndex(2, [..., ...]), 60)
        self.assertEqual(data.getNumElementsForIndex(2, [..., ..., ...]), 60)
        self.assertEqual(
            data.getNumElementsForIndex(2, [..., ..., slice(0, 2)]), 24)
        self.assertEqual(
            data.getNumElementsForIndex(
                2, [..., slice(1, 4), slice(0, 2)]), 18)
        self.assertEqual(
            data.getNumElementsForIndex(
                2, [slice(0, 1), slice(2, 3),
                    slice(0, 2)]), 2)

        dataManager = DataManager('episodes')
        subDataManager = DataManager('steps')
        dataManager.subDataManager = subDataManager
        dataManager.addDataEntry('parameters', 5)
        dataManager.addDataAlias('pAlias', [('parameters', ...)])
        dataManager.addDataEntry('contexts', 2)
        data = dataManager.createDataObject([3, 4, 5])

        self.assertEqual(data.getNumElementsForIndex(0), 3)
        self.assertEqual(data.getNumElementsForIndex(0, [...]), 3)
        self.assertEqual(data.getNumElementsForIndex(1), 12)
        self.assertEqual(data.getNumElementsForIndex(1, [...]), 12)
        self.assertEqual(data.getNumElementsForIndex(1, [..., ...]), 12)
        self.assertEqual(data.getNumElementsForIndex(1, [slice(2, 3)]), 4)
Exemplo n.º 16
0
    def test_setgetDataEntryRanges(self):
        dataManager = DataManager('episodes')
        dataManager.addDataEntry('parameters', 5)  # implicit ranges ([-1 1])
        dataManager.addDataEntry('temperature', 24, -20, 100)

        myData = dataManager.createDataObject([10, 5, 3])

        # this should not raise any exception
        myData.setDataEntry(['parameters'], [], np.ones((10, 5)))
        myData.setDataEntry(['parameters'], [], -np.ones((10, 5)))
        myData.setDataEntry(['parameters'], [slice(0, 5)], 0.97 * np.ones(
            (5, 5)))
        myData.setDataEntry(['parameters'], [slice(0, 5)], -0.5 * np.ones(
            (5, 5)))
        myData.setDataEntry('temperature', [], -20 * np.ones((10, 24)))
        myData.setDataEntry('temperature', [], -19 * np.ones((10, 24)))
        myData.setDataEntry('temperature', [], 99 * np.ones((10, 24)))
        myData.setDataEntry('temperature', [], np.zeros((10, 24)))
Exemplo n.º 17
0
    def test_setDataEntryList(self):
        dataManager = DataManager('episodes')
        dataManager.addDataEntry('parameters', 5, -10, 10)
        dataManager.addDataEntry('contexts', 3, -10, 10)
        myData = dataManager.createDataObject([10])

        # set the data for the parameters and context of all episodes
        myData.setDataEntry(['parameters'], [...], np.ones((10, 5)))
        myData.setDataEntry(['contexts'], [...], np.ones((10, 3)))

        myData.setDataEntryList(
            ['parameters', 'contexts'], [...],
            [np.zeros((10, 5)), np.zeros((10, 3))])

        self.assertTrue((myData.getDataEntry('parameters', [...]) == np.zeros(
            (10, 5))).all())
        self.assertTrue((myData.getDataEntry('contexts', [...]) == np.zeros(
            (10, 3))).all())

        myData.setDataEntryList(
            [('parameters', ['contexts'])], [...],
            [np.hstack((np.ones((10, 5)), 2 * np.ones((10, 3))))])

        self.assertTrue((myData.getDataEntry('parameters', [...]) == np.ones(
            (10, 5))).all())
        self.assertTrue((myData.getDataEntry('contexts', [...]) == 2 * np.ones(
            (10, 3))).all())

        myData.setDataEntryList([('parameters', ['contexts'])], [...],
                                [6 * np.ones((10, 8))])

        self.assertTrue((myData.getDataEntry('parameters',
                                             [...]) == 6 * np.ones(
                                                 (10, 5))).all())
        self.assertTrue((myData.getDataEntry('contexts', [...]) == 6 * np.ones(
            (3))).all())

        myData.setDataEntryList([('parameters', ['contexts'])], [3],
                                7 * np.ones((1, 8)))

        self.assertTrue((myData.getDataEntry('parameters', [3]) == 7 * np.ones(
            (5))).all())
        self.assertTrue((myData.getDataEntry('contexts', 3) == 7 * np.ones(
            (3))).all())
Exemplo n.º 18
0
    def test_getDataObject(self):
        dataManager = DataManager('episodes')
        subDataManager = DataManager('steps')
        subSubDataManager = DataManager('subSteps')

        dataManager.subDataManager = subDataManager
        subDataManager.subDataManager = subSubDataManager

        dataManager.addDataEntry('parameters', 5)
        dataManager.addDataEntry('contexts', 2)
        subDataManager.addDataEntry('states', 1)
        subDataManager.addDataEntry('actions', 2)
        subSubDataManager.addDataEntry('subStates', 1)
        subSubDataManager.addDataEntry('subActions', 2)

        myData = dataManager.createDataObject([10, 5, 1])

        self.assertTrue(dataManager.finalized)
        self.assertEqual(len(myData.dataStructure['parameters']), 10)
        self.assertEqual(len(myData.dataStructure['contexts']), 10)
        self.assertEqual(len(myData.dataStructure['parameters'][0]), 5)
        self.assertEqual(len(myData.dataStructure['parameters'][9]), 5)
        self.assertEqual(len(myData.dataStructure['contexts'][0]), 2)
        self.assertEqual(len(myData.dataStructure['contexts'][9]), 2)
        self.assertEqual(len(myData.dataStructure['steps']), 10)
        self.assertEqual(len(myData.dataStructure['steps'][0]), 3)
        self.assertEqual(len(myData.dataStructure['steps'][0]['states']), 5)
        self.assertEqual(len(myData.dataStructure['steps'][0]['states'][0]), 1)
        self.assertEqual(len(myData.dataStructure['steps'][0]['actions']), 5)
        self.assertEqual(len(myData.dataStructure['steps'][0]['actions'][0]),
                         2)
        self.assertEqual(len(myData.dataStructure['steps'][0]['actions'][1]),
                         2)
        self.assertEqual(len(myData.dataStructure['steps'][0]['subSteps']), 5)
        self.assertEqual(
            len(myData.dataStructure['steps'][0]['subSteps'][0]['subActions']),
            1)
        self.assertEqual(
            len(myData.dataStructure['steps'][0]['subSteps'][0]['subActions']),
            1)
        self.assertEqual(
            len(myData.dataStructure['steps'][0]['subSteps'][0]['subActions']
                [0]), 2)
Exemplo n.º 19
0
    def test_PeriodicEntry(self):
        dataManager = DataManager("manager", isTimeSeries=False)
        dataManager.addDataEntry("entry1", 1)
        dataManager.addDataEntry("entry2", 3, isPeriodic=[True, False, True])
        dataManager.addDataAlias("entries", [('entry1', ...), ('entry2', ...)])

        periodTest = dataManager.getPeriodicity('entries')
        self.assertTrue(periodTest == [False, True, False, True])
        data = dataManager.createDataObject(2)
        data.setDataEntry('entries', ...,
                          np.array([[0, 8, 0, 2], [0, 2, 0, -8]]))

        entryPeriodic = data.getDataEntry('entries_periodic')
        self.assertTrue(
            sum(
                sum(
                    np.abs(entryPeriodic -
                           np.array([[0, 1.71681469, 0, 2],
                                     [0, 2, 0, 4.56637061]])))) < 0.001)
Exemplo n.º 20
0
    def test_getDataEntryList(self):
        dataManager = DataManager('episodes')
        dataManager.addDataEntry('parameters', 5)
        dataManager.addDataEntry('contexts', 3)
        myData = dataManager.createDataObject([10])

        # set the data for the parameters and context of all episodes
        myData.setDataEntry(['parameters'], [...], np.ones((10, 5)))
        myData.setDataEntry(['contexts'], [...], np.ones((10, 3)))

        result = myData.getDataEntryList(['parameters', 'contexts'], [...])

        self.assertTrue(isinstance(result, list))
        self.assertTrue((result[0] == np.ones((10, 5))).all())
        self.assertTrue((result[1] == np.ones((10, 3))).all())

        result = myData.getDataEntryList([('parameters', 'contexts')], [...])

        self.assertEqual(len(result), 1)
        self.assertTrue((result[0] == np.ones((10, 8))).all())
Exemplo n.º 21
0
    def testDiagionalGaussian(self):
        num_cpu = 1
        tf_config = tf.ConfigProto(inter_op_parallelism_threads=num_cpu,
                                   intra_op_parallelism_threads=num_cpu)
        session = tf.Session(config=tf_config)
        session.__enter__()

        dataManager = DataManager('data')
        dataManager.addDataEntry('states', 10)
        dataManager.addDataEntry('actions', 5)

        generatorMean = tfutils.continuous_MLP_generator([100, 100])
        generatorLogStd = tfutils.diagional_log_std_generator()

        gaussian = DiagonalGaussian_Base(dataManager, ['states'], ['actions'],
                                         generatorMean, generatorLogStd)

        data = dataManager.createDataObject([10])

        data[...].states = np.random.normal(0, 1, data[...].states.shape)
    def test_getExpectation(self):
        dataManager = DataManager('values')
        dataManager.addDataEntry('X', 2)
        dataManager.addDataEntry('Y', 1)
        f = FunctionLinearInFeatures(dataManager, ['X'], ['Y'], None, None)

        X = np.array([[1, 1], [2, 1]])
        f.bias = np.ones((1)) * 5
        f.weights = np.array([
            [2, 1],
        ])
        Y = f.bias + np.dot(X, f.weights.transpose())

        self.assertTrue((Y == f.computeOutput(inputFeatures=X)).all())

        # Todo implement fromData?
        self.assertTrue((Y == f(X)).all())

        data = dataManager.createDataObject(2)
        data.setDataEntry('X', ..., X)

        data[...] >> f
        self.assertTrue((Y == data.Y).all())
Exemplo n.º 23
0
import numpy as np
from pypost.mappings.Gaussian import LinearDiagonalGaussian


num_cpu = 1
tf_config = tf.ConfigProto(inter_op_parallelism_threads=num_cpu, intra_op_parallelism_threads=num_cpu)
session = tf.Session(config=tf_config)
session.__enter__()

dataManager = DataManager('data')
dataManager.addDataEntry('states', 10)
dataManager.addDataEntry('actions', 5)

gaussian = LinearDiagonalGaussian(dataManager, ['states'], ['actions'])

data = dataManager.createDataObject([10])
data[...].states = np.random.normal(0, 1, data[...].states.shape)

# Sample from Gaussian and write back in data
data[...] >> gaussian >= data

# compute logLikelihood
data[...] >= gaussian.logLike

# Compute log likelihood
gaussianOther = LinearDiagonalGaussian(dataManager, ['states'], ['actions'])

# the param_* properties are created automatically by parsing the mean and logStd tensors. They can be set and read as normal numpy arrays
gaussianOther.param_final_b = np.random.normal(0, 1, (5,))
gaussianOther.param_logstd = np.random.normal(0, 1, (5,))
Exemplo n.º 24
0
 def test_addDataEntry_after_finalize(self):
     dataManager = DataManager('episodes')
     myData = dataManager.createDataObject(10)
     self.assertRaises(RuntimeError, dataManager.addDataEntry, 'parameters',
                       5)
Exemplo n.º 25
0
import numpy as np
'''
In this example we are going to see how to use aliases.
'''

# create data manager
dataManager = DataManager('episodes')
subDataManager = DataManagerTimeSeries('steps')

subDataManager.addDataEntry('states', 2)
subDataManager.addDataEntry('actions', 3, dataType=DataType.discrete)

dataManager.subDataManager = subDataManager

# create data object with 10 episodes
data = dataManager.createDataObject(10)

# fill episodes. we want to use a different number of timesteps for each episode

for i in range(0, 10):
    numTimeSteps = i + 5
    data.reserveStorage(numTimeSteps, [i])

    print('Size of states matrix: ', data[i].states.shape)

    #fill in some dummy actions
    data[i].allStates = np.array(
        np.vstack(
            (range(0,
                   numTimeSteps + 1), range(0,
                                            numTimeSteps + 1)))).transpose()
Exemplo n.º 26
0
                                  -3,
                                  1,
                                  dropBoundarySamples=False)

# now we only need to connect the data managers and finalize them
dataManager.subDataManager = subDataManager

# Initialization of the data
# so far we have defined the structure of our data
# now we want to create new data

numTimeSteps = 1000

# here we create new data object with 1 episodes, 10000 steps
# this method will also finalize the dataManager
myData = dataManager.createDataObject([100, numTimeSteps])

# for time series, we have special aliases for accesing the next element, previous element and all elements

# Fill all states with series
allStates = np.zeros((numTimeSteps + 1, 5))

allStates[:, 0] = np.array(range(0, numTimeSteps + 1)).transpose()
allStates[:, 1] = np.array(range(0, numTimeSteps + 1)).transpose() + 0.1
allStates[:, 2] = np.array(range(0, numTimeSteps + 1)).transpose() + 0.2
allStates[:, 3] = np.array(range(0, numTimeSteps + 1)).transpose() + 0.3
allStates[:, 4] = np.array(range(0, numTimeSteps + 1)).transpose() + 0.4

for i in range(0, 100):
    myData[i].allStates = allStates
Exemplo n.º 27
0
    def test_setgetDataEntry(self):
        dataManager = DataManager('episodes')
        subDataManager = DataManager('steps')
        subSubDataManager = DataManager('subSteps')

        dataManager.subDataManager = subDataManager
        subDataManager.subDataManager = subSubDataManager

        subSubDataManager.addDataEntry('subActions', 2, -10, 10)

        myData = dataManager.createDataObject([10, 5, 3])

        # set the data for all subActions of all episodes, steps and subSteps
        myData.setDataEntry(['steps', 'subSteps', 'subActions'], [],
                            np.ones((150, 2)))

        # all subActions in the 3rd subSteps of all steps of the 2nd episode
        # should have different parameters
        myData.setDataEntry(['steps', 'subSteps', 'subActions'], [1, ..., 2],
                            np.array([[2, 1], [2, 2], [2, 3], [2, 4], [2, 5]]))

        self.assertTrue((myData.dataStructure['steps'][0]['subSteps'][0]
                         ['subActions'][0] == np.array([1, 1])).all())

        self.assertTrue((myData.dataStructure['steps'][1]['subSteps'][1]
                         ['subActions'][1] == np.array([1, 1])).all())

        self.assertTrue((myData.dataStructure['steps'][9]['subSteps'][4]
                         ['subActions'][2] == np.array([1, 1])).all())

        self.assertTrue((myData.dataStructure['steps'][1]['subSteps'][4]
                         ['subActions'][1] == np.array([1, 1])).all())

        self.assertTrue((myData.dataStructure['steps'][1]['subSteps'][0]
                         ['subActions'][2] == np.array([2, 1])).all())

        self.assertTrue((myData.dataStructure['steps'][1]['subSteps'][4]
                         ['subActions'][2] == np.array([2, 5])).all())

        # tests for getDataEntry
        self.assertTrue(
            (myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                 [0, 0]) == myData.dataStructure['steps'][0]
             ['subSteps'][0]['subActions']).all())

        self.assertTrue(
            (myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                 1) == np.array([[1, 1], [1, 1], [2,
                                                                  1], [1, 1],
                                                 [1, 1], [2, 2], [1,
                                                                  1], [1, 1],
                                                 [2, 3], [1, 1], [1,
                                                                  1], [2, 4],
                                                 [1, 1], [1, 1], [2,
                                                                  5]])).all())

        self.assertTrue(
            (myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                 [1, ..., 2]) == np.array([[2, 1], [2, 2],
                                                           [2, 3], [2, 4],
                                                           [2, 5]])).all())

        self.assertTrue(
            (myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                 [1, 3, 2]) == np.array([[2, 4]])).all())

        self.assertTrue(
            (myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                 [2, 3, 2]) == np.array([[1, 1]])).all())

        # all subActions in every second subStep of all steps of the 2nd episode
        # should have different parameters
        myData.setDataEntry(['steps', 'subSteps', 'subActions'],
                            [1, slice(0, 5, 2), 2],
                            np.array([[3, 1], [3, 2], [3, 3]]))

        self.assertTrue(
            (myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                 1) == np.array([[1, 1], [1, 1], [3,
                                                                  1], [1, 1],
                                                 [1, 1], [2, 2], [1,
                                                                  1], [1, 1],
                                                 [3, 2], [1, 1], [1,
                                                                  1], [2, 4],
                                                 [1, 1], [1, 1], [3,
                                                                  3]])).all())

        # all subActions in the first 3 subSteps of all steps of the 2nd episode
        # should have different parameters
        myData.setDataEntry(['steps', 'subSteps', 'subActions'],
                            [1, slice(0, -2), 2],
                            np.array([[4, 1], [4, 2], [4, 3]]))

        self.assertTrue(
            (myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                 1) == np.array([[1, 1], [1, 1], [4,
                                                                  1], [1, 1],
                                                 [1, 1], [4, 2], [1,
                                                                  1], [1, 1],
                                                 [4, 3], [1, 1], [1,
                                                                  1], [2, 4],
                                                 [1, 1], [1, 1], [3,
                                                                  3]])).all())

        self.assertRaises(ValueError, myData.setDataEntry,
                          ['steps', 'subSteps', 'subActions'],
                          [1, 2, slice(0, 1)], np.array([[5, 1], [5, 2]]))

        self.assertRaises(ValueError, myData.setDataEntry,
                          ['steps', 'subSteps', 'subActions'], [1, ..., 0],
                          np.array([[5, 1], [5, 2], [5, 3]]))

        self.assertRaises(ValueError, myData.setDataEntry,
                          ['steps', 'subSteps', 'subActions'],
                          [1, slice(0, -2), 2], np.array([[5, 1], [5, 2]]))

        self.assertRaises(ValueError, myData.setDataEntry,
                          ['steps', 'subSteps', 'subActions'],
                          [1, slice(0, -2), 2], np.array([]))

        self.assertRaises(ValueError, myData.setDataEntry,
                          ['steps', 'subSteps', 'subActions'],
                          [1, slice(0, -2), 2],
                          np.array([[5, 1], [5, 2], [5, 3], [5, 4]]))
Exemplo n.º 28
0
    def test_getDataEntryDimensions(self):
        dataManager = DataManager('episodes')
        subDataManager = DataManager('steps')
        subSubDataManager = DataManager('subSteps')

        dataManager.subDataManager = subDataManager
        subDataManager.subDataManager = subSubDataManager

        dataManager.addDataEntry('parameters', 5)
        subDataManager.addDataEntry('states', 1)
        subSubDataManager.addDataEntry('subStates', 1)
        subSubDataManager.addDataEntry('subActions', 2)

        myData = dataManager.createDataObject([10, 5, 3])

        self.assertEqual(myData.getDataEntry(['parameters']).shape[0], 10)

        self.assertEqual(
            myData.getDataEntry(['parameters'], [...]).shape[0], 10)

        self.assertEqual(myData.getDataEntry(['parameters'], [1]).shape[0], 1)

        self.assertEqual(
            myData.getDataEntry(['steps', 'states'], [1]).shape[0], 5)

        self.assertEqual(
            myData.getDataEntry(['steps', 'states'], [3]).shape[0], 5)

        self.assertEqual(
            myData.getDataEntry(['steps', 'states'], [..., 1]).shape[0], 10)

        self.assertEqual(
            myData.getDataEntry(['steps', 'states'], [..., ...]).shape[0], 50)

        self.assertEqual(myData.getDataEntry(['steps', 'states']).shape[0], 50)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [..., ..., ...]).shape[0], 150)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions']).shape[0],
            150)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [..., ..., 1]).shape[0], 50)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [..., 1, ...]).shape[0], 30)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [..., 1]).shape[0], 30)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [..., 1, 1]).shape[0], 10)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [1, ..., ...]).shape[0], 15)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [1]).shape[0], 15)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                1).shape[0], 15)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [1, ..., 1]).shape[0], 5)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [1, 1, ...]).shape[0], 3)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [1, 1, 1]).shape[0], 1)
Exemplo n.º 29
0
 def test_addDataAlias_after_finalize(self):
     dataManager = DataManager('episodes')
     myData = dataManager.createDataObject([10])
     self.assertRaises(RuntimeError, dataManager.addDataAlias, 'alias', [])
Exemplo n.º 30
0
dataManager.addDataEntry('contexts', 5)

# add an alias
# parameterAlias points to the first two dimensions of parameters
dataManager.addDataAlias('parameterAlias', [('parameters',  slice(0, 2))])

# it's also possible to create an alias that points to another alias
# 'aliasAlias' will now be the aquivalent to 'parameters'
dataManager.addDataAlias('aliasAlias', [('parameterAlias', ...), ('contexts', slice(2, 5))])

dataManager.addDataAlias('parametersAliasVertical', [('parameters',  slice(0, 2)), ('parameters',  slice(2, 4))], useConcatVertical=True)


# create the data object
myData = dataManager.createDataObject([3])

# set the entries of the 'parameters' entry. This will also affect the alias
# that points to 'parameters'
parameters = myData[...].parameters
parameters[:] = [1, 2, 3, 4, 5]
myData[...].parameters = parameters

# print all parameters
print('initial parameters\n', parameters, '\n\n')

# print all parameters again (using the alias)
aliasAlias = myData[...].aliasAlias
print('initial aliasAlias\n', aliasAlias, '\n\n')

# update the parameters via the alias