Main Content

システムの固定小数点データ型の最適化

データ型の最適化は、元のシステム動作を指定された許容誤差内で維持しながら、目的関数 (指定されたシステムにおける生成コードの合計ビット幅、推定される演算子の数など) を最小化しようとします。最適化中に、ソフトウェアは元のモデルのシミュレーションを実行することで、ベースラインを確立します。次に、モデルの固定小数点バージョンを作成して、シミュレーションを実行し、新しいデータ型を使用して動作を決定します。最適化により、指定された動作の制約を満たし、かつ目的関数を最小化するモデルが選択されます。

最適化するシステムを含むモデルには、次の特性が必要です。

  • モデル内のすべてのブロックで固定小数点データ型がサポートされていなければなりません。

  • モデル内のブロックに指定されている設計範囲は、シミュレーション範囲と一致していなければなりません。

  • モデルに MATLAB Function ブロックが含まれる場合、固定小数点変換でサポートされている MATLAB® 言語機能を使用しなければなりません。詳細については、固定小数点の自動変換でサポートされる MATLAB 言語機能を参照してください。

  • モデルには有限のシミュレーションの停止時間が設定されていなければなりません。

最適化プロセスでは、ソフトウェアによっていくつかの設定とモデル コンフィギュレーション パラメーターが変更されます。こうした変更の目的には、診断の非表示、シミュレーション データ インスペクターによるログの有効化、結果によって消費されるメモリの削減、モデルの有効性の確認、最適化プロセスの高速化、データ型オーバーライドの無効化が含まれます。詳細については、データ型最適化によるモデル コンフィギュレーションの変更を参照してください。これらの診断は、最適化の完了後に元に戻すことができます。

データ型の最適化のベスト プラクティス

制約の定義

新しい固定小数点実装の動作が許容可能かどうかを判定するために、最適化には正しく定義された動作の制約が必要です。制約を定義するには、モデル内でfxpOptimizationOptionsオブジェクトのaddToleranceメソッドを使用するか、1 つ以上のModel Verificationブロックを使用します。詳細については、動作の制約の指定を参照してください。

ロックされているデータ型を最小限にする

最適化するシステム内のブロックの [固定小数点ツールによる変更に対してデータ型の設定をロックする] 設定が有効になっている場合、新しい解を求める最適化プロセスの自由度が最低限に抑えられます。

モデル管理および調査

関数 fxpopt は、解と呼ばれる一連の固定小数点実装を含むOptimizationResultオブジェクトを返します。最適化プロセスにより、指定された動作の制約を満たす固定小数点実装が見つかった場合、解はコストで並べ替えられ、最も小さいコスト (ビット幅または演算子の数) の最適解が配列の最初の要素として指定されます。

最適化で動作の制約を満たす固定小数点実装が見つからない場合、解は、ベースライン モデルとの最大の差の絶対値によって、最小の差が最初の要素になるように順序付けられます。

OptimizationResult オブジェクトのexploreメソッドを使用して、見つかった最適解を調べます。また、見つかった他の解も同じように調べることができます。維持する解を選択するまでモデルを保存して閉じないでください。モデルを閉じたり保存したりすると、別の解をさらに調査できなくなります。

固定小数点データ型の最適化

この例では、指定した許容誤差に基づいてシステムで使用されるデータ型を最適化する方法を示します。

最初に、データ型を最適化するシステムを開きます。

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

fxpOptimizationOptions オブジェクトを作成して、設計目標を満たすように制約と許容誤差を定義します。fxpOptimizationOptions オブジェクトの UseParallel プロパティを true に設定して、最適化の反復を並列実行します。AllowableWordLengths プロパティを使用して、設計で許容する語長も指定できます。

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]
             UseParallel: 1

   Advanced Options
         AdvancedOptions: [1×1 struct]

addTolerance メソッドを使用して、システムの元の動作と最適化された固定小数点データ型を使用した動作の差の許容誤差を定義します。

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

関数 fxpopt を使用して最適化を実行します。設計対象のシステム内のオブジェクトの範囲と fxpOptimizationOptions オブジェクトで指定された制約がソフトウェアで解析され、総ビット幅を最小限に抑えながら異種混合のデータ型がシステムに適用されます。

result = fxpopt(model, sud, opt);
Starting parallel pool (parpool) using the 'local' profile ...
Connected to the parallel pool (number of workers: 4).
	+ Preprocessing
	+ Modeling the optimization problem
		- Constructing decision variables
	+ Running the optimization solver
