Main Content

문제 기반 접근법에서 추가 파라미터 전달하기

최적화 문제에서 목적 함수 또는 제약 조건 함수는 때때로 독립 변수 외에 추가로 파라미터를 가집니다. 추가 파라미터는 데이터이거나, 최적화 도중에 변경되지 않는 변수를 나타낼 수 있습니다.

문제 기반 접근법에 이러한 파라미터를 포함시키려면 목적 함수 또는 제약 조건 함수에서 작업 공간 변수를 참조하기만 하면 됩니다.

전달된 데이터가 있는 최소제곱 문제

예를 들어 다음과 같이 particle.mat 파일에 행렬 Cd가 있으며 이러한 행렬은 문제에 대한 데이터를 나타낸다고 가정하겠습니다. 데이터를 작업 공간으로 불러옵니다.

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;

문제에 데이터 Cd를 포함시키려면 목적 함수 표현식에서 이들을 참조하기만 하면 됩니다. 문제를 풉니다.

[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개 가진 벡터이고 a, b, 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을 최적화 표현식으로 변환하고 이 변환된 표현식을 목적 함수로 설정할 수 있습니다. Supported Operations for Optimization Variables and Expressions 항목과 Convert Nonlinear Function to Optimization Expression 항목을 참조하십시오.

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.

참고 항목

관련 항목