基于问题的非负线性最小二乘法
此示例说明如何使用几种算法来求解线性最小二乘问题,并以解为非负的约束为条件。线性最小二乘问题的形式为
.
在这种情况下,将解限制为非负数,。
首先,将数组 和 加载到您的工作区中。
load particle查看每个数组的大小。
sizec = size(C)
sizec = 1×2
2000 400
sized = size(d)
sized = 1×2
2000 1
创建一个适当大小的优化变量 x,用于与 C 相乘。对 0 元素施加 x 的下界。
x = optimvar('x',sizec(2),'LowerBound',0);
创建目标函数表达式。
residual = C*x - d; obj = sum(residual.^2);
创建一个名为 nonneglsq 的优化问题,并将目标函数包含在问题中。
nonneglsq = optimproblem('Objective',obj);找到该问题的默认求解器。
opts = optimoptions(nonneglsq)
opts =
lsqlin options:
Options used by current Algorithm ('interior-point'):
(Other available algorithms: 'active-set', 'trust-region-reflective')
Set properties:
No properties.
Default properties:
Algorithm: 'interior-point'
ConstraintTolerance: 1.0000e-08
Display: 'final'
LinearSolver: 'auto'
MaxIterations: 200
OptimalityTolerance: 1.0000e-08
StepTolerance: 1.0000e-12
Show options not used by current Algorithm ('interior-point')
使用默认求解器求解该问题。
[sol,fval,exitflag,output] = solve(nonneglsq);
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>
要查看优化过程的详细信息,请检查输出结构体。
disp(output)
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'
输出结构体显示,lsqlin 求解器使用稀疏内部线性求解器进行内点算法,并经过 9 次迭代才达到约 1e-6 的一阶最优性测度。
更改算法
信赖区域反射算法处理边界约束问题。看看它在求解这个问题上表现如何。
opts.Algorithm = 'trust-region-reflective'; [sol2,fval2,exitflag2,output2] = solve(nonneglsq,'Options',opts);
Solving problem using lsqlin. Local minimum possible. lsqlin stopped because the relative change in function value is less than the function tolerance.
disp(output2)
algorithm: 'trust-region-reflective'
iterations: 14
firstorderopt: 5.2620e-08
cgiterations: 54
constrviolation: []
linearsolver: []
message: 'Local minimum possible.↵↵lsqlin stopped because the relative change in function value is less than the function tolerance.'
solver: 'lsqlin'
这一次,求解器进行更多次迭代,并得出具有更低(更好)的一阶最优性测度的解。
为了获得更好的一阶最优性测度,请尝试将 SubproblemAlgorithm 选项设置为 'factorization'。
opts.SubproblemAlgorithm = 'factorization'; [sol3,fval3,exitflag3,output3] = solve(nonneglsq,'Options',opts);
Solving problem using lsqlin. Optimal solution found.
disp(output3)
algorithm: 'trust-region-reflective'
iterations: 11
firstorderopt: 1.3973e-14
cgiterations: 0
constrviolation: []
linearsolver: []
message: 'Optimal solution found.'
solver: 'lsqlin'
使用此选项可使一阶最优性测度接近于零,这是最好的结果。
变更求解器
lsqnonneg 求解器是专门为处理非负线性最小二乘而设计的。尝试一下该求解器。
[sol4,fval4,exitflag4,output4] = solve(nonneglsq,'Solver','lsqnonneg');
Solving problem using lsqnonneg.
disp(output4)
iterations: 184
algorithm: 'active-set'
message: 'Optimization terminated.'
solver: 'lsqnonneg'
lsqnonneg 没有报告一阶最优性测度。相反,调查在 fval 输出中返回的残差范数。要查看较低有效位数字,请从每个残差范数中减去 22.5794。
t = table(fval - 22.5794, fval2 - 22.5794, fval3 - 22.5794, fval4 - 22.5794,... 'VariableNames',{'default','trust-region-reflective','factorization','lsqnonneg'})
t=1×4 table
default trust-region-reflective factorization lsqnonneg
__________ _______________________ _____________ __________
5.0804e-05 4.9179e-05 4.9179e-05 4.9179e-05
默认求解器的残差范数比其他三个求解器的残差范数略高(更差),在这种显示精度水平下,它们的残差范数是无法区分的。要查看哪个最小,请从两个结果中减去 lsqnonneg 结果。
disp([fval2 - fval4,fval3 - fval4])
1.0e-12 *
0.7176 0.7034
lsqnonneg 残差范数最小,几乎可以忽略不计。然而,lsqnonneg 需要最多的迭代才能收敛。