This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Optimize Lookup Tables for Memory-Efficiency Programmatically

The following examples show how to generate memory-efficient lookup tables programmatically. Using the command-line equivalent of the Lookup Table Optimizer, you can:

  • Optimize an existing Lookup Table block.

  • Generate a lookup table from a Math Function block.

  • Generate a lookup table from a function or function handle.

  • Generate a lookup table from a Subsystem block.

Approximate a Function Using a Lookup Table

This example shows how to generate a memory-efficient lookup table that approximates the sin function. Define the approximation problem by creating a FunctionApproximation.Problem object.

P = FunctionApproximation.Problem('sin')
P = 

  1x1 FunctionApproximation.Problem with properties:

    FunctionToApproximate: @(x)sin(x)
           NumberOfInputs: 1
               InputTypes: "numerictype(0,16,13)"
         InputLowerBounds: 0
         InputUpperBounds: 6.2832
               OutputType: "numerictype(1,16,14)"
                  Options: [1x1 FunctionApproximation.Options]

The FunctionToApproximate and NumberOfInputs properties of the Problem object are inferred from the definition of the object, and cannot be edited after creation. All other properties are writable.

Edit the FunctionApproximation.Options object to specify additional constraints to use in the optimization process. For example, constrain the breakpoints of the generated lookup table to even spacing.

P.Options.BreakpointSpecification = 'EvenSpacing'
P = 

  1x1 FunctionApproximation.Problem with properties:

    FunctionToApproximate: @(x)sin(x)
           NumberOfInputs: 1
               InputTypes: "numerictype(0,16,13)"
         InputLowerBounds: 0
         InputUpperBounds: 6.2832
               OutputType: "numerictype(1,16,14)"
                  Options: [1x1 FunctionApproximation.Options]

Specify additional constraints, such as the absolute and relative tolerances of the output, and word length constraints.

P.Options.AbsTol = 2^-10;
P.Options.RelTol = 2^-6;
P.Options.WordLengths = [8,16];

Use the solve method to solve the optimization problem. MATLAB™ displays the iterations of the optimization process. The solve method returns a FunctionApproximation.LUTSolution object.

S = solve(P)
|  ID |  Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification |             Error(Max,Current) | 
|   0 |             48 |        0 |          2 |               8 |           16 |             EvenSpacing |     9.765625e-04, 1.007261e+00 |
|   1 |             32 |        0 |          2 |               8 |            8 |             EvenSpacing |     9.765625e-04, 1.007261e+00 |
|   2 |            288 |        0 |         17 |               8 |           16 |             EvenSpacing |     9.765625e-04, 2.794368e-01 |
|   3 |            560 |        0 |         34 |               8 |           16 |             EvenSpacing |     9.765625e-04, 9.553784e-02 |
|   4 |           1104 |        0 |         68 |               8 |           16 |             EvenSpacing |     9.765625e-04, 1.970943e-03 |
|   5 |           3264 |        1 |        203 |               8 |           16 |             EvenSpacing |     9.765625e-04, 7.988139e-05 |
|   6 |             64 |        0 |          2 |              16 |           16 |             EvenSpacing |     9.765625e-04, 9.853516e-01 |
|   7 |             48 |        0 |          2 |              16 |            8 |             EvenSpacing |     9.765625e-04, 9.853516e-01 |
|   8 |            304 |        0 |         17 |              16 |           16 |             EvenSpacing |     9.765625e-04, 4.538564e-03 |
|   9 |            576 |        0 |         34 |              16 |           16 |             EvenSpacing |     9.765625e-04, 3.008540e-03 |
|  10 |           1120 |        0 |         68 |              16 |           16 |             EvenSpacing |     9.765625e-04, 1.909908e-03 |
|  11 |           2208 |        0 |        136 |              16 |           16 |             EvenSpacing |     9.765625e-04, 4.473384e-03 |
|  12 |           1664 |        0 |        102 |              16 |           16 |             EvenSpacing |     9.765625e-04, 7.647212e-03 |
|  13 |           1936 |        0 |        119 |              16 |           16 |             EvenSpacing |     9.765625e-04, 2.886470e-03 |
|  14 |           2064 |        0 |        127 |              16 |           16 |             EvenSpacing |     9.765625e-04, 7.769283e-03 |
|  15 |           2128 |        0 |        131 |              16 |           16 |             EvenSpacing |     9.765625e-04, 1.484936e-02 |
|  16 |           2160 |        0 |        133 |              16 |           16 |             EvenSpacing |     9.765625e-04, 1.509350e-02 |
|  17 |           2176 |        1 |        134 |              16 |           16 |             EvenSpacing |     9.765625e-04, 1.398881e-04 |
|  18 |             48 |        0 |          2 |               8 |           16 |         EvenPow2Spacing |     9.765625e-04, 1.300802e+00 |
|  19 |            432 |        0 |         26 |               8 |           16 |         EvenPow2Spacing |     9.765625e-04, 3.322094e-02 |
|  20 |            832 |        0 |         51 |               8 |           16 |         EvenPow2Spacing |     9.765625e-04, 3.322094e-02 |
|  21 |           1648 |        1 |        102 |               8 |           16 |         EvenPow2Spacing |     9.765625e-04, 7.988139e-05 |
|  22 |             64 |        0 |          2 |              16 |           16 |         EvenPow2Spacing |     9.765625e-04, 1.300816e+00 |
|  23 |            448 |        0 |         26 |              16 |           16 |         EvenPow2Spacing |     9.765625e-04, 3.315991e-02 |
|  24 |            848 |        0 |         51 |              16 |           16 |         EvenPow2Spacing |     9.765625e-04, 3.315991e-02 |

