Main Content

sample

Sample states from Gaussian state sampler

Since R2023b

    Description

    states = sample(sampler) returns a state sample selected using the Gaussian state sampling approach.

    example

    states = sample(sampler,numsamples) returns the specified number of state samples.

    example

    Examples

    collapse all

    Sample an SE(2) state space using a Gaussian state sampler, and observe the impact of the sampler parameter values on the sampling results.

    Set the seed value to ensure you generate the same results.

    rng(50,"twister");

    Create a simple occupancy map with a narrow passage.

    map = binaryOccupancyMap;
    occupied = [5*ones(9,1),[1; 2; 3; 4; 5; 7; 8; 9; 10]];
    setOccupancy(map,occupied,1);
    figure(Position=[0, 0, 200, 200])
    show(map)

    Figure contains an axes object. The axes object with title Binary Occupancy Grid, xlabel X [meters], ylabel Y [meters] contains an object of type image.

    Define the lower and upper limits of the state space variables x, y, and theta from the occupancy map.

    x = map.XWorldLimits;
    y =  map.YWorldLimits;
    theta = [-pi pi];

    Create a state space SE(2) object using the specified state space variables. Check the validity of states in the input state space by using a state validator. Set the validation distance to 0.01.

    ss = stateSpaceSE2([x; y; theta]);
    sv = validatorOccupancyMap(ss,Map=map);
    sv.ValidationDistance = 0.01;

    Sample State Space Using Gaussian State Sampler

    Create a Gaussian state sampler with default parameter values. By default:

    • The maximum number of attempts that the sampler must take for finding the state samples is set to 10.

    • The standard deviation values along the x,y, and θ directions are set to 0.1, 0.1, and 0.0628, respectively.

    sampler_orig = stateSamplerGaussian(sv)
    sampler_orig = 
      stateSamplerGaussian with properties:
    
               StateSpace: [1x1 stateSpaceSE2]
           StateValidator: [1x1 validatorOccupancyMap]
        StandardDeviation: [0.1000 0.1000 0.0628]
              MaxAttempts: 10
    
    

    Generate 40 samples for motion planning from the input state space.

    states_orig = sample(sampler_orig,40);

    You can generate optimal samples by modifying the maximum number of attempts and standard deviation values. If the samples are scattered all over the input space, increase the maximum number of attempts and the standard deviation values to concentrate the state samples around the obstacle boundary.

    Vary Maximum Number of Attempts

    Create copies of the original state sampler object and modify the maximum number of attempts, property of the sampler, MaxAttempts, to study its impact on the sampling results. Set the standard deviation values to default values.

    Set the maximum number of attempts to find valid samples to 100, and generate 40 new samples from the input state space.

    sampler_2 = copy(sampler_orig);
    sampler_2.MaxAttempts = 100;
    states_2 = sample(sampler_2,40);

    Set the maximum number of attempts to find valid samples to 200, and generate 40 new samples from the input state space.

    sampler_3 = copy(sampler_orig);
    sampler_3.MaxAttempts = 200;
    states_3 = sample(sampler_3,40);

    Display the results using the helperDisplayStates helper function. Note that, as the number of attempts increases, the samples concentrate more around the obstacle boundary.

    helperDisplayStates(map,states_orig,sampler_2,states_2,sampler_3,states_3,"MaxAttempts");

    Figure contains 3 axes objects and other objects of type uipanel. Axes object 1 with title Binary Occupancy Grid, xlabel X [meters], ylabel Y [meters] contains 2 objects of type image, line. One or more of the lines displays its values using only markers This object represents State. Axes object 2 with title Binary Occupancy Grid, xlabel X [meters], ylabel Y [meters] contains 2 objects of type image, line. One or more of the lines displays its values using only markers This object represents State. Axes object 3 with title Binary Occupancy Grid, xlabel X [meters], ylabel Y [meters] contains 2 objects of type image, line. One or more of the lines displays its values using only markers This object represents State.

    Vary Standard Deviation

    Create copies of the original state sampler object and modify the standard deviation, property of the sampler, StandardDeviation, to study its impact on the sampling results. Set the maximum number of attempts to 200.

    Generate 40 samples with the default standard deviation values.

    sampler_orig.MaxAttempts = 200;
    states_orig = sample(sampler_orig,40);

    Set the standard deviation values to [0.01 0.01 0.06]. Generate 40 new samples from the input state space.

    sampler_4 = copy(sampler_orig);
    sampler_4.StandardDeviation = [0.01 0.01 0.06];
    states_4 = sample(sampler_4,40);

    Set the standard deviation values to [0.5 0.5 0.06]. Generate 40 new samples from the input state space.

    sampler_5 = copy(sampler_orig);
    sampler_5.StandardDeviation = [0.5 0.5 0.06];
    states_5 = sample(sampler_5,40);

    Display the results using the helperDisplayStates helper function. Note that, as you increase the standard deviation values, the samples concentrate more around the obstacle boundary. However, if the standard deviation values are greater than the width of the narrow passages in the input space, the sampler generates incorrect results.

    helperDisplayStates(map,states_orig,sampler_4,states_4,sampler_5,states_5,"Std.Deviation");

    Figure contains 3 axes objects and other objects of type uipanel. Axes object 1 with title Binary Occupancy Grid, xlabel X [meters], ylabel Y [meters] contains 2 objects of type image, line. One or more of the lines displays its values using only markers This object represents State. Axes object 2 with title Binary Occupancy Grid, xlabel X [meters], ylabel Y [meters] contains 2 objects of type image, line. One or more of the lines displays its values using only markers This object represents State. Axes object 3 with title Binary Occupancy Grid, xlabel X [meters], ylabel Y [meters] contains 2 objects of type image, line. One or more of the lines displays its values using only markers This object represents State.

    Helper Function

    helperDisplayStates displays results using a custom figure window.

    function helperDisplayStates(map,states_orig,sampler_2,states_2,sampler_3,states_3,select)
    if select == "MaxAttempts"
        title_1 = "MaxAttempts = 10 (Default value)";
        title_2 = strcat("MaxAttempts = ",num2str(sampler_2.MaxAttempts));
        title_3 = strcat("MaxAttempts = ",num2str(sampler_3.MaxAttempts));
    elseif select == "Std.Deviation"
        title_1 = "StandardDeviation = [0.1 0.1 0.06] (Default value)";
        title_2 = strcat("StandardDeviation = [0.01 0.01 0.06]");
        title_3 = strcat("StandardDeviation = [0.5 0.5 0.06]");
    end
    
    fig_1 = figure(Position=[0 0 700 300]);
    movegui("center")
    panel_1 = uipanel(fig_1, ...
        Position=[0 0 0.33 1], ...
        Title=title_1);
    hPlot1 = axes(panel_1);
    show(map,Parent=hPlot1);
    hold on;
    plot(states_orig(:,1),states_orig(:,2),plannerLineSpec.state{:})
    hold off
    
    panel_2 = uipanel(fig_1, ...
        Position=[0.33 0 0.33 1], ...
        Title=title_2);
    hPlot2 = axes(panel_2);
    show(map,Parent=hPlot2);
    hold on;
    plot(states_2(:,1),states_2(:,2),plannerLineSpec.state{:})
    hold off
    
    panel_3 = uipanel(fig_1, ...
        Position=[0.66 0 0.33 1], ...
        Title=title_3);
    hPlot3 = axes(panel_3);
    show(map,Parent=hPlot3);
    hold on;
    plot(states_3(:,1),states_3(:,2),plannerLineSpec.state{:})
    hold off
    end

    Input Arguments

    collapse all

    State sampler object, specified as a stateSamplerGaussian object.

    Number of samples, specified as a positive integer.

    Data Types: double

    Output Arguments

    collapse all

    State samples, returned as an M-by-N matrix of real values. M is the number of states. N is the number of state variables. Each row of the matrix specifies the state variables corresponding to the input state space model. For example, for the SE(2) state space model, N is 3 and each row is of the form [x, y, theta].

    Data Types: double

    Version History

    Introduced in R2023b

    See Also

    Objects

    Functions