Wikipedia:Reference desk/Archives/Computing/2016 January 12#BFS MATLAB

{{#ifeq:{{PAGENAME}}|Special:Undelete| |{{#if:|

}} {{#ifeq:{{NAMESPACE}}|Wikipedia|{{#switch:{{NAMESPACE}}|= |
}}|{{error:not substituted|Archive header}}
}}}} {{#if:|
}}
width = "100%"
colspan="3" align="center" | Computing desk
width="20%" align="left" | < January 11

! width="25%" align="center"|<< Dec | January | Feb >>

! width="20%" align="right" |{{#ifexist:Wikipedia:Reference desk/Archives/Computing/2016 January 13|January 13|Current desk}} >

align=center width=95% style="background: #FFFFFF; border: 1px solid #003EBA;" cellpadding="8" cellspacing="0"
style="background: #5D7CBA; text-align: center; font-family:Arial; color:#FFFFFF;" | Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is {{#ifexist:Wikipedia:Reference desk/Archives/Computing/2016 January 22|an archive page|a transcluded archive page}}. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.

__TOC__

= January 12 =

BFS MATLAB

hello, I am an Israeli student for ‏‎Information System Engineering‎‏, and I have a question in matlab.

I have to change this code in order to suit it for any size (any n*n)

{{collapse top|title=Breadth first search}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%This code executes breadth first search. This simulation is meant to

%mimick a robot navigating through a grid and planning a path around

%obstacles. The user defines the obstacles, goal, and starting position.

%This simulation is set up for a 5 x 5 grid that ranges from (0,0) to

%(5,5). This code is written for Matlab 7.0.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all

close all

clc

%%% THIS IS WHERE THE OBSTACLES ARE DEFINED %%%

% obstacles(1,:)=[2,4];

% obstacles(2,:)=[3,4];

% obstacles(3,:)=[4,2];

% obstacles(4,:)=[4,3];

% obstacles(5,:)=[4,4];

% obstacles(1,:)=[1,1];

% obstacles(2,:)=[1,2];

% obstacles(3,:)=[1,3];

% obstacles(4,:)=[2,1];

% obstacles(5,:)=[3,1];

obstacles(1,:)=[4,5];

obstacles(2,:)=[4,1];

obstacles(3,:)=[4,2];

obstacles(4,:)=[4,3];

obstacles(5,:)=[4,4];

%%% THIS IS WHERE THE STARTING POSITION %%%

%%% AND GOAL LOCATION ARE DEFINED %%%

startingPosition=[5,5];

goal=[0,5];

%Define the colors for plotting the results

obstacleColor=[1,0,0]; %red

nodeColor=[0,1,0]; %green

expandColor=[0,0,0]; %black

goalColor=[0,0,1]; %blue

pathColor=[0,1,1]; %cyan

%Plot the grid and obstacles

scatter(obstacles(:,1),obstacles(:,2),100,obstacleColor,'filled');

grid ;

axis([0 5 0 5]);

hold on;

%Plot the goal position

scatter(goal(1,1),goal(1,2),100,goalColor,'filled');

%Initialize variables

fringeCount=1; %Used for stepping through the fringe set (keeps track of the current node in the fringe set)

tempCount=1; %Used for expanding the fringe set (keeps track of the end of the fringe set)

%Initialize the fringe set. the structure is:

%fringe(xPosition, yPosition, parentNode)

fringe(fringeCount,:)=[startingPosition,fringeCount];

%This loop executes until the goal is found

while (~((fringe(fringeCount,1)==goal(1,1)) & (fringe(fringeCount,2)==goal(1,2))))

%Plot the current node

scatter(fringe(fringeCount,1),fringe(fringeCount,2),100,nodeColor,'filled');

%Expand the fringe set to the left, right, top and bottom of the current node

for x=-1:1

for y=-1:1

%This is a simple test to ensure that the fringe set isn't

%expanded diagonally as the robot can not move diagonally

if (x*y==0)

%'failsTest' is used to determine when a node can not be

%expanded because it is outside the grid, on an obstacle,

%or it has already been expanded.

failsTest=0;

%'tempNode' is the current node that is trying to be

%expanded

tempNode=[fringe(fringeCount,1)+x,fringe(fringeCount,2)+y, fringeCount];

%Test to see if the node is outside grid

if ( (tempNode(1,1)<0) | (tempNode(1,2)<0) ) | ( (tempNode(1,1)>5) | (tempNode(1,2)>5) )

failsTest=failsTest+1;

end

%If it did not fail the first test, test to see if node is

%already in fringe set.

if (failsTest<1)

for i=1:size(fringe,1)

if (tempNode(1,1)==fringe(i,1)) & (tempNode(1,2)==fringe(i,2))

failsTest=failsTest+1;

end

end

end

%If it did not fail the other tests, test to see if node is

%an obstacle

if (failsTest<1)

for i=1:size(obstacles,1)

if (tempNode(1,1)==obstacles(i,1))&(tempNode(1,2)==obstacles(i,2))

failsTest=failsTest+1;

end

end

end

%If it doesn't fail any tests, add to end of fringe set.

%In breadth first search, nodes are removed from the end of

%the fring set, so to make things easy we add new nodes to

%the end.

if (failsTest<1)

fringe(fringeCount+tempCount,:)=tempNode;

scatter(tempNode(1,1),tempNode(1,2),100,expandColor,'filled');

tempCount=tempCount+1;

end

end

end

end

%Increment to the next node. When you increment, you must also

%decrement tempCount since it is defined as a position in the fringe

%set relative to fringeCount

fringeCount=fringeCount+1;

tempCount=tempCount-1;

pause(5);

end

%Initialize a counter

i=1;

%Trace back through the parent nodes to receover the path

while ~(fringeCount==1)

path(i,:)=[fringe(fringeCount,1),fringe(fringeCount,2)];

fringeCount=fringe(fringeCount,3);

i=i+1;

end

%Add the start position to the path

path(i,:)=startingPosition;

%Plot the path

plot(path(:,1),path(:,2));

scatter(path(:,1),path(:,2),100,pathColor,'filled');

{{collapse bottom}}

— Preceding unsigned comment added by 132.73.205.93 (talk) 09:51, 12 January 2016 (UTC)

:Hello. What is your question? (I have formatted and collapsed your code.) -- ToE 13:08, 12 January 2016 (UTC)

I have to change this code for suit it to any N*N size instead of 5*5. 132.73.202.96 (talk) 13:30, 12 January 2016 (UTC)

:That is understandable. What is the problem? Do you know how to variable-length array or vector? 199.15.144.250 (talk) 14:00, 12 January 2016 (UTC)

::No. I don't know.132.72.232.249 (talk) 14:17, 12 January 2016 (UTC)

:::It is rather simple. Assume I used a=[] to create an empty vector. I want to add a value of 7 to it. I use a=[a ; 7];. That resets a to be equal to a with 7 shoved onto the end. I can add 3 to it with a=[a ; 3];. Now my vector holds 7 in the first index and 3 in the second index. Once you've filled the vector, you can loop over it just as you did with your fixed arrays. 199.15.144.250 (talk) 15:40, 12 January 2016 (UTC)

::::Right, but the declaration of obstacles will also have to be refactored a bit. OP will have to do something to deal with turning 5 definitions into N many. I would probably declare N at the start, then load in the obstacle input from another file into a correctly sized array within the algorithm. The algorithm can be adapted to arbitrary N, but only if the N inputs are known in advance. SemanticMantis (talk) 15:19, 13 January 2016 (UTC)

<tt>null-environment</tt> in R5RS [[Scheme (programming language)|Scheme]]

I'm confused about the difference between null-environment and scheme-report-environment in R5RS. I thought that (scheme-report-environment 5) basically returned the same environment that a program sees at startup (before any define), and that (null-environment 5) returned an environment in which only the syntax was defined, but not plain functions (per Null-environment returns a specifier for an environment that is empty except for the (syntactic) bindings for all syntactic keywords defined in this report from R5RS). But:

> (eval display (null-environment 5))

==> #

in Racket/R5RS, and likewise

> (eval display (null-environment 5))

$1 = #

in Guile, i.e. both of the languages return environments with bindings for the standard function. Can somebody enlighten me? Thanks! --Stephan Schulz (talk) 18:17, 12 January 2016 (UTC)

:Sorry - got it! Of course it has to be

> (eval 'display (null-environment 5))

:...otherwise the variable gets evaluated in the outer environment before it ever gets into the eval. --Stephan Schulz (talk)