Jump to content

Wikipedia:Reference desk/Archives/Computing/2016 January 12

From Wikipedia, the free encyclopedia
Computing desk
< January 11 << Dec | January | Feb >> January 13 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


January 12

[edit]

BFS MATLAB

[edit]

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)

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');

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

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

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)[reply]

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)[reply]
No. I don't know.132.72.232.249 (talk) 14:17, 12 January 2016 (UTC)[reply]
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)[reply]
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)[reply]

null-environment in R5RS Scheme

[edit]

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))
==> #<procedure:mdisplay>

in Racket/R5RS, and likewise

> (eval display (null-environment 5))
$1 = #<procedure display (_ #:optional _)>

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)[reply]

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)
[edit]

Can users who buy a software license "sublet" the software? That is, offer it as a subscription service? --Bickeyboard (talk) 19:52, 12 January 2016 (UTC)[reply]

It depends on the license. If the license says you cannot rent the software, you can't. If it says you can, you can. For example, Microsoft licenses do not allow renting except when they specifically state that you can as in this MS announcement. 199.15.144.250 (talk) 20:15, 12 January 2016 (UTC)[reply]
  • The question is also if the law allows them to curtail the liberty of the buyers in such way. Copyright holders might try to restrict the use of their products. There was a similar case concerning CDs/DVDs. I know that the United States Supreme Court had to decide whether CDs/DVDs can be re-sold. Basically, the question is if the buyers are owners somehow, and of what. --Bickeyboard (talk) 21:43, 12 January 2016 (UTC)[reply]
If you are talking about commercial software (and not some variant of open source) I'm fairly certain that it would be safe to assume the answer is NO in almost all cases. Vespine (talk) 20:39, 12 January 2016 (UTC)[reply]
OK, but I go to an Internet café where they have several programs installed, including MS Office, many games, Photoshop and other stuff. I am just paying £1 per hour. Am I renting the software? Or just paying for the internet connection + hardware use? Obviously, many people go there for the software. --Bickeyboard (talk) 21:39, 12 January 2016 (UTC)[reply]
I'm a programmer, and I have some interest in copyright, especially in the digital realm. Your observation is astute. In a sense, I guess you could say you are "renting" the software, although this term isn't actually used in the law and contracts. It's usually only called "renting" when talking about tangible property. Places like computer cafes have to purchase site licenses for proprietary software. The licenses set out what they're allowed to do with the software, and they can be audited to ensure they're complying with the license. "Personal" software licenses that you get with "personal" editions of software usually don't allow you to do things like charge people for using the software. Go and read a license agreement for some big proprietary software package like Microsoft Office to get an idea of all the restrictions. Purely from a financial perspective, part of the money you pay is going towards the licensing fees of all the software. Of course we're assuming everyone's doing things legally. In less-developed countries with lax legal systems it's often the case that most computer cafes and similar places just ignore copyrights, but in countries with a stronger rule of law it's harder to get away with. --71.119.131.184 (talk) 23:09, 12 January 2016 (UTC)[reply]
"Commercial software" and "open source" are not mutually exclusive. --71.119.131.184 (talk) 23:09, 12 January 2016 (UTC)[reply]
That's true, however most "commercial" open source software does not make money selling software licenses, which is what the OP was asking about. Vespine (talk) 01:55, 13 January 2016 (UTC)[reply]
A license can be tied to a specific single user, to an organization, or to a machine. They can also limit the number of copies that run simultaneously or limit the use, forcing a no-commercial-use policy, or only for educational purposes. Combinations among these models exist. Each licensing agreement is different. Renting hardware with a legal version of the software installed seems to be a different legal question. Indeed, this seems to be a question that generates long legal battles, if it became an issue between providers and consumers. Scicurious (talk) 00:33, 13 January 2016 (UTC)[reply]

It seems to me that this question is asking for legal advice, in which case we are not allowed to answer it here. In any case the answer may well vary according to what country we are talking about. --76.69.45.64 (talk) 21:38, 13 January 2016 (UTC)[reply]