データ型最適化を使用した演算子の数の最小化
この例では、fxpOptimizationOptions クラスの ObjectiveFunction プロパティを使用して、生成コード内の演算子の数を最小化する方法について説明します。これにより、Simulink® モデルから生成される C コードのプログラム メモリ サイズが削減されます。
モデルを開く
最初に、データ型を最適化するシステムを開きます。
open_system("mFilter2D.slx")

許容誤差と設定の定義

fxpOptimizationOptions オブジェクトを作成して、許容誤差と最適化の設定を定義します。
opt = fxpOptimizationOptions();
addTolerance メソッドを使用して、システムの元の動作と最適化された固定小数点データ型を使用した動作の差の許容誤差を定義します。
tol = 1e-4; opt.addTolerance([model '/output_signal'],1,'AbsTol',tol);
最適化された解の近傍探索を実行するため、詳細オプション PerformNeighborhoodSearch を true に設定します。
opt.AdvancedOptions.PerformNeighborhoodSearch = true;
ObjectiveFunction プロパティを OperatorCount に設定し、最適化によって生成コード内の推定される演算子の数を最小化するよう指示します。
opt.ObjectiveFunction = 'OperatorCount';
関数 fxpopt を使用して最適化を実行します。設計対象のシステム内のオブジェクトの範囲と fxpOptimizationOptions オブジェクトで指定された制約がソフトウェアで解析され、演算子の総数を最小限に抑えながら異種混合のデータ型がシステムに適用されます。
optRes = fxpopt(model,sud,opt);
+ Checking for unsupported constructs. + Preprocessing + Modeling the optimization problem - Constructing decision variables + Running the optimization solver - Evaluating new solution: cost 87, does not meet the tolerances. - Evaluating new solution: cost 87, does not meet the tolerances. - Evaluating new solution: cost 87, does not meet the tolerances. - Evaluating new solution: cost 87, does not meet the tolerances. - Evaluating new solution: cost 87, does not meet the tolerances. - Evaluating new solution: cost 85, does not meet the tolerances. - Evaluating new solution: cost 81, does not meet the tolerances. - Evaluating new solution: cost 83, does not meet the tolerances. - Evaluating new solution: cost 83, does not meet the tolerances. - Evaluating new solution: cost 83, does not meet the tolerances. - Evaluating new solution: cost 83, does not meet the tolerances. - Evaluating new solution: cost 83, does not meet the tolerances. - Evaluating new solution: cost 83, does not meet the tolerances. - Evaluating new solution: cost 83, does not meet the tolerances. - Evaluating new solution: cost 81, does not meet the tolerances. - Evaluating new solution: cost 83, meets the tolerances. - Updated best found solution, cost: 83 - Evaluating new solution: cost 90, meets the tolerances. - Evaluating new solution: cost 83, does not meet the tolerances. - Evaluating new solution: cost 85, does not meet the tolerances. - Evaluating new solution: cost 89, meets the tolerances. - Evaluating new solution: cost 85, does not meet the tolerances. - Evaluating new solution: cost 88, meets the tolerances. - Evaluating new solution: cost 92, meets the tolerances. - Evaluating new solution: cost 90, does not meet the tolerances. - Evaluating new solution: cost 83, does not meet the tolerances. - Evaluating new solution: cost 89, does not meet the tolerances. + Optimization has finished. - Neighborhood search complete. - Reached limit of number of iterations without updates to the current best solution. + Fixed-point implementation that met the tolerances found. - Total cost: 83 - Maximum absolute difference: 0.000091 - Use the explore method of the result to explore the implementation.
OptimizationResult オブジェクト optRes の explore メソッドでシミュレーション データ インスペクターを開いて設計を調べ、opt オブジェクトで指定された数値許容誤差の範囲内で演算子の数が最小になっていることを確認します。
explore(optRes)
OptimizationResult オブジェクト optRes の openSimulationManager メソッドを使用して、シミュレーション マネージャーで最適化中のシミュレーション実行を検査します。
openSimulationManager(optRes)