コード例 #1
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))
コード例 #2
0
    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))
コード例 #3
0
    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))
コード例 #4
0
    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))
コード例 #5
0
    def test_get_when_single_element_in_myarray(self):
        # Arrange
        myarray = MyArray('i', 100)

        # Act
        element = myarray.get(0)

        # Assert
        self.assertEqual(100, element)
コード例 #6
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))
コード例 #7
0
    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))
コード例 #8
0
    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)
コード例 #9
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))
コード例 #10
0
    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))
class Trainer(object):

    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 init(self):
        """ generated source for method init """
        self.historyActions.clear()
        self.historyObservationSequences.clear()
        self.historyRewards.clear()
        self.closedLoopSimulator.requestInit(self)

    def train(self, nIterationLocal):
        """ generated source for method train """
        #  TODO Auto-generated constructor stub
        i = 0
        while i < nIterationLocal:
            #  TODO Auto-generated constructor stub
            self.closedLoopSimulator.requestUpdate(self)
            self.valueFunctionOptimizer.requestUpdate(self)
            self.policyOptimizer.requestUpdate(self)
            i += 1

    def requestTrain(self, builder):
        """ generated source for method requestTrain """
        self.init()
        cnt = 0
        while True:
            if cnt < self.nIteration:
                builder.saveAgent(self)
                self.train(self.nSaveInterval)
                cnt += self.nSaveInterval
            else:
                builder.saveAgent(self)
                break

    def addObservationSequence(self, observationSequence):
        """ generated source for method addObservationSequence """
        self.historyObservationSequences.add(observationSequence)

    def addAction(self, action):
        """ generated source for method addAction """
        self.historyActions.add(action)

    def addReward(self, reward):
        """ generated source for method addReward """
        self.historyRewards.add(reward)

    def getObservationSequence(self, idx):
        """ generated source for method getObservationSequence """
        return self.historyObservationSequences.get(idx)

    def getAction(self, idx):
        """ generated source for method getAction """
        return self.historyActions.get(idx)

    def getReward(self, idx):
        """ generated source for method getReward """
        return self.historyRewards.get(idx)

    def reward(self, observationSequence, action):
        """ generated source for method reward """
        return self.rewardGiver.evaluate(observationSequence, action)

    def addTimeSimulation(self):
        """ generated source for method addTimeSimulation """
        self.timeSimulation += 1

    def markPolicyUpdate(self):
        """ generated source for method markPolicyUpdate """
        self.timeLastUpdate = self.timeSimulation

    def getTimeSimulation(self):
        """ generated source for method getTimeSimulation """
        return self.timeSimulation

    def getTimeLastUpdate(self):
        """ generated source for method getTimeLastUpdate """
        return self.timeLastUpdate

    def setTimeSimulation(self, timeSimulation):
        """ generated source for method setTimeSimulation """
        self.timeSimulation = timeSimulation

    def setTimeLastUpdate(self, timeLastUpdate):
        """ generated source for method setTimeLastUpdate """
        self.timeLastUpdate = timeLastUpdate