def test_count_batch3(self): interactions = [ Interaction(1,[1,2],0), Interaction(1,[1,2],1), Interaction(1,[1,2],2) ] rewards = [ [3,3], [4,4], [5,5] ] simulation = MemorySimulation(interactions,rewards) batch_simulation = Batch(count=3).filter(simulation) self.assertEqual(3, len(batch_simulation.interaction_batches)) self.assertEqual(1, len(batch_simulation.interaction_batches[0])) self.assertEqual(0, batch_simulation.interaction_batches[0][0].key) self.assertEqual(1, len(batch_simulation.interaction_batches[1])) self.assertEqual(1, batch_simulation.interaction_batches[1][0].key) self.assertEqual(1, len(batch_simulation.interaction_batches[2])) self.assertEqual(2, batch_simulation.interaction_batches[2][0].key)
def test_interactions(self): simulation = MemorySimulation([ Interaction(1, [1, 2, 3], [0, 1, 2]), Interaction(2, [4, 5, 6], [2, 3, 4]) ]) interactions = list(simulation.read()) self.assertEqual(interactions[0], interactions[0]) self.assertEqual(interactions[1], interactions[1])
def test_interactions(self): interactions = [Interaction(1, [1,2,3], 0), Interaction(2,[4,5,6],1)] reward_sets = [[0,1,2], [2,3,4]] simulation = MemorySimulation(interactions, reward_sets) self.assertEqual(1 , simulation.interactions[0].context) self.assertEqual([1,2,3], simulation.interactions[0].actions) self.assertEqual([0,1,2], simulation.reward([(0,0),(0,1),(0,2)])) self.assertEqual(2 , simulation.interactions[1].context) self.assertEqual([4,5,6], simulation.interactions[1].actions) self.assertEqual([2,3,4], simulation.reward([(1,0),(1,1),(1,2)]))
def test_sort3(self) -> None: interactions = [ Interaction((1, 2), [1], [1]), Interaction((1, 9), [1], [1]), Interaction((1, 3), [1], [1]) ] mem_interactions = interactions srt_interactions = list(Sort(*[0, 1]).filter(mem_interactions)) self.assertEqual((1, 2), mem_interactions[0].context) self.assertEqual((1, 9), mem_interactions[1].context) self.assertEqual((1, 3), mem_interactions[2].context) self.assertEqual((1, 2), srt_interactions[0].context) self.assertEqual((1, 3), srt_interactions[1].context) self.assertEqual((1, 9), srt_interactions[2].context)
def test_PCA(self): interactions = [ Interaction((1, 2), [1], [1]), Interaction((1, 9), [1], [1]), Interaction((7, 3), [1], [1]) ] mem_interactions = interactions pca_interactions = list(PCA().filter(interactions)) self.assertEqual((1, 2), mem_interactions[0].context) self.assertEqual((1, 9), mem_interactions[1].context) self.assertEqual((7, 3), mem_interactions[2].context) self.assertNotEqual((1, 2), pca_interactions[0].context) self.assertNotEqual((1, 9), pca_interactions[1].context) self.assertNotEqual((7, 3), pca_interactions[2].context)
def test_PCA(self): interactions = [ Interaction((1,2), [1], 0), Interaction((1,9), [1], 1), Interaction((7,3), [1], 2) ] mem_sim = MemorySimulation(interactions, [[1],[1],[1]]) pca_sim = PCA().filter(mem_sim) self.assertEqual((1,2), mem_sim.interactions[0].context) self.assertEqual((1,9), mem_sim.interactions[1].context) self.assertEqual((7,3), mem_sim.interactions[2].context) self.assertNotEqual((1,2), pca_sim.interactions[0].context) self.assertNotEqual((1,9), pca_sim.interactions[1].context) self.assertNotEqual((7,3), pca_sim.interactions[2].context)
def test_sort2(self) -> None: interactions = [ Interaction((1,2), [1], 0), Interaction((1,9), [1], 1), Interaction((1,3), [1], 2) ] mem_sim = MemorySimulation(interactions, [[1],[1],[1]]) srt_sim = Sort([0,1]).filter(mem_sim) self.assertEqual((1,2), mem_sim.interactions[0].context) self.assertEqual((1,9), mem_sim.interactions[1].context) self.assertEqual((1,3), mem_sim.interactions[2].context) self.assertEqual((1,2), srt_sim.interactions[0].context) self.assertEqual((1,3), srt_sim.interactions[1].context) self.assertEqual((1,9), srt_sim.interactions[2].context)
def test_take4(self): interactions = [ Interaction(1,[1,2],0), Interaction(1,[1,2],1), Interaction(1,[1,2],2) ] rewards = [ [3,3], [4,4], [5,5] ] simulation = MemorySimulation(interactions,rewards) take_simulation = Take(4).filter(simulation) self.assertEqual(3, len(simulation.interactions)) self.assertEqual(0, len(take_simulation.interactions))
def test_take1(self): interactions = [ Interaction(1,[1,2],0), Interaction(1,[1,2],1), Interaction(1,[1,2],2) ] rewards = [ [3,3], [4,4], [5,5] ] simulation = MemorySimulation(interactions,rewards) take_simulation = Take(1).filter(simulation) self.assertEqual(1, len(take_simulation.interactions)) self.assertEqual(0, take_simulation.interactions[0].key) self.assertEqual(3, len(simulation.interactions)) self.assertEqual(0, simulation.interactions[0].key) self.assertEqual(1, simulation.interactions[1].key) self.assertEqual(2, simulation.interactions[2].key)
def test_shuffle(self): interactions = [ Interaction(1,[1,2],0), Interaction(1,[1,2],1), Interaction(1,[1,2],2) ] rewards = [ [3,3], [4,4], [5,5] ] simulation = MemorySimulation(interactions,rewards) shuffled_simulation = Shuffle(40).filter(simulation) self.assertEqual(len(shuffled_simulation.interactions), len(simulation.interactions)) self.assertEqual(1, shuffled_simulation.interactions[0].key) self.assertEqual(2, shuffled_simulation.interactions[1].key) self.assertEqual(0, shuffled_simulation.interactions[2].key) self.assertEqual(0, simulation.interactions[0].key) self.assertEqual(1, simulation.interactions[1].key) self.assertEqual(2, simulation.interactions[2].key)
def test_actions_correct_1(self) -> None: self.assertSequenceEqual([1, 2], Interaction(None, [1, 2], [1, 2]).actions)
def test_context_correct_1(self) -> None: self.assertEqual(None, Interaction(None, [1, 2], [1, 2]).context)
def test_constructor_context(self) -> None: Interaction((1, 2, 3, 4), [1, 2], [1, 2])
def test_constructor_no_context(self) -> None: Interaction(None, [1, 2], [1, 2])
def test_actions_correct_3(self) -> None: self.assertSequenceEqual([(1, 2), (3, 4)], Interaction(None, [(1, 2), (3, 4)], [1, 2]).actions)
def test_actions_correct_2(self) -> None: self.assertSequenceEqual(["A", "B"], Interaction(None, ["A", "B"]).actions)