## Optimize Fixed-Point Data Types for a System

Data type optimization seeks to minimize an objective function, such as the total bit-width or an estimated count of operators in generated code for a specified system, while maintaining original system behavior within a specified tolerance. During the optimization, the software establishes a baseline by simulating the original model. It then constructs different fixed-point versions of your model and runs simulations to determine the behavior using the new data types. The optimization selects the model that minimizes the objective function while also meeting the specified behavioral constraints.

The model containing the system you want to optimize must have the following characteristics:

All blocks in the model must support fixed-point data types.

The design ranges specified on blocks in the model must be consistent with the simulation ranges.

If the model contains a MATLAB Function block, it must use MATLAB

^{®}language features supported for fixed-point conversion. For more information, see MATLAB Language Features Supported for Automated Fixed-Point Conversion.The model must have finite simulation stop time.

During the optimization process, the software makes changes to several settings and model configuration parameters. These purpose of these changes include suppressing diagnostics, enabling logging with the Simulation Data Inspector, reducing the memory consumed by the result, ensuring validity of the model, accelerating the optimization process, and turning off data type override. For more information, see Model Configuration Changes Made During Data Type Optimization. You can restore these diagnostics after the optimization is complete.

### Best Practices for Optimizing Data Types

#### Define Constraints

To determine if the behavior of a new fixed-point implementation is acceptable, the
optimization requires well-defined behavioral constraints. To define a constraint, use the
`addTolerance`

method of the
`fxpOptimizationOptions`

object, or use one
or more Model Verification blocks in your model. For more
information, see Specify Behavioral Constraints.

#### Minimize Locked Data Types

When the **Lock data types against changes by the fixed-point tools**
setting of a block within the system you want to optimize is enabled, it minimizes the
freedom of the optimization process to find new solutions.

### Model Management and Exploration

The `fxpopt`

function returns an `OptimizationResult`

object containing a
series of fixed-point implementations called solutions. If the optimization process finds a
fixed-point implementation that meets the specified behavioral constraints, the solutions
are sorted by cost, giving the best solution with the smallest cost (bit width or operator
counts) as the first element of the array.

In cases where the optimization is not able to find a fixed-point implementation meeting the behavioral constraints, the solutions are ordered by maximum absolute difference from the baseline model, with the smallest difference as the first element.

Explore the best found solution using the `explore`

method of the
`OptimizationResult`

object. You can also explore any of the other found
solutions in the same manner. Do not save and close the model until you select the solution
you want to keep. Closing or saving the model inhibits further exploration of different
solutions.

### Optimize Fixed-Point Data Types

This example shows how to optimize the data types used by a system based on specified tolerances.

To begin, open the system for which you want to optimize the data types.

model = 'ex_auto_gain_controller'; sud = 'ex_auto_gain_controller/sud'; open_system(model)

Create an `fxpOptimizationOptions`

object to define constraints and tolerances to meet your design goals. Set the `UseParallel`

property of the `fxpOptimizationOptions`

object to `true`

to run iterations of the optimization in parallel. You can also specify word lengths to allow in your design through the `AllowableWordLengths`

property.

opt = fxpOptimizationOptions('AllowableWordLengths', 10:24, 'UseParallel', true)

opt = fxpOptimizationOptions with properties: MaxIterations: 50 MaxTime: 600 Patience: 10 Verbosity: High AllowableWordLengths: [10 11 12 13 14 15 16 17 18 19 20 21 22 23 24] ObjectiveFunction: BitWidthSum UseParallel: 1 Advanced Options AdvancedOptions: [1×1 DataTypeOptimization.AdvancedFxpOptimizationOptions]

Use the `addTolerance`

method to define tolerances for the differences between the original behavior of the system, and the behavior using the optimized fixed-point data types.

tol = 10e-2; addTolerance(opt, [model '/output_signal'], 1, 'AbsTol', tol);

Use the `fxpopt`

function to run the optimization. The software analyzes ranges of objects in your system under design and the constraints specified in the `fxpOptimizationOptions`

