Пример #1
0
    def test001(self):

        nLevers = 10
        nHorizonValueOptimization = 3

        valueFunctionApproximator = ConcValueFunctionApproximator(nLevers)
        agent = ConcAgent(nLevers)

        valueFunctionOptimizer = ConcValueFunctionOptimizer(
            valueFunctionApproximator, agent, nHorizonValueOptimization)

        assert isinstance(valueFunctionOptimizer, ConcValueFunctionOptimizer)

        observationSequences = MyArray()

        Nstep = 10
        Ny = 1
        observationSequence = ObservationSequence()
        for _ in range(Nstep + 1):
            y = np.random.randn(1, Ny).astype(np.float32)
            observationSequence.add(ConcObservation(y))
            observationSequences.add(observationSequence)

        nIntervalPolicyOptimization = 10
        nBatchPolicyOptimization = 2**5
        policyOptimizer = ConcPolicyOptimizer(agent, valueFunctionApproximator,
                                              nIntervalPolicyOptimization,
                                              nBatchPolicyOptimization)

        policyOptimizer.train(observationSequences)
 def test005(self):
     
     builder = self.builder        
     
     buildOrders = MyArray()            
     buildOrder = ConcBuildOrder(nIteration=100
                 , nSeq=2
                 , nHorizonValueOptimization=1
                 , nIntervalPolicyOptimization=10
                 , nBatchPolicyOptimization=2**5
                 , nSaveInterval=2**5
                 , description="test"
                 , tConstant = 10
                 , nHiddenValueApproximator = 2**5
                 , sdPolicy = 0.1
                 , nActionsSampledFromPolicy = 2**1
                 , amplitudeDv = 0.0
                 , environmentName = "AsmSimulator"
                 , asmPenaltyType = 0
                 , asmWeightOnCost = 0.9
                 , amplitudePeriodicDv= 2.0
                 , agentEnableDcomponent = True
                 , agentEnableIcomponent = True
                 , agentLimitBy = "clamp"                    
                 )
     
     buildOrders.add(buildOrder)
     
     builder.build(buildOrders)
    def test_size_when_more_than_one_element_in_myarray(self):
        # Arrange
        myarray = MyArray('i', 100, 200)

        # Act
        size = myarray.size()

        # Assert
        self.assertEqual(2, size)
    def test_set_when_more_than_one_element_in_myarray(self):
        # Arrange
        myarray = MyArray('i', 100, 200)

        # Act
        myarray.set(1, 300)

        # Assert
        self.assertEqual(300, myarray.get(1))
    def test_set_when_single_element_in_myarray(self):
        # Arrange
        myarray = MyArray('i', 100)

        # Act
        myarray.set(0, 200)

        # Assert
        self.assertEqual(200, myarray.get(0))
    def test_size_when_single_element_in_myarray(self):
        # Arrange
        myarray = MyArray('i', 100)

        # Act
        size = myarray.size()

        # Assert
        self.assertEqual(1, size)
    def test_pop_when_single_element_in_myarray(self):
        # Arrange
        myarray = MyArray('i', 20)

        # Act
        myarray.pop()

        # Assert
        self.assertEqual(0, myarray.size())
    def test_size_when_empty_myarray(self):
        # Arrange
        myarray = MyArray()

        # Act
        size = myarray.size()

        # Assert
        self.assertEqual(0, size)
    def test_get_when_single_element_in_myarray(self):
        # Arrange
        myarray = MyArray('i', 100)

        # Act
        element = myarray.get(0)

        # Assert
        self.assertEqual(100, element)
 def requestUpdate(self, trainer):
     """ generated source for method requestUpdate """
     nSample = 0
     observationSequences = None
     if (trainer.getTimeSimulation() + 1) % self.nIntervalPolicyOptimization == 0:
         observationSequences = MyArray()
         nSample = trainer.getTimeSimulation() + 2
         for i in Utils.myRandomRrandint(0, nSample, self.nBatchPolicyOptimization):
             observationSequences.add(trainer.getObservationSequence(i))
         self.train(observationSequences)
         trainer.markPolicyUpdate()
    def test_get_when_more_than_one_element_in_myarray(self):
        # Arrange
        myarray = MyArray('i', 100, 200)

        # Act
        element_1 = myarray.get(0)
        element_2 = myarray.get(1)

        # Assert
        self.assertEqual(100, element_1)
        self.assertEqual(200, element_2)
    def test001(self):

        nHiddenValueApproximator = 2**3
        sdPolicy = 0.01

        valueFunctionApproximator = ConcValueFunctionApproximator(
            nHiddenValueApproximator)
        agent = ConcAgent(ConcEnvironment.nMv, sdPolicy)

        observationSequences = MyArray()

        Nstep = 10
        observationSequence = ObservationSequence()
        for _ in range(Nstep + 1):
            y = np.random.randn(1, ConcEnvironment.nPv).astype(
                np.float32)  # (1, nPv)
            observationSequence.add(ConcObservation(y))
            observationSequences.add(observationSequence)

        u = np.random.randn(1, ConcEnvironment.nMv)  # (1, nMv)
        action = ConcAction(u)

        # to initialize the internal parameters
        agent(observationSequence)
        valueFunctionApproximator(observationSequence, action)

        nIntervalPolicyOptimization = 10
        nBatchPolicyOptimization = 2**5
        nActionsSampledFromPolicy = 2**3

        policyOptimizer = ConcPolicyOptimizer(agent, valueFunctionApproximator,
                                              nIntervalPolicyOptimization,
                                              nBatchPolicyOptimization,
                                              nActionsSampledFromPolicy)

        param0_policy = [elm.numpy() for elm in agent.trainable_variables]
        param0_valfunc = [
            elm.numpy()
            for elm in valueFunctionApproximator.trainable_variables
        ]
        policyOptimizer.train(observationSequences)
        param1_policy = [elm.numpy() for elm in agent.trainable_variables]
        param1_valfunc = [
            elm.numpy()
            for elm in valueFunctionApproximator.trainable_variables
        ]

        for (elm0, elm1) in zip(param0_valfunc, param1_valfunc):
            assert np.all(elm0 == elm1)

        for (elm0, elm1) in zip(param0_policy, param1_policy):
            assert not np.all(elm0 == elm1)
