def test_len_loaded_memories(self): with TempDir() as temp_directory: replay_memories1_file_name = os.path.join(temp_directory, 'replay_memories.dat') replay_memories1 = ReplayMemories(replay_memories1_file_name, max_current_memories_in_ram=100) replay_memories1.append(self._create_replay_memory(consequent_reward=34.0)) replay_memories1.append(self._create_replay_memory(consequent_reward=35.0)) replay_memories1.save() replay_memories2 = ReplayMemories(replay_memories1_file_name, max_current_memories_in_ram=100) self.assertEquals(len(replay_memories2), 2)
def test_sample_loaded_memories(self): with TempDir() as temp_directory: replay_memories_file_name = os.path.join(temp_directory, 'replay_memories.dat') replay_memories1 = ReplayMemories(replay_memories_file_name, max_current_memories_in_ram=100) replay_memory1 = self._create_replay_memory(action_index=11) replay_memory2 = self._create_replay_memory(action_index=22) replay_memory3 = self._create_replay_memory(action_index=32) replay_memory4 = self._create_replay_memory(action_index=42) replay_memory5 = self._create_replay_memory(action_index=52) replay_memory6 = self._create_replay_memory(action_index=62) replay_memory7 = self._create_replay_memory(action_index=72) replay_memories1.append(replay_memory1) replay_memories1.append(replay_memory2) replay_memories1.append(replay_memory3) replay_memories1.append(replay_memory4) replay_memories1.append(replay_memory5) replay_memories1.append(replay_memory6) replay_memories1.append(replay_memory7) replay_memories1.save() replay_memories2 = ReplayMemories(replay_memories_file_name, max_current_memories_in_ram=100) sampled_replay_memories = replay_memories2.sample(5, seed=3) expected_replay_memories = [replay_memory2, replay_memory4, replay_memory7, replay_memory3, replay_memory5] self.assertItemsEqual(sampled_replay_memories, expected_replay_memories)
def test_multiple_saves(self): with TempDir() as temp_directory: replay_memories1_file_name = os.path.join(temp_directory, 'replay_memories.dat') replay_memories1 = ReplayMemories(replay_memories1_file_name, max_current_memories_in_ram=100) replay_memory1 = self._create_replay_memory(action_index=11) replay_memory2 = self._create_replay_memory(action_index=22) replay_memories1.append(replay_memory1) replay_memories1.append(replay_memory2) replay_memories1.save() replay_memories1.save() replay_memories2 = ReplayMemories(replay_memories1_file_name, max_current_memories_in_ram=100) expected_replay_memories = [replay_memory1, replay_memory2] self.assertSequenceEqual(list(replay_memories2), expected_replay_memories)
def test_retrieval_when_max_current_memories_is_reached(self): with TempDir() as temp_directory: replay_memories1_file_name = os.path.join(temp_directory, 'replay_memories.dat') replay_memories1 = ReplayMemories(replay_memories1_file_name, max_current_memories_in_ram=3) replay_memory1 = self._create_replay_memory(action_index=11) replay_memory2 = self._create_replay_memory(action_index=22) replay_memory3 = self._create_replay_memory(action_index=33) replay_memories1.append(replay_memory1) replay_memories1.append(replay_memory2) replay_memories1.append(replay_memory3) replay_memories2 = ReplayMemories(replay_memories1_file_name, max_current_memories_in_ram=100) expected_replay_memories = [replay_memory1, replay_memory2, replay_memory3] self.assertSequenceEqual(list(replay_memories2), expected_replay_memories)
def test_save_after_max_current_memories(self): with TempDir() as temp_directory: replay_memories_file_name = os.path.join(temp_directory, 'replay_memories.dat') replay_memories = ReplayMemories(replay_memories_file_name, max_current_memories_in_ram=2) replay_memory1 = self._create_replay_memory(action_index=11) replay_memory2 = self._create_replay_memory(action_index=22) replay_memory3 = self._create_replay_memory(action_index=33) replay_memories.append(replay_memory1) replay_memories.append(replay_memory2) replay_memories.append(replay_memory3) replay_memories.save() expected_replay_memories = [replay_memory1, replay_memory2, replay_memory3] self.assertSequenceEqual(list(replay_memories), expected_replay_memories)
def test_sample_recent_memories_after_appending_to_loaded_memories(self): with TempDir() as temp_directory: replay_memories_file_name = os.path.join(temp_directory, 'replay_memories.dat') replay_memories1 = ReplayMemories(replay_memories_file_name, max_current_memories_in_ram=100) replay_memory1 = self._create_replay_memory(action_index=11) replay_memory2 = self._create_replay_memory(action_index=22) replay_memory3 = self._create_replay_memory(action_index=32) replay_memory4 = self._create_replay_memory(action_index=42) replay_memory5 = self._create_replay_memory(action_index=52) replay_memory6 = self._create_replay_memory(action_index=62) replay_memory7 = self._create_replay_memory(action_index=72) replay_memories1.append(replay_memory1) replay_memories1.append(replay_memory2) replay_memories1.append(replay_memory3) replay_memories1.save() replay_memories2 = ReplayMemories(replay_memories_file_name, max_current_memories_in_ram=100) replay_memories2.append(replay_memory4) replay_memories2.append(replay_memory5) replay_memories2.append(replay_memory6) replay_memories2.append(replay_memory7) sampled_replay_memories = replay_memories2.sample(5, recent_memories_span=5) self.assertTrue(replay_memory3 in sampled_replay_memories) self.assertTrue(replay_memory4 in sampled_replay_memories) self.assertTrue(replay_memory5 in sampled_replay_memories) self.assertTrue(replay_memory6 in sampled_replay_memories) self.assertTrue(replay_memory7 in sampled_replay_memories)
def test_sample_recent_memories(self): with TempDir() as temp_directory: replay_memories_file_name = os.path.join(temp_directory, 'replay_memories.dat') replay_memories = ReplayMemories(replay_memories_file_name, max_current_memories_in_ram=100) replay_memory1 = self._create_replay_memory(action_index=11) replay_memory2 = self._create_replay_memory(action_index=22) replay_memory3 = self._create_replay_memory(action_index=32) replay_memory4 = self._create_replay_memory(action_index=42) replay_memory5 = self._create_replay_memory(action_index=52) replay_memory6 = self._create_replay_memory(action_index=62) replay_memory7 = self._create_replay_memory(action_index=72) replay_memories.append(replay_memory1) replay_memories.append(replay_memory2) replay_memories.append(replay_memory3) replay_memories.append(replay_memory4) replay_memories.append(replay_memory5) replay_memories.append(replay_memory6) replay_memories.append(replay_memory7) sampled_replay_memories = replay_memories.sample(2, recent_memories_span=2) print(sampled_replay_memories) self.assertTrue(replay_memory6 in sampled_replay_memories) self.assertTrue(replay_memory7 in sampled_replay_memories)
def test_prefetch_max_memories_in_ram_less_than_total_memories(self): with TempDir() as temp_directory: replay_memories1_file_name = os.path.join(temp_directory, 'replay_memories.dat') replay_memories1 = ReplayMemories(replay_memories1_file_name, max_current_memories_in_ram=100) replay_memory1 = self._create_replay_memory(action_index=11) replay_memory2 = self._create_replay_memory(action_index=22) replay_memory3 = self._create_replay_memory(action_index=33) replay_memories1.append(replay_memory1) replay_memories1.append(replay_memory2) replay_memories1.append(replay_memory3) replay_memories1.save() replay_memories2 = ReplayMemories(replay_memories1_file_name, max_current_memories_in_ram=2, prefetch_into_ram=True) expected_replay_memories = [replay_memory1, replay_memory2, replay_memory3] self.assertSequenceEqual(list(replay_memories2), expected_replay_memories)
def test_prefetch(self): with TempDir() as temp_directory: replay_memories1_file_name = os.path.join(temp_directory, 'replay_memories.dat') replay_memories1 = ReplayMemories(replay_memories1_file_name, max_current_memories_in_ram=100) replay_memory1 = self._create_replay_memory(action_index=11) replay_memory2 = self._create_replay_memory(action_index=22) replay_memory3 = self._create_replay_memory(action_index=33) replay_memories1.append(replay_memory1) replay_memories1.append(replay_memory2) replay_memories1.append(replay_memory3) replay_memories1.save() replay_memories2 = ReplayMemories(replay_memories1_file_name, max_current_memories_in_ram=100, prefetch_into_ram=True) # This block should be idented outside the TempDir block, since we want to test that memories are not fetched from disk expected_replay_memories = [replay_memory1, replay_memory2, replay_memory3] print(list(replay_memories2)) print(expected_replay_memories) self.assertSequenceEqual(list(replay_memories2), expected_replay_memories)
def test_sample_size_smaller_than_recent_memories(self): with TempDir() as temp_directory: replay_memories_file_name = os.path.join(temp_directory, 'replay_memories.dat') replay_memories1 = ReplayMemories(replay_memories_file_name, max_current_memories_in_ram=100) replay_memory1 = self._create_replay_memory(action_index=11) replay_memory2 = self._create_replay_memory(action_index=22) replay_memory3 = self._create_replay_memory(action_index=32) replay_memory4 = self._create_replay_memory(action_index=42) replay_memories1.append(replay_memory1) replay_memories1.append(replay_memory2) replay_memories1.append(replay_memory3) replay_memories1.save() replay_memories2 = ReplayMemories(replay_memories_file_name, max_current_memories_in_ram=100) replay_memories2.append(replay_memory4) sampled_replay_memories = replay_memories2.sample(2, recent_memories_span=3, seed=4) expected_replay_memories = [replay_memory2, replay_memory4] self.assertItemsEqual(sampled_replay_memories, expected_replay_memories)
def test_len(self): replay_memories = ReplayMemories("non_existing_previous_memories.dat", max_current_memories_in_ram=100) replay_memories.append(self._create_replay_memory()) replay_memories.append(self._create_replay_memory()) self.assertEquals(len(replay_memories), 2)