矩形排料问题组合优化问题.docx
《矩形排料问题组合优化问题.docx》由会员分享,可在线阅读,更多相关《矩形排料问题组合优化问题.docx(19页珍藏版)》请在冰豆网上搜索。
矩形排料问题组合优化问题
《二维矩形条带装箱问题的底部左齐择优匹配算法_蒋兴波》matlab的实现,不包括遗传算法部分。
functionarea=PackingAlgorithm(length,width,length1,width1,length2,width2,length3,width3,restrict1,restrict2)
area=0;
frameCount=1;
count1=0;
count2=0;
runLLABF;
functionrunLLABF
rectBig.length=length;
rectBig.width=width;
rectSmall
(1).length=length1;
rectSmall
(1).width=width1;
rectSmall
(1).color='r';
rectSmall
(2).length=length2;
rectSmall
(2).width=width2;
rectSmall
(2).color='b';
rectSmall(3).length=length3;
rectSmall(3).width=width3;
rectSmall(3).color='g';
edges
(1).x=0;
edges
(1).y=0;
edges
(1).length=rectBig.length;
edges
(2).x=-100;
edges
(2).y=10000;
edges
(2).length=0;
edges(3).x=rectBig.length+100;
edges(3).y=10000;
edges(3).length=0;
while
(1)
flag=-1;
if(flag<0)
[sortedEdges,lowestEdge,id]=edgesSort(edges);
[edges,flag]=FullFitFirst(sortedEdges,lowestEdge,id,rectSmall);
if(flag<0)
[sortedEdges,lowestEdge,id]=edgesSort(edges);
[edges,flag]=WidthFitFirst(sortedEdges,lowestEdge,id,rectSmall);
end
if(flag<0)
[sortedEdges,lowestEdge,id]=edgesSort(edges);
[edges,flag]=HeightFitFirst(sortedEdges,lowestEdge,id,rectSmall);
end
if(flag<0)
[sortedEdges,lowestEdge,id]=edgesSort(edges);
[edges,flag]=PlaceabelFirst(sortedEdges,lowestEdge,id,rectSmall);
end
if(flag<0)
[sortedEdges,lowestEdge,id]=edgesSort(edges);
[edges,flag]=cannotPalce(sortedEdges,lowestEdge,id,rectSmall,flag)
end
end
ifcount1>=restrict1
rectSmall
(1).length=100000;
rectSmall
(1).width=100000;
end
ifcount2>=restrict2
rectSmall
(2).length=100000;
rectSmall
(2).width=100000;
end
sortRect=sort([rectSmall
(1).length,rectSmall
(1).width,...
rectSmall
(2).length,rectSmall
(2).width,...
rectSmall(3).length,rectSmall(3).width]);
minRect=sortRect
(1);
minRect2=sortRect
(2);
[sortedEdges,lowestEdge,id]=edgesSort(edges);
[~,h]=size(sortedEdges);
fori=1:
h
if(sortedEdges(i).y+minRect<=width)
break;
end
end
ifi==h
break;
end
ifi==h-1&&lowestEdge.x+minRect2>length
break
end
ifframeCount>300
break;
end
end
end
functioninitial
rectBig.length=30;
rectBig.width=20;
rectSmall
(1).length=4;
rectSmall
(1).width=3;
rectSmall
(2).length=3;
rectSmall
(2).width=3;
rectSmall(3).length=4;
rectSmall(3).width=1;
edges
(1).x=0;
edges
(1).y=0;
edges
(1).length=rectBig.length;
%edges
(1).x=12;
%edges
(1).y=10;
%edges
(1).length=2;
%
%edges
(2).x=3;
%edges
(2).y=8;
%edges
(2).length=2;
%
%edges(3).x=6;
%edges(3).y=4;
%edges(3).length=1;
%
%edges(4).x=1;
%edges(4).y=8;
%edges(4).length=2;
end
function[sortedEdges,lowestEdge,id]=edgesSort(edges)
sortedEdges=edges;
[~,m]=size(sortedEdges);
forj=1:
m
fori=j:
m
if(sortedEdges(i).xtmpedge=sortedEdges(j);
sortedEdges(j)=sortedEdges(i);
sortedEdges(i)=tmpedge;
end
end
end
[~,m]=size(sortedEdges);
disp(m)
if(m>=2)
i=2;
while
(1)
if(sortedEdges(i-1).y==sortedEdges(i).y)
sortedEdges(i-1).length=sortedEdges(i-1).length+sortedEdges(i).length;
forj=i:
(m-1)
sortedEdges(j)=sortedEdges(j+1);
end
sortedEdges(m)=[];
[~,n]=size(sortedEdges);
m=n;
continue;
end
[~,n]=size(sortedEdges);
m=n;
ifi==n
break;
end
i=i+1;
end
else
lowestEdge=sortedEdges
(1);
end
lowestEdges=sortedEdges;
[~,n]=size(lowestEdges);
y=lowestEdges
(1).y;
fori=2:
n
if(y>lowestEdges(i).y)
y=lowestEdges(i).y;
end
end
fori=1:
n
if(lowestEdges(i).y==y)
lowestEdge=lowestEdges(i);
id=i;
break;
end
end
end
function[Edges,flag]=FullFitFirst(Edges,lEdge,lEdgeId,rectSmall)
fori=1:
3
if((rectSmall(i).length==lEdge.length)...
&&((lEdge.y+rectSmall(i).width==Edges(lEdgeId-1).y)||(lEdge.y+rectSmall(i).width==Edges(lEdgeId+1).y)))
if(lEdge.y+rectSmall(i).width<=width)
Edges(lEdgeId).y=lEdge.y+rectSmall(i).width;
flag=1;
figurePlot(lEdge,rectSmall(i),rectSmall(i).color);
area=area+rectSmall(i).width*rectSmall(i).length;
ifi==1
count1=count1+1;
end
ifi==2
count2=count2+1;
end
break;
else
flag=-1;
end
else
flag=-1;
end
if((rectSmall(i).width==lEdge.length)...
&&((lEdge.y+rectSmall(i).length==Edges(lEdgeId-1).y)||(lEdge.y+rectSmall(i).length==Edges(lEdgeId+1).y)))
if(lEdge.y+rectSmall(i).length<=width)
Edges(lEdgeId).y=lEdge.y+rectSmall(i).length;
flag=1;
figurePlotRotation(lEdge,rectSmall(i),rectSmall(i).color);
area=area+rectSmall(i).width*rectSmall(i).length;
ifi==1
count1=count1+1;
end
ifi==2
count2=count2+1;
end
break;
else
flag=-1;
end
else
flag=-1;
end
end
end
function[Edges,flag]=WidthFitFirst(Edges,lEdge,lEdgeId,rectSmall)
count=1;
%selected=zeros(1,3);
fori=1:
3
if(rectSmall(i).length==lEdge.length&&rectSmall(i).width+lEdge.y<=width)
selected(count).index=i;
selected(count).area=rectSmall(i).length*rectSmall(i).width;
selected(count).rotation=0;
count=count+1;
flag=1;
continue;
else
flag=-1;
end
if(rectSmall(i).width==lEdge.length&&rectSmall(i).length+lEdge.y<=width)
selected(count).index=i;
selected(count).area=rectSmall(i).length*rectSmall(i).width;
selected(count).rotation=1;
count=count+1;
flag=1;
else
flag=-1;
end
end
if(flag==1)
[~,n]=size(selected);
fori=1:
n
forj=i:
n
if(selected(i).area>selected(j).area)
tmpSelected=selected(i);
selected(i)=selected(j);
selected(j)=tmpSelected;
end
end
end
index=selected(n).index;
if(selected(n).rotation==0)
iflEdge.y+rectSmall(index).width<=width
Edges(lEdgeId).y=lEdge.y+rectSmall(index).width;
flag=1;
figurePlot(lEdge,rectSmall(index),rectSmall(index).color);
area=area+rectSmall(index).width*rectSmall(index).length;
ifindex==1
count1=count1+1;
end
ifindex==2
count2=count2+1;
end
else
flag=-1;
end
end
if(selected(n).rotation==1)
iflEdge.y+rectSmall(index).length<=width
Edges(lEdgeId).y=lEdge.y+rectSmall(index).length;
flag=1;
figurePlotRotation(lEdge,rectSmall(index),rectSmall(index).color);
area=area+rectSmall(index).width*rectSmall(index).length;
ifindex==1
count1=count1+1;
end
ifindex==2
count2=count2+1;
end
else
flag=-1;
end
end
end
end
function[Edges,flag]=HeightFitFirst(Edges,lEdge,lEdgeId,rectSmall)
[~,n]=size(Edges);
fori=1:
3
if(rectSmall(i).lengthEdges(n+1).x=Edges(lEdgeId).x+rectSmall(i).length;
Edges(n+1).y=Edges(lEdgeId).y;
Edges(n+1).length=Edges(lEdgeId).length-rectSmall(i).length;
Edges(lEdgeId).y=lEdge.y+rectSmall(i).width;
Edges(lEdgeId).length=rectSmall(i).length;
flag=1;
figurePlot(lEdge,rectSmall(i),rectSmall(i).color);
area=area+rectSmall(i).width*rectSmall(i).length;
ifi==1
count1=count1+1;
end
ifi==2
count2=count2+1;
end
else
flag=-1;
end
if(flag==1)
break;
end
if(rectSmall(i).width<=lEdge.length)&&(lEdge.y+rectSmall(i).length==Edges(lEdgeId-1).y)
Edges(n+1).x=Edges(lEdgeId).x+rectSmall(i).width;
Edges(n+1).y=Edges(lEdgeId).y;
Edges(n+1).length=Edges(lEdgeId).length-rectSmall(i).width;
Edges(lEdgeId).y=lEdge.y+rectSmall(i).length;
Edges(lEdgeId).length=rectSmall(i).width;
flag=1;
figurePlotRotation(lEdge,rectSmall(i),rectSmall(i).color);
area=area+rectSmall(i).width*rectSmall(i).length;
ifi==1
count1=count1+1;
end
ifi==2
count2=count2+1;
end
else
flag=-1;
end
if(flag==1)
break;
end
end
end
function[Edges,flag]=PlaceabelFirst(Edges,lEdge,lEdgeId,rectSmall)
count=1;
[~,m]=size(Edges);
selected
(1).index=1;
selected
(1).area=0;
selected
(1).rotation=0;
selected
(2).index=1;
selected
(2).area=0;
selected
(2).rotation=0;
selected(3).index=1;
selected(3).area=0;
selected(3).rotation=0;
fori=1:
3
if(rectSmall(i).lengthselected(count).index=i;
selected(count).area=rectSmall(i).length*rectSmall(i).width;
selected(count).rotation=0;
count=count+1;
flag=1;
continue;
else
flag=-1;
end
if(rectSmall(i).widthselected(count).index=i;
selected(count).area=rectSmall(i).length*rectSmall(i).width;
selected(count).rotation=1;
count=count+1;
flag=1;
else
flag=-1;
end
end
ifflag==1
n=count-1;
fori=1:
n
forj=i:
n
if(selected(i).area>selected(j).area)
tmpSelected=selected(i);
selected(i)=selected(j);
selected(j)=tmpSelected;
end
end
end
index=selected(n).index;
if(selected(n).rotation==0)
Edges(m+1).x=lEdge.x+rectSmall(index).length;
Edges(m+1).y=lEdge.y;
Edges(m+1).length=lEdge.length-rectSmall(index).length;
Edges(lEdgeId).y=lEdge.y+rectSmall(index).width;
Edges(lEdgeId).length=rectSmall(index).length;
figurePlot(lEdge,rectSmall(index),rectSmall(index).color);
area=area+rectSmall(index).width*rectSmall(index).length;
ifindex==1
count1=count1+1;
end
ifindex==2
count2=count2+1;
end
end
if(selected(n).rotation==1)
Edges(m+1).x=lEdge.x+rectSmall(index).width;
Edges(m+1).y=lEdge.y;
Edges(m+1).length=lEdge.length-rectSmall(index).width;
Edges(lEdgeId).y=lEdge.y+rectSmall(index).length;
Edges(lEdgeId).length=rectSmall(index).width;
figurePlotRotation(lEdge,rectSmall(index),rectSmall(index).color);
area=