找回密码
 注册
Simdroid-非首页
查看: 247|回复: 14

请教:三重循环优化

[复制链接]
发表于 2008-4-5 07:07:38 | 显示全部楼层 |阅读模式 来自 大连理工大学
问题:判断两凸多面体是否相交
方法:检测第一个多面体的每个面是否与第二个多面体的面有交点(r0=(abs(z1-z2)<=.5);)

分析:多面体每个面为三角形,三顶点M1,M2,M3确定一平面。该平面的法线向量n与向量M1M2,M1M3都垂直,
          所以n=M1M2叉乘M1M3,提取系数a,b,c.根据平面的点法式方程,得所求平面的方程:
          a(x-x1)+b(y-y1)+c(z-z1)=0;

效率极其低,大家有没有什么好的方法,不妨分享以下!
小弟先谢谢了!

程序:
  1. function N=myoverlaptest
  2. % 2008 4 5 4:34
  3. n=50;
  4. X1=rand(n,3).*4;
  5. c1=convhulln(X1);
  6. X2=X1+3.5;
  7. c2=convhulln(X2);

  8. number=2;
  9. len1=size(c1,1)

  10. figure,hold on,view(3)
  11. for i=1:size(c1,1)
  12. j=c1(i,[1 2 3 1]);
  13. patch(X1(j,1),X1(j,2),X1(j,3),[.5 .5 .5])
  14. end
  15. for i=1:size(c2,1)
  16. j=c2(i,[1 2 3 1]);
  17. patch(X2(j,1),X2(j,2),X2(j,3),[.5 .5 .5]);
  18. end

  19. N=0; % 不相交
  20. m=size(c1,1);
  21. mm=0;
  22. for i=1:m

  23. A=X1(c1(i,:),1)'; % x坐标 (1*3)
  24. B=X1(c1(i,:),2)'; % y坐标
  25. C=X1(c1(i,:),3)'; % z坐标

  26. x=min(A):.1:max(A);
  27. y=min(B):.1:max(B);
  28. [x,y]=meshgrid(x,y);

  29. M1M2=[A(2)-A(1),B(2)-B(1),C(2)-C(1)];
  30. M1M3=[A(3)-A(1),B(3)-B(1),C(3)-C(1)];

  31. syms I J K
  32. n1=det([I,J,K;M1M2;M1M3]); % 求平面的法线向量

  33. a=double(maple('coeff',n1,I));
  34. b=double(maple('coeff',n1,J));
  35. c=double(maple('coeff',n1,K));

  36. d=-(a*A(1)+b*B(1)+c*C(1));

  37. z1=-(a/c*x+b/c*y+d/c);

  38. for j=1:number-1
  39. cj=c2;
  40. X_convex1=X2;
  41. for k=1:size(c2,1)
  42. mm=mm+1;
  43. A=X_convex1(cj(k,:),1)'
  44. B=X_convex1(cj(k,:),2)'
  45. C=X_convex1(cj(k,:),3)'

  46. M1M2=[A(2)-A(1),B(2)-B(1),C(2)-C(1)]
  47. M1M3=[A(3)-A(1),B(3)-B(1),C(3)-C(1)]

  48. n2=det([I,J,K;M1M2;M1M3])

  49. a=double(maple('coeff',n2,I))
  50. b=double(maple('coeff',n2,J))
  51. c=double(maple('coeff',n2,K))

  52. d=-(a*A(1)+b*B(1)+c*C(1))

  53. z2=-(a/c*x+b/c*y+d/c)

  54. r0=(abs(z1-z2)<=.5);
  55. zz=r0.*z1;yy=r0.*y;xx=r0.*x;

  56. x2=[A,A(1)];y2=[B,B(1)]
  57. in=inpolygon(xx(r0~=0),yy(r0~=0),x2',y2');
  58. if all(in==0)
  59. continue % 不相交,则进入下轮循环
  60. else
  61. N=1
  62. return
  63. end
  64. if mm>len1^2
  65. disp('死循环');
  66. return
  67. end
  68. end
  69. end
  70. end[
复制代码

[ 本帖最后由 chengweifeng 于 2008-5-22 18:01 编辑 ]
发表于 2008-4-5 10:48:14 | 显示全部楼层 来自 河南郑州
Simdroid开发平台
两个多面体相交,重合部分的三维坐标肯定相等,先将x轴坐标相等的数据a选出来,再将a中y轴坐标相等的数据b选出来,最后将b中z轴坐标相等的数据c选出来,c就是两个多面体相交的数据。
回复 不支持

使用道具 举报

发表于 2008-4-5 10:57:44 | 显示全部楼层 来自 美国
Suppose there are two convex bodies A and B whose vertices are A1, A2,...,An and B1,B2,...Bm, respectively.  I think the following statements are equivalent:

(1) A \cap B =   \emptyset   
(2) for all i=1,2,...,n,  Ai \notin B
(3) for all i=1,2,...,m,  Bi \notin A

So instead of checking the intersection of faces between A and B, I will proceed with checking whether all vertices of A are out of B.  If it is true, A and B have no intersection.
回复 不支持

使用道具 举报

 楼主| 发表于 2008-4-5 13:10:38 | 显示全部楼层 来自 大连理工大学
to:hhwuai007
    你的方法让我有种焕然一新的感觉,但是个人暂时的感觉 ,你的方法在程序上实现恐怕更为不易。因为“重合部分的三维坐标肯定相等”----〉最终还是归结到求彼此相交面的交点,好像又回到了我的方法上来。那么,效率问题仍然是没有跨越。
回复 不支持

使用道具 举报

发表于 2008-4-5 13:54:20 | 显示全部楼层 来自 湖北武汉
考虑体B的所有线段是否与体A面相交,同理考虑A
得到体A某面方程F(X,Y,Z)=0
带入体B某线段两坐标
G=F(点一)×F(点二)
是否存在
       G<0线面相交
       G>0不相交
       G=0线有点在面上
没仔细考虑,直接根据直线和直线判断相交类比过来

[ 本帖最后由 shunfly 于 2008-4-5 13:55 编辑 ]
回复 不支持

使用道具 举报

 楼主| 发表于 2008-4-5 14:18:16 | 显示全部楼层 来自 大连理工大学
To:pcqsl
     Actually,checking the intersection of two convex hulln,two conditions must be considered.The following pictures show what  should be taken into account:


I think the ways you supplied are just available for the second conditon.Maybe the following function show what your mean,I call it Volume Add text Method

  1. function N=AddVolume
  2. % 体积和法测试两多面体是否相交,相交则N=1,反之N=0
  3. % 但是此法不能判断另一类特殊的多面体相交情况
  4. % 2008 3 23  cheng wf
  5. % chengwf@yahoo.cn
  6. clear all
  7. clc
  8. [x,y,z]=sphere(20);
  9. i=1+round(rand.*4);
  10. x1=x(11,i);y1=y(11,i);z1=z(11,i);
  11. x2=x(11,i+5);y2=y(11,i+5);z2=z(11,i+5);
  12. x3=x(11,i+10);y3=y(11,i+10);z3=z(11,i+10);
  13. x4=x(11,i+15);y4=y(11,i+15);z4=z(11,i+15);
  14. i1_row=1;i1_coloun=1+round(rand.*19);
  15. j1_row=21;j1_coloun=1+round(rand.*19);
  16. z5=z(i1_row,i1_coloun);
  17. x5=x(i1_row,i1_coloun);
  18. y5=y(i1_row,i1_coloun);
  19. z6=z(j1_row,j1_coloun);
  20. x6=x(j1_row,j1_coloun);
  21. y6=y(j1_row,j1_coloun);
  22. if i<=3
  23.    j=i+2;
  24. elseif i>3&i<=5
  25.     j=i-2;
  26. end
  27. x7=x(16,j);y7=y(16,j);z7=z(16,j);
  28. x8=x(16,j+5);y8=y(16,j+5);z8=z(16,j+5);
  29. x9=x(16,j+10);y9=y(16,j+10);z9=z(16,j+10);
  30. x10=x(16,j+15);y10=y(16,j+15);z10=z(16,j+15);
  31. x11=x(6,j);y11=y(6,j);z11=z(6,j);
  32. x12=x(6,j+5);y12=y(6,j+5);z12=z(6,j+5);
  33. x13=x(6,j+10);y13=y(6,j+10);z13=z(6,j+10);
  34. x14=x(6,j+15);y14=y(6,j+15);z14=z(6,j+15);

  35. xi=[x1;x2;x3;x4;x5;x6;x7;x8;x9;x10;x11;x12;x13;x14];
  36. yi=[y1;y2;y3;y4;y5;y6;y7;y8;y9;y10;y11;y12;y13;y14];
  37. zi=[z1;z2;z3;z4;z5;z6;z7;z8;z9;z10;z11;z12;z13;z14];
  38. X=[xi,yi,zi];
  39. [c,V1]=convhulln(X);
  40. hold on
  41. for i=1:size(c,1)
  42.     j=c(i,[1 2 3 1]);
  43.     patch(X(j,1),X(j,2),X(j,3),[.5 .5 .5]);
  44. end
  45. view(3)
  46. % X2=[xi+1.5,yi,zi]; % 不相交 N=0
  47. X2=[xi+.5,yi,zi];    % 相交   N=1
  48. [c2,V2]=convhulln(X2);
  49. for i=1:size(c2,1);
  50.     j=c2(i,[1 2 3 1]);
  51.     patch(X2(j,1),X2(j,2),X2(j,3),[.5 .3 .2]);
  52. end
  53. % ======相交判断=====
  54. % 体积和测试法
  55. i=0;
  56. j=0;
  57. % N=0;  % 不相交
  58. for i=1:length(X) % 对第1个多面体的每个顶点进行检测
  59.     v=0;
  60.     for j=1:size(c2,1)
  61.         k=c2(j,;
  62.         x_convexj=[X2(k,1);X(i,1)];
  63.         y_convexj=[X2(k,2);X(i,2)];
  64.         z_convexj=[X2(k,3);X(i,3)];
  65.         convexj=[x_convexj,y_convexj,z_convexj];
  66.         [L,vj]=convhulln(convexj);
  67.         v=v+vj;
  68.      end
  69.      if abs(v-V2)<5*10e-5
  70. %      if v==V2
  71.         N=1.0  % 相交
  72.         return
  73.     else
  74.         N=0   % 是否相交,待定
  75. %         调用其他函数判断另一种相交情况
  76.      end
  77. end
  78. return
复制代码

[ 本帖最后由 chengweifeng 于 2008-4-5 21:24 编辑 ]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
回复 不支持

使用道具 举报

 楼主| 发表于 2008-4-5 14:25:52 | 显示全部楼层 来自 大连理工大学
To: shunfly 兄
   你的方法理论上可行,但是就我#1的帖子看,其实很难提取出每个多面体的棱.另外,此法同样难逃三重循环的限制。你试一下我#1的帖子就知道效率是何等的低。:'(
回复 不支持

使用道具 举报

发表于 2008-4-6 08:17:45 | 显示全部楼层 来自 美国
原帖由 chengweifeng 于 2008-4-5 14:18 发表
To:pcqsl
     Actually,checking the intersection of two convex hulln,two conditions must be considered.The following pictures show what  should be taken into account:


You are correct.  I made mistakes.  

By giving more thought to your problem, I would like to proceed with Matlab function fmincon().   The fundamental reason is that *ANY POINT* inside a *CONVEX HULL* can be expressed with a linear function of the vertices.   Let us define convex bodies A and B with vertices A1...An, and B1...Bm.  Then
for all a \in A, exists p1,p2,...,pn where for i=1...n, 0<= pi<=1, and p1+p2+...+pn=1,  such that   a=A1*p1+A2*p2+...+An*pn.  With these constrains on p1,...pn, the express never give points outside of A.  There is a similar result on B.

Then your problem is equivalent to find the minimum distance between two points, where one is from A and the other is from B.   Compare to your algorithm, this program should have better  performance.  However, since it is based on the "Medium-Scale Algorithm", it will be slow if the number of vertices is large.
  1. %%%%%  main program
  2. n=50;
  3. X1=rand(n,3).*4;
  4. c1=convhulln(X1);
  5. X2=X1+3.5;
  6. c2=convhulln(X2);

  7. a=X1(unique(c1),:);   %  only vertices involved
  8. b=X2(unique(c2),:);
  9. N=checkintersection(a,b)  %  N=1 means there exists an intersection


  10. %%%%%  sub program
  11. function r=checkintersection(a,b)

  12. a=a';
  13. b=b';

  14. Na=size(a,2);
  15. Nb=size(b,2);
  16. dim=size(a,1);

  17. Acon=zeros(1,Na+Nb);
  18. bcon=1;
  19. Aeq=[ones(1,Na) zeros(1,Nb);zeros(1,Na) ones(1,Nb);[a -b]];
  20. beq=[1;1;zeros(dim,1)];

  21. dist=@(p)(distance(p,a,b));
  22. p0=[1;zeros(Na-1,1);1;zeros(Nb-1,1)];

  23. options= optimset('display','off','LargeScale','off','outputfcn',@outfun);

  24. [p,fval,exitflag]=fmincon(dist,p0,Acon,bcon,Aeq,beq,zeros(Na+Nb,1),ones(Na+Nb,1),[],options);

  25. if exitflag>0 || exitflag==-1
  26.     r=true;
  27. else
  28.     r=false;
  29. end;

  30. %%%%%  calculate the distance,  points obtained from coefficients x and vertices a and b
  31. function d=distance(x,a,b)
  32.         v1=a*x(1:size(a,2));
  33.         v2=b*x(size(a,2)+1:end);
  34.         d=norm(v1-v2);            

  35. %%%%%  To check whether a small distance presents, if yes, stop.   
  36. %%%%%  This is a trick for the case where the intersection is "easy" to find.
  37. function stop=outfun(x, optimValues, state)
  38.         stop=false;
  39.         delta=1e-16;
  40.         if optimValues.fval<delta        % distance is small enough
  41.             if sum( x>-delta )+ sum(x<1+delta) == 2*length(x)   
  42.            % coefficients meet constraints ( in other words, the point is inside the convex bodies.)
  43.                 stop=true;
  44.             end
  45.         end;
  46.     end;
复制代码

[ 本帖最后由 pcqsl 于 2008-4-6 11:15 编辑 ]
回复 不支持

使用道具 举报

 楼主| 发表于 2008-4-6 16:00:22 | 显示全部楼层 来自 大连理工大学

for all a \in A, exists p1,p2,...,pn where for i=1...n, 0<= pi<=1, and p1+p2+...+pn=1,  such that   a=A1*p1+A2*p2+...+An*pn.  With these constrains on p1,...pn, the express never give points outside of A.

pn is a weighted coefficient,right?!You are Great!
Out of regard for your thought,I will take it into account seriously.
The result will be posted here after my test on your function

Thanks again!

[ 本帖最后由 chengweifeng 于 2008-4-6 16:22 编辑 ]
回复 不支持

使用道具 举报

 楼主| 发表于 2008-4-26 21:47:34 | 显示全部楼层 来自 大连理工大学
To:pcqsl
*ANY POINT* inside a *CONVEX HULL* can be expressed with a linear function of the vertices.

The code you supplied is more efficient than mine.Thank you very much!
but it still works only for the second condition!
I think the method supplied by hhwuai007 may be a good way!
回复 不支持

使用道具 举报

发表于 2008-4-27 01:06:19 | 显示全部楼层 来自 美国
原帖由 chengweifeng 于 2008-4-26 21:47 发表

but it still works only for the second condition!



I don't think you understand this algorithm fully.  My code apparently works for both cases.  I give an example for the first situation:
  1. a=[0 0 0;1 0 0;1 1 0;1 0 1]
  2. b=[0.5 1 0.3;1 1.5 0.3;1 2 .5;.75 -1 .5]

  3. figure;
  4. hold off
  5. plot3(a(:,1),a(:,2),a(:,3),'r.','markersize',20)
  6. hold on;
  7. plot3(b(:,1),b(:,2),b(:,3),'m.','markersize',20)

  8. grid on

  9. ha=convhulln(a)

  10. for i=1:size(ha,1)
  11. j=ha(i,[1 2 3 1]);
  12. patch(a(j,1),a(j,2),a(j,3),[.5 .5 .5])
  13. end
  14. hold on

  15. hb=convhulln(b)
  16. for i=1:size(hb,1)
  17. j=hb(i,[1 2 3 1]);
  18. patch(b(j,1),b(j,2),b(j,3),[.5 .5 .5]*1.5)
  19. end

  20. alpha(.6)

  21. N=checkintersection(a,b)  %  N=1 means there exists an intersection
复制代码
N returns 1

Can you show a counter example when my code failed?

[ 本帖最后由 pcqsl 于 2008-4-27 01:11 编辑 ]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
回复 不支持

使用道具 举报

 楼主| 发表于 2008-5-6 09:27:44 | 显示全部楼层 来自 大连理工大学
To pcqsl:
是的,我错了,程序运行良好!
感谢pcqsl的鼎力相助,帮了我的大忙!:victory:
同时也向该论坛所有帮助和鼓励过我的兄弟们表示感谢,因为你们,这段孤独的旅程才会走的那么坚决!
回复 不支持

使用道具 举报

 楼主| 发表于 2008-5-22 00:15:35 | 显示全部楼层 来自 大连理工大学
惩罚函数法:有约束的最优化问题 ——〉无约束最优化问题

惩罚函数法的基本思想:借助惩罚函数把约束问题转化为无约问题,进而用无约束最优化方法来求解。由于约束的非线性,不能用消元法将问题转化为无约束问题,因此,在求解时必须同时兼顾到既使目标函数值下降,又要满足约束条件这两个方面。实现这一点的途径是由目标函数和约束函数组成辅助函数,把原来的约束问题转化为极小化辅助问题的无约束问题。

惩罚函数法包括:内部惩罚函数法(内点法)、外部惩罚函数法(外点法)、乘子法

内点法:对企图从内部穿越可行域边界的点在目标函数中加入相应的“障碍”,距边界越近,障碍越大,在边界上给以无穷大的障碍,从而保障迭代一直在可行域内部移动。

外点法:对违反约束的点在目标函数中加入相应的“惩罚”,而对可行点不予惩罚。此法的迭代点一般在可行域外部移动。

乘子法:?

***:在通常遇到的实际问题中,如果根据问题的性质,知道函数f(x,y)的最小值(最大值)一定在D的内部取得,而函数在D内 只有一个驻点,那么可以肯定该驻点处的函数值就是函数f(x,y)在D上的最小/最大值。

***:极值问题,对于函数的自变量,除了限制在函数的定义域内以外,并无其他条件-------〉无约束优化
       在实际问题中,有时会遇到对函数的自变量还有附加条件的极值问题-------〉有约束优化问题
回复 不支持

使用道具 举报

 楼主| 发表于 2011-4-2 09:20:10 | 显示全部楼层 来自 四川
最近重新关注这个问题,并对pcqsl提出的方法非常感兴趣,只是对8#程序中的
“Acon=zeros(1,Na+Nb); bcon=1;”两句始终理解不透,不知道其理论意义及其在
fmincon函数中的作用/意义何在?恳请路过高手指点迷津。不胜感激!
回复 不支持

使用道具 举报

发表于 2011-4-2 10:01:42 | 显示全部楼层 来自 山东烟台
14# chengweifeng 呵呵,还能翻出三年之前的帖子,不容易啊,
回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Simapps系列直播

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-10-4 23:19 , Processed in 0.053996 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表