标签:培训 第一周 sum Len 数模 zeros result end Gen
易拉罐下料问题
问题: 某公司采用一套冲压设备生产一种罐装饮料的易拉罐,这种易拉罐是用镀锡板冲压制成的.易拉罐为圆柱形,包括罐身、上盖和下底,罐身高10 cm,上盖和下底的直径均为5 cm.该公司使用两种不同规格的镀锡板原料:规格1的镀锡板为正方形,边长24 cm;规格2的镀锡板为长方形,长、宽分别为32 cm和28 cm.由于生产设备和生产工艺的限制,对于规格Ⅰ的镀锡板原料,只可以按照图1中的模式1、模式⒉或模式3进行冲压;对于规格2的镀锡板原料只能按照模式4进行冲压.使用模式1模式⒉模式3、模式4进行每次冲压所需要的时间分别为1.5 s,2 s,1 s,3 s.
该工厂每周工作40 h,每周可供使用的规格1规格⒉的镀锡板原料分别为5万张和2万张.目前每只易拉罐的利润为0.10元,原料余料损失为0. 001元/cm3(如果周末有罐身、上盖或下底不能配套组装成易拉罐出售,也看做是原料余料损失).
求解&结果
model :
sets :
row/1..4/ : remain, x, stampingTime, spec1, spec2, body, bottom;
endsets
calc :
pi = @acos(-1); !pi值;
s1 = pi * 5 *10; !罐身面积;
s2 = pi * 5 ^ 2 / 4; !罐底或盖面积;
remain(1) = 24 * 24 - s1 - 10 * s2; !模式1余料损失;
remain(2) = 24 * 24 - 2 * s1 - 4 * s2; !模式2余料损失;
remain(3) = 24 * 24 - 16 * s2; !模式3余料损失;
remain(4) = 32 * 28 - 4 * s1 - 5 * s2; !模式4余料损失;
totalTime = 40 * 3600; !总时间;
endcalc
data :
stampingTime = 1.5, 2, 1, 3; !冲压时间;
spec1 = 1, 1, 1, 0; !规格1;
spec2 = 0, 0, 0, 1; !规格2;
totalSpec1 = 50000; !规格1总数;
totalSpec2 = 20000; !规格2总数;
body = 1, 2, 0, 4; !每个模式的罐身个数;
bottom = 10, 4, 16, 5; !每个模式的罐底或者盖子个数;
enddata
max = 0.1 * count - 0.001 * (@sum(row : remain * x) + s1 * L1 + s2 * L2);
@sum(row : stampingTime * x) <= totalTime;
@sum(row : spec1 * x) <= totalSpec1;
@sum(row : spec2 * x) <= totalSpec2;
count <= @sum(row : body * x);
2 * count <= @sum(row : bottom * x);
L1 = @sum(row : body * x) - count;
L2 = @sum(row : bottom * x) - 2 * count;
@for(row : @gin(x));
@gin(count); @gin(L1); @gin(L2);
end
Global optimal solution found.
Objective value: 4298.014
Objective bound: 4298.014
Variable Value Reduced Cost
COUNT 160250.0 -0.1000000
L1 0.000000 0.1570796
L2 0.000000 0.1963495E-01
X( 1) 0.000000 0.2225708
X( 2) 40125.00 0.1833009
X( 3) 3750.000 0.2618407
X( 4) 20000.00 0.1695067
天然肠衣问题
问题:天然肠衣制作加工是我国的一个传统产业。肠衣经过清洗整理后被分割成长度不等的小段(原料),进入组装工序。传统的生产方式依靠人工,边丈量原料长度边心算,将原材料按指定根数和总长度组装出成品(捆)。原料按长度分档,通常以o.5米为一档,如: 14-14.4米按14米计算,14.5米-14.9米按14.5米计算,其余的依此类推。
所有模式
%%
clear, close, clc
tic
%%
l = [14 : 0.5 : 22.5, 23.5, 25.5]; % 20种长度
a = [35,29,30,42,28,42,45,49,50,64,52,63,49,35,27,16,12,2,6,1]; %20种原料的根数
cnt1 = floor(sum(a) / 4); % 由总根数求得最多捆数
cnt2 = floor(l * a' / 88.5); % 由总米数求得最多捆数
fprintf('最大捆数为:%5d\n', min(cnt1, cnt2))
n = size(l, 2);
aMin = zeros(1, n); % 初始化各种长度的类型肠衣的最多根数
for i = 1 : n
aMin(i) = min([5, floor(89.5 / l(i)), a(i)]); % 各种长度的类型肠衣的最多根数
end
count = 0;
res = zeros(1, 20);
result = zeros(1, 20);
for i1 = 0 : aMin(1)
result(1) = i1;
Len = result * [l(1 : 1), zeros(1,n - 1)]';
Gen = sum(result(1 : 1));
if Len > 89.5 || Gen > 5
break;
end
for i2 = 0 : aMin(2)
result(2) = i2;
Len = result * [l(1 : 2), zeros(1,n - 2)]';
Gen = sum(result(1 : 2));
if Len > 89.5 || Gen > 5
break;
end
for i3 = 0 : aMin(3)
result(3) = i3;
Len = result * [l(1 : 3), zeros(1,n - 3)]';
Gen = sum(result(1 : 3));
if Len > 89.5 || Gen > 5
break;
end
for i4 = 0 : aMin(4)
result(4) = i4;
Len = result * [l(1 : 4), zeros(1,n - 4)]';
Gen = sum(result(1 : 4));
if Len > 89.5 || Gen > 5
break;
end
for i5 = 0 : aMin(5)
result(5) = i5;
Len = result * [l(1 : 5), zeros(1,n - 5)]';
Gen = sum(result(1 : 5));
if Len > 89.5 || Gen > 5
break;
end
for i6 = 0 : aMin(6)
result(6) = i6;
Len = result * [l(1 : 6), zeros(1,n - 6)]';
Gen = sum(result(1 : 6));
if Len > 89.5 || Gen > 5
break;
end
for i7 = 0 : aMin(7)
result(7) = i7;
Len = result * [l(1 : 7), zeros(1,n - 7)]';
Gen = sum(result(1 : 7));
if Len > 89.5 || Gen > 5
break;
end
for i8 = 0 : aMin(8)
result(8) = i8;
Len = result * [l(1 : 8), zeros(1,n - 8)]';
Gen = sum(result(1 : 8));
if Len > 89.5 || Gen > 5
break;
end
for i9 = 0 : aMin(9)
result(9) = i9;
Len = result * [l(1 : 9), zeros(1,n - 9)]';
Gen = sum(result(1 : 9));
if Len > 89.5 || Gen > 5
break;
end
for i10 = 0 : aMin(10)
result(10) = i10;
Len = result * [l(1 : 10), zeros(1,n - 10)]';
Gen = sum(result(1 : 10));
if Len > 89.5 || Gen > 5
break;
end
for i11 = 0 : aMin(11)
result(11) = i11;
Len = result * [l(1 : 11), zeros(1,n - 11)]';
Gen = sum(result(1 : 11));
if Len > 89.5 || Gen > 5
break;
end
for i12 = 0 : aMin(12)
result(12) = i12;
Len = result * [l(1 : 12), zeros(1,n - 12)]';
Gen = sum(result(1 : 12));
if Len > 89.5 || Gen > 5
break;
end
for i13 = 0 : aMin(13)
result(13) = i13;
Len = result * [l(1 : 13), zeros(1,n - 13)]';
Gen = sum(result(1 : 13));
if Len > 89.5 || Gen > 5
break;
end
for i14 = 0 : aMin(14)
result(14) = i14;
Len = result * [l(1 : 14), zeros(1,n - 14)]';
Gen = sum(result(1 : 14));
if Len > 89.5 || Gen > 5
break;
end
for i15 = 0 : aMin(15)
result(15) = i15;
Len = result * [l(1 : 15), zeros(1,n - 15)]';
Gen = sum(result(1 : 15));
if Len > 89.5 || Gen > 5
break;
end
for i16 = 0 : aMin(16)
result(16) = i16;
Len = result * [l(1 : 16), zeros(1,n - 16)]';
Gen = sum(result(1 : 16));
if Len > 89.5 || Gen > 5
break;
end
for i17 = 0 : aMin(17)
result(17) = i17;
Len = result * [l(1 : 17), zeros(1,n - 17)]';
Gen = sum(result(1 : 17));
if Len > 89.5 || Gen > 5
break;
end
for i18 = 0 : aMin(18)
result(18) = i18;
Len = result * [l(1 : 18), zeros(1,n - 18)]';
Gen = sum(result(1 : 18));
if Len > 89.5 || Gen > 5
break;
end
for i19 = 0 : aMin(19)
result(19) = i19;
Len = result * [l(1 : 19), zeros(1,n - 19)]';
Gen = sum(result(1 : 19));
if Len > 89.5 || Gen > 5
break;
end
for i20 = 0 : aMin(20)
result(20) = i20;
Len = result * [l([1 : 20]), zeros(1,n - 20)]';
Gen = sum(result);
if Len >= 88.5 && Len <= 89.5 && Gen >= 4 && Gen <= 5
count = count + 1;
res(count, :) = result;
if i20 == 1
result(20) = 0;
break;
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
%%
toc
%% 文件写入
fid = fopen('changyi.txt', 'w');
for i = 1 : size(res, 1);
for j = 1 : 20
fprintf(fid, "%3d", res(i, j));
end
fprintf(fid, "\n");
end
fclose(fid);
结果
所有符合的模式1.txt
求解最优模式
Model :
Sets :
row/1..2783/ : x, y;
col/1..20/ : l, a;
link(row, col) : b;
Endsets
Data :
l=14,14.5,15,15.5,16,16.5,17,17.5,18,18.5,19,19.5,20,20.5,21,21.5,22,22.5,23.5,25.5;
a=35,29,30,42,28,42,45,49,50,64,52,63,49,35,27,16,12,2,6,1;
b = @file('changyi1.txt');
@text('30.txt') = @writefor(row(i) | x(i) #gt# 0 : @name(x), ' ', x(i), @newline(1));
Enddata
Min = z3;
137 = @sum(row : x); !目标函数1;
3 = x(1) + x(2) + x(3); !目标函数2;
z3 = @sum(row : y); !目标函数3;
@for(col(j) : @sum(row(i) : x(i) * b(i, j)) <= a(j)); !原料约束;
@for(row : x <= 10000 * y);
@for(row : x >= y);
@for(row : @gin(x));
@for(row : @bin(y));
End
Feasible solution found.
Objective value: 14.00000
Objective bound: 8.000000
Infeasibilities: 0.000000
Extended solver steps: 126503
Total solver iterations: 4318795
打印
%%
clear, close, clc
%% 读入数据
load 1.txt
totalModel = X1;
[~, modelRow, ~, modelNum] = textread('10.txt','%s %d %s %d');
x = [modelRow, modelNum];
%%
l = [14 : 0.5 : 22.5, 23.5, 25.5]; % 20种长度
a = [35,29,30,42,28,42,45,49,50,64,52,63,49,35,27,16,12,2,6,1]; %20种原料的根数
totalGen = 0; % 总根数
totalKun = 0; % 总捆数
totalLen = 0; % 总长度
for i = 1 : length(modelRow)
preLen = 0; % 当前长度
preGen = 0; % 当前根数
i1 = modelRow(i);
for k = 1 : length(l)
preLen = preLen + totalModel(i1, k) * l(k);
preGen = preGen + totalModel(i1, k);
end
fprintf("第%02d行,模式%04d为:", i, x(i, 1));
for j = 1 : length(l)
if totalModel(i1, j) > 0
fprintf('(%3.1f米, %1d根 )', l(j), totalModel(i1, j))
end
end
fprintf('长度%3.1f 米,%1d根,%2d捆 \n', preLen, preGen, x(i, 2))
totalLen = totalLen + preLen * x(i, 2);
totalKun = totalKun + x(i, 2);
totalGen = totalGen + preGen * x(i, 2);
end
fprintf('总长度%6.1f, 总根数%6d, 总捆数%5d\n',totalLen, totalGen, totalKun);
结果
如有错误以及可以改进的地方欢迎在下方评论区留言!
标签:培训,第一周,sum,Len,数模,zeros,result,end,Gen 来源: https://blog.csdn.net/zy440458/article/details/118633185
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。