object to apply heterogeneous data types to your system while minimizing total bit width.

result = fxpopt(model, sud, opt);

+ Starting data type optimization... + Checking for unsupported constructs. Starting parallel pool (parpool) using the 'Processes' profile ... 25-Apr-2024 11:22:37: Job Queued. Waiting for parallel pool job with ID 1 to start ... 25-Apr-2024 11:23:38: Job Queued. Waiting for parallel pool job with ID 1 to start ... Connected to parallel pool with 8 workers. + Preprocessing + Modeling the optimization problem - Constructing decision variables + Running the optimization solver - Evaluating new solution: cost 180, does not meet the behavioral constraints. - Evaluating new solution: cost 198, does not meet the behavioral constraints. - Evaluating new solution: cost 216, does not meet the behavioral constraints. - Evaluating new solution: cost 234, does not meet the behavioral constraints. - Evaluating new solution: cost 252, does not meet the behavioral constraints. - Evaluating new solution: cost 270, does not meet the behavioral constraints. - Evaluating new solution: cost 288, does not meet the behavioral constraints. - Evaluating new solution: cost 306, meets the behavioral constraints. - Evaluating new solution: cost 324, meets the behavioral constraints. - Evaluating new solution: cost 342, meets the behavioral constraints. - Evaluating new solution: cost 360, meets the behavioral constraints. - Evaluating new solution: cost 378, meets the behavioral constraints. - Evaluating new solution: cost 396, meets the behavioral constraints. - Evaluating new solution: cost 414, meets the behavioral constraints. - Evaluating new solution: cost 432, meets the behavioral constraints. - Updated best found solution, cost: 306 - Evaluating new solution: cost 304, meets the behavioral constraints. - Evaluating new solution: cost 304, meets the behavioral constraints. - Evaluating new solution: cost 301, meets the behavioral constraints. - Evaluating new solution: cost 305, does not meet the behavioral constraints. - Evaluating new solution: cost 305, meets the behavioral constraints. - Evaluating new solution: cost 301, meets the behavioral constraints. - Evaluating new solution: cost 299, meets the behavioral constraints. - Evaluating new solution: cost 299, meets the behavioral constraints. - Evaluating new solution: cost 296, meets the behavioral constraints. - Evaluating new solution: cost 299, meets the behavioral constraints. - Evaluating new solution: cost 291, meets the behavioral constraints. - Evaluating new solution: cost 296, does not meet the behavioral constraints. - Evaluating new solution: cost 299, meets the behavioral constraints. - Evaluating new solution: cost 300, meets the behavioral constraints. - Evaluating new solution: cost 296, does not meet the behavioral constraints. - Evaluating new solution: cost 301, meets the behavioral constraints. - Evaluating new solution: cost 303, meets the behavioral constraints. - Evaluating new solution: cost 299, meets the behavioral constraints. - Evaluating new solution: cost 304, does not meet the behavioral constraints. - Evaluating new solution: cost 300, meets the behavioral constraints. - Evaluating new solution: cost 295, meets the behavioral constraints. - Evaluating new solution: cost 302, meets the behavioral constraints. - Evaluating new solution: cost 303, does not meet the behavioral constraints. - Evaluating new solution: cost 302, does not meet the behavioral constraints. - Evaluating new solution: cost 298, meets the behavioral constraints. - Evaluating new solution: cost 298, does not meet the behavioral constraints. - Evaluating new solution: cost 277, does not meet the behavioral constraints. - Evaluating new solution: cost 298, does not meet the behavioral constraints. - Evaluating new solution: cost 297, does not meet the behavioral constraints. - Evaluating new solution: cost 303, meets the behavioral constraints. - Evaluating new solution: cost 304, meets the behavioral constraints. - Evaluating new solution: cost 303, meets the behavioral constraints. - Evaluating new solution: cost 305, meets the behavioral constraints. - Evaluating new solution: cost 296, does not meet the behavioral constraints. - Evaluating new solution: cost 301, does not meet the behavioral constraints. - Evaluating new solution: cost 302, meets the behavioral constraints. - Evaluating new solution: cost 299, meets the behavioral constraints. - Evaluating new solution: cost 297, meets the behavioral constraints. - Evaluating new solution: cost 300, does not meet the behavioral constraints. - Evaluating new solution: cost 292, meets the behavioral constraints. - Updated best found solution, cost: 304 - Updated best found solution, cost: 301 - Updated best found solution, cost: 299 - Updated best found solution, cost: 296 - Updated best found solution, cost: 291 - Evaluating new solution: cost 286, meets the behavioral constraints. - Evaluating new solution: cost 276, meets the behavioral constraints. - Evaluating new solution: cost 283, meets the behavioral constraints. - Evaluating new solution: cost 285, does not meet the behavioral constraints. - Evaluating new solution: cost 288, meets the behavioral constraints. - Evaluating new solution: cost 289, meets the behavioral constraints. - Evaluating new solution: cost 288, does not meet the behavioral constraints. - Evaluating new solution: cost 289, meets the behavioral constraints. - Evaluating new solution: cost 290, does not meet the behavioral constraints. - Evaluating new solution: cost 285, meets the behavioral constraints. - Evaluating new solution: cost 281, meets the behavioral constraints. - Evaluating new solution: cost 284, meets the behavioral constraints. - Evaluating new solution: cost 286, meets the behavioral constraints. - Evaluating new solution: cost 278, does not meet the behavioral constraints. - Evaluating new solution: cost 279, does not meet the behavioral constraints. - Evaluating new solution: cost 283, meets the behavioral constraints. - Evaluating new solution: cost 284, meets the behavioral constraints. - Evaluating new solution: cost 283, meets the behavioral constraints. - Evaluating new solution: cost 290, meets the behavioral constraints. - Evaluating new solution: cost 288, meets the behavioral constraints. - Evaluating new solution: cost 283, meets the behavioral constraints. - Evaluating new solution: cost 289, meets the behavioral constraints. - Evaluating new solution: cost 286, meets the behavioral constraints. - Evaluating new solution: cost 288, meets the behavioral constraints. - Evaluating new solution: cost 283, meets the behavioral constraints. - Evaluating new solution: cost 284, meets the behavioral constraints. - Evaluating new solution: cost 282, meets the behavioral constraints. - Evaluating new solution: cost 284, meets the behavioral constraints. - Evaluating new solution: cost 277, does not meet the behavioral constraints. - Evaluating new solution: cost 286, meets the behavioral constraints. - Evaluating new solution: cost 278, does not meet the behavioral constraints. - Evaluating new solution: cost 277, meets the behavioral constraints. - Evaluating new solution: cost 281, meets the behavioral constraints. - Evaluating new solution: cost 290, meets the behavioral constraints. - Evaluating new solution: cost 288, meets the behavioral constraints. - Evaluating new solution: cost 288, meets the behavioral constraints. - Evaluating new solution: cost 287, does not meet the behavioral constraints. - Evaluating new solution: cost 282, meets the behavioral constraints. - Evaluating new solution: cost 286, does not meet the behavioral constraints. - Evaluating new solution: cost 277, meets the behavioral constraints. - Updated best found solution, cost: 286 - Updated best found solution, cost: 276 + Optimization has finished. - Neighborhood search complete. - Maximum number of iterations completed. + Fixed-point implementation that satisfies the behavioral constraints found. The best found solution is applied on the model. - Total cost: 276 - Maximum absolute difference: 0.077478 - Use the explore method of the result to explore the implementation.

Use the `explore`

method of the `OptimizationResult`

object, `result`

, to start the Simulation Data Inspector and explore the design containing the smallest total number of bits while maintaining the numeric tolerances specified in the `opt`

object.

explore(result);

You can revert your model back to its original state using the `revert`

method of the `OptimizationResult`

object.

revert(result);