热启动 quadprog
此示例显示了热启动对象如何提高大型密集二次问题的解速度。创建一个具有 N
变量和 10N
线性不等式约束的缩放问题。将 N
设置为 1000。
rng default % For reproducibility N = 1000; rng default A = randn([10*N,N]); b = 5*ones(size(A,1),1); f = sqrt(N)*rand(N,1); H = (4+N/10)*eye(N) + randn(N); H = H + H'; Aeq = []; beq = []; lb = -ones(N,1); ub = -lb;
为 quadprog
创建一个热启动对象,从零开始。
opts = optimoptions('quadprog','Algorithm','active-set'); x0 = zeros(N,1); ws = optimwarmstart(x0,opts);
求解问题并计算结果。
tic [ws1,fval1,eflag1,output1,lambda1] = quadprog(H,f,A,b,Aeq,beq,lb,ub,ws);
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>
toc
Elapsed time is 9.221035 seconds.
该解具有几个有效的线性不等式约束,并且没有有效的边界。
nnz(lambda1.ineqlin)
ans = 211
nnz(lambda1.lower)
ans = 0
nnz(lambda1.upper)
ans = 0
求解器需要几百次迭代才能收敛。
output1.iterations
ans = 216
将一个随机目标更改为其原始值的两倍。
idx = randi(N); f(idx) = 2*f(idx);
从之前的热启动解开始,求解具有新目标的问题。
tic [ws2,fval2,eflag2,output2,lambda2] = quadprog(H,f,A,b,Aeq,beq,lb,ub,ws1);
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>
toc
Elapsed time is 1.490214 seconds.
求解器花费更少的时间来求解新问题。
新的解具有大约相同数量的主动约束。
nnz(lambda2.ineqlin)
ans = 214
nnz(lambda2.lower)
ans = 0
nnz(lambda2.upper)
ans = 0
新的解与之前的解很接近。
norm(ws2.X - ws1.X)
ans = 0.0987
norm(ws2.X)
ans = 2.4229
速度的差异主要是由于求解器进行的迭代次数少得多。
output2.iterations
ans = 29