YALMIP is not working with parfor loop
13 次查看(过去 30 天)
显示 更早的评论
Hello,
I have the problem with decision variables and solving multiple problems using parfor loop.
I define some decision variables before parfor loop and when I run parfor loop I got the error: "Matrix dimensions must agree", because my decision variables are now empty vectors/matrices.
Here I attach a piece of my code for which I already get the error.
rep = 100;
n=2;
m=2;
T=4;
P_min=[0.0800; 0.0800];
P_max=[0.2; 0.2];
Pd = [1.0800 1.0800 1.0800 1.0800;
0.9700 0.9700 0.9700 0.9700];
Qd = [0.2200 0.2200 0.2200 0.2200;
0.2000 0.2000 0.2000 0.2000];
lambda_l = sdpvar(m,1);
lambda_u = sdpvar(m,1);
lambda = sdpvar(n,1);
h_bas = 0;
h_bas = h_bas + sum(lambda_l.*P_min.*(~isinf(P_min))-lambda_u.*P_max.*(~isinf(P_max)));
cons_bas = [];
cons_bas = [cons_bas, lambda_l>=0,lambda_u>=0];
for t=1:T
parfor k=1:rep
h = h_bas + sum(lambda.*Pd(:,t));
cons = [cons_bas];
options = sdpsettings('solver','mosek','verbose',0);
optimize(cons,-h,options);
end
end
Could you please help me to fix this problem?
Best,
Adriana
回答(2 个)
Walter Roberson
2021-3-3
YALMIP permits you to define variables using calls such as
P = sdvpar(1)
and it expects that variable to be distinguished from
Q = sdvpar(1)
That can only happen if YALMIP is retaining state about which variables have been created and what their properties are.
In MATLAB, the methods of recording state like this are:
- in graphics objects (not likely at all to be the case here)
- in the base workspace
- in persistent variables
- in global variables
- in handle objects
- in class variables
However, graphics objects, base workspace, persistent variables, and global variables are not copied to parallel workers.
Objects (that are plainly referenced) are copied to parallel workers, but through a method equivalent of "save" and "load" --- a process that copies only serializable data, and loses dynamic properties, and effectively disconnects the copied clones from the original objects.
I suspect that the code was not designed with parallel use in mind.
7 个评论
Johan Löfberg
2021-3-9
编辑:Johan Löfberg
2021-3-9
EDIT: Works now in newer MATLAB version!
YALMIP does not work with parfor, and it cannot be fixed or circumvented.
For YALMIP specific questions, you are much better off posting quetions on the YALMIP google groups forum.
3 个评论
Johan Löfberg
2021-3-9
编辑:Johan Löfberg
2021-3-9
BTW, your code is going to run much faster using an optimizer construct, to the extent that the for parfor might be redundant, as most of the time in your version is spent in general overhead. I think this version survuves parfor in 2016 too, as an optimizer object is disconnected from YALMIPs global database and can be saved/loaded
c = sdpvar(m,1);
Solver = optimizer(cons_bas,h_bas + c'*lambda,options,c,lambda);
for t=1:T
parfor k=1:rep
Solver(Pd(:,t))
end
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!