varindex
ソルバーベースの変数インデックスへの問題変数のマッピング
説明
例
最適化問題を作成します。
x = optimvar('x',3); y = optimvar('y',3,3); prob = optimproblem('Objective',x'*y*x);
問題を構造体に変換します。
problem = prob2struct(prob);
すべての prob 変数の problem で線形インデックスを取得します。
idx = varindex(prob); disp(idx.x)
1 2 3
disp(idx.y)
4 5 6 7 8 9 10 11 12
y インデックスのみを取得します。
idxy = varindex(prob,'y')idxy = 1×9
4 5 6 7 8 9 10 11 12
この例は、問題ベースのアプローチまたはソルバーベースのアプローチを使用して同じ情報の大部分を取得する方法を示します。まず、問題を作成し、問題ベースのアプローチを使用して解きます。
x = optimvar('x',3,1,'LowerBound',1,'UpperBound',1); y = optimvar('y',3,3,'LowerBound',-1,'UpperBound',1); prob = optimproblem('Objective',x'*y*x + [2 3 4]*x); rng default x0.x = rand(3, 1); x0.y = rand(3, 3); [solp,fvalp,exitflagp,outputp] = solve(prob,x0);
Solving problem using fmincon. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance. <stopping criteria details>
次に、prob2struct を使用してこの問題をソルバーベース形式に変換します。fmincon ソルバーが問題で自動勾配を使用するようにするには、SpecifyObjectiveGradient オプションを true に設定します。
solverprob = prob2struct(prob,x0);
solverprob.options = optimoptions(solverprob.options,"SpecifyObjectiveGradient",true);fmincon を使用して、問題を解きます。
[sols,fvals,exitflags,outputs] = fmincon(solverprob);
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance. <stopping criteria details>
fmincon の解を、solve によって返された構造体形式に変換するには、varindex を使用して適切な構造体を作成します。
idx = varindex(prob); sol.x = sols(idx.x); sol.y = sols(idx.y);
varindex が使用する y インデックスは線形インデックスです。サイズが x0.y になるように変数 sol.y を形状変更します。
sol.y = reshape(sol.y,size(x0.y));
2 つの解の構造体が完全に一致しているか確認します。
isequal(sol,solp)
ans = logical
1
2 つのアプローチが完全に同等ではないのは、fmincon は solve とは違い、ラグランジュ乗数などの引数を多く返せるからです。
入力引数
最適化問題または方程式問題。OptimizationProblem オブジェクトまたは EquationProblem オブジェクトとして指定します。最適化問題は optimproblem を使用して作成し、方程式問題は eqnproblem を使用して作成します。
警告
問題ベースのアプローチでは、目的関数、非線形等式、および非線形不等式における複素数値はサポートされていません。関数の計算に複素数値が含まれていると、それが中間値としてであっても、最終結果が不正確になる場合があります。
例: prob = optimproblem; prob.Objective = obj; prob.Constraints.cons1 = cons1;
例: prob = eqnproblem; prob.Equations = eqs;
変数名。文字ベクトルまたは string として指定します。
例: 'x'
データ型: char | string
出力引数
問題変数の線形インデックス。構造体または整数ベクトルとして返されます。prob2struct を使用して prob を問題構造体に変換すると、idx は結果として得られる問題構造体に変数インデックスを指定します。この問題構造体は prob の変数に対応します。
idx = varindex(prob)を呼び出す場合、返されるidxは構造体です。構造体のフィールド名はprobの変数名です。各フィールドの値は、関連付けられたソルバーベースの問題変数で変数によるマッピングが行われる線形インデックスの整数ベクトルです。idx = varindex(prob,varname)を呼び出す場合、返されるidxは、関連付けられたソルバーベースの問題変数で変数varnameによるマッピングが行われる線形インデックスのベクトルです。
問題インデックスの取得を参照してください。
バージョン履歴
R2019a で導入
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)