how to evaluate a function in string with parfor?

6 ビュー (過去 30 日間)
Julie G
Julie G 2017 年 11 月 14 日
コメント済み: OCDER 2017 年 11 月 17 日
Hi everybody, I have a problem evaluating the value of a function in string with parfor. Before I started using parfor, I used "eval", which worked quite well. I know "eval" does not work well with parfor and the one recommended the most online is "feval". However, I do not know how to use it. More specifically, i am doing genetic programming with the following code randomly generating a function for each individual.
parfor v = 1:8;
popu{v} = gpols_init(popusize,maxtreedepth,symbols);
tree{v} = popu{v}.chrom{jf}.tree;
aa{1,jf,v} = tree_stringrc(tree{v},1,symbols);
end
The output in "aa" looks like the following.
val(:,:,1) =
'XX(iv,1)' '(XX(iv,9))/(XX(iv…' '(((XX(iv,7))*exp(…' '(XX(iv,1))*sqrt(X…' '((XX(iv,2))*log((…'
what I need to do is to evaluate the value of each expression, which I do not know how to do. I will appreciate if someone could help me with it. Thanks. Best, J.
  3 件のコメント
Julie G
Julie G 2017 年 11 月 14 日
編集済み: Walter Roberson 2017 年 11 月 17 日
The reason is I have to run quite a lot simulations and parfor would save a lot of time. I tried "function handles" with the following code
str = 'a{1,jf,v}';
fh = str2func(str)
but still it had problems like the following.
Warning: The input to STR2FUNC "a{1,jf,v}" is not a valid function name. This will generate an error in a future release.
In parallel_function>make_general_channel/channel_general (line 914)
In remoteParallelFunction (line 38)
Warning: The input to STR2FUNC "a{1,jf,v}" is not a valid function name. This will generate an error in a future release.
In parallel_function>make_general_channel/channel_general (line 914)
In remoteParallelFunction (line 38)
Warning: The input to STR2FUNC "a{1,jf,v}" is not a valid function name. This will generate an error in a future release.
In parallel_function>make_general_channel/channel_general (line 914)
In remoteParallelFunction (line 38)
Stephen23
Stephen23 2017 年 11 月 14 日
編集済み: Stephen23 2017 年 11 月 14 日
@Julie G: Why not generate those functions entirely using function handles, and avoid using strings entirely? Have you considered exclusively staying in the realm of function handles?
Also: what is XX ?

サインインしてコメントする。

採用された回答

OCDER
OCDER 2017 年 11 月 14 日
For str2func, you also need to include the "@(var1, var2, var3, ...)" string. This is used to tell Matlab what part of the string are variables, while the rest of the string are treated as function names and math operators.
XX = rand(10); %What is XX? I'm assuming it's a matrix
iv = 10; %Assuming this is just an index of XX
aa{1,1,1} = 'XX(iv,1) * sin(XX(iv,1))'; %Example string of a function. XX and iv are variables.
FuncHandle = str2func(['@(XX, iv)' aa{1,1,1}]);%Define "XX" and "iv" as variable names in the string using @(XX, iv).
Output = FuncHandle(XX, iv); %Evaluate the string as function.
  11 件のコメント
Julie G
Julie G 2017 年 11 月 17 日
Hi Donald. i added "parpool(12)" to my matlab file. but error message appeared in unix as below.
{Undefined variable "com" or class "com.mathworks.toolbox.distcomp.pmode.SessionInfo.NULL_SESSION_INFO".
Error in parpool (line 82) sessionInfo = com.mathworks.toolbox.distcomp.pmode.SessionInfo.NULL_SESSION_INFO;
Error in gp1 (line 156) parpool(12);
}
can you help with this? Thanks.
J.
OCDER
OCDER 2017 年 11 月 17 日
This could be caused if starting matlab with the -nojvm option. Here's a similar Q&A thread for this error. https://www.mathworks.com/matlabcentral/answers/230285-parpool-r2014a-fails-on-linux

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeParallel Computing Fundamentals についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by