Problem with matlabFunction and Nonlinear Optimization

I have been trying for a week to solve a problem I'm having using "matlabFunction" to convert a symbolic function to an anonymous function. The problem is that if I let "matlabFunction" run with the default settings it never finishes (I left it for 5 days before doing a control+c). If I turn off the default optimizer (i.e. 'Optimize',false) then matlabFunction returns a function different to that given by the original symbolic function and worse yet every point you evaluate the matlabFunction at returns a NaN whereas the symbolic function returns the proper value. Unfortunately the code I am working on is proprietary and I cannot post it. I have instead posted a sample function which uses the exact syntax I am using. The only difference in the code posted and the one I'm using is in "Jfunc(r)" where the posted one is just a generic polynomial.
clear all, close all, clc
r = sym('r',[1,9]);
assume(r,'real')
Jfunc(r) = r(1)^2 + r(2)^3 + r(3)^4 + r(4)^5 + r(5)^6 + r(6)^7 + r(7)^8 + r(8)^9 + r(9)^10
Jfunc2 = matlabFunction(Jfunc,'Optimize',false)
JfuncFunc = @(r) Jfunc2(r(1),r(2),r(3),r(4),r(5),r(6),r(7),r(8),r(9))
lb = [0,0,0,0,0,0,0,0,0]; % Lowerbound of algebraic bounds on variables
ub = [1,1,1,1,1,1,1,1,1]; % Upperbound of algebraic bounds on variables
r0 = ones(1,9); %Initial parameters nondimensionalized [rho1,rho2,rho2,EI1,EI2,EI3,cA,cB,cC]
problem = createOptimProblem('fmincon','x0',r0,...
'objective',JfuncFunc,'lb',lb,'ub',ub);
%Parallel Processing Multistart
ms = MultiStart('StartPointsToRun','bounds','UseParallel',true);
tic
parpool('IdleTimeout', 240)
[delta_updated,f] = run(ms,problem,100)
delete(gcp)
toc
The actual Junc(r) I am using is too long to post, it won't even display in the MATLAB window. However if I evaluate:
Jfunc(1,1,1,1,1,1,1,1,1) = 0.000546
Jfunc2(1,1,1,1,1,1,1,1,1) = NaN
JfuncFunc([1,1,1,1,1,1,1,1,1]) = NaN
The code I posted runs fine however when I substitute in my more complicated function I get the following error:
Error using barrier
Objective function is undefined at initial point. Fmincon cannot continue.
Error in fmincon (line 798)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
Error in fmultistart
Error in MultiStart/run (line 271)
fmultistart(problem,startPointSets,msoptions);
Error in CostFunctionOptimization_3BeamElms_test (line 238)
[delta_updated,f] = run(ms,problem,5000)
Caused by:
Failure in call to the local solver with supplied problem structure.
If I instead try to optimize Jfunc (a symfun) instead of JfuncFunc I get the following error (since the function returns sym and not double):
Error using fmincon (line 684)
FMINCON requires all values returned by functions to be of data type double.
Error in fmultistart
Error in MultiStart/run (line 271)
fmultistart(problem,startPointSets,msoptions);
Caused by:
Failure in call to the local solver with supplied problem structure.
Does anyone have any suggestion on how I can perform this optimization either using a symfun or convert a symfun to anonymous without matlabFunction? Any help or suggestions would be greatly appreciated.

Antworten (1)

Star Strider
Star Strider am 22 Mai 2017
I’m not certain of the problem you’re experiencing. This works for me, and with the 'vars' name-value pair, produces a function with the appropriate syntax for the Optimization Toolbox and other functions:
r = sym('r',[1,9]);
assume(r,'real')
Jfunc(r) = r(1)^2 + r(2)^3 + r(3)^4 + r(4)^5 + r(5)^6 + r(6)^7 + r(7)^8 + r(8)^9 + r(9)^10
Jfunc2 = matlabFunction(Jfunc, 'vars', [{r}])
Jfunc2 =
function_handle with value:
@(in1)in1(:,1).^2+in1(:,2).^3+in1(:,3).^4+in1(:,4).^5+in1(:,5).^6+in1(:,6).^7+in1(:,7).^8+in1(:,8).^9+in1(:,9).^10
This took a couple seconds, including the Command Window output (I didn’t time it).

1 Kommentar

Ben
Ben am 22 Mai 2017
Bearbeitet: Ben am 22 Mai 2017
Thank you for your comments and taking the time to look at my question. My problem is that the posted code works properly but when I replace the simple polynomial function "Jfunc" with my actual function matlabFunction either goes into a seemingly infinite loop trying to optimize the function or returns the incorrect function when I disable the optimizer. I'm trying to find a way to convert a symbolic function into a function which returns doubles without matlabFunction. I have attached a revised code below which will illustrate my problem using a condensed version of my actual function. Warning, it takes 10-15 minutes to run on my computer (which is why I posted the simple polynomial earlier, sounds like that was more confusing so I apologize). You will need to download the three .mat files and place them in the active directory for the code to run.
clear all, close all, clc
tic
r = sym('r',[1,9]);
assume(r,'real')
SRR = load('SRR.mat');
SRR = SRR.SRR;
Wi_Stacked = load('Wi_Stacked.mat');
Wi_Stacked = Wi_Stacked.Wi_Stacked;
SxxTilda_Stacked = load('SxxTilda_Stacked.mat');
SxxTilda_Stacked = SxxTilda_Stacked.SxxTilda_Stacked;
Jfunc(r) = transpose(Wi_Stacked - SxxTilda_Stacked)*inv(SRR)*(Wi_Stacked - SxxTilda_Stacked);
Jfunc2 = matlabFunction(Jfunc,'Optimize',false);
JfuncFunc = @(r) Jfunc2(r(1),r(2),r(3),r(4),r(5),r(6),r(7),r(8),r(9));
lb = [0,0,0,0,0,0,0,0,0]; % Lowerbound of algebraic bounds on variables
ub = [1,1,1,1,1,1,1,1,1]; % Upperbound of algebraic bounds on variables
r0 = ones(1,9); %Initial parameters nondimensionalized [rho1,rho2,rho2,EI1,EI2,EI3,cA,cB,cC]
toc
problem = createOptimProblem('fmincon','x0',r0,...
'objective',JfuncFunc,'lb',lb,'ub',ub);
%Parallel Processing Multistart
ms = MultiStart('StartPointsToRun','bounds','UseParallel',true);
tic
parpool('IdleTimeout', 240)
[delta_updated,f] = run(ms,problem,100)
delete(gcp)

Melden Sie sich an, um zu kommentieren.

Gefragt:

Ben
am 22 Mai 2017

Bearbeitet:

Ben
am 22 Mai 2017

Community Treasure Hunt

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

Start Hunting!

Translated by