【问题标题】:Optimizing in Parallel in Matlab with Mosek使用 Mosek 在 Matlab 中进行并行优化
【发布时间】:2016-05-08 10:42:06
【问题描述】:

我试图通过调用 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 文档没有详细介绍并行化。如何并行执行上述操作?

【问题讨论】:

    标签: matlab optimization parallel-processing parfor mosek


    【解决方案1】:

    您的代码的问题在于您使用了变量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 都是临时变量。

    【讨论】:

      【解决方案2】:
      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
      end
      

      parfor 不允许在其范围内创建变量。因此,我选择将rres 预分配为单元格,它们可以充当输出存储。请参阅@Daniel's answer 中的prob/ prob2 问题

      【讨论】:

      • 您的代码不起作用,prob 必须是广播变量,因为它是在循环外创建且未编入索引的,但它已被修改,只能用于缩减和切片变量。 Parfor 还允许创建变量,但它们只是临时的,在外部或其他迭代中不可见。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多