Test AStarSearch using a grid with randomly placed obstacles.
------------------------------------------------------------------------
See also AStarSearch, TransformGridCoordinates, NewFig, Plot2D, TextS
------------------------------------------------------------------------
Contents
n = 8;
d.pathCostEstimateFunction = 'GridPathCost';
d.traverseCostFunction = 'GridTraverseCost';
d.successorNodesFunction = 'GridSuccessorNodes';
grid = zeros(n,n);
d.myData.n = n;
nBlocked = floor(20*rand);
rowStart = 4;
colStart = 1;
rowEnd = 4;
colEnd = 8;
blockedNode = [];
for k = 1:nBlocked
while( 1 )
row = ceil(n*rand);
col = ceil(n*rand);
if( ((row == rowStart) & (col == colStart)) | ((row == rowEnd) & (col == colEnd)) )
else
break
end
end
blockedNode(:,k) = [col;row];
grid(row,col) = 1;
end
Put in a one dimensional array
for k = 1:n
d.myData.grid(1,((k-1)*n+1):(k*n)) = grid(k,:);
end
d.n = length(d.myData.grid);
startNode = TransformGridCoordinates( rowStart, colStart, n );
endNode = TransformGridCoordinates( rowEnd, colEnd, n );
tic
path = AStarSearch( startNode, endNode, d );
t = sprintf('A* Search: %2.3 sec',toc);
if( ~isempty(path) )
[y, x] = TransformGridCoordinates( path, n );
Plot2D( x, y, 'x', 'y', t );
else
NewFig( t );
end
n = n + 1;
set( gca, 'xlim', [0 n], 'ylim', [0 n], 'xtick', 0:n, 'ytick', 0:n )
hold on
[row, col] = TransformGridCoordinates( startNode, n-1 );
TextS(col,row-.15,' start')
plot(col,row,'*g')
[row, col] = TransformGridCoordinates( endNode, n-1 );
TextS(col,row-.15,' end')
plot(col,row,'*r')
for k = 1:size(blockedNode,2)
plot(blockedNode(1,k),blockedNode(2,k),'*k');
end