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.

 채택된 답변

Matt J
Matt J 2020년 12월 15일
편집: Matt J 2020년 12월 15일

0 개 추천

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

Jen W
Jen W 2020년 12월 15일
Thanks for that fix. The reason I want to use separate input arguments is that it is easier to track down what each input represents when I give them different names, instead of x(1), x(2), etc, especially when variables are used in objective functions and constraints.
Matt J
Matt J 2020년 12월 15일
편집: Matt J 2020년 12월 15일
Well, you are free to unpack the vector input into smaller pieces inside the body of the objective function. For example, suppose I had an objective involving a 50x1 vector x and a 50x1 vector y. Then I might write a function in terms of z=[x;y] as follows,
function fval=objective(z)
x=z(1:50);
y=z(51:100);
fval=dot(x.^2,y); %I made this up.
end
I would of course never want to make a function call of the form objective(x1,x2,...,x50,y1,y2,...,y50) and I would certainly never want to write the expression for fval as,
fval=y(1)*x(1)^2 + y(2)*x(2)^2 + . . . + y(50)*x(50)^2 ;
Aside from being cumbersome to write, this implementation of fval doesn't take advantage of Matlab's fast matrix arithmetic.
Then what if the constraints and the objective have different variables? For example, variables x, y, and z are used in the constraints, but only x and z appear in the objective, like in the code below. How can I make sure that the same corresponding variables are used in all the equations? Thank you.
% constraint
c = []; % nonlinear inequality constraint is none
ceq = @(x) x(1)^2 + x(2) + x(3)^3; % nonlinear equality constraint
% objective
obj = @(x) x(1) + x(3) % is this right?
Matt J
Matt J 2020년 12월 15일
편집: Matt J 2020년 12월 15일
The way you've written the objective is fine, assuming the input vector x contains all the unknowns in the problem. The only requirement is that you pass x=[x1,x2,x3] to both the objective and constraint functions. The solver doesn't know or care which x(i) you actually use within those functions.
It isn't really even true to say that the constraints and objective in your example have different variables. Hypothetically, you could have written the same obj in a way that uses all three variables,
obj = @(x) x(1) + 0*x(2) + x(3)
although you would never do so in practice. That would just waste computation.
Thank you very much, that helps. I have another piece of code that has error after I changed the scaler variables to vector variables:
f = @(x) x(1)^2 + 3*x(2);
jacob_f = @(x)jacobian(f(x), [x(1), x(2)]);
a = [1, 7];
feval(jacob_f, a)
I am expecting an output of [2, 3] since they are the first derivative of f after plugging in the 'a' value. Instead I received an error: undefined function 'jacobian' for input arguments of type 'double'. My guess is that I cannot use function handle f(x) in jacobian. However, in the global optimization problem, the variables can't be made symbolic neither. Could you please give some advice on how to solve this issue? Appreciate it.
Matt J
Matt J 2020년 12월 15일
편집: Matt J 2020년 12월 15일
You need to keep track more carefully of what is going to be a symbolic function and what is going to be a numeric function:
syms fsym(x1,x2)
fsym(x1,x2)=x1^2 + 3*x2;
jacob_fsym=jacobian(fsym), %symbolic
jacob_fsym(x1, x2) = 
(2x13)
fnum = matlabFunction(fsym);
jacob_fnum = matlabFunction(jacob_fsym), %numeric, but scalar args (x1,x2)
jacob_fnum = function_handle with value:
@(x1,x2)[x1.*2.0,3.0]
f=@(x) fnum(x(1),x(2));
jacob_f=@(x) jacob_fnum(x(1),x(2)) %numeric, but vector arg x=[x1,x2]
jacob_f = function_handle with value:
@(x)jacob_fnum(x(1),x(2))
a = [1, 7];
jacob_f(a)
ans = 1×2
2 3
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)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기

질문:

2020년 12월 15일

편집:

2020년 12월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by