コード例 #1
0
ファイル: testStepSampler.py プロジェクト: LCAS/pypost-core
    def testSampling(self):

        settings = SettingsManager.getDefaultSettings()
        settings.numTimeSteps = 40
        environment = TestEnvironment(self.dataManager)
        policy = TestPolicy(self.dataManager)
        reward = TestReward(self.dataManager)

        self.sampler.setInitStateSampler(initState)
        self.sampler.setTransitionFunction(environment)
        self.sampler.setPolicy(policy)
        self.sampler.setRewardFunction(reward)

        data = self.dataManager.createDataObject([10, 100])

        data[slice(0, 10)] >> self.sampler

        states = data.getDataEntry('states', 1)
        actions = data.getDataEntry('actions', 2)
        rewards = data.getDataEntry('rewards', 3)

        statesTarget = np.array(range(1, 41))
        self.assertTrue(
            (abs(states.transpose() - statesTarget) < 0.00001).all())
        self.assertTrue((abs(states * 2 - actions) < 0.00001).all())
        self.assertTrue((abs(states * 2 - rewards) < 0.00001).all())
コード例 #2
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()
コード例 #3
0
    def setUp(self):
        defaultSettings = SettingsManager.getDefaultSettings()
        defaultSettings.setProperty('noiseStd', 1.0)
        defaultSettings.setProperty('initialStateDistributionMinRange',
                                    np.asarray([np.pi - np.pi, -2]))
        defaultSettings.setProperty('initialStateDistributionMaxRange',
                                    np.asarray([np.pi + np.pi, 2]))
        defaultSettings.setProperty('initialStateDistributionType', 'Uniform')
        defaultSettings.setProperty('dt', 0.025)
        defaultSettings.setProperty('initSigmaActions', 1.0)
        defaultSettings.setProperty('initialStateDistributionMinRange',
                                    np.asarray([np.pi - np.pi, -2]))
        defaultSettings.setProperty('initialStateDistributionMaxRange',
                                    np.asarray([np.pi + np.pi, 2]))

        self.sampler = EpisodeWithStepsSampler()
        self.episodeManager = self.sampler.getEpisodeDataManager()
        self.stepManager = self.episodeManager.subDataManager
        self.pendulum = Pendulum(self.episodeManager)

        self.sampler.stepSampler.setIsActiveSampler(
            StepBasedEpisodeTerminationSampler(self.episodeManager,
                                               'bla',
                                               numTimeSteps=40))

        initialStateSampler = InitialStateSamplerStandard(self.episodeManager)

        dummyActionAndReward = DummyActionAndReward(
            self.episodeManager.subDataManager, 1)

        self.sampler.setTransitionFunction(self.pendulum.getExpectedNextState)
        self.sampler.setInitStateSampler(initialStateSampler.sampleInitState)
        self.sampler.setActionPolicy(dummyActionAndReward.sampleAction)
        self.sampler.setRewardFunction(dummyActionAndReward.sampleReward)
        self.sampler.setReturnFunction(dummyActionAndReward.sampleReward)
コード例 #4
0
 def setUp(self):
     self.optimizerName = 'myOptimizer'
     self.settings = SettingsManager.getDefaultSettings()
     self.settings.setProperty(self.optimizerName + 'maxNumIterations', 100)
     self.settings.setProperty(self.optimizerName + 'method',
                               SciPyBoxConstrainedAlgorithms.L_BFGS_B)
     self.optimizer = SciPyBoxConstrained(
         2, optimizationName=self.optimizerName)
コード例 #5
0
    def setUp(self):
        self.optimizerName = 'rosen_optimizer'
        self.settings = SettingsManager.getDefaultSettings()
        self.settings.setProperty(self.optimizerName + 'method', SciPyUnconstrainedAlgorithms.CG)

        self.optimizer = SciPyUnconstrained(2, optimizationName=self.optimizerName)
        # use this, else we start directly at the optimum
        self.x0 = np.asarray([5, 5])
        self.reference_params, self.reference_value, _ = self.optimizer.optimize(rosen, rosen_der, self.x0)
コード例 #6
0
ファイル: SettingsClient.py プロジェクト: LCAS/pypost-core
    def __init__(self, namespace=None):
        '''Creates a new object.
        '''
        self.settingsClientName = SettingsManager.getNextClientName()
        self.settings = SettingsManager.getDefaultSettings()

        self.suffixString = self.settings.getSuffixString()
        '''
        Get a unique name for this client
        '''
        self._localPropertyMap = {}
