Huckleberry Febbo
Huckleberry Febbo

Reputation: 313

fast evaluation of analytical jacobian in 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).

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);

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.

METHOD 1:

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

I also tried:

METHOD 2:

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

Then I saved J_fun

during optimization something like this is called:

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

This is also waaay too slow.

METHOD # 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)

Times:

METHOD 1:Elapsed time is 0.038066 seconds.

METHOD 2:Elapsed time is 0.017192 seconds.

METHOD 3:Elapsed time is 0.000372 seconds.

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).

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?

COMMENT/UPDATE #2:

METHOD 4

Credit for this goes to horchler.

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

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.

MY ACTUAL CONSTRAINT evaluation time comparison:

METHOD 3: Elapsed time is 1.252072 seconds.

METHOD 4: Elapsed time is 0.716127 seconds.

So, METHOD 4 is certainly faster.

Upvotes: 2

Views: 1643

Answers (1)

Huckleberry Febbo
Huckleberry Febbo

Reputation: 313

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)

Upvotes: 0

Related Questions