can anyone help me about Error using optim.prob​lemdef.Opt​imizationP​roblem/sol​ve Invalid argument at position 2. Value must be finite.

2 次查看(过去 30 天)
when i use optimization toolbox, i get this errors:
Error using optim.problemdef.OptimizationProblem/solve
Invalid argument at position 2. Value must be finite.
Error in structureOpt (line 23)
[vv,sol,fval,exitflag,output] = solve(prob,x0,'Solver','ga','Options',options);
Caused by:
Failure in user-supplied fitness function evaluation.
Cannot continue.
Failure in initial user-supplied fitness function
evaluation. GA cannot continue.
I really can't find what goes wrong, The attachment is my program
thank you very much!
ub=[];
for i = 1:48
ub=[ub,'1'];
end
vv=optimvar('vv','LowerBound',0,'UpperBound',bin2dec(ub),'Type','integer');
[jc,rrc,allrea,allima,~]=fcn2optimexpr(@problemfun,vv);
ans = 'optim.problemdef.OptimizationVariable'
ans = 'double'
prob=optimproblem;
prob.ObjectiveSense='maximize';
prob.Objective=jc;
tol=0.1;
prob.Constraints.rrc1=rrc-tol-6<=0;
prob.Constraints.rrc2=rrc-6+tol>=0;
prob.Constraints.allrea=allrea>=0;
prob.Constraints.allima=allima>=0; %'FunctionTolerance', 1e-8, ...
options=optimoptions(@ga,'PopulationSize', 150, ...
'MaxGenerations', 400, ...
'EliteCount', 10, ...
'PlotFcn',@gaplotbestf);
rng default
x0.vv=randperm(bin2dec(ub),1);
problemfun(x0)
ans = 'struct'
Error using dec2bin
D must be numeric.

Error in solution>form_h (line 31)
nodestr=dec2bin(v);

