Global search optimization error

1 view (last 30 days)
Jen W
Jen W on 15 Dec 2020
Edited: Matt J on 15 Dec 2020
Hello community,
I am trying to debug a global optimization problem with objective function having multiple inputs. The code is here:
gs = GlobalSearch;
opts = optimoptions(@fmincon,'Algorithm','interior-point');
sixmin = @(x, y)(4*x.^2 - 2.1*x.^4 + x.^6/3 ...
+ x.*y - 4*y.^2 + 4*y.^4);
problem = createOptimProblem('fmincon','x0',[-1,2],...
'objective',sixmin,'lb',[-3,-3],'ub',[3,3],...
'options',opts);
run(gs,problem)
This code is adapted from https://www.mathworks.com/help/gads/run-the-solver.html , "Example of run with GlobalSearch" to illustrate my point. The only difference is the variable in the objective function handle here is (x, y) instead of a single (x). The error message is of this code is:
I am wondering why I cannot use (x,y) in the function handle, and how I can correct it if I have to use multiple inputs due to the large number of inputs in the real problem I am solving. Thank you.

Accepted Answer

Matt J
Matt J on 15 Dec 2020
Edited: Matt J on 15 Dec 2020
The fix, for your posted example, is to do,
problem = createOptimProblem('fmincon','x0',[-1,2],...
'objective',@(p) sixmin(p(1),p(2)),'lb',[-3,-3],'ub',[3,3],...
'options',opts);
I am wondering why I cannot use (x,y) in the function handle, and how I can correct it if I have to use multiple inputs due to the large number of inputs in the real problem I am solving.
If you have many unknowns, it makes more sense that both the function arguments and the expressions within the function be written in terms of vector variables rather than scalar variables. It is much easier and more compact to pass a 100x1 vector to a function, than to write a function call with 100 separate input arguments. This is, in any case, the syntax that the solver expects, as you will see in the documentation.
  8 Comments
Jen W
Jen W on 15 Dec 2020
Hi Matt, that works perfectly. One last question: how to output the variables as well as the optimized objective value. After adding the code below, I can only see the output of 65 variables. Is there a way to also output the best obj? Thank you.
gs = GlobalSearch;
opts = optimoptions(@fmincon,'Algorithm','interior-point', 'MaxFunctionEvaluations', 1e5, 'MaxIterations', 1e5);
init = [1e4;1e4;1e4;1e4;1e4;1e4;1e4;1e4;1e4;1e4;1e4;1e4;1e4;1e4;1e4;1e4;1e4;1e4;1e4;1e4;1e4;1e4;1e4;1e4;1e4; 0.2;0.085;1e-2;1e-2; 0.2757;0.1329;1e-2;0.5714; 0.37;1e-2;1e-2;0.6; 1e-2;0.47;1e-2;0.5; 1e-2;1e-2;0.75;0.22; 0.2;0.085;1e-2;1e-2; 0.2757;0.1329;1e-2;0.5714; 0.37;1e-2;1e-2;0.6; 1e-2;0.47;1e-2;0.5; 1e-2;1e-2;0.75;0.22];
prob = createOptimProblem('fmincon', 'x0', init, 'objective', obj, 'lb', lowb, 'ub', uppb, 'options', opts, 'nonlcon', nonlinfcn)
run(gs, prob)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!