問題ベースの最適化における自動微分の効果
自動微分を使用すると、問題ベースの solve
関数で必要な関数評価回数が減少する傾向があり、関数がより安定して動作できるようになります。
既定で、solve
は、必要に応じて、自動微分を使用して、目的関数と非線形制約関数の勾配を評価します。自動微分は、fcn2optimexpr
関数を使用しない最適化変数に対する演算として表現された関数に適用されます。詳細については、Optimization Toolbox の自動微分と非線形関数から最適化式への変換を参照してください。
最小化問題
次の目的関数を最小化する問題について考えます。
これらの変数を表す最適化問題と目的関数式を作成します。
prob = optimproblem; x = optimvar('x',2); y = optimvar('y',2); fun1 = 100*(x(2) - x(1)^2)^2 + (1 - x(1))^2; fun2 = exp(-sum((x - y).^2))*exp(-exp(-y(1)))*sech(y(2)); prob.Objective = fun1 - fun2;
最小化には、非線形制約 が適用されます。
prob.Constraints.cons = sum(x.^2 + y.^2) <= 4;
問題の解決と解法プロセスの検証
初期点から始まる問題を解きます。
init.x = [-1;2]; init.y = [1;-1]; [xproblem,fvalproblem,exitflagproblem,outputproblem] = solve(prob,init);
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.
disp(fvalproblem)
-0.5500
disp(outputproblem.funcCount)
77
disp(outputproblem.iterations)
46
output
構造体は、solve
が fmincon
を呼び出すことを示しています。問題を解くには、77 回の関数評価と 46 回の反復が必要です。解での目的関数値は fvalproblem = -0.55
です。
自動微分を使用しない問題の解決
自動微分による効率の向上を確認するには、代わりに、有限差分勾配を使用するように solve
の名前と値のペア引数を設定します。
[xfd,fvalfd,exitflagfd,outputfd] = solve(prob,init,... "ObjectiveDerivative",'finite-differences',"ConstraintDerivative",'finite-differences');
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.
disp(fvalfd)
-0.5500
disp(outputfd.funcCount)
265
disp(outputfd.iterations)
46
有限差分勾配の近似を使用すると、solve
の関数評価が 77 回から 269 回に増えます。反復回数はほぼ同じで、報告される解での目的関数値も同じです。最終解の点は、表示精度に対して同じです。
disp([xproblem.x,xproblem.y])
0.8671 1.0433 0.7505 0.5140
disp([xfd.x,xfd.y])
0.8671 1.0433 0.7505 0.5140
要約すると、最適化における自動微分の主な効果は、関数評価の回数が減少することです。