Error in solution>problemfun (line 134)
hc=form_h(vv,map,text3,nloc);
[vv,sol,fval,exitflag,output] = solve(prob,x0,'Solver','ga','Options',options);
% rng default
% x0.vv=randperm(bin2dec(ub),1);
% [vv,sol,fval,exitflag,output] = solve(prob);
function h1=form_h(v,map,text3,nloc)
class(v)
nodestr=dec2bin(v);
nodenum=strfind(nodestr,'1')+6;
aa=zeros(6,9);
for j=1:6
aa1=j;
for i=1:5
aa(j,i)=aa1+6*(i-1);
end
end
for j=1:6
aa1=30+(j-1)*(1+2*4);
for i=1:4
aa(j,5+i)=aa1+i*2;
end
end
a4=[1+4*6 2+4*6 3+4*6 4+4*6 5+4*6 6+4*6];
[lia1,~]=ismember(aa(1,:),nodenum(1,1));
[lia2,~]=ismember(aa(2,:),nodenum(1,1));
[lia3,~]=ismember(aa(3,:),nodenum(1,1));
[lia4,~]=ismember(aa(4,:),nodenum(1,1));
[lia5,~]=ismember(aa(5,:),nodenum(1,1));
[lia6,~]=ismember(aa(6,:),nodenum(1,1));
[lia7,~]=ismember(a4,nodenum(1,end));
if sum(lia1)
nodenum=[2,nodenum];
elseif sum(lia2)
nodenum=[1,nodenum];
elseif sum(lia3)
nodenum=[4,nodenum];
elseif sum(lia4)
nodenum=[3,nodenum];
elseif sum(lia5)
nodenum=[6,nodenum];
else
nodenum=[5,nodenum];
end
if lia7==0
[lia1,~]=ismember(aa(1,:),nodenum(1,end));
[lia2,~]=ismember(aa(2,:),nodenum(1,end));
[lia3,~]=ismember(aa(3,:),nodenum(1,end));
[lia4,~]=ismember(aa(4,:),nodenum(1,end));
[lia5,~]=ismember(aa(5,:),nodenum(1,end));
[lia6,~]=ismember(aa(6,:),nodenum(1,end));
if sum(lia1)
nodenum=[nodenum,2+4*6];
elseif sum(lia2)
nodenum=[nodenum,1+4*6];
elseif sum(lia3)
nodenum=[nodenum,4+4*6];
elseif sum(lia4)
nodenum=[nodenum,3+4*6];
elseif sum(lia5)
nodenum=[nodenum,6+4*6];
else
nodenum=[nodenum,5+4*6];
end
end
h=zeros(size(map,1),size(nodenum,2)-1);
a=nloc(nodenum(1,1),:)-nloc(nodenum(1,2),:);
a=a/norm(a);
b=nloc(nodenum(1,3),:)-nloc(nodenum(1,2),:);
b=b/norm(b);
c=(nodenum(1,2)-1)*6;
h(c+1:c+3,1)=(a+b)';
for i=3:size(nodenum,2)-1
a=nloc(nodenum(1,i-1),:)-nloc(nodenum(1,i),:);
b=nloc(nodenum(1,i+1),:)-nloc(nodenum(1,i),:);
a=a/norm(a);
b=b/norm(b);
c=(nodenum(1,i)-1)*6;
h(c+1:c+3,i-1)=(a+b)';
end
a=nloc(nodenum(1,end-1),:)-nloc(nodenum(1,end),:);
a=a/norm(a);
c=(nodenum(1,end)-1)*6;
h(c+1:c+3,end)=a';
[a,b]=sort(map(:,2));
h1=h(b,:);
end
function [jc,rrc,allrea,allima,hc]=problemfun(vv)
load('data.mat');
alp=0.01;
bet=0.01;
cc=alp*mm+bet*kk;
n=size(mm,1);
sys.n=n;
[V,D] = eig(kk,mm);
[lambda,ki]=sort(diag(D));
V=V(:,ki);
num=3;
Vc=V(:,1:num);
mc=Vc'*mm*Vc;
kc=Vc'*kk*Vc;
ccc=Vc'*cc*Vc;
hc=form_h(vv,map,text3,nloc);
A=[zeros(size(mc,1)),eye(size(mc,1));
-1*inv(mc)*kc,-1*inv(mc)*ccc];
B=[zeros(size(mc,1),size(hc,2));
inv(mc)*Vc'*hc];
X=lyap(A,B*B');
[~,b]=eig(X);
jc=trace(X)*geomean(diag(b))/std(diag(b));
rc=[];
for i=1:size(A,1)
rc=[rc,A^(i-1)*B];
end
rrc=rank(rc);
[~,ba]=eig(A);
ba=diag(ba);
rea=real(ba);
ima=imag(ba);
allrea=all(rea);
allrea=min(abs(allrea))-1;
allima=ima<=0;
allima=all(allima);
allima=min(abs(allima))-1;
end

采纳的回答

Torsten
Torsten 2023-8-8
移动:Torsten 2023-8-8
Above your error - written out more clearly so that you as the coder should be able to understand the problem.
In
nodestr=dec2bin(v);
v is of class "struct", but it must be of class "numeric", "logical" or "char".
And you know that "ga" expects a double scalar value to be returned from "problemfun" ? jc is taken as this value from "ga". Is this your intention ?
  1 个评论
binbin cao
binbin cao 2023-8-9
编辑:binbin cao 2023-8-9
thanks for your reply.
jc is my objective function. and vv is optimvar. the value of vv is converted to binary.
ub=[];
for i = 1:48
ub=[ub,'1'];
end
vv=optimvar('vv','LowerBound',0,'UpperBound',bin2dec(ub),'Type','integer');
The maximum value of vv is achieved by converting the binary number consisting of 48 ‘1’ to decimal.
The optimization objective is to find an optimal arrangement of 0 and 1 for a 48-bit binary number, such that the value of jc is maximized.
The program is able to run when the maximum value of vv is obtained by converting the binary number consisting of 24 ‘1’ to decimal.
Later, I made some modifications to form_h.m, and it led to an increased range of required vv values.
Changing the maximum value of vv to be obtained by converting the binary number consisting of 48 ones to decimal, the program becomes infeasible to run.
thank you very much.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by