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)
#define a function that will get the next location based on the chosen action def get_next_location(current_row_index, current_column_index, action_index): new_row_index = current_row_index new_column_index = current_column_index if actions[action_index] == 'up' and current_row_index > 0: new_row_index -= 1 elif actions[action_index] == 'right' and current_column_index < environment_columns - 1: new_column_index += 1 elif actions[action_index] == 'down' and current_row_index < environment_rows - 1: new_row_index += 1 elif actions[action_index] == 'left' and current_column_index > 0: new_column_index -= 1 return new_row_index, new_column_index
#Define a function that will get the shortest path between any location within the warehouse that #the robot is allowed to travel and the item packaging location. def get_shortest_path(start_row_index, start_column_index): #return immediately if this is an invalid starting location if is_terminal_state(start_row_index, start_column_index): return [] else: #if this is a 'legal' starting location current_row_index, current_column_index = start_row_index, start_column_index shortest_path = [] shortest_path.append([current_row_index, current_column_index]) #continue moving along the path until we reach the goal (i.e., the item packaging location) while not is_terminal_state(current_row_index, current_column_index): #get the best action to take action_index = get_next_action(current_row_index, current_column_index, 1.) #move to the next location on the path, and add the new location to the list current_row_index, current_column_index = get_next_location(current_row_index, current_column_index, action_index) shortest_path.append([current_row_index, current_column_index]) return shortest_path #define training parameters epsilon = 0.9 #the percentage of time when we should take the best action (instead of a random action) discount_factor = 0.9 #discount factor for future rewards learning_rate = 0.9 #the rate at which the AI agent should learn
#run through 1000 training episodes for episode in range(1000): #get the starting location for this episode row_index, column_index = get_starting_location()
#continue taking actions (i.e., moving) until we reach a terminal state #(i.e., until we reach the item packaging area or crash into an item storage location) while not is_terminal_state(row_index, column_index): #choose which action to take (i.e., where to move next) action_index = get_next_action(row_index, column_index, epsilon)
#perform the chosen action, and transition to the next state (i.e., move to the next location) old_row_index, old_column_index = row_index, column_index #store the old row and column indexes row_index, column_index = get_next_location(row_index, column_index, action_index) #receive the reward for moving to the new state, and calculate the temporal difference reward = rewards[row_index, column_index] old_q_value = q_values[old_row_index, old_column_index, action_index] temporal_difference = reward + (discount_factor * np.max(q_values[row_index, column_index])) - old_q_value #update the Q-value for the previous state and action pair new_q_value = old_q_value + (learning_rate * temporal_difference) q_values[old_row_index, old_column_index, action_index] = new_q_valueprint('Training complete!') #display a few shortest paths print(get_shortest_path(3, 9)) #starting at row 3, column 9 print(get_shortest_path(5, 0)) #starting at row 5, column 0 print(get_shortest_path(9, 5)) #starting at row 9, column 5 #display an example of reversed shortest path path = get_shortest_path(5, 2) #go to row 5, column 2 path.reverse() print(path) 解释每行代码及注释的含义
- Assistant
这段代码实现了一个机器学习算法,用于训练一个 AI agent,在一个模拟的 warehouse environment 内学习最优路径以收集订单。(AI agent指机器学习模型,模拟的 warehouse environment是指一种模拟环境,其中AI agent可以采取一些动作,例如向上走或向下走,并以某种方式被奖励或惩罚,例如奖励100分成功地收集了订单,而在仓库的墙上撞了之后就会受到惩罚)
以下是逐行代码及其注释:
import numpy as np导入 NumPy 库,用于支持多维数组和矩阵运算。
environment_rows = 11 environment_columns = 11设置环境的行和列,即仓库环境的大小。
q_values = np.zeros((environment_rows, environment_columns, 4))创建一个三维的 numpy 数组,保存每一个状态 state 和 action pair 的 Q 值,即 Q(s, a)。这个数组有 11 行、11 列,并且包含一个“动作”维度。动作维度由四个层组成,使我们能够跟踪每个状态下每个可能的动作的 Q 值。每个 (state, action) pair 的值被初始化为 0。
actions = ['up', 'right', 'down', 'left']定义了模型的动作动作,包括向上走、向右走、向下走和向左走。
rewards = np.full((environment_rows, environment_columns), -100.) rewards[0, 5] = 100.创建一个二维的 numpy 数组,用于保存每个 state 的奖励值。它的大小为 11 行、11 列,并且每个值都被初始化为-100。在这个数组中,我们将可以容纳的所有状态的奖励值都设置为 -1,以允许 AI agent 在这个区域内移动并尝试找到一个更有利的位置。最终位置为 0 行、5 列的站点,即物品打包区,被设置为 100 分的奖励值。
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)]定义了通道位置 (i.e., 待拣货货架和原料库的位置等,表示未被占用),其中包括以下区域:
- 第一行有八个位置和最后一个位置
- 第二行的第 1、7 和 9 个位置
- 第三行的 1 到 7 个位置和最后一个位置
- 第四行的 3 和 7 个位置
- 第五行的所有位置
- 第六行的第 5 个位置
- 第七行的第一行和最后一个位置
- 第八行的第 3 和 7 个位置
- 第九行的所有位置
for row_index in range(1, 10): for column_index in aisles[row_index]: rewards[row_index, column_index] = -1.设置所有通道位置的奖励为 -1,表示不会受到任何惩罚。
def is_terminal_state(current_row_index, current_column_index): if rewards[current_row_index, current_column_index] == -1.: return False else: return True定义一个函数,用于确定指定的位置是否为终止状态。
def get_starting_location(): current_row_index = np.random.randint(environment_rows) current_column_index = np.random.randint(environment_columns) 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定义一个函数,用于在环境中选择一个合法的起始位置,它返回起始控制点的位置。
def get_next_action(current_row_index, current_column_index, epsilon): if np.random.random() < epsilon: return np.argmax(q_values[current_row_index, current_column_index]) else: return np.random.randint(4)定义一个函数,提供深度学习模型采用的 至贪心选择算法(ε-greedy selection algorithm),该算法将随机选择并动作十分之一的概率。在“exploration”(探索)阶段,它允许 AI agent 在状态空间中探索,并为所有可能采取的动作随机分配奖励。在训练过程的后期(即在“exploitation”阶段),AI agent 将根据具有最大 Q 值的奖励值制定动作。
def get_next_location(current_row_index, current_column_index, action_index): new_row_index = current_row_index new_column_index = current_column_index if actions[action_index] == 'up' and current_row_index > 0: new_row_index -= 1 elif actions[action_index] == 'right' and current_column_index < environment_columns - 1: new_column_index += 1 elif actions[action_index] == 'down' and current_row_index < environment_rows - 1: new_row_index += 1 elif actions[action_index] == 'left' and current_column_index > 0: new_column_index -= 1 return new_row_index, new_column_index定义一个函数,该函数将 AI agent 得到的 action_index 放入当前位置,返回下一个位置的行和列坐标。
def get_shortest_path(start_row_index, start_column_index): if is_terminal_state(start_row_index, start_column_index): return [] else: current_row_index, current_column_index = start_row_index, start_column_index shortest_path = [] shortest_path.append([current_row_index, current_column_index]) while not is_terminal_state(current_row_index, current_column_index): action_index = get_next_action(current_row_index, current_column_index, 1.) current_row_index, current_column_index = get_next_location(current_row_index, current_column_index, action_index) shortest_path.append([current_row_index, current_column_index]) return shortest_path定义了一个函数 get_shortest_path,用于查找仓库任何一点到订单物品打包位置的最短路径。
最后,在主要循环中,执行以下内部循环(1000 次训练迭代):
- get_starting_location 返回起始控制点的位置
- 持续采取 action 直到到达终止状态
- get_next_action 根据 至贪心选择算法选择下一步采取的 action
- get_next_location 从当前位置个选择新的下一个状态
- get_shortest_path 查找当前状态到物品打包区的最短路径
- 计算神经网络输出。这通常涉及计算模型的误差或准确率值。
- 为神经网络更新权重
- 重复,直到训练完成。
最后,我们显示一些样例最短路径以及倒序的最短路径,来验证模型的可行性。
- User
#define actions #numeric action codes: 0 = up, 1 = right, 2 = down, 3 = left含义
- Assistant
这段代码是定义了动作,包括向上移动、向右移动、向下移动和向左移动。同时也定义了每个动作的数字代码,其中 0 表示向上移动,1 表示向右移动,2 表示向下移动,3 表示向左移动。这些代码将在 AI agent 选择动作时使用。
- User
#define aisle locations (i.e., white squares) for rows 1 through 9 #store locations in a dictionary含义
- Assistant
这段代码是定义了通道位置,即没有物品的货架和原料库的位置等,用于保存在一个字典中。字典的键为行号,值为一个列表,其中包含通道位置的列索引。在这个案例中,通道位置被表示成一个白色正方形。这些位置是 AI agent 可以移动的有效位置。将这些位置与奖励关联起来可让 AI agent 在这些区域中移动,并尝试找到一个更优的位置。
- User
i for i in range(1, 10) 是什么意思
- Assistant
这是Python中的列表推导式(List Comprehension),它生成一个包含整数1到9(不包括10)的列表。i代表
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.