Best Solution
|  ID |  Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification |             Error(Max,Current) |
|  21 |           1648 |        1 |        102 |               8 |           16 |         EvenPow2Spacing |     9.765625e-04, 7.988139e-05 |


S = 

  1x1 FunctionApproximation.LUTSolution with properties:

          ID: 21
    Feasible: "true"

Compare the numerical behavior of the original function with the numerical behavior of the generated lookup table stored in the solution, S.

err = compare(S)
err = 

  struct with fields:

    Breakpoints: [51473x1 double]
       Original: [51473x1 double]
    Approximate: [51473x1 double]

You can access the lookup table data stored in the LUTSolution object.

t = S.TableData
t = 

  struct with fields:

       BreakpointValues: {[1x102 double]}
    BreakpointDataTypes: [1x1 embedded.numerictype]
            TableValues: [1x102 double]
          TableDataType: [1x1 embedded.numerictype]
          IsEvenSpacing: 1
          Interpolation: Linear

To access the generated Lookup Table block, use the approximate method.

S.approximate

Optimize an Existing Lookup Table

This example shows how to optimize an existing Lookup Table block for memory efficiency. Open the model containing the Lookup Table block that you want to optimize.

load_system('sldemo_fuelsys');
open_system('sldemo_fuelsys/fuel_rate_control/airflow_calc');

Create a FunctionApproximation.Problem object to define the optimization problem and constraints.

P = FunctionApproximation.Problem('sldemo_fuelsys/fuel_rate_control/airflow_calc/Pumping Constant')
P = 

  1x1 FunctionApproximation.Problem with properties:

    FunctionToApproximate: 'sldemo_fuelsys/fuel_rate_control/airflow_calc/Pumping Constant'
           NumberOfInputs: 2
               InputTypes: [1x2 string]
         InputLowerBounds: [50 0.0500]
         InputUpperBounds: [1000 0.9500]
               OutputType: "numerictype('single')"
                  Options: [1x1 FunctionApproximation.Options]

Specify additional constraints by modifying the Options object associated with the Problem object, P.

P.Options.BreakpointSpecification = "EvenSpacing"
P = 

  1x1 FunctionApproximation.Problem with properties:

    FunctionToApproximate: 'sldemo_fuelsys/fuel_rate_control/airflow_calc/Pumping Constant'
           NumberOfInputs: 2
               InputTypes: [1x2 string]
         InputLowerBounds: [50 0.0500]
         InputUpperBounds: [1000 0.9500]
               OutputType: "numerictype('single')"
                  Options: [1x1 FunctionApproximation.Options]

Solve the optimization problem.

