装箱问题 FFD 算法实现(matlab 语言)
%装箱问题 FFD 算法实现
%物品的体积
W =
[0.125,0.268,0.159,0.168,0.126,0.168,0.249,0.536,0.427,0.179,0.182,0.149,0.156,0.152,0.135,0.1
61,0.191,0.183,0.174,0.198];
%箱子的编号
V = 1:1:20;
%箱子的体积
U = [1.4,1.3,1.0,1.1];
%箱子的状态
state = zeros(20,4);
%第一步:将物品体积按照从大到小排序
for i = 1:length(W) - 1
for j = i + 1 :length(W)
if W(i) < W(j)
temp = W(i);
temp_v = V(i);
W(i) = W(j);
V(i) = V(j);
W(j) = temp;
V(j) = temp_v;
end
end
end
%第二步:每次将体积最大的物品放到剩余体积最小的箱子中
for i = 1:length(W)
%找到剩余体积最大的箱子
max = 0;%保存剩余体积最大的体积
max_k = 0;%保存最大体积的箱子的编号
for j = 1:length(U)
if max < U(j)
max = U(j);
max_k = j;
end
end
%将最大的物品放到剩余体积最大的箱子
if W(i) < U(max_k) & state(V(i),max_k) == 0%如果物品的体积比剩余的箱子的体积大
state(V(i),max_k) = 1;%状态发生变化
U(max_k) = U(max_k) - W(i);%箱子的体积变化
else%如果物品的体积比箱子的体积小,则找到下一个剩余体积最小的箱子
maxx = 0;