converte the anonymous constraint function using fcn2optimexpr
조회 수: 1 (최근 30일)
이전 댓글 표시
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1315380/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1315385/image.png)
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
Torsten
2023년 3월 6일
편집: Torsten
2023년 3월 6일
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
채택된 답변
Matt J
2023년 3월 6일
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
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear Algebra에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!