S = solve(P)
|  ID |  Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification |             Error(Max,Current) | 
|   0 |          12128 |        1 |    [18 19] |         [32 32] |           32 |          ExplicitValues |     7.812500e-03, 0.000000e+00 |
|   1 |          11840 |        1 |    [18 19] |         [16 32] |           32 |          ExplicitValues |     7.812500e-03, 0.000000e+00 |
|   2 |             80 |        0 |      [2 2] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 1.045582e-01 |
|   3 |           2928 |        0 |    [18 20] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 3.133151e-02 |
|   4 |          11280 |        0 |    [36 39] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 2.742526e-02 |
|   5 |           6528 |        0 |    [27 30] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 3.297625e-02 |
|   6 |           8480 |        0 |    [31 34] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 3.020075e-02 |
|   7 |          10344 |        0 |    [33 39] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 3.020075e-02 |
|   8 |          10656 |        0 |    [34 39] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 2.742526e-02 |
|   9 |          10968 |        0 |    [35 39] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 2.742526e-02 |
|  10 |            128 |        0 |      [2 2] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.048788e-01 |
|  11 |           2832 |        0 |    [18 19] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.426438e-02 |
|  12 |          11040 |        1 |    [36 38] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 2.082726e-03 |
|  13 |           6144 |        0 |    [27 28] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 9.765960e-03 |
|  14 |           8280 |        1 |    [31 33] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 5.650558e-03 |
|  15 |           7056 |        0 |    [29 30] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 8.803874e-03 |
|  16 |           7536 |        1 |    [30 31] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 7.327236e-03 |
|  17 |            112 |        0 |      [2 2] |          [16 8] |           16 |             EvenSpacing |     7.812500e-03, 1.052361e-01 |
|  18 |           5808 |        0 |    [18 20] |          [16 8] |           16 |             EvenSpacing |     7.812500e-03, 3.194828e-02 |
|  19 |           1808 |        0 |    [10 11] |          [16 8] |           16 |             EvenSpacing |     7.812500e-03, 4.010030e-02 |
|  20 |           3408 |        0 |    [14 15] |          [16 8] |           16 |             EvenSpacing |     7.812500e-03, 3.247673e-02 |
|  21 |           4400 |        0 |    [16 17] |          [16 8] |           16 |             EvenSpacing |     7.812500e-03, 3.247672e-02 |
|  22 |           4944 |        0 |    [17 18] |          [16 8] |           16 |             EvenSpacing |     7.812500e-03, 3.622878e-02 |
|  23 |           5488 |        0 |    [17 20] |          [16 8] |           16 |             EvenSpacing |     7.812500e-03, 3.622878e-02 |
|  24 |            160 |        0 |      [2 2] |         [16 32] |           16 |             EvenSpacing |     7.812500e-03, 1.055567e-01 |
|  25 |           5568 |        0 |    [18 19] |         [16 32] |           16 |             EvenSpacing |     7.812500e-03, 1.448403e-02 |
|  26 |           1696 |        0 |    [10 10] |         [16 32] |           16 |             EvenSpacing |     7.812500e-03, 3.366882e-02 |
|  27 |           3232 |        0 |    [14 14] |         [16 32] |           16 |             EvenSpacing |     7.812500e-03, 2.069179e-02 |
|  28 |           4192 |        0 |    [16 16] |         [16 32] |           16 |             EvenSpacing |     7.812500e-03, 1.697001e-02 |
|  29 |           4720 |        0 |    [17 17] |         [16 32] |           16 |             EvenSpacing |     7.812500e-03, 1.629277e-02 |
|  30 |           4992 |        0 |    [17 18] |         [16 32] |           16 |             EvenSpacing |     7.812500e-03, 1.629277e-02 |
|  31 |             80 |        0 |      [2 2] |          [16 8] |            8 |         EvenPow2Spacing |     7.812500e-03, 8.718936e-02 |
|  32 |           7248 |        0 |    [30 30] |          [16 8] |            8 |         EvenPow2Spacing |     7.812500e-03, 3.020075e-02 |
|  33 |            128 |        0 |      [2 2] |         [16 32] |            8 |         EvenPow2Spacing |     7.812500e-03, 8.842091e-02 |
|  34 |           7056 |        0 |    [30 29] |         [16 32] |            8 |         EvenPow2Spacing |     7.812500e-03, 8.138325e-03 |
|  35 |           3696 |        0 |    [30 15] |         [16 32] |            8 |         EvenPow2Spacing |     7.812500e-03, 1.056678e-02 |
|  36 |            112 |        0 |      [2 2] |          [16 8] |           16 |         EvenPow2Spacing |     7.812500e-03, 8.776325e-02 |
|  37 |           3888 |        0 |     [30 8] |          [16 8] |           16 |         EvenPow2Spacing |     7.812500e-03, 3.088659e-02 |
|  38 |            160 |        0 |      [2 2] |         [16 32] |           16 |         EvenPow2Spacing |     7.812500e-03, 8.914277e-02 |
|  39 |           3936 |        0 |     [30 8] |         [16 32] |           16 |         EvenPow2Spacing |     7.812500e-03, 3.205415e-02 |
|  40 |            176 |        0 |      [2 2] |          [16 8] |           32 |         EvenPow2Spacing |     7.812500e-03, 8.776436e-02 |

