This example shows how to perform informed sampling using a pretrained MPNet. Up to a certain number of iterations, an MPNet state sampler predicts samples using the pretrained MPNet. The samples predicted using the MPNet are biased towards a subspace that contains an optimal path solution. After the specified number of iterations, the sampler switches to the uniform sampling approach. You can change the number of iterations by specifying the `MaxLearnedSamples`

property of the `stateSamplerMPNET`

object.

**Load Pretrained MPNet**

Load a data file containing a pretrained MPNet into the MATLAB® workspace. The MPNet has been trained on various 2-D maze maps with widths and heights of 10 meters and resolutions of 2.5 cells per meter. Each maze map contains a passage width of 5 grid cells and wall thickness of 1 grid cell.

data = *struct with fields:*
encodingSize: [9 9]
lossWeights: [100 100 0]
mazeParams: {[5] [1] 'MapSize' [10 10] 'MapResolution' [2.5000]}
stateBounds: [3x2 double]
trainedNetwork: [1x1 dlnetwork]

**Create Maze Map for Sampling**

Create a random maze map for sampling. The grid size ($\mathrm{MapSize}\times \mathrm{MapResolution}$) must be same the as that of the maps used for training the MPNet.

Specify the start pose and goal pose.

Visualize the input map.

**Create MPNet State Sampler**

Specify the state bounds, and create an SE(2) state space object.

Configure the `mpnetSE2`

object to use the pretrained MPNet for predicting state samples on a random map. Set the `EncodingSize`

property values of the `mpnetSE2`

object to that of the value used for training the network.

Create an MPNet state sampler for computing state samples to use for motion planning. Specify the map environment, the start pose and the goal pose as inputs to the state sampler. The default value for `MaxLearnedSamples`

is `50`

. This implies that the first 50 samples returned by the state sampler are learning-based samples, predicted using the pretrained MPNet.

sampler_orig =
stateSamplerMPNET with properties:
StateSpace: [1x1 stateSpaceSE2]
MotionPlanningNetwork: [1x1 mpnetSE2]
Environment: [1x1 binaryOccupancyMap]
StartState: [2 10 0]
GoalState: [5 4 0]
MaxLearnedSamples: 50
GoalThreshold: 1

**Sample State Space Using MPNet State Sampler**

Generate 50 state samples that lie between the start pose and the goal pose using the MPNet state sampler. In this case, the MPNet state sampler generates all 50 samples by using the pretrained MPNet.

**Create Copies of MPNet State Sampler and Vary Maximum Learned Samples**

Create copies of the default MPNet state sampler object. Modify the maximum learned samples property of the sampler to study its impact on the sampling results.

Set the `MaxLearnedSamples`

property to `10`

. In this case, the MPNet state sampler generates the first 10 samples using the pretrained MPNet and the remaining 40 samples using the uniform sampling approach.

Set the `MaxLearnedSamples`

property to `30`

. In this case, the MPNet state sampler generates the first 30 samples using the pretrained MPNet and the remaining 20 samples using the uniform sampling approach.

**Sample State Space Using Modified MPNet State Sampler**

Use each of the modified MPNet state samplers to generate the same number of samples as that of the default sampler. In this case, use them each to generate 50 samples.

**Plot and Compare Sampling Results**

Create a figure with adequate space to display plots for all three state samplers.

Plot the output of the MPNet state sampler with the maximum number of learned samples set to 10.

Plot the output of the MPNet state sampler with the maximum number of learned samples set to 30.

Plot the output of the default MPNet state sampler. The default value for the maximum number of learned samples is 50. As you increase the maximum number of learned samples to match the number of state samples to generate, the output samples become more focused along a path between the given start pose and the goal pose.

**Further Exploration**

From the sampling results, you can infer that the MPNet state sampler returns optimal state samples if the maximum number of learned samples is equal to the maximum number of samples to be computed. However, if the MPNet is trained on a data set with fewer map environments or path samples, the prediction accuracy of the network decreases. The MPNet state sampler might return an insufficient number of learned samples between the start pose and goal pose, and you cannot use it for path planning. In such cases, the value of `MaxLearnedSamples`

must be less than the number of samples required. This enables the MPNet state sampler to switch to the uniform sampling approach after a specified number of iterations, thus guaranteeing a path solution.