matlab程序,哪位大神可以优化算法节省时间,这个程序耗时太长了T^T

mip版  关注:286  答案:2  悬赏:40

解决时间 2021-01-23 21:14

ee3fb03c4297f185db348f1c0a46b0eb.png

已解决

2021-01-23 13:39

clear

clc

a=0;

b=100;

m=1;

n=9;

q=0;

contain=1000;

ipslo=0.01;

cover1=unifrnd(a,b,1,contain);

cover2=unifrnd(a,b,1,contain);

step=1;

Options.plot=1;

v=[a a;a b;b b;b a];

P=polytope(v);

Options.pbound=P;

r1=unifrnd(a,b,m,n);

r2=unifrnd(a,b,m,n);

while q==0

step=step+1;

[ml,nl]=size(r1);

if ml>nl

r1=rot90(r1);

r2=rot90(r2);

end

[e]=mpt_voronoi([r1' r2'],Options);

ee=cell(n,3);

for ie=1:n

thise=extreme(e(ie));

thise(thise>b)=b;

thise(thise<0)=0;

k=convhull(thise(:,1),thise(:,2));

[px,py]=poly2cw(thise(k,1),thise(k,2));

[mm,nn]=size(px);

pc=[];

nm=0;

for j=1:mm-1

for k=j+1:mm

ans1=sqrt((px(j,1)-px(k,1))^2+((py(j,1)-py(k,1))^2));

ans2=[ans1,j,k];

pc=[pc;ans2];

nm=nm+1;

end

end

for ll=1:n

turth=myinpolygon(r1(ll),r2(ll),px,py);

if turth==1

opoint=[r1(ll),r2(ll)];

break;

end

end

turthn=[];

for ll=1:contain

turth=myinpolygon(cover1(ll),cover2(ll),px,py);

turthn=[turthn,double(turth)];

end

pc=sortrows(pc,1);

r=pc(nm,1)/2;

point=[(px(pc(nm,2))+px(pc(nm,3)))/2,(py(pc(nm,2))+py(pc(nm,3)))/2];

ee{ie,1}=[px,py];

ee{ie,2}=r;

ee{ie,3}=sqrt((opoint(1,1)-point(1,1))^2+(opoint(1,2)-point(1,2))^2);

ee{ie,4}=point;

ee{ie,5}=polyarea(px,py);

ee{ie,6}=sum(sum(turthn));

ee{ie,7}=opoint;

end

dis=[];circle=[];area=[];radius=[];cont=[];opo=[];

for ie=1:n

radius=[radius;ee(ie,2)];

dis=[dis;ee(ie,3)];

circle=[circle;ee(ie,4)];

area=[area;ee(ie,5)];

cont=[cont;ee(ie,6)];

opo=[opo,ee(ie,6)];

end

radius=cell2mat(radius);

dis=cell2mat(dis);

circle=cell2mat(circle);

area=cell2mat(area);

cont=cell2mat(cont);

opo=cell2mat(opo);

if sum(sum(dis))else

r1=circle(:,1);

r2=circle(:,2);

continue;

end

if min(min(cont))<119

if min(min(radius))<15.28

n

q=1;

end

end

if step<100

ii=find(area==min(area));

opo(ii)=[];

next=unifrnd(a,b,1,2);

opo=[opo;next];

r1=opo(:,1);

r2=opo(:,2);

elseif step>100

n=n+1;

r1=unifrnd(a,b,m,n);

r2=unifrnd(a,b,m,n);

step=0;

end

end

子程序,网上下的,大神勿怪啊

function flag = myinpolygon(X,Y,xv,yv)

%judge the point in the polygon;

xv(1,:)=[];yv(1,:)=[];

vertex(:,1) = xv;

vertex(:,2) = yv;

dt = DelaunayTri(vertex);

k = convexHull(dt);

xv2 = dt.X(k(1:(end-1)),1);

yv2 = dt.X(k(1:(end-1)),2);

flag = inpolygon(X,Y,xv2,yv2);

最佳答案

d8485a5d0634ae9c3686693da898dece.png

2021-01-23 14:31

修改算法。。。这一大串的代码有多少人会仔细看啊,说明/注释都没有。

找出问题自己想办法改吧

大致看了下,主要时间大概还是在多重循环那了。想缩减时间的话,简化一下那部分

也可以调用matlab自带的计时工具,看那个函数占用时间做多。

全部回答

de2f9b67e0d20bb5e02f6059f7080b4a.png

1楼

2021-01-23 15:20

虽然我很聪明,但这么说真的难到我了

我要举报

如果感觉以上信息为低俗/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!

点此我要举报以上信息!

推荐资讯

大家都在看

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