Best Solution
|  ID |  Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification |             Error(Max,Current) |
|  16 |           7536 |        1 |    [30 31] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 7.327236e-03 |


S = 

  1x1 FunctionApproximation.LUTSolution with properties:

          ID: 16
    Feasible: "true"

Compare the numerical behavior of the original lookup table, with the optimized lookup table.

compare(S)
ans = 

  1x2 struct array with fields:

    Breakpoints
    Original
    Approximate

Generate the new Lookup Table block using the approximate method.

S.approximate

Visualize Pareto Front for Memory Optimization Versus Absolute Tolerance

When you want to optimize for both memory and absolute tolerance, it is helpful to visualize the tradeoffs between the two. This example creates a lookup table approximation of the function 1-exp(-x) with varying levels of absolute tolerance and creates a plot of each solution found. In the final plot you can view the tradeoffs between memory efficiency and numeric fidelity.

nTol = 32; % Initialize variables
solutions = cell(1,nTol);
objectiveValues = cell(1,nTol);
constraintValues = cell(1,nTol);
memoryUnits = 'bytes';

% Options for absolute tolerance
absTol = 2.^linspace(-12,-4,nTol);

% Relative tolerance is set to 0
relTol = 0;

% Initialize options
options = FunctionApproximation.Options( ...
    'RelTol', relTol, ...
    'BreakpointSpecification', 'EvenSpacing', ...
    'Display', false, ...
    'WordLengths', 16);

% Setup the approximation problem
problem = FunctionApproximation.Problem( ...
    @(x) 1 - exp(-x), ...
    'InputTypes',numerictype(0,16), ...
    'OutputType',numerictype(1,16,14), ...
    'InputLowerBounds',0, ...
    'InputUpperBounds',5, ...
    'Options',options);

% Execute to find solutions with different tolerances
for iTol = 1:nTol
    problem.Options.AbsTol = absTol(iTol);
    solution = solve(problem);
    objectiveValues{iTol} = arrayfun(@(x) x.totalMemoryUsage(memoryUnits), solution.AllSolutions);
    constraintValues{iTol} = arrayfun(@(x) x.Feasible, solution.AllSolutions);
    solutions{iTol} = solution;
end


% Plot results

h = figure();
hold on;

for iTol = 1:nTol
    for iObjective = 1:numel(objectiveValues{iTol})
        if constraintValues{iTol}(iObjective)
            markerColor = 'g';
        else
            markerColor = 'r';
        end
        plot(absTol(iTol),objectiveValues{iTol}(iObjective), ...
            'Marker', '.' ,'LineStyle', 'none', ...
            'MarkerSize', 24, ...
            'MarkerEdgeColor', markerColor)
    end
end

xlabel('AbsTol')
ylabel(['MemoryUsage (',memoryUnits,')'])
h.Children.XScale = 'log';
h.Children.YMinorGrid = 'on';
grid on
box on
hold off;

Solutions that are infeasible, meaning they do not meet the required absolute tolerance are marked in red. Solutions that are feasible are marked in green. As the absolute tolerance increases, the approximation finds solutions which use less memory. When the absolute tolerance is lower, indicating higher numerical fidelity, the required memory also increases.

See Also

Apps

Classes

Related Topics