# stateSamplerGaussian

## Description

The `stateSamplerGaussian`

object creates a state sampler for motion
planning by using Gaussian distribution. Use this sampler for motion planning in narrow
passages. For information about how the Gaussian state sampler selects valid state samples,
see Find Sample States Using Gaussian State Sampler.

## Creation

### Syntax

### Description

creates a
Gaussian state sampler associated with a `sampler`

= stateSamplerGaussian`stateSpaceSE2`

object with default settings.

creates a Gaussian state sampler to generate samples for a valid state space. Use the
`sampler`

= stateSamplerGaussian(`stateValidator`

)`validatorOccupancyMap`

object to create a state validator, and use the state
validator as input to the Gaussian state sampler.

sets properties using one or name-value arguments in addition to the input argument in the
previous syntax. You can specify the `sampler`

= stateSamplerGaussian(___,`Name=Value`

)`StandardDeviation`

and
`MaxAttempts`

properties as name-value arguments.

## Properties

`StateSpace`

— SE(2) state space

`stateSpaceSE2`

object

This property is read-only.

SE(2) state space, stored as an `stateSpaceSE2`

object. This property is set by the input state validator
`stateValidator`

.

`StateValidator`

— State validator definition

`validatorOccupancyMap`

object | object of subclass of `nav.StateValidator`

class

This property is read-only.

State validator definition, specified as a `validatorOccupancyMap`

object or an object of a subclass of the `nav.StateValidator`

class. This property is set by the input state validator
`stateValidator`

.

`StandardDeviation`

— Standard deviation for Gaussian distribution

*N*-element row vector

Standard deviation for Gaussian distribution, specified as an
*N*-element row vector. *N* is the number of state
variables. For the SE(2) state space, the standard deviation must be a three-element row
vector of form [*σ _{x}*

*σ*

_{y}*σ*].

_{θ}The object computes the default value for the standard deviation using the minimum
and the maximum bounds of the state variables *x*,
*y*, and *θ*.

$$\begin{array}{l}{\sigma}_{x}=\frac{1}{100}\left({x}_{\mathrm{max}}-{x}_{\mathrm{min}}\right)\\ {\sigma}_{y}=\frac{1}{100}\left({y}_{\mathrm{max}}-{y}_{\mathrm{min}}\right)\\ {\sigma}_{\theta}=\frac{1}{100}\left({\theta}_{\mathrm{max}}-{\theta}_{\mathrm{min}}\right)\end{array}$$

Programmatically, you can read the bounds of the state variables from
the `StateSpace`

property of the `stateSamplerGaussian`

object. The `stateSamplerGaussian`

object computes the default value for the
standard deviation
as:

`1/100*(sampler.StateSpace.StateBounds(:,2)-sampler.StateSpace.StateBounds(:,1))`

**Note**

For correct results, the standard deviation values must not be more than the width of the narrow passages in the configuration space.

**Data Types: **`single`

| `double`

`MaxAttempts`

— Threshold for maximum number of attempts

`10`

(default) | positive integer

Threshold for the maximum number of attempts that the sampler can take to find the valid samples, specified as a positive integer. The sampler stops searching if it obtains the optimal number of valid samples before the specified threshold.

You can fine-tune this parameter value alongside the standard deviation parameter value. If the samples returned by the state sampler are scattered all over the configuration space, you can increase the number of attempts to enable the sampler to find samples that are concentrated along the obstacle boundary. Further, if you decrease the standard deviation value, you must consider increasing the maximum number of attempts to enable the sampler to find optimal samples.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

## Object Functions

## Examples

### Sample SE(2) State Space Using Gaussian State Sampler

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)

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 $\mathit{x},\mathit{y},$ and $\theta $ 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");`

**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");`

**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

### Perform Motion Planning Using Gaussian State Space Sampling Approach

Sample a state space for motion planning by using Gaussian distribution, and then use the sampled states to find an optimal path between two points in the input state space. Use a PRM path planner to compute an optimal path between two points.

Set the random number seed to ensure repeatability.

`rng(100,"twister");`

**Create Occupancy Map and Find State Variables**

Load a binary map representing the input state space environment into MATLAB*®* workspace.

`load("mapData.mat")`

Create an occupancy map from the input.

map = occupancyMap(narrowPassageMap);

Find 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 Gaussian State Sampler**

Create a state space SE(2) object using the specified state space variables.

stateSpace = stateSpaceSE2([x; y; theta]);

Check the validity of the states in the input state space by using a state validator.

stateValidator = validatorOccupancyMap(stateSpace,Map=map);

Create a Gaussian state sampler to sample the specified state space. Set the standard deviation values.

sampler = stateSamplerGaussian(stateValidator,StandardDeviation=[25 5 0.05]);

**Configure PRM Path Planner**

Configure the PRM path planner. Use the Gaussian state sampler to sample the input state space.

planner = plannerPRM(stateSpace,stateValidator,StateSampler=sampler);

**Find Optimal Path Between Two States**

Specify the start point and the goal point in the input state space.

start = [55 50 0]; goal = [210 190 0];

Compute the optimal path between the start point and the goal point using the PRM path planner.

[path,info] = plan(planner,start,goal);

**Visualize the Results**

Display the occupancy map.

```
figure
show(map)
hold on
```

Plot the start point and the goal point. Specify the default color and line properties for plotting the start and goal points by using the `plannerLineSpec.start`

t and `plannerLineSpec.goal`

functions, respectively.

plot(start(1),start(2),plannerLineSpec.start{:}); plot(goal(1),goal(2),plannerLineSpec.goal{:});

If the planner has found an optimal path between the start and goal states, plot the results. Use the `plannerLineSpec.path`

function to specify the default color and line properties for plotting the path.

if info.IsPathFound plot(path.States(:,1),path.States(:,2),plannerLineSpec.path{:}) legend else disp("Path not found. Try modifying the planner parameters."); end

## More About

### Find Sample States Using Gaussian State Sampler

The sampler repeatedly generates pairs of states. The sampler selects the first state sample in each pair using uniform distribution, and the second state sample using Gaussian distribution centered around the first sample.

The sampler determines the distance between the first sample and second sample in each
pair by using the Gaussian parameters *σ _{x}*,

*σ*, and

_{y}*σ*.

_{θ}*σ*specifies the spread along the_{x}*x*-direction*σ*specifies the spread along the_{y}*y*-direction.*σ*specifies the spread along the angular direction._{θ}

The state validator considers a generated pair of states valid if one state sample from the pair lies on the obstacle and the other lies in the free-space. The sample from the valid pair that lies in the free-space is returned as a state sample for motion planning.

**How to Select Standard Deviation Values?**

To get the optimal samples for motion planning, you can either decrease or increase the standard deviation values.

When you decrease the value of standard deviation along a particular direction, the distance between two samples in each valid pair decreases along the specified direction. Decrease the standard deviation values if the input space for motion planning has too narrow of passages. You must also increase the maximum number of attempts for the sampler to find optimal samples.

When you increase the value of standard deviation along a particular direction, the distance between two samples in each valid pair increases along the specified direction. Increase the standard deviation values if the input space for motion planning does not have too narrow of passages.

## Version History

**Introduced in R2023b**

## See Also

### Functions

### Objects

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)