Global search optimization error
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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.
採用された回答
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
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.
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.
Jen W
2020 年 12 月 15 日
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?
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.
Jen W
2020 年 12 月 15 日
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.
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) =
(2 x13)
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
●
Jen W
2020 年 12 月 15 日
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 についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
