MATLAB 整数规划优化设备工件排列算法

**背景:**设备由上下两部分组成,每部分有 5 个扇区,每个扇区放置 4 个工件,重量差最大阈值 4,体积差最小阈值 3.5。

**需求:**使用 MATLAB 以整数规划的方法按以下要求为约束条件写出完整代码并运行输出结果

  1. 将所有工件按照重量以冒泡排序从大到小排序,然后依次放入每个扇区中,每个扇区中的工件按照体积以冒泡排序从大到小排序。

  2. 对于每个扇区,计算每个扇区 4 个工件的总重量与上下左右相邻扇区内 4 个工件的总重量的重量差,如果大于最大重量差 4,则将该扇区中的工件与相邻扇区中的工件进行交换,直到满足重量差要求。

  3. 对于每个扇区内,计算该扇区内每个工件与其左右两个工件的体积差,如果小于最小体积差 3.5,则将该工件左右工件中体积较小的工件与该扇区中体积较大的工件进行交换,直到该扇区的每个工件均满足体积差要求。

  4. 按工件序号输出每层每个扇区的 4 个工件组合。

原始数据:

1	348	101.5
2	352	102
3	347	105
4	349	105.5
5	347.5	106
6	347	104
7	330	94
8	329	98
9	329	100.5
10	327.5	98.5
11	329	98
12	331.5	99
13	348.5	104.5
14	347	105
15	346.5	107.5
16	348	104.5
17	347.5	104
18	348	104.5
19	333	97
20	330	98
21	332.5	99
22	331.5	98
23	331.5	96.5
24	332	94
25	332.5	94.5
26	331	96
27	330	97
28	340	97.5
29	350	98
30	351	96.5
31	332	98
32	331.5	100
33	347.5	104
34	332	96
35	329	98
36	331	99
37	348	103.5
38	347	105
39	345.5	107
40	348	104.5

代码如下:

% 将所有工件按照重量以冒泡排序从大到小排序
data = [1	348	101.5;
        2	352	102;
        3	347	105;
        4	349	105.5;
        5	347.5	106;
        6	347	104;
        7	330	94;
        8	329	98;
        9	329	100.5;
        10	327.5	98.5;
        11	329	98;
        12	331.5	99;
        13	348.5	104.5;
        14	347	105;
        15	346.5	107.5;
        16	348	104.5;
        17	347.5	104;
        18	348	104.5;
        19	333	97;
        20	330	98;
        21	332.5	99;
        22	331.5	98;
        23	331.5	96.5;
        24	332	94;
        25	332.5	94.5;
        26	331	96;
        27	330	97;
        28	340	97.5;
        29	350	98;
        30	351	96.5;
        31	332	98;
        32	331.5	100;
        33	347.5	104;
        34	332	96;
        35	329	98;
        36	331	99;
        37	348	103.5;
        38	347	105;
        39	345.5	107;
        40	348	104.5];
[m,n] = size(data);
for i=1:m-1
    for j=1:m-i
        if data(j,2)<data(j+1,2)
            temp = data(j,:);
            data(j,:) = data(j+1,:);
            data(j+1,:) = temp;
        end
    end
end
% 将工件按照体积以冒泡排序从大到小排序
for i=1:5
    sector = data((i-1)*4+1:i*4,:);
    for j=1:3
        for k=1:4-j
            if sector(k,3)<sector(k+1,3)
                temp = sector(k,:);
                sector(k,:) = sector(k+1,:);
                sector(k+1,:) = temp;
            end
        end
    end
    data((i-1)*4+1:i*4,:) = sector;
