converte the anonymous constraint function using fcn2optimexpr
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
N=20
x=optimvar('x',N,'LowerBound',-1,'UpperBound',1);
y=optimvar('y',N,'LowerBound',-1,'UpperBound',1);
z=optimvar('z',N,'LowerBound',-2,'UpperBound',0);
elecprob=optimproblem;
cons1=x.^2+y.^2+(z+1).^2;
elecprob.Constraints.cons1=cons1<=1;
% elecprob.Constraints.plane1 = z <= -x-y;
% elecprob.Constraints.plane2 = z <= -x+y;
% elecprob.Constraints.plane3 = z <= x-y;
% elecprob.Constraints.plane4 = z <= x+y;
%% if use anonymous constraint function ra[lace the code upside,
%% the results are different,and there is something wrong with the
%% anonymous constraint function and fcn2optimexpr
con2=@(x,y,z)(z+abs(x)+abs(y));
cons2=fcn2optimexpr(con2,x,y,z);
elecprob.Constraints.cons2=cons2<=0;
expr=optimexpr(1);
for ii=1:(N-1)
for jj=(ii+1):N
exp(ii)=(x(ii)-x(jj))^2+(y(ii)-y(jj))^2+(z(ii)-z(jj))^2;
expr=expr+(exp(ii))^(-1/2);
end
end
elecprob.Objective=expr;
rng default % For reproducibility
x0 = randn(N,3);
for ii=1:N
x0(ii,:) = x0(ii,:)/norm(x0(ii,:))/2;
x0(ii,3) = x0(ii,3) - 1;
end
init.x = x0(:,1);
init.y = x0(:,2);
init.z = x0(:,3);
[sol,fval,flag]=solve(elecprob,init)
If use anonymous constraint function replace the code in the % noted , the fval results 166.2480 are different from right result :163.0099, and there is something wrong with the anonymous constraint function and fcn2optimexpr.
1 Kommentar
Torsten
am 6 Mär. 2023
Bearbeitet: Torsten
am 6 Mär. 2023
You shouldn't name a variable "exp" (see below) because exp is reserved for the exponential function in MATLAB. But the optimal value of the objective remains the same.
Are you sure about the optimal value being 163.0099 instead of 166.2480 ? Could you set the optimal x,y and z values as initial values and see what happens ?
N=20;
x=optimvar('x',N,'LowerBound',-1,'UpperBound',1);
y=optimvar('y',N,'LowerBound',-1,'UpperBound',1);
z=optimvar('z',N,'LowerBound',-2,'UpperBound',0);
elecprob=optimproblem;
cons1=x.^2+y.^2+(z+1).^2;
elecprob.Constraints.cons1=cons1<=1;
% elecprob.Constraints.plane1 = z <= -x-y;
% elecprob.Constraints.plane2 = z <= -x+y;
% elecprob.Constraints.plane3 = z <= x-y;
% elecprob.Constraints.plane4 = z <= x+y;
%% if use anonymous constraint function ra[lace the code upside,
%% the results are different,and there is something wrong with the
%% anonymous constraint function and fcn2optimexpr
con2=@(x,y,z)(z+abs(x)+abs(y));
cons2=fcn2optimexpr(con2,x,y,z);
elecprob.Constraints.cons2=cons2<=0;
expr=optimexpr;
for ii=1:(N-1)
for jj=(ii+1):N
val=(x(ii)-x(jj))^2+(y(ii)-y(jj))^2+(z(ii)-z(jj))^2;
expr=expr+val^(-1/2);
end
end
elecprob.Objective=expr;
rng default % For reproducibility
x0 = randn(N,3);
for ii=1:N
x0(ii,:) = x0(ii,:)/norm(x0(ii,:))/2;
x0(ii,3) = x0(ii,3) - 1;
end
init.x = x0(:,1);
init.y = x0(:,2);
init.z = x0(:,3);
opts = optimoptions(elecprob)
opts.MaxFunctionEvaluations = 10000
[sol,fval,flag]=solve(elecprob,init,Options=opts)
sol.x
sol.y
sol.z
Akzeptierte Antwort
Matt J
am 6 Mär. 2023
N=20;
x=optimvar('x',N,'LowerBound',-1,'UpperBound',1);
y=optimvar('y',N,'LowerBound',-1,'UpperBound',1);
z=optimvar('z',N,'LowerBound',-2,'UpperBound',0);
elecprob=optimproblem;
cons1=x.^2+y.^2+(z+1).^2;
elecprob.Constraints.cons1=cons1<=1;
%% if use anonymous constraint function ra[lace the code upside,
%% the results are different,and there is something wrong with the
%% anonymous constraint function and fcn2optimexpr
con2=@(x,y,z)(z+abs(x)+abs(y));
cons2=fcn2optimexpr(con2,x,y,z);
elecprob.Constraints.cons2=cons2<=0;
[I,J]=find(triu(ones(N),1));
elecprob.Objective=sum(1./ sqrt( (x(I)-x(J)).^2 + (y(I)-y(J)).^2 + (z(I)-z(J)).^2) );
rng default % For reproducibility
x0 = randn(N,3);
for ii=1:N
x0(ii,:) = x0(ii,:)/norm(x0(ii,:))/2;
x0(ii,3) = x0(ii,3) - 1;
end
init.x = x0(:,1);
init.y = x0(:,2);
init.z = x0(:,3);
opts = optimoptions(elecprob);
opts.MaxIterations = 50000;
opts.OptimalityTolerance=1e-16;
opts.StepTolerance=1e-16;
opts.FunctionTolerance=1e-16;
opts.Algorithm='active-set';
[sol,fval,flag]=solve(elecprob,init,Options=opts);
fval
3 Kommentare
Siehe auch
Kategorien
Mehr zu Linear Algebra finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!