while any(openSet(:) > 0) % Find the minimum fScore within the open set [~, current] = min(fScore(:)); % If we've reached the goal if current == goal % Get the full path and return it final = get_path(cameFrom, current); return end % Linear index -> row, col subscripts rc = rem(current - 1, mapSize(1)) + 1; cc = (current - rc) / mapSize(1) + 1; % Remove CURRENT from openSet openSet(rc, cc) = false; % Place CURRENT in closedSet closedSet(rc, cc) = true; fScore(rc, cc) = inf; gScoreCurrent = gScore(rc, cc) + costs(rc, cc); % Get all neighbors of CURRENT. Neighbors are adjacent indices on % the map, including diagonals. % Col 1 = Row, Col 2 = Col, Col 3 = Distance to the neighbor n_ss = [ ... rc + 1, cc + 1, S2 ; ... rc + 1, cc + 0, 1 ; ... rc + 1, cc - 1, S2 ; ... rc + 0, cc - 1, 1 ; ... rc - 1, cc - 1, S2 ; ... rc - 1, cc - 0, 1 ; ... rc - 1, cc + 1, S2 ; ... rc - 0, cc + 1, 1 ; ... ]; % keep valid indices only 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); % Add each neighbor to the open set openSet(neighbors) = true; % TENTATIVE_GSCORE is the score from START to NEIGHBOR. tentative_gscores = gScoreCurrent + costs(neighbors) .* dists; % IXBETTER indicates where a better path was found ixBetter = tentative_gscores < gScore(neighbors); bestNeighbors = neighbors(ixBetter); % For the better paths, update scores cameFrom(bestNeighbors) = current; gScore(bestNeighbors) = tentative_gscores(ixBetter); fScore(bestNeighbors) = gScore(bestNeighbors) + compute_cost(mapSize, bestNeighbors, gr, gc); end % while end
时间: 2023-11-28 11:06:41 浏览: 40
这段代码是 A* 算法的核心实现部分,用于寻找两点之间的最短路径。具体来说,它依次对起点周围的格子进行探索,计算每个格子到起点的代价(gScore)以及到终点的估计代价(fScore),并将其加入 openSet 中。然后从 openSet 中选取 fScore 最小的格子进行探索,直到找到终点或 openSet 中没有格子可供探索为止。
在探索每个格子时,首先将其从 openSet 中移除并加入 closedSet 中,然后计算该格子与周围格子的代价,并将未被探索过的格子加入 openSet 中。如果发现了新的更优路径,就更新该格子到起点的代价和 fScore,并将其加入到 cameFrom 列表中,表示它是从哪个格子转移而来的。
最终,如果找到了终点,就从 cameFrom 列表中回溯路径。
相关问题
while any(openSet(:) > 0)
这行代码是在进行一个循环操作,判断 openSet 中是否还有元素的值大于0。其中,openSet 是一个矩阵或数组,它用于存储待扩展的节点。在循环的每一轮中,代码会使用 any 函数来判断 openSet 中是否还有元素的值大于0。如果有,则继续执行下一轮循环;如果没有,则跳出循环,表示搜索过程已经结束。
在 A* 算法中,openSet 通常用于存储待扩展的节点,每个节点都有一个 f 值,表示从起点到该节点的实际代价加上该节点到目标位置的启发式代价。在搜索过程中,算法会遍历所有待扩展的节点,并选择其中 f 值最小的节点进行扩展。如果 openSet 中没有节点了,就表示搜索已经完成,算法找到了起点到目标位置的最短路径。
CREATE PROCEDURE tp() BEGIN SET @sum:=1024; SET @num:=1024; SET @i:=0; WHILE @num>0 DO SET @num:=@num/2-1; SET @i:=@i+1; END WHILE; SELECT @i; END为什么不输出i
在您的存储过程中,虽然您声明了变量和循环,但您没有将循环的结果赋值给任何变量或表,因此存储过程无法输出任何结果。您可以使用SELECT语句将@i的值输出到控制台或将其存储在表中。例如,您可以将SELECT语句添加到存储过程的最后:
```
CREATE PROCEDURE tp()
BEGIN
SET @sum:=1024;
SET @num:=1024;
SET @i:=0;
WHILE @num>0 DO
SET @num:=@num/2-1;
SET @i:=@i+1;
END WHILE;
SELECT @i;
END
```
然后,您可以使用以下语句来执行存储过程并查看结果:
```
CALL tp();
```
这将在控制台上输出值1,这是@i的值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)