I want to execute Astar algorithm for path finding in the below code.Can someone please correct my code with .There seems to be a looping error.
3 ビュー (過去 30 日間)
古いコメントを表示
function P = Astar3D(Env,start,goal)
% Initialize the open and closed lists
openList = containers.Map('KeyType','double','ValueType','str')
closedList = containers.Map('KeyType','double','ValueType','str')
% Add the starting node to the open list with a cost of 0
% Convert start node to scalar key
startKey = num2str(start)
% Add the starting node to the open list with a cost of 0
openlist('startKey')= 0;
% Loop until the open list is empty
while ~isempty(openList)
% Find the node with the lowest f cost
[fValues, fKeys] = sort(values(openList));
currentKey = keys(openList);
current = currentKey{fKeys(1)};
minF = fValues(1);
% Check if the current node is the goal node
if current == goal
P = closedList;
return
end
% Remove the current node from the open list and add it to the closed list
openList.remove(current);
closedList(current) = minF;
% Find the neighbors of the current node
neighbors = find_neighbors(current,Env);
% Loop through each neighbor
for i = 1:length(neighbors)
neighbor = neighbors(i);
% Calculate the g, h, and f costs for the neighbor
gCost = closedList(current) + distance(current,neighbor);
hCost = distance(neighbor,goal);
fCost = gCost + hCost;
% If the neighbor is already on the closed list and the new f cost is higher, skip it
if closedList.isKey(neighbor) && closedList(neighbor) <= fCost
continue
end
% If the neighbor is already on the open list and the new f cost is higher, skip it
if openList.isKey(neighbor) && openList(neighbor) <= fCost
continue
end
% Add the neighbor to the open list
openList(neighbor) = fCost;
end
end
% If the open list is empty and the goal node has not been found, return an empty path
P = [];
end
% Helper function to find the neighbors of a node in the 3D environment
function neighbors = find_neighbors(node,Env)
% Initialize an empty list for the neighbors
neighbors = [];
% Loop through all possible adjacent nodes
for dx = -1:1
for dy = -1:1
for dz = -1:1
% Skip the current node
if dx == 0 && dy == 0 && dz == 0
continue
end
% Calculate the coordinates of the adjacent node
x = node(1) + dx;
y = node(2) + dy;
z = node(3) + dz;
% Check if the adjacent node is within the bounds of the environment
if x < 1 || x > size(Env,1) || y < 1 || y > size(Env,2) || z < 1 || z > size(Env,3)
continue
end
% Check if the adjacent node is blocked
if Env(x,y,z) == 1
continue
end
% Add the adjacent node to the list of neighbors
neighbors(end+1,:) = [x y z];
end
end
end
end
% Helper function to calculate the Euclidean distance between two nodes
function d = distance(node1,node2)
d = sqrt(sum((node1-node2).^2));
end
Error:
startKey = num2str(start)
% Add the starting node to the open list with a cost of 0
openlist('startKey')= 0;
% Loop until the open list is empty
while ~isempty(openList)
% Find the node with the lowest f cost
[fValues, fKeys] = sort(values(openList));
currentKey = keys(openList);
current = currentKey{fKeys(1)};
minF = fValues(1);
Error: File: untitled.m Line: 46 Column: 7
Invalid text character. Check for unsupported symbol, invisible character, or pasting of non-ASCII characters.
2 件のコメント
Walter Roberson
2023 年 3 月 26 日
Please attach your untitled.m file so that we can examine the exact content.
回答 (1 件)
Walter Roberson
2023 年 3 月 27 日
The file you attached contains
openlist(1,1) startKey
Notice you forgot the "=" in the assignment.
You have a number of problems with the way you use the containers.Map.
- there is no ValueType of 'str'
- ValueType of 'string' is not supported
- You do not even try to assign a string() object -- you assign character vector
- You also assign numeric 0 -- so you would need to use ValueType 'any'
- You try to use two subscripts on the container, but only a single subscript is supported
- You later use only a single subscript for the container
- You try to assign to openlist and closedlist but the containers are named openList and closedList
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Import and Export についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!