# Optimize Data Types Using Multiple Simulation Scenarios

This example shows how to create multiple simulation scenarios, and use the scenarios to optimize the fixed-point data types of a system.

Open the model. In this example, you optimize the data types of the Controller subsystem. The model is set up to use either a ramp input, or a random input. The model uses an Assertion block rather than using signal tolerances to verify the numerical behavior of the fixed-point implementation. For more information, see Specify Behavioral Constraints.

```model = 'ex_controllerHarness'; open_system(model); ``` ### Create the Simulation Scenarios

Create a `Simulink.SimulationInput` object that contains the different scenarios. Use both the ramp input as well as four different seeds for the random input.

```si = Simulink.SimulationInput.empty(5, 0); % scan through 4 different seeds for the random input rng(1); seeds = randi(1e6, [1 4]); for sIndex = 1:length(seeds) si(sIndex) = Simulink.SimulationInput(model); si(sIndex) = si(sIndex).setVariable('SOURCE', 2); % SOURCE == 2 corresponds to the random input si(sIndex) = si(sIndex).setBlockParameter([model '/Random/uniformRandom'], 'Seed', num2str(seeds(sIndex))); % scan through the seeds si(sIndex) = si(sIndex).setUserString(sprintf('random_%i', seeds(sIndex))); end % setting SOURCE == 1 corresponds to the ramp input si(5) = Simulink.SimulationInput(model); si(5) = si(5).setVariable('SOURCE', 1); si(5) = si(5).setUserString('Ramp'); ```

### Specify Fixed-Point Optimization Options

To specify options for the optimization, such as the number of iterations and method for range collection, use the `fxpOptimizationOptions` object. This example uses derived range analysis to collect ranges for the system.

```options = fxpOptimizationOptions('MaxIterations', 3e2, 'Patience', 50); options.AdvancedOptions.PerformNeighborhoodSearch = false; % use derived range analysis for range collection options.AdvancedOptions.UseDerivedRangeAnalysis = true ```
```options = fxpOptimizationOptions with properties: MaxIterations: 300 MaxTime: 600 Patience: 50 Verbosity: High AllowableWordLengths: [2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ... ] ObjectiveFunction: BitWidthSum UseParallel: 0 Advanced Options AdvancedOptions: [1x1 DataTypeOptimization.AdvancedFxpOptimizationOptions] ```

Specify the simulation input objects as simulation scenarios in the advanced options.

```options.AdvancedOptions.SimulationScenarios = si; ```

### Run Optimization and Explore the Results

During the optimization, the software derives ranges for all simulation scenarios specified in the advanced options. The software verifies solutions against each simulation input scenario.

```result = fxpopt(model, [model '/Controller'], options) ```
``` + Starting data type optimization... + Checking for unsupported constructs. + Preprocessing + Modeling the optimization problem - Constructing decision variables + Running the optimization solver - Evaluating new solution: cost 496, does not meet the behavioral constraints. - Evaluating new solution: cost 976, does not meet the behavioral constraints. - Evaluating new solution: cost 1936, meets the behavioral constraints. - Updated best found solution, cost: 1936 + Optimization has finished. + Fixed-point implementation that satisfies the behavioral constraints found. The best found solution is applied on the model. - Total cost: 1936 - Use the explore method of the result to explore the implementation. result = OptimizationResult with properties: Model: 'ex_controllerHarness' SystemUnderDesign: 'ex_controllerHarness/Controller' FinalOutcome: 'Fixed-point implementation that satisfies the behavioral constraints found. The best found solution is applied on the model.' OptimizationOptions: [1x1 fxpOptimizationOptions] Solutions: [1x1 DataTypeOptimization.OptimizationSolution] ```

You can explore each solution as it compares to each simulation scenario you defined. Explore the best found solution and view it with the ramp simulation input. The ramp input is simulation scenario five.

```solutionIndex = 1; % get the best found solution scenarioIndex = 5; % get the 5th scenario (ramp) solution = explore(result, solutionIndex, scenarioIndex); ```
{"ft_lang_label":"__label__en","ft_lang_prob":0.6522238,"math_prob":0.9660799,"size":4170,"snap":"2022-27-2022-33","text_gpt3_token_len":930,"char_repetition_ratio":0.16130581,"word_repetition_ratio":0.0726257,"special_character_ratio":0.21726619,"punctuation_ratio":0.17014925,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98255897,"pos_list":[0,1,2],"im_url_duplicate_count":[null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-28T10:28:36Z\",\"WARC-Record-ID\":\"<urn:uuid:a534d5b2-e22f-460b-891f-6f56f14280bf>\",\"Content-Length\":\"79595\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a8b94de5-b295-490d-b0d8-62c1d1a0be0c>\",\"WARC-Concurrent-To\":\"<urn:uuid:9b6f431b-8a5a-4534-af33-214a67af82a5>\",\"WARC-IP-Address\":\"23.56.192.82\",\"WARC-Target-URI\":\"https://au.mathworks.com/help/fixedpoint/ug/optimize-data-types-using-multiple-simulation-scenarios.html\",\"WARC-Payload-Digest\":\"sha1:TKUSAFGNBZFN3PYDZAWWWHDTCW7EZ3UT\",\"WARC-Block-Digest\":\"sha1:LCSJSJX74DFOOWBUNRDEDJVERICF6RLF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103360935.27_warc_CC-MAIN-20220628081102-20220628111102-00443.warc.gz\"}"}