我试图通过调用MOSEK来求解Matlab中的锥程序,同时改变其中一个约束的界限。
我想并行地这样做,以利用我所拥有的所有核心。这里有一个修改过的例子来说明我的观点。
testBounds=[0.1, 0.15, 0.2, 0.25, 0.3];
clear prob;
[r, res] = mosekopt('symbcon');
prob.c = [0 0 0 1 1 1];
% Specify the non-conic part of the problem.
prob.a = sparse([1 1 2 0 0 0]);
prob.buc = 1;
prob.blx = [0 0 0 -inf -inf -inf];
prob.bux = inf*ones(6,1);
% Specify the cones.
prob.cones.type = [res.symbcon.MSK_CT_QUAD, res.symbcon.MSK_CT_RQUAD];
prob.cones.sub = [4, 1, 2, 5, 6, 3];
prob.cones.subptr = [1, 4];
for i=1:5
% Specify the changing bound
prob.blc = testBounds(i);
% Optimize the problem.
[r,res]=mosekopt('minimize',prob);
% Display the primal solution.
res.sol.itr.xx'
end我试着用parfor做这件事,但这是不允许的。不幸的是,MOSEK文档并没有详细介绍平面化。我如何并行地进行上述工作?
发布于 2016-01-30 19:16:27
代码的问题在于使用变量prob。在算法级别上,它是独立的,因为循环的每一次迭代都使用它自己的blc设置,而不使用任何以前的数据,parfor不支持这种使用。最简单的解决方案不是修改变量prob,而是在每次迭代中复制它,使prob成为一个广播,prob2成为一个局部变量:
parfor ii=1:5
% Specify the changing bound
%copy broadcast variable prob to a temporary variable prob2
%this way the iteration has writing capabilities
prob2=prob
prob2.blc = testBounds(ii);
% Optimize the problem.
[r,res]=mosekopt('minimize',prob2);
% Display the primal solution.
res.sol.itr.xx'
end代码的另一个问题是返回数据的方式。parfor在处理数据时没有顺序,因此只将其显示到控制台不会给出任何有用的结果。也很慢。我不知道您到底需要什么,数据类型是什么,因此我没有触及代码的这一部分。我的答案中的代码进行了计算,但是没有返回任何结果,,因为res和r都是临时变量。
发布于 2016-01-30 19:08:15
N=5;
r = cell(1,N);
res = cell(1,N);
for ii=1:N
% Specify the changing bound
prob2=prob;
prob2.blc = testBounds(ii);
% Optimize the problem.
[r{ii},res{ii}]=mosekopt('minimize',prob2);
% Display the primal solution.
res{ii}.sol.itr.xx' %'// better not display at all during calculation
endparfor不允许在其范围内创建变量。因此,我选择预先分配r和res作为单元,它可以充当输出存储。参见prob/ prob2在@Daniel's answer中的问题
https://stackoverflow.com/questions/35105827
复制相似问题