lsqlin优化计算(matlab)

lsqlin optimized calculation (matlab)

我计算约束线性最小二乘问题的解如下:

lb = zeros(7,1);
ub = ones(7,1);
for i = 1:size(b,2)
   x(:,i) = lsqlin(C,b(:,i),[],[],[],[],lb,ub);
end

其中 Cm x 7bm x nn 非常大,导致计算时间缓慢。有什么方法可以加快此过程并摆脱缓慢的 for 循环。我使用 lsqlin 而不是 pinv\ 因为我需要将我的解决方案限制在 0–1 的边界(lbub)。

for 循环不一定是任何缓慢的原因 – 你不是 pre-allocating 和 lsqlin is probably printing out a lot of stuff on each iteration. However, you may be able to speed this up by turning your C matrix into a sparse block diagonal matrix, C2, with n identical blocks (see here)。这一次性解决了所有 n 个问题。如果新的 C2 不是稀疏的,您可能会使用更多的内存,并且计算可能比 for 循环花费更长的时间。

n = size(b,2);
C2 = kron(speye(n),C);
b2 = b(:);
lb2 = repmat(lb,n,1); % or zeros(7*n,1);
ub2 = repmat(ub,n,1); % or ones(7*n,1);
opts = optimoptions(@lsqlin,'Algorithm','interior-point','Display','off');
x = lsqlin(C2,b2,[],[],[],[],lb2,ub2,[],opts);

使用 optimoptions, I've specified the algorithm 并将 'Display' 设置为 'off' 以确保任何输出和警告都不会减慢计算速度。

在我的机器上,这比使用 for 循环(使用适当的 pre-allocation 和设置选项)快 6-10 倍。这种方法假设具有 m*n*7 个元素的稀疏 C2 矩阵可以放入内存中。如果没有,基于 for 循环的方法将是唯一的选择(除了编写您自己的 lsqlin 的专用版本或利用问题中的任何其他备用方法)。