Analyzing and transferring files to the workers ...done.
		- Evaluating new solution: cost 180, does not meet the tolerances.
		- Evaluating new solution: cost 198, does not meet the tolerances.
		- Evaluating new solution: cost 216, does not meet the tolerances.
		- Evaluating new solution: cost 234, does not meet the tolerances.
		- Evaluating new solution: cost 252, does not meet the tolerances.
		- Evaluating new solution: cost 270, does not meet the tolerances.
		- Evaluating new solution: cost 288, does not meet the tolerances.
		- Evaluating new solution: cost 306, meets the tolerances.
		- Evaluating new solution: cost 324, meets the tolerances.
		- Evaluating new solution: cost 342, meets the tolerances.
		- Evaluating new solution: cost 360, meets the tolerances.
		- Evaluating new solution: cost 378, meets the tolerances.
		- Evaluating new solution: cost 396, meets the tolerances.
		- Evaluating new solution: cost 414, meets the tolerances.
		- Evaluating new solution: cost 432, meets the tolerances.
		- Updated best found solution, cost: 306
		- Evaluating new solution: cost 304, meets the tolerances.
		- Evaluating new solution: cost 304, meets the tolerances.
		- Evaluating new solution: cost 301, meets the tolerances.
		- Evaluating new solution: cost 305, does not meet the tolerances.
		- Evaluating new solution: cost 305, meets the tolerances.
		- Evaluating new solution: cost 301, meets the tolerances.
		- Evaluating new solution: cost 299, meets the tolerances.
		- Evaluating new solution: cost 299, meets the tolerances.
		- Evaluating new solution: cost 296, meets the tolerances.
		- Evaluating new solution: cost 299, meets the tolerances.
		- Evaluating new solution: cost 291, meets the tolerances.
		- Evaluating new solution: cost 296, does not meet the tolerances.
		- Evaluating new solution: cost 299, meets the tolerances.
		- Evaluating new solution: cost 300, meets the tolerances.
		- Evaluating new solution: cost 296, does not meet the tolerances.
		- Evaluating new solution: cost 301, meets the tolerances.
		- Evaluating new solution: cost 303, meets the tolerances.
		- Evaluating new solution: cost 299, meets the tolerances.
		- Evaluating new solution: cost 304, does not meet the tolerances.
		- Evaluating new solution: cost 300, meets the tolerances.
		- 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 280, meets the tolerances.
		- Evaluating new solution: cost 287, meets the tolerances.
		- Evaluating new solution: cost 288, does not meet the tolerances.
		- Evaluating new solution: cost 287, does not meet the tolerances.
		- Evaluating new solution: cost 283, meets the tolerances.
		- Evaluating new solution: cost 283, does not meet the tolerances.
		- Evaluating new solution: cost 262, does not meet the tolerances.
		- Evaluating new solution: cost 283, does not meet the tolerances.
		- Evaluating new solution: cost 282, does not meet the tolerances.
		- Evaluating new solution: cost 288, meets the tolerances.
		- Evaluating new solution: cost 289, meets the tolerances.
		- Evaluating new solution: cost 288, meets the tolerances.
		- Evaluating new solution: cost 290, meets the tolerances.
		- Evaluating new solution: cost 281, does not meet the tolerances.
		- Evaluating new solution: cost 286, does not meet the tolerances.
		- Evaluating new solution: cost 287, meets the tolerances.
		- Evaluating new solution: cost 284, meets the tolerances.
		- Evaluating new solution: cost 282, meets the tolerances.
		- Evaluating new solution: cost 285, does not meet the tolerances.
		- Evaluating new solution: cost 277, meets the tolerances.
		- Updated best found solution, cost: 280
		- Updated best found solution, cost: 277
		- Evaluating new solution: cost 272, meets the tolerances.
		- Evaluating new solution: cost 266, meets the tolerances.
		- Evaluating new solution: cost 269, meets the tolerances.
		- Evaluating new solution: cost 271, does not meet the tolerances.
		- Evaluating new solution: cost 274, meets the tolerances.
		- Evaluating new solution: cost 275, meets the tolerances.
		- Evaluating new solution: cost 274, does not meet the tolerances.
		- Evaluating new solution: cost 275, meets the tolerances.
		- Evaluating new solution: cost 276, does not meet the tolerances.
		- Evaluating new solution: cost 271, meets the tolerances.
		- Evaluating new solution: cost 267, meets the tolerances.
		- Evaluating new solution: cost 270, meets the tolerances.
		- Evaluating new solution: cost 272, meets the tolerances.
		- Evaluating new solution: cost 264, does not meet the tolerances.
		- Evaluating new solution: cost 265, does not meet the tolerances.
		- Evaluating new solution: cost 269, meets the tolerances.
		- Evaluating new solution: cost 270, meets the tolerances.
		- Evaluating new solution: cost 269, meets the tolerances.
		- Evaluating new solution: cost 276, meets the tolerances.
		- Evaluating new solution: cost 274, meets the tolerances.
		- Updated best found solution, cost: 272
		- Updated best found solution, cost: 266
	+ Optimization has finished.
		- Neighborhood search complete.
		- Maximum number of iterations completed.
	+ Fixed-point implementation that met the tolerances found.
		- Total cost: 266
		- Maximum absolute difference: 0.087035
		- Use the explore method of the result to explore the implementation.

OptimizationResult オブジェクト resultexplore メソッドでシミュレーション データ インスペクターを起動して設計を調べ、opt オブジェクトで指定された数値許容誤差の範囲内で総ビット数が最小になっていることを確認します。

 explore(result);

OptimizationResult オブジェクトの revert メソッドを使用してモデルを元の状態に戻すことができます。

 revert(result);

参考

関数

クラス

関連するトピック