Global optimization - data type error

Hi,
I am trying to minimize the objective below using global optimization. The error I received is: "Error using fmincon. FMINCON requires all values returned by functions to be of data type double. Caused by: Failure in initial call to fmincon with user-supplied problem structure." I don't know how to fix this error. Any idea will be appreciated. Thanks in advance!
syms Cm Cc Cv Co
Cf = 1 - Cc - Cm - Cv - Co;
G = Cm*log(Cm) + Cc*log(Cc) + Cv*log(Cv) + Co*log(Co);
% Calculate the derivative of G w.r.t. the four variables
sym_mu_c = diff(G, Cc);
sym_mu_m = diff(G, Cm);
sym_mu_v = diff(G, Cv);
sym_mu_o = diff(G, Co);
% This function is to be minimized
sym_dis = vpa((sym_mu_c.^2 + sym_mu_m.^2 + sym_mu_v.^2 + sym_mu_o.^2).^0.5);
% Convert syms function to matlab function
mu_dis = matlabFunction(sym_dis);
% Bounds of the four variables
lowb = [0;0;0;0];
uppb = [1;1;1;1];
% Objective
obj = @(x)mu_dis;
% Define problem
gs = GlobalSearch;
opts = optimoptions(@fmincon, 'Algorithm', 'interior-point', 'MaxFunctionEvaluations', 1e6, 'MaxIterations', 1e6);
init = [0.204; 0.075; 1e-2; 1e-2];
prob = createOptimProblem('fmincon', 'x0', init, 'objective', obj, 'lb', lowb, 'ub', uppb, 'options', opts);
[x, fval] = run(gs, prob)

 Accepted Answer

Matt J
Matt J on 4 Jan 2021
Edited: Matt J on 4 Jan 2021
mu_dis = matlabFunction( sym_mu_c.^2 + sym_mu_m.^2 + sym_mu_v.^2 + sym_mu_o.^2 );
obj=@(x) mu_dis( x(1), x(2), x(3), x(4) )

8 Comments

Thanks. Are the variables in vector x arranged in an alphabetical order?
The correspondence can be seen from the signature of mu_dis
@(Cc,Cm,Co,Cv)(log(Cc)+1.0).^2+(log(Cm)+1.0).^2+(log(Co)+1.0).^2+(log(Cv)+1.0).^2
In other words x(1)=Cc,...,x(4)=Cv
Yes, that makes me wonder if it is always in alphabetical order... I have done another optimization problem before, and if I remember correctly, the x vector variables are in alphabetical order as well.
Alphabetical order is the default, but you can request a different ordering if you like, e.g.,
>> mu_dis = matlabFunction(sym_dis,'Vars',[Co,Cv, Cc,Cm])
mu_dis =
function_handle with value:
@(Co,Cv,Cc,Cm)(log(Cc)+1.0).^2+(log(Cm)+1.0).^2+(log(Co)+1.0).^2+(log(Cv)+1.0).^2
matlabFunction(str2sym('A+A1+A2+A10+a+a1+a2+a10+B+b+x+y+z+X+Y+Z+A1_1+A1_10+A10_1+A10_10+A2_1+A1_2'))
ans =
function_handle with value:
@(A,A1,A2,A10,A10_1,A10_10,A1_1,A1_2,A2_1,A1_10,B,X,Y,Z,a,a1,a2,a10,b,x,y,z)A+A1+A2+A10+A10_1+A10_10+A1_1+A1_2+A2_1+A1_10+B+X+Y+Z+a+a1+a2+a10+b+x+y+z
So all capital letters are before all lower-case letters, and the first level sorting is according to the first letter.
For the variables that have numbers without underscore, the sort is numeric rather than alphabetic (alphabetic would require that A10 be before A2 because '1' is before '2'.) But notice that A10_1 is before A1_1 and that A1_10 is after A2_1 and A10_10 is before A1_10
I think you would be hard-pressed to call it "alphabetical order".
I think you would be hard-pressed to call it "alphabetical order".
Perhaps so, but that is what the documentation calls it,
I have now filed a bug report about the order being documented as "alphabetical".
Thanks Matt and Walter.

Sign in to comment.

More Answers (0)

Asked:

on 4 Jan 2021

Commented:

on 5 Jan 2021

Community Treasure Hunt

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

Start Hunting!