Description Welcome to the third assignment for this course. The goal of this assignment…
- User
Description Welcome to the third assignment for this course. The goal of this assignment is to acquire experience in defining and solving a reinforcement learning (RL) environment, following Gym standards. The assignment consists of three parts. The first part focuses on defining an environment that is based on a Markov decision process (MDP). In the second part, we will apply a tabular method SARSA to solve an environment that was previously defined. In the third part, we apply the Q-learning method to solve a grid-world environment. Part I: Define an RL Environment [30 points] In this part, we will define a grid-world reinforcement learning environment as an MDP. While building an RL environment, you need to define possible states, actions, rewards and other parameters. STEPS:
- Choose a scenario for your grid world. You are welcome to use the visualizationdemo as a reference to visualize it. An example of idea for RL environment: • Theme: Lawnmower Grid World with batteries as positive rewards and rocks as negative rewards. • States: {S1 = (0,0), S2 = (0,1), S3 = (0,2), S4 = (0,3), S5 = (1,0), S6 = (1,1), S7 = (1,2), S8 = (1,3), S9 = (2,0), S10 = (2,1), S11 = (2,2), S12 = (2,3), S13 = (3,0), S14 = (3,1), S15 = (3,2), S16 = (3,3)} • Actions: {Up, Down, Right, Left} • Rewards: {-5, -6, +5, +6} • Objective: Reach the goal state with maximum reward
- Define an RL environment following the scenario that you chose. Environment requirements: • Min number of states: 12 • Min number of actions: 4 • Min number of rewards: 4 Environment definition should follow the OpenAI Gym structure, which includes thebasic methods. You can use the “Defining RL env” demo as a base code. def init:
Initializes the class
Define action and observation space
def step:
Executes one timestep within the environment
Input to the function is an action
def reset:
Resets the state of the environment to an initial state
def render:
Visualizes the environment
Any form like vector representation or visualizing
usingmatplotlib is sufficient 3. Run a random agent for at least 10 timesteps to show that the environment logic is defined correctly. Print the current state, chosen action, reward and return your grid world visualization for each step.
- Assistant
- User
In your report for Part I:
- Describe the environment that you defined. Provide a set of actions, states, rewards, main objective, etc.
- Provide visualization of your environment.
- Safety in AI: Write a brief review (∼ 5 sentences) explaining how you ensure the safety of your environment. E.g. how do you ensure that the agent chooses only actions that are allowed, that agent is navigating within defined state-space, etc
- Assistant
- Environment Description:
The Lawnmower Grid World environment is a 4x4 grid. The agent starts from the top-left corner (0,0) and the main objective is to reach the bottom-right corner (3,3) with the maximum reward.
-
States: {(0,0), (0,1), (0,2), (0,3), (1,0), (1,1), (1,2), (1,3), (2,0), (2,1), (2,2), (2,3), (3,0), (3,1), (3,2), (3,3)}
-
Actions: {Up (0), Down (1), Left (2), Right (3)}
-
Rewards: {-5 (Rock), -6 (Rock), +5 (Battery), +6 (Battery)}
- Visualization:
When running the code provided in the previous answer, the grid visualization is produced using
matplotliblibrary, which represents agent's current position, goal position, and rewards placed on the grid.- Safety in AI:
To ensure the safety of the environment, the agent is restricted to the actions defined in the action space, and the agent's movements are bounded by the grid dimensions. The environment logic defined in the
stepmethod guarantees that the agent moves within the valid state space by clamping the agent's position when an action is applied. Additionally, the agent can only choose from the allowed actions as defined by the action space. This prevents any possibility of the agent to move out of the grid or take any undefined actions, thus ensuring a safe environment for the agent to explore. - User
Initial State: (0, 0)
AttributeError Traceback (most recent call last) in <cell line: 72>() 70 state = env.reset() 71 print('Initial State:', state) ---> 72 env.render() 73 74 # Run Random Agent
1 frames in render(self) 50 51 def render(self): ---> 52 grid = np.zeros_like(self.state_matrix) 53 for x in range(4): 54 for y in range(4):
AttributeError: 'LawnmowerGridWorld' object has no attribute 'state_matrix'
- Assistant
- User
Part II: Solve your environment using – SARSA [40 points] In this part, we implement SARSA (State-Action-Reward-State-Action) algorithm and apply it to solve the env defined in Part 1. SARSA is an on-policy reinforcement learning algorithm. The agent updates its Q-values based on the current state, action, reward, and next state, action pair. It uses an exploration-exploitation strategy to balance between exploring new actions and exploiting the knowledge gained so far. STEPS:
- Apply SARSA algorithm to solve the environment that was defined in Part I.
- Try hyperparameter tuning on at least two parameters to get better results for SARSA. You can explore hyperparameter tuning libraries, e.g. Optuna or make it manually. Parameters to tune: a. Discount factor (γ) b. Epsilon decay rate c. Epsilon min/max values d. Number of episodes e. Max timesteps Try at least 3 different values for each of the parameters that you choose.
- Provide the reward graphs and your explanation for each result. In total, you should have at least 3 graphs and your explanations. Make your suggestion on the most efficient hyperparameters values for your problem setup.
- Assistant