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.
回答(1 个)
Star Strider
2017-5-22
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).
类别
在 帮助中心 和 File Exchange 中查找有关 Conversion Between Symbolic and Numeric 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!