Matlab 全局搜索功能:错误消息,无法开始搜索最小值
Matlab's GlobalSearch function : error message and can't start the searching for minimum
我有一个矩阵方程组,我想在其中找到 2 个 7x7 矩阵(所以我正在使用 (1x98) 向量)。
我想使用 GlobalSearch Matlab 函数时遇到问题。这是我的代码:
Eq1 = (P1.')*(a.')*a*P1 + (P1.')*(a.')*b*P2 + (P2.')*(b.')*a*P1 + (P2.')*(b.')*b*P2 - eye(7);
Eq2 = F1*a*P1 + F1*b*P2 + F2*a*P1 + F2*b*P2 - (a*P1 + b*P2)*D;
Eqs = reshape([Eq1,Eq2],2*7*7,[]);
Fun = matlabFunction(Eqs);
F = @(x) Fun(...
x( 1 ), x( 2 ), x( 3 ), x( 4 ), x( 5 ), x( 6 ), x( 7 ),...
x( 8 ), x( 9 ), x( 10 ), x( 11 ), x( 12 ), x( 13 ), x( 14 ),...
x( 15 ), x( 16 ), x( 17 ), x( 18 ), x( 19 ), x( 20 ), x( 21 ),...
x( 22 ), x( 23 ), x( 24 ), x( 25 ), x( 26 ), x( 27 ), x( 28 ),...
x( 29 ), x( 30 ), x( 31 ), x( 32 ), x( 33 ), x( 34 ), x( 35 ),...
x( 36 ), x( 37 ), x( 38 ), x( 39 ), x( 40 ), x( 41 ), x( 42 ),...
x( 43 ), x( 44 ), x( 45 ), x( 46 ), x( 47 ), x( 48 ), x( 49 ),...
x( 50 ), x( 51 ), x( 52 ), x( 53 ), x( 54 ), x( 55 ), x( 56 ),...
x( 57 ), x( 58 ), x( 59 ), x( 60 ), x( 61 ), x( 62 ), x( 63 ),...
x( 64 ), x( 65 ), x( 66 ), x( 67 ), x( 68 ), x( 69 ), x( 70 ),...
x( 71 ), x( 72 ), x( 73 ), x( 74 ), x( 75 ), x( 76 ), x( 77 ),...
x( 78 ), x( 79 ), x( 80 ), x( 81 ), x( 82 ), x( 83 ), x( 84 ),...
x( 85 ), x( 86 ), x( 87 ), x( 88 ), x( 89 ), x( 90 ), x( 91 ),...
x( 92 ), x( 93 ), x( 94 ), x( 95 ), x( 96 ), x( 97 ), x( 98 ));
x0 = ones(2*7*7,1);
gs = GlobalSearch;
options = optimoptions('fmincon','Algorithm', 'interior-point',...
'UseParallel',true, 'Display','off','MaxFunctionEvaluations',6000, 'TolCon', 1e-4, 'TolFun', 1e-4);
problem = createOptimProblem('fmincon', ...
'objective',F, ...
'x0',x0, ...
'lb',[-1*ones(98)], ...
'ub',[1*ones(98)], ...
'options',options)
[x,fval] = run(gs,problem)
但不幸的是,我在执行时遇到以下错误:
problem =
struct with fields:
objective: [function_handle]
x0: [98x1 double]
Aineq: []
bineq: []
Aeq: []
beq: []
lb: [98x98 double]
ub: [98x98 double]
nonlcon: []
solver: 'fmincon'
options: [1x1 optim.options.Fmincon]
Warning: Length of lower bounds is > length(x); ignoring extra bounds.
> In checkbounds (line 27)
In checkglobalsearchnlpinputs (line 36)
In globalsearchnlp
In GlobalSearch/run (line 340)
In compute_solving_Matricial_Global (line 96)
Warning: Length of upper bounds is > length(x); ignoring extra bounds.
> In checkbounds (line 41)
In checkglobalsearchnlpinputs (line 36)
In globalsearchnlp
In GlobalSearch/run (line 340)
In compute_solving_Matricial_Global (line 96)
Error using fmincon (line 635)
Supplied objective function must return a scalar value.
Error in globalsearchnlp
Error in GlobalSearch/run (line 340)
globalsearchnlp(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,options,localOptions);
Error in compute_solving_Matricial_Global (line 96)
[x,fval] = run(gs,problem)
Caused by:
Failure in initial call to fmincon with user-supplied problem structure.
如何修复此错误消息?
我认为我正确地使用了 GlobalSearch
,但这个问题可能来自不正确的下限或上限,甚至是我使用的向量大小。
不,这不是尺寸问题。虽然由于代码不完整我们无法重现错误,但错误信息很明确:
Supplied objective function must return a scalar value.
Tyr 使用静态向量调用 F
并检查其结果的大小。它应该 return 一个单一的值。
但是也有关于upper/lower边界的警告:
...
x0: [98x1 double]
...
lb: [98x98 double]
ub: [98x98 double]
...
根据 fmincon
文档:
x0
— Initial point
Initial point, specified as a real vector or real array. Solvers use the number of elements in, and size of, x0
to determine the number and size of variables that fun
accepts.
现在,您指定 x0
为 2*7*7 by 1
,而 lb
和 ub
为 98 by 98
。它们应该都一样大。
我有一个矩阵方程组,我想在其中找到 2 个 7x7 矩阵(所以我正在使用 (1x98) 向量)。
我想使用 GlobalSearch Matlab 函数时遇到问题。这是我的代码:
Eq1 = (P1.')*(a.')*a*P1 + (P1.')*(a.')*b*P2 + (P2.')*(b.')*a*P1 + (P2.')*(b.')*b*P2 - eye(7);
Eq2 = F1*a*P1 + F1*b*P2 + F2*a*P1 + F2*b*P2 - (a*P1 + b*P2)*D;
Eqs = reshape([Eq1,Eq2],2*7*7,[]);
Fun = matlabFunction(Eqs);
F = @(x) Fun(...
x( 1 ), x( 2 ), x( 3 ), x( 4 ), x( 5 ), x( 6 ), x( 7 ),...
x( 8 ), x( 9 ), x( 10 ), x( 11 ), x( 12 ), x( 13 ), x( 14 ),...
x( 15 ), x( 16 ), x( 17 ), x( 18 ), x( 19 ), x( 20 ), x( 21 ),...
x( 22 ), x( 23 ), x( 24 ), x( 25 ), x( 26 ), x( 27 ), x( 28 ),...
x( 29 ), x( 30 ), x( 31 ), x( 32 ), x( 33 ), x( 34 ), x( 35 ),...
x( 36 ), x( 37 ), x( 38 ), x( 39 ), x( 40 ), x( 41 ), x( 42 ),...
x( 43 ), x( 44 ), x( 45 ), x( 46 ), x( 47 ), x( 48 ), x( 49 ),...
x( 50 ), x( 51 ), x( 52 ), x( 53 ), x( 54 ), x( 55 ), x( 56 ),...
x( 57 ), x( 58 ), x( 59 ), x( 60 ), x( 61 ), x( 62 ), x( 63 ),...
x( 64 ), x( 65 ), x( 66 ), x( 67 ), x( 68 ), x( 69 ), x( 70 ),...
x( 71 ), x( 72 ), x( 73 ), x( 74 ), x( 75 ), x( 76 ), x( 77 ),...
x( 78 ), x( 79 ), x( 80 ), x( 81 ), x( 82 ), x( 83 ), x( 84 ),...
x( 85 ), x( 86 ), x( 87 ), x( 88 ), x( 89 ), x( 90 ), x( 91 ),...
x( 92 ), x( 93 ), x( 94 ), x( 95 ), x( 96 ), x( 97 ), x( 98 ));
x0 = ones(2*7*7,1);
gs = GlobalSearch;
options = optimoptions('fmincon','Algorithm', 'interior-point',...
'UseParallel',true, 'Display','off','MaxFunctionEvaluations',6000, 'TolCon', 1e-4, 'TolFun', 1e-4);
problem = createOptimProblem('fmincon', ...
'objective',F, ...
'x0',x0, ...
'lb',[-1*ones(98)], ...
'ub',[1*ones(98)], ...
'options',options)
[x,fval] = run(gs,problem)
但不幸的是,我在执行时遇到以下错误:
problem =
struct with fields:
objective: [function_handle]
x0: [98x1 double]
Aineq: []
bineq: []
Aeq: []
beq: []
lb: [98x98 double]
ub: [98x98 double]
nonlcon: []
solver: 'fmincon'
options: [1x1 optim.options.Fmincon]
Warning: Length of lower bounds is > length(x); ignoring extra bounds.
> In checkbounds (line 27)
In checkglobalsearchnlpinputs (line 36)
In globalsearchnlp
In GlobalSearch/run (line 340)
In compute_solving_Matricial_Global (line 96)
Warning: Length of upper bounds is > length(x); ignoring extra bounds.
> In checkbounds (line 41)
In checkglobalsearchnlpinputs (line 36)
In globalsearchnlp
In GlobalSearch/run (line 340)
In compute_solving_Matricial_Global (line 96)
Error using fmincon (line 635)
Supplied objective function must return a scalar value.
Error in globalsearchnlp
Error in GlobalSearch/run (line 340)
globalsearchnlp(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,options,localOptions);
Error in compute_solving_Matricial_Global (line 96)
[x,fval] = run(gs,problem)
Caused by:
Failure in initial call to fmincon with user-supplied problem structure.
如何修复此错误消息?
我认为我正确地使用了 GlobalSearch
,但这个问题可能来自不正确的下限或上限,甚至是我使用的向量大小。
不,这不是尺寸问题。虽然由于代码不完整我们无法重现错误,但错误信息很明确:
Supplied objective function must return a scalar value.
Tyr 使用静态向量调用 F
并检查其结果的大小。它应该 return 一个单一的值。
但是也有关于upper/lower边界的警告:
... x0: [98x1 double] ... lb: [98x98 double] ub: [98x98 double] ...
根据 fmincon
文档:
x0
— Initial pointInitial point, specified as a real vector or real array. Solvers use the number of elements in, and size of,
x0
to determine the number and size of variables thatfun
accepts.
现在,您指定 x0
为 2*7*7 by 1
,而 lb
和 ub
为 98 by 98
。它们应该都一样大。