주요 콘텐츠

이 번역 페이지는 최신 내용을 담고 있지 않습니다. 최신 내용을 영문으로 보려면 여기를 클릭하십시오.

quadprog에 대한 코드 생성하기

quadprog 코드 생성의 첫 단계

이 예제에서는 quadprog 최적화 솔버에 대한 코드를 생성하는 방법을 보여줍니다. 코드를 생성하려면 MATLAB® Coder™ 라이선스가 필요합니다. 코드 생성 요구 사항에 대한 자세한 내용은 quadprog에 대한 코드 생성을 위한 배경 정보 항목을 참조하십시오.

다음 2차 표현식을 최소화하는 문제입니다.

12xTHx+fTx

여기서

H=[111122124]

그리고,

f=[231]

여기에는 제약 조건 0x1, x=1/2이 적용됩니다.

문제와 제약 조건을 만드는 코드를 포함하는 test_quadp.m이라는 파일을 만듭니다. 이 파일은 "active-set" 알고리즘을 사용하기 위한 옵션을 설정해야 합니다. 또한, 생성된 코드와 동일한 코드를 사용해 MATLAB에서 결과를 확인할 수 있도록 UseCodegenSolver 옵션을 true로 설정합니다.

function [x,fval] = test_quadp
H = [1,-1,1
    -1,2,-2
    1,-2,4];
f = [2;-3;1];
lb = zeros(3,1);
ub = ones(size(lb));
Aeq = ones(1,3);
beq = 1/2;
x0 = zeros(3,1);
opts = optimoptions("quadprog",...
    Algorithm="active-set",UseCodegenSolver=true);
[x,fval] = quadprog(H,f,[],[],Aeq,beq,lb,ub,x0,opts);
end

test_quadp 파일에 대한 코드를 생성합니다.

codegen -config:mex test_quadp

약간의 시간이 지나면, codegentest_quadp_mex.mexw64라는 이름의 MEX 파일을 만듭니다(파일 확장자는 시스템에 따라 다름). 결과로 생성된 C 코드를 실행합니다.

[x,fval] = test_quadp_mex
x =

         0
    0.5000
         0


fval =

   -1.2500

효율성을 위해 예제 수정하기

Optimization Code Generation for Real-Time Applications 항목에 나와 있는 몇 가지 제안 사항에 따라, 검사 횟수를 줄이고 정적 메모리 할당을 사용하도록 생성된 코드를 구성하십시오.

cfg = coder.config("mex");
cfg.IntegrityChecks = false;
cfg.SaturateOnIntegerOverflow = false;
cfg.EnableDynamicMemoryAllocation = "Off";

다음 코드를 포함하는 test_quadp2.m이라는 이름의 파일을 만듭니다. 이 코드는 디폴트 값 1e-8보다 느슨한 최적성 허용오차를 설정합니다.

function [x,fval,eflag,output] = test_quadp2
H = [1,-1,1
    -1,2,-2
    1,-2,4];
f = [2;-3;1];
lb = zeros(3,1);
ub = ones(size(lb));
Aeq = ones(1,3);
beq = 1/2;
x0 = zeros(3,1);
opts = optimoptions("quadprog",...
    Algorithm="active-set",UseCodegenSolver=true,...
    OptimalityTolerance=1e-5);
[x,fval,eflag,output] = quadprog(H,f,[],[],Aeq,beq,lb,ub,x0,opts);
end

test_quadp2 파일에 대한 코드를 생성합니다.

codegen -config cfg test_quadp2
Code generation successful.

결과로 생성된 코드를 실행합니다.

[x,fval,eflag,output] = test_quadp2_mex
x =

         0
    0.5000
         0


fval =

   -1.2500


eflag =

     1


output = 

  struct with fields:

          algorithm: 'active-set'
      firstorderopt: 8.8818e-16
    constrviolation: 0
         iterations: 3

'active-set' 알고리즘은 중지하는 점에 도달할 때까지 최적성 허용오차를 검사하지 않기 때문에 이 허용오차를 변경해도 최적화 과정에 영향을 미치지 않습니다.

허용되는 반복 횟수를 2로 제한하는 세 번째 파일을 만들어 최적화 과정에 미치는 영향을 확인합니다.

function [x,fval,exitflag,output] = test_quadp3
H = [1,-1,1
    -1,2,-2
    1,-2,4];
f = [2;-3;1];
lb = zeros(3,1);
ub = ones(size(lb));
Aeq = ones(1,3);
beq = 1/2;
x0 = zeros(3,1);
opts = optimoptions("quadprog",...
    Algorithm="active-set",UseCodegenSolver=true,...
    MaxIterations=2);
[x,fval,exitflag,output] = quadprog(H,f,[],[],Aeq,beq,lb,ub,x0,opts)

이러한 설정이 솔버에 미치는 영향을 확인하기 위해 코드 생성 없이 MATLAB에서 test_quadp3을 실행합니다.

[x,fval,exitflag,output] = test_quadp3
Solver stopped prematurely.

quadprog stopped because it exceeded the iteration limit,
options.MaxIterations = 2.000000e+00.


x =

         0
    0.5000
   -0.0000


fval =

   -1.2500


exitflag =

     0


output = 

  struct with fields:

          algorithm: 'active-set'
      firstorderopt: 2.0000
    constrviolation: 1.1102e-16
         iterations: 2
            message: 'Solver stopped prematurely.↵↵quadprog stopped because it exceeded the iteration limit,↵options.MaxIterations = 2.000000e+00.'

이 경우, 솔버는 디폴트 값보다 적은 수의 스탭을 거쳐 해에 도달했습니다. 하지만 반복 횟수를 제한하면 일반적으로 솔버가 올바른 해에 도달할 수 없습니다.

참고 항목

| (MATLAB Coder) |

도움말 항목