Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import numpy as np import random # Define the grid world GRID _ SIZE = ( 4 , 5 ) START _ STATE = (
import numpy as np
import random
# Define the grid world
GRIDSIZE
STARTSTATE
GOALSTATE
OBSTACLES
# Qlearning parameters
LEARNINGRATE
DISCOUNTFACTOR
EPISODES
# Initialize Qtable
qtable npzerosGRIDSIZE GRIDSIZE # actions: up down, left, right
# Define actions
ACTIONS UP "DOWN", "LEFT", "RIGHT"
# Function to choose an action using epsilongreedy strategy
def chooseactionstate epsilon:
if random.uniform epsilon:
return random.choicerange # choose a random action
else:
return npargmaxqtablestate state
# Function to perform Qlearning
def qlearning:
for episode in rangeEPISODES:
state STARTSTATE
while state GOALSTATE:
action chooseactionstate epsilon
nextstate takeactionstate action
reward calculaterewardnextstate
updateqtablestate action, reward, nextstate
state nextstate
# Function to take an action and return the next state
def takeactionstate action:
if action : # UP
return max state state
elif action : # DOWN
return minGRIDSIZE state state
elif action : # LEFT
return state max state
elif action : # RIGHT
return state minGRIDSIZE state
# Function to calculate the reward for a given state
def calculaterewardstate:
if state GOALSTATE:
return
elif state in OBSTACLES:
return
else:
return
# Function to update the Qtable based on the Qlearning update rule
def updateqtablestate action, reward, nextstate:
bestfuturevalue npmaxqtablenextstate nextstate
currentvalue qtablestate state action
newvalue LEARNINGRATE currentvalue LEARNINGRATE reward DISCOUNTFACTOR bestfuturevalue
qtablestate state action newvalue
# Run Qlearning algorithm
qlearning
# Print the learned Qtable
printLearned Qtable:"
printqtable
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started