Main Content

copy

Create deep copy of MPNet state sampler object

Since R2023b

    Description

    sampler2 = copy(sampler1) creates a deep copy of the specified MPNet state sampler object.

    example

    Examples

    collapse all

    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 = load("mazeMapTrainedMPNET.mat")
    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 (MapSize×MapResolution) must be same the as that of the maps used for training the MPNet.

    rng(10,"twister")
    map = mapMaze(5,1,MapSize=[12 12],MapResolution=2.0833);

    Specify the start pose and goal pose.

    start = [2 10 0];
    goal = [5 4 0];

    Visualize the input map.

    figure
    show(map)
    hold on
    plot(start(1),start(2),plannerLineSpec.start{:})
    plot(goal(1),goal(2),plannerLineSpec.goal{:})
    legend(Location="bestoutside")
    hold off

    Figure contains an axes object. The axes object with title Binary Occupancy Grid, xlabel X [meters], ylabel Y [meters] contains 3 objects of type image, line. One or more of the lines displays its values using only markers These objects represent Start, Goal.

    Create MPNet State Sampler

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

    x = map.XWorldLimits;
    y = map.YWorldLimits;
    theta = [-pi pi];
    stateBounds = [x; y; theta];
    stateSpace = stateSpaceSE2(stateBounds);

    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.

    mpnet = mpnetSE2(Network=data.trainedNetwork,StateBounds=stateBounds,EncodingSize=data.encodingSize);

    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(stateSpace,mpnet,Environment=map,StartState=start,GoalState=goal)
    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.

    states_orig = sample(sampler_orig,50);

    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.

    sampler_2 = copy(sampler_orig);
    sampler_2.MaxLearnedSamples = 10;

    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.

    sampler_3 = copy(sampler_orig);
    sampler_3.MaxLearnedSamples = 30;

    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.

    states_2 = sample(sampler_2,50);
    states_3 = sample(sampler_3,50);

    Plot and Compare Sampling Results

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

    fig1 = figure(Position=[0,0,1200,300]);

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

    subplot(1,3,1,Parent=fig1)
    show(map)
    hold on
    plot(start(1),start(2),plannerLineSpec.start{:})
    plot(goal(1),goal(2),plannerLineSpec.goal{:})
    plot(states_2(:,1),states_2(:,2),plannerLineSpec.state{:})
    title("MaxLearnedSamples = 10")
    hold off

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

    subplot(1,3,2,Parent=fig1)
    show(map)
    hold on
    plot(start(1),start(2),plannerLineSpec.start{:})
    plot(goal(1),goal(2),plannerLineSpec.goal{:})
    plot(states_3(:,1),states_3(:,2),plannerLineSpec.state{:})
    title("MaxLearnedSamples = 30")
    hold off

    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.

    subplot(1,3,3,Parent=fig1)
    show(map)
    hold on
    plot(start(1),start(2),plannerLineSpec.start{:})
    plot(goal(1),goal(2),plannerLineSpec.goal{:})
    plot(states_orig(:,1),states_orig(:,2),plannerLineSpec.state{:})
    title("MaxLearnedSamples = 50 (default)")
    hold off

    Figure contains 3 axes objects. Axes object 1 with title MaxLearnedSamples = 10, xlabel X [meters], ylabel Y [meters] contains 4 objects of type image, line. One or more of the lines displays its values using only markers These objects represent Start, Goal, State. Axes object 2 with title MaxLearnedSamples = 30, xlabel X [meters], ylabel Y [meters] contains 4 objects of type image, line. One or more of the lines displays its values using only markers These objects represent Start, Goal, State. Axes object 3 with title MaxLearnedSamples = 50 (default), xlabel X [meters], ylabel Y [meters] contains 4 objects of type image, line. One or more of the lines displays its values using only markers These objects represent Start, Goal, State.

    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.

    Input Arguments

    collapse all

    MPNet state sampler object, specified as a stateSamplerMPNET object.

    Output Arguments

    collapse all

    Copy of MPNet state sampler object, returned as a stateSamplerMPNET object.

    Extended Capabilities

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    Version History

    Introduced in R2023b

    See Also

    Objects

    Functions