import numpy as np #define the shape of the environment (i.e., its states)…
- User
import numpy as np #define the shape of the environment (i.e., its states) environment_rows = 11 environment_columns = 11
#Create a 3D numpy array to hold the current Q-values for each state and action pair: Q(s, a) #The array contains 11 rows and 11 columns (to match the shape of the environment), as well as a third “action” dimension. #The “action” dimension consists of 4 layers that will allow us to keep track of the Q-values for each possible action in #each state (see next cell for a description of possible actions). #The value of each (state, action) pair is initialized to 0. q_values = np.zeros((environment_rows, environment_columns, 4)) #define actions #numeric action codes: 0 = up, 1 = right, 2 = down, 3 = left actions = [‘up’, ‘right’, ‘down’, ‘left’] #Create a 2D numpy array to hold the rewards for each state. #The array contains 11 rows and 11 columns (to match the shape of the environment), and each value is initialized to -100. rewards = np.full((environment_rows, environment_columns), -100.) rewards[0, 5] = 100. #set the reward for the packaging area (i.e., the goal) to 100
#define aisle locations (i.e., white squares) for rows 1 through 9 aisles = {} #store locations in a dictionary aisles[1] = [i for i in range(1, 10)] aisles[2] = [1, 7, 9] aisles[3] = [i for i in range(1, 8)] aisles[3].append(9) aisles[4] = [3, 7] aisles[5] = [i for i in range(11)] aisles[6] = [5] aisles[7] = [i for i in range(1, 10)] aisles[8] = [3, 7] aisles[9] = [i for i in range(11)]
#set the rewards for all aisle locations (i.e., white squares) for row_index in range(1, 10): for column_index in aisles[row_index]: rewards[row_index, column_index] = -1.
#print rewards matrix for row in rewards: print(row) #define a function that determines if the specified location is a terminal state def is_terminal_state(current_row_index, current_column_index): #if the reward for this location is -1, then it is not a terminal state (i.e., it is a ‘white square’) if rewards[current_row_index, current_column_index] == -1.: return False else: return True
#define a function that will choose a random, non-terminal starting location def get_starting_location(): #get a random row and column index current_row_index = np.random.randint(environment_rows) current_column_index = np.random.randint(environment_columns) #continue choosing random row and column indexes until a non-terminal state is identified #(i.e., until the chosen state is a ‘white square’). while is_terminal_state(current_row_index, current_column_index): current_row_index = np.random.randint(environment_rows) current_column_index = np.random.randint(environment_columns) return current_row_index, current_column_index
#define an epsilon greedy algorithm that will choose which action to take next (i.e., where to move next) def get_next_action(current_row_index, current_column_index, epsilon): #if a randomly chosen value between 0 and 1 is less than epsilon, #then choose the most promising value from the Q-table for this state. if np.random.random() < epsilon: return np.argmax(q_values[current_row_index, current_column_index]) else: #choose a random action return np.random.randint(4)
- Assistant
这段代码实现了一个机器学习算法(Q-Learning),用于训练 AI 代理在仓库环境中找到从起始位置到目标位置的最短路径。首先详细解释代码中每个部分的作用。
- 定义环境的形状:
environment_rows = 11 environment_columns = 11这两行代码定义了仓库环境的大小,具体为 11×11 的网格。
- 初始化 Q 值表:
q_values = np.zeros((environment_rows, environment_columns, 4))创建一个 3D numpy 数组,用于保存每个状态和动作对的 Q 值。数组有 11 行、11 列以及第三个 "动作" 维度包含 4 层,分别对应每个可能的动作。
- 定义动作:
actions = ['up', 'right', 'down', 'left']定义可能的动作:上、右、下、左。
- 初始化奖励表:
rewards = np.full((environment_rows, environment_columns), -100.)创建一个 2D numpy 数组,用于保存每个状态的奖励。数组有 11 行、11 列,初始值为 -100。然后设置目标位置(打包区域)的奖励为 100。
-
根据定义的走廊(无障碍区域),将奖励更新为 -1,使得 AI 代理能够寻找可通行的路径。
-
定义函数
is_terminal_state,该函数通过检查给定状态的奖励是否为 -1 以判断该状态是否为终止状态。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.