valid_row = n_ss(:,1) >= 1 & n_ss(:,1) <= mapSize(1); valid_col = n_ss(:,2) >= 1 & n_ss(:,2) <= mapSize(2); n_ss = n_ss(valid_row & valid_col, :); % subscripts -> linear indices neighbors = n_ss(:,1) + (n_ss(:,2) - 1) .* mapSize(1); % only keep neighbors in the map and not in the closed set ixInMap = map(neighbors) & ~closedSet(neighbors); neighbors = neighbors(ixInMap); % distance to each kept neighbor dists = n_ss(ixInMap, 3);
时间: 2024-04-02 21:37:08 浏览: 23
这段代码是一个 A* 寻路算法的核心部分。它首先利用 `n_ss` 矩阵中的行索引和列索引,计算出每个邻居格子在一维矩阵中的索引,并将超出地图范围的邻居排除掉。然后,它判断哪些邻居格子在地图范围内,并且还没有被访问过,将这些邻居的索引保留下来。接着,它计算每个保留下来的邻居格子与当前格子之间的距离,并将距离记录在 `dists` 向量中。最终,`neighbors` 向量和 `dists` 向量中保存的就是当前格子的可达邻居格子的索引和距离。这些信息将在 A* 算法的下一步中被用来计算每个邻居格子的预估代价和真实代价,以选择最优路径。
相关问题
valid_num=100 x_valid=x_date[:valid_num] y_valid=x_date[:valid_num]
This code assigns the first 100 elements of the x_date array to the x_valid array and also assigns the first 100 elements of the x_date array to the y_valid array. This is commonly done when splitting data into training and validation sets for machine learning models.
def PrepareDataset(speed_matrix, BATCH_SIZE = 40, seq_len = 10, pred_len = 1, train_propotion = 0.7, valid_propotion = 0.2):
"""
Function to prepare dataset for training and validation
Args:
speed_matrix: numpy array of shape (num_days, num_timesteps, num_nodes)
BATCH_SIZE: batch size for training (default = 40)
seq_len: sequence length (number of timesteps) for input (default = 10)
pred_len: number of timesteps to predict (default = 1)
train_propotion: proportion of dataset to use for training (default = 0.7)
valid_propotion: proportion of dataset to use for validation (default = 0.2)
Returns:
train_data: PyTorch DataLoader object for training data
valid_data: PyTorch DataLoader object for validation data
"""
# Calculate number of days
num_days = speed_matrix.shape[0]
# Calculate number of nodes
num_nodes = speed_matrix.shape[2]
# Calculate total number of timesteps
total_timesteps = num_days * speed_matrix.shape[1]
# Create input and target sequences
input_seq = []
target_seq = []
# Loop through each day
for day in range(num_days):
# Loop through each timestep
for timestep in range(speed_matrix.shape[1] - seq_len - pred_len):
# Extract input sequence
input_seq.append(speed_matrix[day, timestep:timestep+seq_len, :])
# Extract target sequence
target_seq.append(speed_matrix[day, timestep+seq_len:timestep+seq_len+pred_len, :])
# Convert input and target sequences to numpy arrays
input_seq = np.array(input_seq)
target_seq = np.array(target_seq)
# Split dataset into training, validation, and testing sets
train_size = int(total_timesteps * train_propotion)
valid_size = int(total_timesteps * valid_propotion)
test_size = total_timesteps - train_size - valid_size
train_input = input_seq[:train_size]
train_target = target_seq[:train_size]
valid_input = input_seq[train_size:train_size+valid_size]
valid_target = target_seq[train_size:train_size+valid_size]
test_input = input_seq[train_size+valid_size:]
test_target = target_seq[train_size+valid_size:]
# Convert training and validation data to PyTorch DataLoader objects
train_data = DataLoader(TensorDataset(torch.Tensor(train_input), torch.Tensor(train_target)), batch_size=BATCH_SIZE, shuffle=True)
valid_data = DataLoader(TensorDataset(torch.Tensor(valid_input), torch.Tensor(valid_target)), batch_size=BATCH_SIZE, shuffle=True)
return train_data, valid_data
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)