在MATLAB中快速分析jacobian的评估 [英] fast evaluation of analytical jacobian in MATLAB

查看:225
本文介绍了在MATLAB中快速分析jacobian的评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提出了一个优化问题,需要对雅可比矩阵进行非常快速的评估.我使用MATLAB的符号工具箱根据约束条件分析性地计算了雅可比行列式.我需要一个分析雅可比行列式,以便它是准确的(我一直认为这会很快,但是我当前的实现不是这种情况).

I have formulated a optimization problem that requires a very fast evaluation of the Jacobian matrix. I calculated the Jacobian analytically from my constraints using MATLAB's symbolic toolbox. I need an analytical Jacobian so that it is accurate (and I was thinking it would be fast, but that is not the case with my current implementation).

这部分代码只计算一次,结果J被保存,然后加载到下一部分

This part of the code is calculated once and the result J, is saved and then loaded in the next part

% symbolic variables
dvs = 5;
X=sym('X',[dvs,1]);

% multiple functions
for k = 1:4 
    f1(X(k+1),X(k)) = (X(k+1) - X(k));
    c(k) = f1;
end

J = jacobian(c,X);

x = ones(dvs,1);

以下是瓶颈,在优化过程中计算得出,每次加载J,x是设计变量的集合,X是变量的符号表达式.

THE FOLLOWING IS THE BOTTLENECK, calculated inside optimization, J is loaded each time, x is the set of design variables, X are the symbolic expressions for the the variables.

方法1:

J_n = double(subs(J,X,x));

我也尝试过:

方法2:

J_fun(X) = jacobian(c,X);

然后我保存了J_fun

Then I saved J_fun

在优化过程中,这样的事情叫做:

during optimization something like this is called:

x = [1 3 4 5 10];
x=num2cell(x);
J_fun(x{:})

这也太慢了.

方法#3

http://www.mathworks. com/help/symbolic/create-matlab-functions-from-mupad-expressions.html

J_fun2 = matlabFunction(J_fun);
x = [1 3 4 5 10];
x=num2cell(x);
J_fun2(x)

时间:

方法1:经过的时间为0.038066秒.

METHOD 1:Elapsed time is 0.038066 seconds.

方法2:经过的时间为0.017192秒.

METHOD 2:Elapsed time is 0.017192 seconds.

方法3:经过的时间为0.000372秒.

METHOD 3:Elapsed time is 0.000372 seconds.

注释/更新#1:这可能很明显,但是使用上面的任何方法,请确保将Jacobian传递给函数,而不是加载它.速度更快!

COMMENT/UPDATE #1: This is probably obvious, but using any of the methods above make sure that you pass the Jacobian to the function instead of loading it. It is MUUUCCH faster!

对于这个问题,它很快,但是对于我的问题,它却慢得多(我有一个更大的问题).

For this problem it is fast, but for my problem it is much slower (I have a much larger problem).

在这个玩具问题上,方法3比其他两个要快得多,但是如果我可以加快速度,我会喜欢的.有什么想法吗?

On this toy problem, METHOD 3 is much faster than the other two, but I would love it if I could speed this up some more. Any thoughts?

评论/更新#2:

方法4

这笔钱归霍希勒所有.

这似乎是一个新站点,因为在研究此问题时我没有注意到它: http://www.mathworks .com/help/optim/ug/symbolic-math-toolbox-calculates-gradients-and-hessians.html

This seems to be a new site, because I did not notice it when I was researching this problem: http://www.mathworks.com/help/optim/ug/symbolic-math-toolbox-calculates-gradients-and-hessians.html

filename = 'func.m';
matlabFunction(J_fun,'file',filename,'vars',{X});
x = [1 3 4 5 10];
x=num2cell(x);
pause
tic
func(x')
toc

与方法3极为相似,不同之处在于,您正在构建和优化可以查看的实际功能.从性能的角度来看,它与方法3相似(对于简单的示例),我目前正在针对此问题运行此代码,并且可能永远无法完成,方法3花费了大约10分钟的时间才能制作出.mat文件来计算约束,方法4运行了5个小时.现在,我必须计算一个雅可比行列式和大约1000个黑森州.....我正在考虑建立一个群集来执行此操作,但是我不确定它是否会完成.

Very similar to METHOD 3, except you are building and OPTIMIZING an actual function that you can look at. Performance wise it is similar to METHOD 3 (for the trivial example), I am currently running this code for my problem and it may never finish, it took about 10 minutes to for METHOD 3 to make a .mat file that calculated the constraint, METHOD 4 ran for 5 hours. Now I have to calculate a Jacobian and about 1000 Hessians.....I am looking into setting up a cluster to do this, but I am not sure that it will ever finish.

我的实际约束评估时间比较:

方法3:经过的时间为1.252072秒.

METHOD 3: Elapsed time is 1.252072 seconds.

方法4:经过的时间为0.716127秒.

METHOD 4: Elapsed time is 0.716127 seconds.

因此,方法4肯定更快.

So, METHOD 4 is certainly faster.

推荐答案

方法#3 使用此处描述的方法,它可以以可接受的速度运行!

METHOD # 3 Using the methods described here it is running at an acceptable speed!

http://www.mathworks. com/help/symbolic/create-matlab-functions-from-mupad-expressions.html

J_fun2 = matlabFunction(J_fun);
x = [1 3 4 5 10];
x=num2cell(x);
J_fun2(x)

这篇关于在MATLAB中快速分析jacobian的评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