Main Content

問題ベースのアプローチでの追加パラメーターの受け渡し

最適化問題では、目的関数または制約関数は独立変数のほかにパラメーターをもつことがあります。追加パラメーターではデータを使用したり、最適化中に変化しない変数を表したりできます。

問題ベースのアプローチでこれらのパラメーターを含めるには、単に目的関数または制約関数のワークスペース変数を参照します。

渡されたデータを使用した最小二乗問題

たとえば、particle.mat ファイルに行列 C および d があり、これらの行列が問題のデータを表しているとします。データをワークスペースに読み込みます。

load particle

行列のサイズを表示します。

disp(size(C))
        2000         400
disp(size(d))
        2000           1

ベクトル C*x の形成に適切なサイズの最適化変数 x を作成します。

x = optimvar('x',size(C,2));

x が非負であるという制約の下で C*x – d の各項の二乗和を最小化する最適化問題を作成します。

x.LowerBound = 0;
prob = optimproblem;
expr = sum((C*x - d).^2);
prob.Objective = expr;

目的関数の式でデータ C および d を参照するだけで、これらのデータを問題に含めることができます。問題を解きます。

[sol,fval,exitflag,output] = solve(prob)
Solving problem using lsqlin.

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.
sol = struct with fields:
    x: [400x1 double]

fval = 22.5795
exitflag = 
    OptimalSolution

output = struct with fields:
            message: 'Minimum found that satisfies the constraints....'
          algorithm: 'interior-point'
      firstorderopt: 9.9673e-07
    constrviolation: 0
         iterations: 9
       linearsolver: 'sparse'
       cgiterations: []
             solver: 'lsqlin'

追加パラメーターを使用した非線形問題

非線形問題に同じアプローチを使用します。たとえば、複数の変数の目的関数があり、これらの変数の一部が最適化の固定データであるとします。

type parameterfun
function y = parameterfun(x,a,b,c) 
y = (a - b*x(1)^2 + x(1)^4/3)*x(1)^2 + x(1)*x(2) + (-c + c*x(2)^2)*x(2)^2;

この目的関数では、x は 2 要素ベクトル、ab、および c はスカラー パラメーターです。最適化変数を作成し、ワークスペースでパラメーター値を割り当てます。

a = 4;
b = 2.1;
c = 4;
x = optimvar('x',2);

最適化問題を作成します。この目的関数は x の有理関数であるため、目的を最適化変数として指定できます。点 x0.x = [1/2;1/2] から始めて問題を解きます。

prob = optimproblem;
prob.Objective = parameterfun(x,a,b,c);
x0.x = [1/2;1/2];
[sol,fval] = solve(prob,x0)
Solving problem using fminunc.

Local minimum found.

Optimization completed because the size of the gradient is less than
the value of the optimality tolerance.
sol = struct with fields:
    x: [2x1 double]

fval = -1.0316

parameterfun がサポート対象関数で構成されていない場合は、parameterfun を最適化式に変換して、その式を目的として設定します。詳細については、最適化変数および式でサポートされる演算非線形関数から最適化式への変換を参照してください。

expr = fcn2optimexpr(@parameterfun,x,a,b,c);
prob.Objective = expr;
[sol,fval] = solve(prob,x0)
Solving problem using fminunc.

Local minimum found.

Optimization completed because the size of the gradient is less than
the value of the optimality tolerance.
sol = struct with fields:
    x: [2x1 double]

fval = -1.0316

Copyright 2018–2020 The MathWorks, Inc.

参考

関連するトピック