如何优化这段代码
clear,clc,close all
n=80; AB=400; M=100000;
prob=optimproblem;
x=optimvar(‘x’,3,n,’LowerBound’,zeros);
flagx=optimvar(‘flagx’,1,n,’Type’,’integer’,’LowerBound’,0,’UpperBound’,1);
con7=sum(x(3,:)./cos(x(1,:)))<=200; %未考虑EF遮蔽光线
con2=[];
for i=1:n
con2=[con2;x(1,i)<=pi/2;x(2,i)<=200;x(3,i)/cos(x(1,i))<=2.5];
end
con8=[];
for i=1:n-1
con8=[con8;sum(x(3,1:i))==x(2,i+1)];
end
p=100;
k=1./tan(2*x(1,:));
b=(x(2,:)+x(3,:)./cos(x(1,:)))./tan(2*x(1,:))+x(3,:).*tan(x(1,:));
b1=x(2,:)./tan(2*x(1,:));
[g, theta]=reflect_point(k,b,x,x(2,:),n);
[g1, theta1]=reflect_point(k,b1,x,x(2,:),n);
con4=[]; con5=[]; con6=[];
for i=1:n
con4=[con4;g(i)>=-5*flagx(i)+M*(flagx(i)-1)];
con5=[con5;g1(i)<=5*flagx(i)-M*(flagx(i)-1)];
con6=[con6;x(1,i)>=1/M];
end
con1=theta>=pi/2-x(1,:)*2;
con3=theta1>=pi/2-x(1,:)*2;
prob.Constraints.con1=con1;
prob.Constraints.con2=con2;
prob.Constraints.con3=con3;
prob.Constraints.con4=con4;
prob.Constraints.con5=con5;
prob.Constraints.con6=con6;
prob.Constraints.con7=con7;
prob.Constraints.con8=con8;
prob.Objective=-sum(flagx);
A(1,1:n)=pi/2;
A(2:3,:)=ones;
x00.x=A;
x00.flagx=ones(1,n);
[sol, fval, flag, out] = solve(prob,x00);
x=sol.x;
k=1./tan(2*x(1,:));
bb(1,:)=(x(2,:)+x(3,:)./cos(x(1,:)))./tan(2*x(1,:))+x(3,:).*sin(x(1,:));
bb(2,:)=x(2,:)./tan(2*x(1,:));
x01=p*k-sqrt(2*p.*bb(1,:)-200*p+(p*k).^2);
y01=(x01.^2)./(2*p)+100;
[gg(1,:), thetaa(1,:)]=reflect_point(k,bb(1,:),x,x(2,:),n);
[gg(2,:), thetaa(2,:)]=reflect_point(k,bb(2,:),x,x(2,:),n);
thetaa(3,:)=pi/2-x(1,:)*2;
function [g, theta] = reflect_point(k,b,x,d,n)
p=100;
x0=p*k-sqrt(2*p.*b-200*p+(p*k).^2);
y0=(x0.^2)./(2*p)+100;
theta=atan(-p./x0);
g=tan(2*theta+2*x(1,:)).*y0+x0;
endclear,clc,close all
n=80; AB=400; M=100000;
prob=optimproblem;
x=optimvar(‘x’,3,n,’LowerBound’,zeros);
flagx=optimvar(‘flagx’,1,n,’Type’,’integer’,’LowerBound’,0,’UpperBound’,1);
con7=sum(x(3,:)./cos(x(1,:)))<=200; %未考虑EF遮蔽光线
con2=[];
for i=1:n
con2=[con2;x(1,i)<=pi/2;x(2,i)<=200;x(3,i)/cos(x(1,i))<=2.5];
end
con8=[];
for i=1:n-1
con8=[con8;sum(x(3,1:i))==x(2,i+1)];
end
p=100;
k=1./tan(2*x(1,:));
b=(x(2,:)+x(3,:)./cos(x(1,:)))./tan(2*x(1,:))+x(3,:).*tan(x(1,:));
b1=x(2,:)./tan(2*x(1,:));
[g, theta]=reflect_point(k,b,x,x(2,:),n);
[g1, theta1]=reflect_point(k,b1,x,x(2,:),n);
con4=[]; con5=[]; con6=[];
for i=1:n
con4=[con4;g(i)>=-5*flagx(i)+M*(flagx(i)-1)];
con5=[con5;g1(i)<=5*flagx(i)-M*(flagx(i)-1)];
con6=[con6;x(1,i)>=1/M];
end
con1=theta>=pi/2-x(1,:)*2;
con3=theta1>=pi/2-x(1,:)*2;
prob.Constraints.con1=con1;
prob.Constraints.con2=con2;
prob.Constraints.con3=con3;
prob.Constraints.con4=con4;
prob.Constraints.con5=con5;
prob.Constraints.con6=con6;
prob.Constraints.con7=con7;
prob.Constraints.con8=con8;
prob.Objective=-sum(flagx);
A(1,1:n)=pi/2;
A(2:3,:)=ones;
x00.x=A;
x00.flagx=ones(1,n);
[sol, fval, flag, out] = solve(prob,x00);
x=sol.x;
k=1./tan(2*x(1,:));
bb(1,:)=(x(2,:)+x(3,:)./cos(x(1,:)))./tan(2*x(1,:))+x(3,:).*sin(x(1,:));
bb(2,:)=x(2,:)./tan(2*x(1,:));
x01=p*k-sqrt(2*p.*bb(1,:)-200*p+(p*k).^2);
y01=(x01.^2)./(2*p)+100;
[gg(1,:), thetaa(1,:)]=reflect_point(k,bb(1,:),x,x(2,:),n);
[gg(2,:), thetaa(2,:)]=reflect_point(k,bb(2,:),x,x(2,:),n);
thetaa(3,:)=pi/2-x(1,:)*2;
function [g, theta] = reflect_point(k,b,x,d,n)
p=100;
x0=p*k-sqrt(2*p.*b-200*p+(p*k).^2);
y0=(x0.^2)./(2*p)+100;
theta=atan(-p./x0);
g=tan(2*theta+2*x(1,:)).*y0+x0;
end clear,clc,close all
n=80; AB=400; M=100000;
prob=optimproblem;
x=optimvar(‘x’,3,n,’LowerBound’,zeros);
flagx=optimvar(‘flagx’,1,n,’Type’,’integer’,’LowerBound’,0,’UpperBound’,1);
con7=sum(x(3,:)./cos(x(1,:)))<=200; %未考虑EF遮蔽光线
con2=[];
for i=1:n
con2=[con2;x(1,i)<=pi/2;x(2,i)<=200;x(3,i)/cos(x(1,i))<=2.5];
end
con8=[];
for i=1:n-1
con8=[con8;sum(x(3,1:i))==x(2,i+1)];
end
p=100;
k=1./tan(2*x(1,:));
b=(x(2,:)+x(3,:)./cos(x(1,:)))./tan(2*x(1,:))+x(3,:).*tan(x(1,:));
b1=x(2,:)./tan(2*x(1,:));
[g, theta]=reflect_point(k,b,x,x(2,:),n);
[g1, theta1]=reflect_point(k,b1,x,x(2,:),n);
con4=[]; con5=[]; con6=[];
for i=1:n
con4=[con4;g(i)>=-5*flagx(i)+M*(flagx(i)-1)];
con5=[con5;g1(i)<=5*flagx(i)-M*(flagx(i)-1)];
con6=[con6;x(1,i)>=1/M];
end
con1=theta>=pi/2-x(1,:)*2;
con3=theta1>=pi/2-x(1,:)*2;
prob.Constraints.con1=con1;
prob.Constraints.con2=con2;
prob.Constraints.con3=con3;
prob.Constraints.con4=con4;
prob.Constraints.con5=con5;
prob.Constraints.con6=con6;
prob.Constraints.con7=con7;
prob.Constraints.con8=con8;
prob.Objective=-sum(flagx);
A(1,1:n)=pi/2;
A(2:3,:)=ones;
x00.x=A;
x00.flagx=ones(1,n);
[sol, fval, flag, out] = solve(prob,x00);
x=sol.x;
k=1./tan(2*x(1,:));
bb(1,:)=(x(2,:)+x(3,:)./cos(x(1,:)))./tan(2*x(1,:))+x(3,:).*sin(x(1,:));
bb(2,:)=x(2,:)./tan(2*x(1,:));
x01=p*k-sqrt(2*p.*bb(1,:)-200*p+(p*k).^2);
y01=(x01.^2)./(2*p)+100;
[gg(1,:), thetaa(1,:)]=reflect_point(k,bb(1,:),x,x(2,:),n);
[gg(2,:), thetaa(2,:)]=reflect_point(k,bb(2,:),x,x(2,:),n);
thetaa(3,:)=pi/2-x(1,:)*2;
function [g, theta] = reflect_point(k,b,x,d,n)
p=100;
x0=p*k-sqrt(2*p.*b-200*p+(p*k).^2);
y0=(x0.^2)./(2*p)+100;
theta=atan(-p./x0);
g=tan(2*theta+2*x(1,:)).*y0+x0;
end 优化问题 MATLAB Answers — New Questions