In this problem, we will implement an Experience Replay buffer for reinforcement learning. The Experience Replay buffer stores a sequence of experiences and allows the agent to sample from the buffer to train the model.
**Example:** Consider a simple grid world where an agent can move up, down, left, or right. The agent receives a reward of 1 for reaching a goal state and -1 for hitting a wall.
**Constraints:** Implement the Experience Replay buffer with a capacity of 1000 experiences and a sampling method that returns a random experience from the buffer.
import numpy as np
class ExperienceReplay:
def __init__(self, capacity):
# Your code here
pass
def add_experience(self, experience):
# Your code here
pass
def sample_experience(self):
# Your code here
pass
Test Cases
{}{'state': [1, 2, 3], 'action': 0, 'reward': 1, 'next_state': [4, 5, 6], 'done': False}{}{'state': [7, 8, 9], 'action': 1, 'reward': -1, 'next_state': [10, 11, 12], 'done': True}