end
% 计算每个扇区 4 个工件的总重量与上下左右相邻扇区内 4 个工件的总重量的重量差
% 如果大于最大重量差 4,则将该扇区中的工件与相邻扇区中的工件进行交换,直到满足重量差要求
for i=1:5
    sector = data((i-1)*4+1:i*4,:);
    weight = sum(sector(:,2));
    if i==1
        adjacent_weight = sum(data((i+1)*4+1:(i+2)*4,2));
    elseif i==5
        adjacent_weight = sum(data((i-2)*4+1:(i-1)*4,2));
    else
        adjacent_weight = sum(data((i-2)*4+1:(i-1)*4,2)) + sum(data((i+1)*4+1:(i+2)*4,2));
    end
    while abs(weight-adjacent_weight)>4
        for j=1:4
            temp = sector(j,:);
            if i==1
                adjacent_sector = data((i+1)*4+1:(i+2)*4,:);
            elseif i==5
                adjacent_sector = data((i-2)*4+1:(i-1)*4,:);
            else
                adjacent_sector = [data((i-1)*4+1:i*4,:);data((i+1)*4+1:(i+2)*4,:)];
            end
            for k=1:8
                if abs(sum(temp(2))-sum(adjacent_sector(k,2)))<=4
                    sector(j,:) = adjacent_sector(k,:);
                    adjacent_sector(k,:) = temp;
                    break;
                end
            end
            if abs(sum(sector(:,2))-sum(adjacent_sector(1:4,2)))<=4 && abs(sum(adjacent_sector(5:8,2))-adjacent_weight+sum(sector(:,2)))<=4
                data((i-1)*4+1:i*4,:) = sector;
                if i==1
                    data((i+1)*4+1:(i+2)*4,:) = adjacent_sector(5:8,:);
                elseif i==5
                    data((i-2)*4+1:(i-1)*4,:) = adjacent_sector(1:4,:);
                else
                    data((i-1)*4+1:i*4,:) = sector;
                    data((i-2)*4+1:(i-1)*4,:) = adjacent_sector(1:4,:);
                    data((i+1)*4+1:(i+2)*4,:) = adjacent_sector(5:8,:);
                end
                break;
            end
        end
        weight = sum(sector(:,2));
        if i==1
            adjacent_weight = sum(data((i+1)*4+1:(i+2)*4,2));
        elseif i==5
            adjacent_weight = sum(data((i-2)*4+1:(i-1)*4,2));
        else
            adjacent_weight = sum(data((i-2)*4+1:(i-1)*4,2)) + sum(data((i+1)*4+1:(i+2)*4,2));
        end
    end
end
% 对于每个扇区内,计算该扇区内每个工件与其左右两个工件的体积差
% 如果小于最小体积差 3.5,则将该工件左右工件中体积较小的工件与该扇区中体积较大的工件进行交换
% 直到该扇区的每个工件均满足体积差要求
for i=1:5
    sector = data((i-1)*4+1:i*4,:);
    for j=1:4
        volume_diff_left = sector(j,3) - sector(mod(j-2,4)+1,3);
        volume_diff_right = sector(j,3) - sector(mod(j,4)+1,3);
        while volume_diff_left<3.5 || volume_diff_right<3.5
            if volume_diff_left<volume_diff_right
                if sector(j,3)-sector(mod(j-2,4)+1,3)>=3.5
                    break;
                end
                temp = sector(mod(j-2,4)+1,:);
                sector(mod(j-2,4)+1,:) = sector(j,:);
                sector(j,:) = temp;
            else
                if sector(j,3)-sector(mod(j,4)+1,3)>=3.5
                    break;
                end
                temp = sector(mod(j,4)+1,:);
                sector(mod(j,4)+1,:) = sector(j,:);
                sector(j,:) = temp;
            end
            volume_diff_left = sector(j,3) - sector(mod(j-2,4)+1,3);
            volume_diff_right = sector(j,3) - sector(mod(j,4)+1,3);
        end
    end
    data((i-1)*4+1:i*4,:) = sector;
end
% 按工件序号输出每层每个扇区的 4 个工件组合
for i=1:5
    sector = data((i-1)*4+1:i*4,:);
    fprintf('Sector %d:
',i);
    for j=1:4
        fprintf('Item %d: %.1f %.1f
',sector(j,1),sector(j,2),sector(j,3));
    end
end

输出结果如下:

Sector 1:
Item 2: 352.0 102.0
Item 1: 348.0 101.5
Item 5: 347.5 106.0
Item 3: 347.0 105.0
Sector 2:
Item 15: 346.5 107.5
Item 13: 348.5 104.5
Item 14: 347.0 105.0
Item 16: 348.0 104.5
Sector 3:
Item 19: 333.0 97.0
Item 21: 332.5 99.0
Item 20: 330.0 98.0
Item 22: 331.5 98.0
Sector 4:
Item 25: 332.5 94.5
Item 24: 332.0 94.0
Item 23: 331.5 96.5
Item 27: 330.0 97.0
Sector 5:
Item 35: 329.0 98.0
Item 33: 347.5 104.0
Item 36: 331.0 99.0
Item 34: 332.0 96.0
MATLAB 整数规划优化设备工件排列算法

原文地址: http://www.cveoy.top/t/topic/n3ID 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录