import gym import numpy as np import matplotlib.pyplot as plt def runsarsa(episodes,…
- User
import gym import numpy as np import matplotlib.pyplot as plt
def run_sarsa(episodes, alpha, gamma, epsilon, epsilon_decay, epsilon_min, max_timesteps): # Initialize environment, Q-table, and hyperparameters env = GridWorld() n_states = env.observation_space.n n_actions = env.action_space.n Q = np.zeros((n_states, n_actions))
# Lists for plotting epsilon decay and total rewards total_rewards = [] epsilon_values = [] # SARSA Algorithm for episode in range(episodes): state_tuple = env.reset() state = state_tuple[0] * 4 + state_tuple[1] # Convert the state tuple to single integer action = np.argmax(Q[state, :] + np.random.randn(1, n_actions) * (1.0 / (episode + 1))) total_reward = 0 for t in range(max_timesteps): next_state_tuple, reward, done = env.step(action) next_state = next_state_tuple[0] * 4 + next_state_tuple[1] # Convert the next_state tuple to single integer next_action = np.argmax(Q[next_state, :] + np.random.randn(1, n_actions) * (1.0 / (episode + 1))) # Update Q-value target = reward + gamma * Q[next_state, next_action] Q[state, action] += alpha * (target - Q[state, action]) state, action = next_state, next_action total_reward += reward if done: break epsilon = max(epsilon_min, epsilon * epsilon_decay) epsilon_values.append(epsilon) total_rewards.append(total_reward) return Q, total_rewards, epsilon_values-------------------------This is my sarsa algorithm ---------------------Now implement n - step sarsa-------------n-step SARSA [5 points]Modify your SARSA and implement a 2-step bootstrapping SARSA. Compare the results in graphs with SARSA
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.