Пример #13
0
 def __add__(self, n):  #数组中每个元素都与数字n相加,或两个数组相加
     if self.__IsNumber(n):
         b=MyArray()
         for v in self.__value:
             b.__value.append(v+n)
         return b
     elif isinstance(n,MyArray):
         if len(n.__value)==len(self.__value):
             c=MyArray()
             for i,j in zip(self.__value,n.__value):
                 c.__value.append(i+j)
             return c
         else:
             print('Lenght not equal')
     else:
         print('Not supported')
 def case001(self):
     """ generated source for method case001 """
     store = Store()
     agentFactory = AgentFactory()
     environmentFactory = EnvironmentFactory()
     trainerFactory = TrainerFactory()
     valueFunctionApproximatorFactory = ValueFunctionApproximatorFactory()
     rewardGiverFactory = RewardGiverFactory()
     myLogger = MyLogger()
     buildOrders = MyArray()
     i = 0
     while i < 10:
         buildOrders.add(BuildOrder(100, 3, 2, 10, 16, 3, ""))
         i += 1
     builder = Builder(store, agentFactory, environmentFactory, trainerFactory, valueFunctionApproximatorFactory, rewardGiverFactory, myLogger)
     builder.build(buildOrders)
Пример #15
0
 def __floordiv__(self, n): #数组中每个元素都与数字n整除,返回新数组
     if not isinstance(n,int):
         print(n,' is not an interger')
         return
     b=MyArray()
     for v in self.__value:
         b.__value.append(v//n)
     return b
Пример #16
0
 def __mod__(self, n): #数组中每个元素都与数字n求余数,返回新数组
     if not self.__IsNumber(n):
         print(r'% operating with',type(n),' and number type is not supported.')
         return
     b = MyArray()
     for v in self.__value:
         b.__value.append(v%n)
     return b
Пример #17
0
 def __pow__(self,n): #数组中每个元素都与数字n进行幂计算,返回新数组
     if not self.__IsNumber(n):
         print('** operating with',type(n),' and number type is not supported.')
         return
     b = MyArray()
     for v in self.__value:
         b.__value.append(v ** n)
     return b
    def test002(self):

        builder = self.builder

        buildOrders = MyArray()

        for k1 in range(3):
            buildOrder = ConcBuildOrder(nIteration=100,
                                        nSeq=2,
                                        nHorizonValueOptimization=1,
                                        nIntervalPolicyOptimization=10,
                                        nBatchPolicyOptimization=2**5,
                                        nSaveInterval=2**5,
                                        description="test %d/3" % (k1 + 1),
                                        nLevers=3)

            buildOrders.add(buildOrder)

        builder.build(buildOrders)
Пример #19
0
 def dot(self, v):  #模拟向量内积
     if not isinstance(v,MyArray):
         print(v,' must be an instance of MyArray.')
         return
     if len(v)!=len(self.__value):
         print('The size must be equal.')
         return
     b=MyArray()
     for m,n in zip(v.__value,self.__value):
         b.__value.append(m*n)
     return sum(b.__value)
Пример #20
0
class TestArray(object):
    if __name__ == "__main__":
        array = MyArray()
        print(array)
        print(array.size())
        print(len(array))

        for i in range(20):
            array.append(i)

        array.insert(0, 10)
        array.remove(10)
        print(array)
    def test_insert_when_empty_myarray(self):
        #Arrange
        myarray = MyArray('i')

        self.assertEqual(0, myarray.size())
        # Act
        myarray.insert(0, 100)

        # Assert
        self.assertEqual(1, myarray.size())
        self.assertEqual(100, myarray.get(0))
    def test_insert_when_single_element_in_myarray(self):
        # Arrange
        myarray = MyArray('i', 20)

        # Act
        myarray.insert(0, 100)

        # Assert
        self.assertEqual(2, myarray.size())
        self.assertEqual(100, myarray.get(0))
        self.assertEqual(20, myarray.get(1))
    def test_insert_when_more_than_one_element_in_myarray(self):
        # Arrange
        myarray = MyArray('i', 20, 30)

        # Act
        myarray.insert(0, 100)

        # Assert
        self.assertEqual(3, myarray.size())
        self.assertEqual(100, myarray.get(0))
        self.assertEqual(20, myarray.get(1))
        self.assertEqual(30, myarray.get(2))
    def test_pop_when_more_than_one_element_in_myarray(self):
        # Arrange
        myarray = MyArray('i', 20, 30)

        # Act
        myarray.pop(0)

        # Assert
        self.assertEqual(1, myarray.size())
        self.assertEqual(30, myarray.get(0))
    def test_append_when_empty_myarray(self):
        # Arrange
        myarray = MyArray('i')

        # Act
        myarray.append(100)

        # Assert
        self.assertEqual(1, myarray.size())
        self.assertEqual(100, myarray.get(0))
    def test_pop_when_more_than_two_element_in_myarray(self):
        # Arrange
        myarray = MyArray('i', 20, 30, 40)

        # Act
        myarray.pop(1)

        # Assert
        self.assertEqual(2, myarray.size())
        self.assertEqual(20, myarray.get(0))
        self.assertEqual(40, myarray.get(1))
 def __init__(self, closedLoopSimulator, valueFunctionOptimizer, policyOptimizer, rewardGiver, nIteration, nSaveInterval):
     """ generated source for method __init__ """
     #  TODO Auto-generated constructor stub
     self.closedLoopSimulator = closedLoopSimulator
     self.valueFunctionOptimizer = valueFunctionOptimizer
     self.policyOptimizer = policyOptimizer
     self.rewardGiver = rewardGiver
     self.nIteration = nIteration
     self.nSaveInterval = nSaveInterval
     self.historyActions = MyArray()
     self.historyObservationSequences = MyArray()
     self.historyRewards = MyArray()
    def test_pop_when_empty_myarray(self):
        # Arrange
        myarray = MyArray()

        # Act & Assert
        self.assertRaises(IndexError, myarray.pop, -1)
    def test_set_when_empty_myarray(self):
        # Arrange
        myarray = MyArray('i')

        # Act & Assert
        self.assertRaises(IndexError, myarray.set, 0, 100)
    def test_pop_when_index_out_bounds(self):
        # Arrange
        myarray = MyArray('i', 20, 30, 40)

        # Act & Assert
        self.assertRaises(IndexError, myarray.pop, 4)