コード例 #7
0
    def setUp(self):

        defaultSettings = SettingsManager.getDefaultSettings()
        defaultSettings.setProperty('numTimeSteps', 20)

        self.sampler = EpisodeWithStepsSampler(samplerNameSteps='steps')
        self.sampler.numSamples = 3
        self.dataManager = self.sampler.getEpisodeDataManager()
        self.sampler.stepSampler.setTerminationFunction(
            NumStepsTerminationFunction(self.dataManager,
                                        None,
                                        numTimeSteps=20))
        environment = MountainCar(self.dataManager)

        self.sampler.setContextSampler(environment.sampleContext)
        self.sampler.setActionPolicy(environment.sampleAction)
        self.sampler.setTransitionFunction(environment.transitionFunction)
        self.sampler.setRewardFunction(environment.sampleReward)
        self.sampler.setInitStateSampler(environment.sampleInitState)
コード例 #8
0
 def setUp(self):
     settings = SettingsManager.getDefaultSettings()
     settings.setProperty('numTimeSteps', 40)
     self.dataManager = DataUtil.createTestManagerSteps()
     self.stepSamplerEpisodes = EpisodeWithStepsSampler(
         self.dataManager, 'episodes', 'steps')
コード例 #9
0
from pypost.common import SettingsManager
from pypost.common import SettingsClient

settings = SettingsManager.getDefaultSettings()

settings.registerProperty('dummyValue', 1)
print('Settings.dummyValue:', settings.dummyValue)


class DummyClass(SettingsClient):
    def __init__(self):
        SettingsClient.__init__(self)

        self.myDummyVariable = 3

        # link to settings in parameter pool. Global name is dummy value
        # If the parameter does not exist, it is registered with the current property value
        self.linkPropertyToSettings('myDummyVariable', globalName='dummyValue')

    def printDummy(self):
        print('Dummy:', self.myDummyVariable)


# dummy instance takes
dummyInstance = DummyClass()

# Value should now be 1.0 (taken from the parameter pool)
dummyInstance.printDummy()

settings.dummyValue = 4.0
# Value should now be 4.0 (taken from the parameter pool)
コード例 #10
0
    def test_setProperty(self):
        SettingsManager.cleanup()
        self.assertEqual(SettingsManager.inDebugMode(), False)
        SettingsManager.activateDebugMode()
        self.assertEqual(SettingsManager.inDebugMode(), True)
        SettingsManager.deactivateDebugMode()
        self.assertEqual(SettingsManager.inDebugMode(), False)

        settings = Settings('testSettings01')
        SettingsManager.pushDefaultSettings(settings)

        cli = SettingsClient()
        cli.setVar('prop_a', 42.21)
        cli.linkPropertyToSettings('prop_a', globalName='A')
        self.assertIs(SettingsManager.getSettings('testSettings01'), settings)
        self.assertEqual(SettingsManager.getSettings('testSettings01').getProperty('A'), 42.21)
        self.assertEqual(cli.settingsClientName[:7], 'client_')

        self.assertEqual(SettingsManager.getDefaultName(), 'testSettings01')
        self.assertEqual(SettingsManager.getDefaultSettings().name, 'testSettings01')

        self.assertEqual(SettingsManager.getRootName(), 'default')

        SettingsManager.popDefaultSettings()
        self.assertEqual(SettingsManager.getDefaultName(), 'default')
        SettingsManager.popDefaultSettings()
        self.assertEqual(SettingsManager.getDefaultName(), 'default')

        cli.setVar('prop_b', False)
        cli.linkPropertyToSettings('prop_b', globalName='B')
        self.assertEqual(settings.getProperty('B'), False)

        cli.setVar('prop_c', 'testStr')
        cli.linkPropertyToSettings('prop_c')
        self.assertEqual(settings.getProperty('prop_c'), 'testStr')

        clonedSettings = SettingsManager.cloneSettings(settings, 'clonedSettings')
        self.assertEqual(clonedSettings.getProperty('A'), 42.21)
        self.assertEqual(clonedSettings.name, 'clonedSettings')


        emptySettings = SettingsManager.cloneSettings(None, 'emptySettings')
        self.assertDictEqual(emptySettings._properties, {})
        cSettings = SettingsManager.cloneSettings(emptySettings, 'testSettings01')
        self.assertEqual(cSettings.getProperty('A'), 42.21)

        SettingsManager.delSettings('notExistant')
        self.assertIsNotNone(SettingsManager.getSettings('testSettings01'))
        SettingsManager.delSettings('testSettings01')
        self.assertIsNone(SettingsManager.getSettings('testSettings01'))

        settings.registerProperty('A', 84.42, setValueIfAlreadyRegistered=False)
        self.assertEqual(settings.getProperty('A'), 42.21)
        settings.registerProperty('A', 84.42, setValueIfAlreadyRegistered=True)
        self.assertEqual(settings.getProperty('A'), 84.42)
        settings.setToClients()
        self.assertEqual(cli.getVar('prop_a'), 84.42)

        self.assertDictEqual(settings.getProperties(), {'A': 84.42, 'B': False, 'prop_c': 'testStr'})

        settings.registerProperty('D', 17)
        settings.setProperty('D', 97)
        self.assertEqual(settings.getProperty('D'), 97)

        settings.setIfEmpty('E', (2, 7))
        self.assertEqual(settings.getProperty('E'), (2, 7))
        settings.setIfEmpty('E', (3, 22))
        self.assertEqual(settings.getProperty('E'), (2, 7))

        clonedSettings.setProperties(settings.getProperties())
        self.assertDictEqual(clonedSettings.getProperties(), settings.getProperties())
        self.assertTrue(clonedSettings.hasValue('D', 97))
        self.assertTrue(clonedSettings.hasProperty('E'))

        #cli.setVar('prop_d', 13)
        #cli.linkProperty('prop_d', 'D', emptySettings)
        #emptySettings.copyProperties(settings)
        #dClis = emptySettings._properties['D'].clients
        #self.assertEqual(emptySettings.getProperty('D'), 97)
        #self.assertListEqual(emptySettings._properties['D'].clients, dClis)
        #self.assertEqual(emptySettings.getProperty('A'), 84.42)

        #cli.setVar('prop_e', 2)
        #cli.linkProperty('prop_e', 'E', clonedSettings)
        #self.assertEqual(cli.getVar('prop_e'), (2, 7))

        self.assertIsNone(settings.getProperty('nonExistent'))

        self.assertSetEqual(settings.getDifferentProperties(clonedSettings), set())

        try:
            settings.informClients('noProp')
        except KeyError:
            self.fail("Unhandled KeyError in Settings.informClients()!")

        settings.removeClients()
        self.assertListEqual(settings._properties['A'].clients, [])

        settings.unregisterProperty('E')
        self.assertIsNone(settings.getProperty('E'))

        try:
            settings.unregisterProperty('E')
        except KeyError:
            self.fail("Unhandled KeyError in Settings.unregisterProperty()!")

        settings.clean()
        self.assertDictEqual(settings._properties, {})


        print("Client Properties:")
        cli.printProperties()
        print("\nClonedSettings Properties:")
        clonedSettings.printProperties()
