이 번역 페이지는 최신 내용을 담고 있지 않습니다. 최신 내용을 영문으로 보려면 여기를 클릭하십시오.
문제 기반 접근법에서 추가 파라미터 전달하기
최적화 문제에서 목적 함수 또는 제약 조건 함수는 때때로 독립 변수 외에 추가로 파라미터를 가집니다. 추가 파라미터는 데이터이거나, 최적화 도중에 변경되지 않는 변수를 나타낼 수 있습니다.
문제 기반 접근법에 이러한 파라미터를 포함시키려면 목적 함수 또는 제약 조건 함수에서 작업 공간 변수를 참조하기만 하면 됩니다.
전달된 데이터가 있는 최소제곱 문제
예를 들어 다음과 같이 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. <stopping criteria details>
sol = struct with fields:
x: [400×1 double]
fval = 22.5795
exitflag = OptimalSolution
output = struct with fields:
message: '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>↵↵Optimization completed: The relative dual feasibility, 1.042714e-16,↵is less than options.OptimalityTolerance = 1.000000e-08, the complementarity measure,↵2.491815e-09, is less than options.OptimalityTolerance, and the relative maximum constraint↵violation, 0.000000e+00, is less than options.ConstraintTolerance = 1.000000e-08.'
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. <stopping criteria details>
sol = struct with fields:
x: [2×1 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. <stopping criteria details>
sol = struct with fields:
x: [2×1 double]
fval = -1.0316
Copyright 2018–2020 The MathWorks, Inc.