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
2 つのアプローチを使用した問題の求解
この例は、問題ベースのアプローチまたはソルバーベースのアプローチを使用して同じ情報の大部分を取得する方法を示します。まず、問題を作成し、問題ベースのアプローチを使用して解きます。
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.
次に、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.
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
とは違い、ラグランジュ乗数などの引数を多く返せるからです。
入力引数
prob
— 最適化問題または方程式問題
OptimizationProblem
オブジェクト | EquationProblem
オブジェクト
最適化問題または方程式問題。OptimizationProblem
オブジェクトまたは EquationProblem
オブジェクトとして指定します。最適化問題は optimproblem
を使用して作成し、方程式問題は eqnproblem
を使用して作成します。
警告
問題ベースのアプローチでは、目的関数、非線形等式、または非線形不等式における複素数値をサポートしていません。関数の計算に複素数値が含まれていると、それが中間値としてであっても、最終結果が不正確になる場合があります。
例: prob = optimproblem; prob.Objective = obj; prob.Constraints.cons1 = cons1;
例: prob = eqnproblem; prob.Equations = eqs;
varname
— 変数名
文字ベクトル | string
変数名。文字ベクトルまたは string として指定します。
例: 'x'
データ型: char
| string
出力引数
idx
— 問題変数の線形インデックス
構造体 | 整数ベクトル
問題変数の線形インデックス。構造体または整数ベクトルとして返されます。prob2struct
を使用して prob
を問題構造体に変換すると、idx
は結果として得られる問題構造体に変数インデックスを指定します。この問題構造体は prob
の変数に対応します。
idx = varindex(prob)
を呼び出す場合、返されるidx
は構造体です。構造体のフィールド名はprob
の変数名です。各フィールドの値は、関連付けられたソルバーベースの問題変数で変数によるマッピングが行われる線形インデックスの整数ベクトルです。idx = varindex(prob,varname)
を呼び出す場合、返されるidx
は、関連付けられたソルバーベースの問題変数で変数varname
によるマッピングが行われる線形インデックスのベクトルです。
詳細は、問題インデックスの取得を参照してください。
バージョン履歴
R2019a で導入
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)