コード例 #11
0
ファイル: DataManager.py プロジェクト: LCAS/pypost-core
    def addDataEntry(self,
                     name,
                     numDimensions,
                     minRange=-1,
                     maxRange=1,
                     isPeriodic=None,
                     dataType=DataType.continuous,
                     takeFromSettings=False,
                     level=0):
        '''
        Function for adding a new data entry. If the same data entry already
        exists, then the properties are overwritten.
        minRange and maxRange are optional arguments (standard values are a
        vector of -1 and +1). Both arguments need to be row vectors with the
        same size as the specified dimensionality.

        :param string name: The name of the data entry
        :param int numDimensions: The number of dimensions of the data entry
        :param minRange: Minimum values (optional, defaults to -1)
        :param maxRange: Maximum values (optional, defaults to +1)
        :type minRange: list or number
        :type maxRange: list or number
        :raises ValueError: If the DataManager has been finalized already or
                            there is a DataAlias of that name.
        '''

        if (not isinstance(dataType, DataType)):
            raise ValueError(
                'Given dataType needs to be instance of enum Data.DataType!')

        if (level > 0):
            subManager = self.getDataManagerForLevel(level)
            subManager.addDataEntry(name=name,
                                    numDimensions=numDimensions,
                                    minRange=minRange,
                                    maxRange=maxRange,
                                    isPeriodic=isPeriodic,
                                    dataType=dataType,
                                    takeFromSettings=takeFromSettings)
            self._dirty = True
        else:
            settings = SettingsManager.getDefaultSettings()

            name = name + settings.getSuffixString()

            if self.finalized:
                raise RuntimeError(
                    "The data manager cannot be modified after "
                    "it has been finalized (data object has been created). Please apply all modifications"
                    "to the manager before creating data objects!")

            # Ensure that the name of the data entry does not conflict with an
            # alias name
            if name in self.dataAliases:
                raise ValueError(
                    "The name of an alias conflicts with a data " +
                    "entry name: " + name)

            if not isinstance(numDimensions, tuple):
                numDimensions = (numDimensions, )

            if isinstance(minRange, numbers.Number):
                minRange = minRange * np.ones(numDimensions)

            if isinstance(maxRange, numbers.Number):
                maxRange = maxRange * np.ones(numDimensions)

            self.dataEntries[name] = DataEntry(name, numDimensions, minRange,
                                               maxRange, isPeriodic, dataType,
                                               takeFromSettings)
            self.dataAliases[name] = DataAlias(name, [(name, ...)],
                                               numDimensions)
            self._dirty = True

            self.addIndexModifiers(name, numDimensions)