2021年高教杯数学建模国赛C题生产企业原材料的订购与运输

avatar
作者
猴君
阅读量:0

最近我们小组做了建模训练,选择的题目是21年的国赛C题,我负责软件部分,这题我刚好会做,第一次独自完成了代码,有点激动,和大家分享一下,共同学习,不过因为我个人比较懒,讲解的不怎么多。。。不过会分享一下完整的代码

其实我的代码还有点问题,转运方案没有按照它的要求,当时没注意到这个问题,所以输出的结果和题目给的表格不太一样。

我路子可能比较野,写的代码有点杂乱无章,感觉算小屎堆了(悲)

因为事后没有整理,所以代码会有一些多余的部分,是当时用过后发,之后又不用的。。。

我也是个建模新手,还有很多不足的地方,欢迎大家指出我的错误,一起交流

大家会看到一个Excel,叫‘问题1评分排名.xlsx’,当时鬼迷心窍弄成这样,现在一看实属脱裤子放屁。这个表格大家复制就能用了

供应商ID材料成本总供货量订单数达标率评分排名
S0011.149250.2087916.15234E-05289
S0021.2273710.6736840.000692156105
S0031131381910.8542710.03694448243
S0041.164330.2038830.000103899245
S0051.269121070.9035090.01939939454
S006130130.21.01257E-05387
S0071.269482400.8666670.01950140953
S008141150.253.79261E-05313
S0091.131190.151.51885E-05348
S0101.1170320.2272730.000400748129
S011185320.6511630.00016209187
S0121.229120.1470598.21066E-06397
S013144200.6551724.70146E-05304
S0141.228160.5925931.05163E-05381
S0151.228150.2777789.81485E-06388
S0161.237170.7272732.77322E-05324
S0171.2138250.3134330.000310438153
S0181.166310.3472220.00010913233
S0191.1109120.1551720.00022839173
S020172330.2359550.000126006210
S0211.280420.6290320.000149329195
S0221.2140160.196970.000315793150
S023116311330.606250.0045182359
S0241.1124450.4415580.000272282163
S02513751260.8592590.00098176891
S0261.228140.8235299.11532E-06392
S0271.281300.5957450.000150732193
S0281.1106190.3469390.000220167175
S0291.168290.2111110.000114417228
S0301.2162280.9655170.000378089133
S0311.1412072400.9250.1160433830
S032171300.3906250.000122868215
S033130170.8421051.25537E-05364
S034130140.0909091.07151E-05379
S0351.2144550.420.000329074145
S0361.1124490.870370.000272614162
S0371506861570.9006210.14275532725
S0381.236220.18752.69251E-05327
S0391385450.6349210.00100650687
S0401.1319052400.9041670.08983018731
S041171280.3225810.000122644216
S0421.2138250.3392860.000310438152
S0431.239180.2205883.32102E-05318
S044130140.7647061.07163E-05377
S0451.131200.2195121.57757E-05346
S0461.21971400.6540880.000486107119
S0471.166280.2022470.000108744237
S0481.165260.440.000105729243
S0491.238190.441863.08765E-05320
S0501.166290.906250.000108869235
S0511.229150.1967211.02113E-05385
S0521.254250.5333337.51754E-05271
S0531.2773110.000139675200
S0541.14131130.7441860.00108777270
S0551.1240412400.90.06766932435
S0561.228170.2954551.12168E-05375
S0571.150220.3448286.37202E-05285
S0581.134200.2105262.15241E-05337
S059147470.3469396.25025E-05288
S060186100.360.000163567186
S061184300.3731340.000159113189
S0621.27550.2352940.000132477203
S063130210.2077921.51106E-05350
S0641.23971260.9029850.00104353180
S0651.2189640.6136360.000455845123
S0661.23881320.8510640.00101863184
S06712331690.9881660.000589569112
S068142160.159424.08295E-05309
S0691.1169700.5612240.000400273130
S070146260.7428575.36666E-05300
S0711.2191120.0833330.000459402122
S0721.228170.3170731.12168E-05374
S0731.132170.739131.59024E-05345
S0741130511710.7213110.0366992744
S0751.23901250.8507460.00102381783
S07613761970.9154230.0009902489
S0771.1128120.1724140.000281908161
S0781.285531010.7943930.02402372650
S0791.164240.4038460.000102722248
S0801192372400.8750.05413160437
S0811.149230.6774196.11551E-05290
S0821.258270.2777788.64829E-05263
S0831.228140.3759.11406E-06393
S084170180.2112680.000118955220
S085146230.3571435.3017E-05301
S0861179492060.8421050.05050194339
S0871.264270.6578950.000103073246
S0881.14251390.8235290.00112292464
S0891.2141250.558140.00031888149
S0901145290.6279070.000330292144
S0911.273290.5192310.000128321206
S0921.13981260.8846150.00104633977
S0931.236120.1694922.38267E-05333
S094141240.4117654.00253E-05311
S0951.132130.1408451.40651E-05358
S0961.273280.4629630.000128216208
S0971.263300.2972970.000100704249
S0981.14132060.928230.00109440468
S0991.260250.5365859.17327E-05259
S100142160.0989014.08295E-05310
S1011.256250.2328778.06782E-05266
S1021.2131500.50.000292281158
S103130180.2539681.31827E-05363
S1041.2155250.2638890.000358284137
S1051.1120250.3454550.000259803167
S1061.277370.9230770.00014037197
S107169290.3650790.000117194224
S1081.12409502400.9458330.6789231854
S1091.236190.3214292.58355E-05328
S11014261280.7077920.00112509663
S1111.131160.3684211.34925E-05361
S1121.112350.31250.000267727165
S11313851260.9015150.00100983986
S1141.2109312290.8528140.03072528245
S1151.23981210.7234040.00104605279
S1161.275230.5945950.000133342202
S1171.1123610.4387760.000270995164
S1181.166280.3571430.000108744236
S1191.22990.1702136.27734E-06401
S1201.133160.406251.75815E-05343
S1211.2137260.4444440.000307664154
S1221.273310.6444440.000128543205
S1231.264492400.8083330.01809527256
S1241.134140.41.92079E-05340
S1251.149160.1864416.01054E-05292
S126147540910.840.13388981127
S1271.2158240.3281250.000366697135
S1281.2133510.6619720.00029796157
S129115251920.7783250.00422069260
S1301.260280.1979179.21415E-05258
S1311.11375122400.9083330.38743277711
S1321.263280.540.00010043250
S133171260.7666670.000122436217
S1341.152210.2769236.90704E-05280
S13518750.2380950.000166287183
S136142190.4090914.1421E-05308
S1371.133190.1785711.89143E-05341
S1381.14111420.7610060.00108381472
S1391.11518622220.968750.42787136810
S1401.13020472190.8863640.8510957893
S1411.14201200.8538460.00110780667
S1421.1121150.3043480.00026226166
S1431.2827872400.8958330.23321661920
S1441.22830.151.40666E-06402
S1451.263280.301370.00010043251
S1461.13521690.711230.00092059698
S1471.110880.350.000225495174
S1481.259270.1758.92396E-05260
S1491251720.8641980.000630385108
S1501.218062400.7958330.00501322757
S15111944982400.9541670.5480204935
S1521.23981240.7080290.00104622378
S1531.1175130.1785710.000414334128
S1541.27634770.6153850.0214339352
S1551.240190.2777783.60939E-05315
S1561.228160.2291671.05157E-05384
S1571.23951200.8409090.00103756981
S1581.237200.3529412.86471E-05321
S1591.2215470.4634150.000527955116
S160145200.2205884.97234E-05303
S161147200.1944445.51741E-05298
S162128140.1011249.11309E-06394
S1631.2252460.4615380.000632024107
S164172220.2179490.000124864212
S1651.275630.5894740.000139395201
S1661.132160.3076921.54154E-05347
S1671.2136280.30.000304934155
S1681.131170.21.40455E-05359
S1691.13591180.951220.00093636194
S1701.2135240.5121950.000301959156
S171172290.3968250.000125537211
S1721.13561280.8613140.00092858496
S1731372160.3589740.00096945692
S1741.13461250.9841270.000900333100
S1751.13661380.9645390.00095731693
S1761.151510.48757.36843E-05274
S177147210.2337665.53475E-05297
S1781.21634510.000381681131
S1791.131140.1621621.24307E-05366
S1801.152520.8474587.65001E-05269
S1811.236160.1896552.48761E-05331
S1821153270.5416670.000352724139
S183128150.1463419.81411E-06390
S1841.273260.4150940.000128016209
S1851.264260.2409640.000102951247
S18613771220.7878790.00098713790
S1871.274260.273810.000130808204
S1881.151230.8846156.66242E-05283
S1891.288921510.8553460.02497915148
S1901.14830.1428575.63779E-05296
S1911.13511190.751880.00091396899
S192130160.1764711.19304E-05368
S1931.1101250.80.000206402176
S19411013652400.9458330.28556979717
S1951.236170.3414632.51805E-05330
S1961.240200.4166673.63449E-05314
S1971.13541190.8480.00092238897
S1981.254220.1494257.47328E-05273
S1991.1130300.56250.000288156159
S2001.165300.3287670.00010623242
S2011.281989280.5306120.2309677921
S2021.14001270.872180.00105201476
S2031.236230.8214292.73152E-05325
S2041.28150.1481480.000149381194
S2051.260300.2957759.24397E-05256
S206131140.9333331.24321E-05365
S2071154440.4065930.000356347138
S2081.28757900.5317460.0245985849
S2091.2163370.397260.000381269132
S2101156941180.6618710.04414718541
S2111.258540.838719.2344E-05257
S212131100.0882351.05487E-05380
S21312461630.9578310.00062473109
S2141.152520.4516137.64999E-05270
S2151.150140.1315796.26627E-05287
S2161.14251290.7162160.00112234365
S2171.2139200.3720930.000313084151
S2181154832400.9291670.04355281842
S2191.236250.6666672.81304E-05322
S220170280.3870970.000119861219
S2211.23921230.976190.00102931582
S2221.132150.3846151.49461E-05351
S223128170.3783781.12163E-05376
S2241.1108950.7818180.000234872172
S2251.132140.81251.44962E-05352
S2261.2200340.4406780.000485251120
S2271.2254300.6923080.000637197106
S2281151150.228070.000346755140
S2291.23548872400.9458330.9999999891
S2301.152250.6764716.96937E-05279
S231131160.1410261.34922E-05362
S2321.1119260.5750.000257038169
S2331.1179570.5647060.000427327127
S234145450.2772285.69767E-05295
S2351.1189310.7631580.000454188124
S236128160.61.05158E-05383
S2371.23281190.9421490.00084944102
S2381.13230.0363641.13591E-05372
S23914301450.9862070.00113732861
S2401.14950.0781255.92449E-05293
S241128110.1428577.01013E-06398
S2421.279290.3846150.000145053196
S2431.263270.220930.000100301253
S2441164062400.8708330.04615383340
S24513861520.8036810.00101438485
S246144180.1805564.66366E-05305
S2471566982400.9458330.15969730624
S2481.169300.3058820.000117314223
S2491.167310.450.000111897229
S2501.235120.5789472.11804E-05338
S2511.23660.0925932.28153E-05335
S252148240.3220345.86212E-05294
S2531223680.9305560.000551517114
S2541.2161260.5333330.000375206134
S2551.236180.4444442.55005E-05329
S2561.14111230.8538460.00108268173
S257131130.1960781.19263E-05370
S2581.14141250.750.0010912269
S2591.167300.5185190.000111767231
S2601.11841280.7517240.000448532125
S2611.169310.4153850.000117437222
S262128130.1785718.41215E-06395
S26314271320.7894740.0011281362
S2641.13250.51.16162E-05371
S2651.2141590.60.000321021148
S2661.265192400.8708330.01829252555
S267182620.5393260.000158066190
S26811297862400.93750.36566075215
S2691.22111430.8451610.000525212117
S270192580.8593750.000184726177
S2711247490.7096770.000618063111
S2721.132140.7777781.44961E-05353
S2731.29484900.8383840.02664727446
S2741.277320.5918370.000139782198
S2751.21585532400.9458330.4467267458
S2761.152190.1923086.87995E-05281
S277130160.1463411.19303E-05369
S2781.262260.2168679.74022E-05255
S279189360.6938780.000173641179
S2801.1127750.4741380.000283765160
S2811.236150.2653062.4588E-05332
S2821.21693402400.98750.4771247337
S2831.230140.0882351.07157E-05378
S28414659724010.13123249828
S2851.235190.1772152.34169E-05334
S2861.167300.7250.000111767230
S2871.16660.10.000107142239
S2881.230130.3333331.01264E-05386
S2891.240140.2142863.50228E-05316
S290131190.2465751.51885E-05349
S2911.279842190.860360.02242068251
S2921.29160900.6186440.02573423847
S2931.219280.1194030.000462181121
S2941188422400.8791670.05301849238
S2951.256560.5903618.782E-05262
S2961.262330.629.84036E-05254
S297131140.1621621.24306E-05367
S2981.151130.7647066.53583E-05284
S2991.262620.3455880.000104921244
S3001.229980.6363640.000763699104
S301189310.750.000173181180
S3021.259250.1910118.89639E-05261
S3031.15490.6428577.3483E-05275
S3041.2156880.830.000365824136
S305128160.751.05162E-05382
S30611260962400.9541670.35526225616
S3071.2781961710.970930.22027906522
S3081.11369982400.9166670.38598431512
S3091.237180.5925932.80229E-05323
S3101.14061250.81250.00106874974
S3111.165310.31250.000106366240
S3121.277320.5081970.000139782199
S3131384380.4637680.0010035588
S314116702350.7787230.00463008358
S3151.132120.4230771.36576E-05360
S3161.169280.5813950.000117079225
S3171.229210.21.43009E-05354
S3181.2346990.887850.000898758101
S3191.166310.3380280.00010913234
S320142200.2258064.16399E-05307
S3211.13320.1666671.41077E-05356
S3221.168290.3906250.000114417227
S3231.229120.4615388.21116E-06396
S3241.14241260.8602940.00111936766
S325173290.3934430.000128321207
S326182300.338710.000153525191
S3271.135170.5172412.26922E-05336
S3281.13340.0810811.42463E-05355
S3291.21565182400.9416670.4409920749
S3301.11366522400.91250.3850092813
S331170500.5135140.00012324213
S3321.2305460.9019610.000781228103
S3331.1178970.6129030.000428024126
S3341148100.0694440.000338221141
S3351.150180.2884626.31314E-05286
S3361.2144310.6382980.000327566146
S3371.236220.72.69255E-05326
S3381.1301092040.9411760.08476898932
S339144440.5833335.4236E-05299
S3401.11714262400.9416670.4830031236
S3411.263270.350.000100301252
S3421224390.9750.000552974113
S343128110.1142867.0101E-06399
S3441.15490.1111117.34829E-05276
S345187140.3939390.000166513182
S3461.1232402400.8708330.06541209736
S347182280.3478260.000153346192
S3481.2924211940.9195980.2603654118
S349142230.5714294.23596E-05306
S3501.239200.2266673.37387E-05317
S351144370.9487185.16705E-05302
S3521.2890312400.9166670.25081233619
S3531.168330.8250.000114931226
S3541.257230.2656258.3165E-05265
S35513170.2592599.44272E-06391
S35611303072400.950.3671289414
S3571220540.750.000542333115
S3581.134100.6666671.80475E-05342
S359186330.6458330.000164977184
S3601.14051310.6821190.00106629175
S36113280802400.8791670.9244573212
S362191690.9857140.000183823178
S3631.256190.3478267.99072E-05267
S3641.1287632400.9416670.08097598333
S3651416312400.950.1172382229
S3661.149490.7704926.80754E-05282
S3671.1263352400.90.07413384534
S368186130.3666670.000163662185
S3691146260.3283580.000332988143
S3701.258250.488.61984E-05264
S3711.154240.2428577.50215E-05272
S3721.229100.256416.9109E-06400
S3731.230150.1690141.13179E-05373
S3741492242400.9958330.13863543326
S3751.1112130.1311480.000236863171
S3761.13581210.7898550.0009337495
S377171310.4912280.000122986214
S3781.149210.3090916.08165E-05291
S3791249360.9729730.000623265110
S380185250.460.000161506188
S3811.2201280.843750.000487885118
S382128150.3783789.81437E-06389
S3831147270.7352940.000335839142
S384189190.3260870.000172362181
S3851.153240.2439027.22719E-05277
S3861.1119410.7037040.000257968168
S3871.133130.1710531.64103E-05344
S3881.1114640.9014080.00024634170
S3891.169310.5434780.000117437221
S3901.166130.2040820.000107415238
S391170310.3037970.000120211218
S3921.14121290.8478260.0010858371
S3931.133101.40902E-05357
S3941.2144100.1363640.000326951147
S3951.275843740.8690480.21364824123
S3961.238250.219783.28205E-05319
S3971.152520.8644077.65001E-05268
S398141160.353.81135E-05312
S3991.165300.3606560.00010623241
S4001.153130.1666677.0951E-05278
S4011.167290.3731340.000111642232
S4021.13580.0547952.03274E-05339

第一问

第一问选取了

材料成本总供货量订单数达标率

几个指标,利用熵权topsis法进行排名,取前五十名

不过数据处理的代码里还有一些计算其他的,写了但是没用上。。。

熵权topsis代码非原创,是借鉴了某位大佬的,不过我忘记是哪位的了。。。。

如果大佬不满意的话请联系我,我把它删掉

第一问代码

%%第一问代码 %处理数据 clc;clear [num,txt,raw]=xlsread('附件1 近5年402家供应商的相关数据.xlsx',1); [num2,txt2,raw2]=xlsread('附件1 近5年402家供应商的相关数据.xlsx',2); %计算供给达标周数 sum_hege=zeros(402,1); [m,n]=size(num); for j=1:m     for i=1:n         if num(j,i)~=0&&num2(j,i)>=num(j,i)             sum_hege(j,1)=sum_hege(j,1)+1;%计算供给达标的周数         end     end end  num_gongji=sum(num~=0,2);%计算企业要求供应商供给的周数 num2_sum=sum(num2,2);%求出供应商一共提供了多少材料; num2_mean=mean(num2,2);%得出平均每个星期提供的材料数量; num2_gongji=sum(num2~=0,2);%计算供应商有供货的周数; num2_wugongji=sum(num2==0,2);%计算没有供货的周数; num2_month=num2_mean*4; dabiaolv=sum_hege./num_gongji;  %将材料量化 cailiao=ones(402,1); xunzhaoA = strcmp(txt2(2:end,2),'A');%找出A,标记为1 mA=find(xunzhaoA==1);%找出字符串所在的索引 xunzhaoB = strcmp(txt2(2:end,2),'B');%找出A,标记为1 mB=find(xunzhaoB==1);%找出字符串所在的索引  cailiao(mA)=1.2; cailiao(mB)=1.1;  data=[cailiao num2_sum  num2_gongji dabiaolv];  bingtu=[sum(num2_sum<100),sum(num2_sum>=100&num2_sum<1000),sum(num2_sum>=1000&num2_sum<10000),sum(num2_sum>=10000)]; pie3(bingtu) legend('供货量小于100','供货量100-1000','供货量1000-10000','供货量大于10000')  %% %熵权topsis x=data;%导入数据 [n,m]=size(x); % 数据的归一化处理 [X,ps]=mapminmax(x',0,1);  ps.ymin=0.002; % 归一化后的最小值 ps.ymax=0.996; % 归一化后的最大值 ps.yrange=ps.ymax-ps.ymin; % 归一化后的极差 X=mapminmax(x',ps); X=X';  % X为归一化后的数据  % 计算比重  for i=1:n      for j=1:m          p(i,j)=X(i,j)/sum(X(:,j));      end  end  %计算熵值  k=1/log(n);  for j=1:m      e(j)=-k*sum(p(:,j).*log(p(:,j)));  end  d=ones(1,m)-e;  % 计算信息熵冗余度 w=d./sum(d);    % 求权值w  %TOPSIS综合评价 A=data;%评价矩阵 W=w%权重  [ma,na]=size(A);  for i=1:na      B(:,i)=A(:,i)*W(i); %得到加权标准化矩阵 end V1=zeros(1,na);            %初始化正理想解和负理想解 V2=zeros(1,na); BMAX=max(B);               %取加权标准化矩阵每列的最大值和最小值 BMIN=min(B);                for i=1:na      V1(i)=BMAX(i);      V2(i)=BMIN(i); end for i=1:ma         C1=B(i,:)-V1;      S1(i)=norm(C1);   %S1,S2分别为离正理想点和负理想点的距离      C2=B(i,:)-V2;      S2(i)=norm(C2);      T(i)=S2(i)/(S1(i)+S2(i)); end output=T'%得出评分 xlswrite('评分.xlsx',output) 

第二问

为了省事,我直接选择只从以上五十家购买材料,我发现,好像,应该,可能只购买材料A或者只购买材料C,最便宜,不过我当时在储存费用方面考虑欠佳。然后限制条件吧啦吧啦,懒得码字了,直接贴代码了。。。大家有不明白的,或者发现我问题的,欢迎在评论区和我交流

第二问代码

%若出现错误,可能是内存问题,重新运行即可 clc;clear; [num,txt,raw]=xlsread('问题1评分排名.xlsx'); data=xlsread('附件1 近5年402家供应商的相关数据.xlsx',2); index=1:402; num2=[index' num]; num3=sortrows(num2,7);%对排名进行升序 wushi=num3(1:50,:); indexC=find(wushi(:,2)==1); C=wushi(indexC,:);  for i=1:20 Max(i)=max(data(C(i,1),:)); end  T=28200*0.72; changku=0;%表示出库材料存量 buy=zeros(20,24); A=0;  f=100000000;%初始化 X=1000;%模拟次数 count=1; for Xunhuan=1:X      for i=1:20 buy(i,1)=min(6000,randi(round([Max(i)*0.3,Max(i)*0.6]))); end if sum(buy(:,1))>=T     changku=sum(buy(:,1))-T;     Changku(1)=changku; for j=2:24%星期     for i=1:20%供应商         fanwei=min(6000,round([Max(i)*0.3,Max(i)*0.6]));         if sum(buy(:,j))<=T||changku<=2*T             buy(i,j)=randi(fanwei);             if sum(buy(:,j))>=T                 changku=changku+sum(buy(:,j))-T;                 if changku>=2*T                     continue                 end             end         end     end     if sum(buy(:,j))<=T         changku=changku-(T-sum(buy(:,j)));     end     Changku(j)=changku;%记录每周仓库的存储数量 end end %数据合并 Buy=zeros(402,24)*nan; Buy(C(:,1),:)=buy;  %% %模拟转运损耗 into=xlsread('附件2 近5年8家转运商的相关数据.xlsx','B2:IG9'); yunshu=zeros(5,24) for j=1:8%第j个转运商 hang=into(j,:); %sunhao=unique(hang); %[m,n]=size(sunhao); for i=1:24 xunzhe=randi(240) yunshu(j,i)=hang(xunzhe) end end  %% %实施转运 p=1:8;; a=1;%第a列 transport=zeros(20,24); for i=1:24%24周     a=1;%第a行     R=0;%运输量 yunshu2=[p' yunshu(:,i)]; yunshu2(find(yunshu2(:,2)==0),:)=[];%剔除不运输的转运商 [m,n]=size(yunshu2); yunshu3=sortrows(yunshu2,2)%进行降序 for j=1:20%20家供应商 indexT=yunshu3(a,1); if R<=6000 R=R+buy(j,i); transport(j,i)=indexT; else     a=a+1;      if a>m         a=m;     end     transport(j,i)=yunshu3(a,1);     R=0;     R=R+buy(j,i); end end end Transport=zeros(402,24)*nan; Transport(C(:,1),:)=transport;  %% %计算损失 Sunshi=zeros(20,24)*nan; for j=1:24;%周     for i=1:20%供应商         Shunshi(i,j)=buy(i,j)*yunshu(transport(i,j),j)     end end Shunshi2=zeros(402,24); Shunshi2(C(:,1),:)=Shunshi;    if length(find(Changku>=0))==24 && changku>2*T     F(count)=sum(sum(buy))*1.5+sum(Changku)*0.5;     count=count+1     if F(count-1)<f         f=F(count-1);         Buy2=Buy;         Transport2=Transport;         Shunshi3=Shunshi2;         xlswrite('问题2损耗',Shunshi3);         xlswrite('问题2订购方案',Buy2);         xlswrite('问题2运输方案',Transport);     end end Xunhuan end         x=1:count-1; F2=sort(F,'descend') plot(x,F2,'-k*','linewidth',1) title('\fontname{宋体}有效模拟图'); xlabel('\fontname{宋体}有效模拟次数');; ylabel('\fontname{宋体}花费成本'); set(gca,'FontName','Times New Roman','fontsize',10.5) box on grid on xlim([1,count-1])  figure  plot(x,F,'-k*','linewidth',1) title('\fontname{宋体}有效模拟图'); xlabel('\fontname{宋体}有效模拟次数');; ylabel('\fontname{宋体}花费成本'); set(gca,'FontName','Times New Roman','fontsize',10.5) box on grid on xlim([1,count-1]) 

第三问代码

我的第三问其实和第二问差不多,还是选第一问得出的五十家供应商,只不过变成三种材料都采购了,并且给采购ABC的数量加了个权重,大概就是随机出三个大于0的数,它们的和为1,之后排序,储存在r2里,通过乘这个权重,达到多买A,少买C

第三问代码

这个代码感觉还有非常大大大的优化空间

因为有的时候会模拟出一些离谱的订单方案,比如说连仓库都欠账了之类的所以在最后加了一个判定,通过判定了才是有效模拟,直接count+1,有效模拟次数是count-1。

其实这个判定和 有效模拟次数 这玩意,第二问原本是没的,但是我在第三问写了这玩意之后,就有一种感觉“不行,第二问也要写,多水点图片”,所以每个问都有了

%第三问代码 %若出现某些错误,导致终止程序,可能是内存问题,重新运行即可 clc;clear; [num,txt,raw]=xlsread('问题1评分排名.xlsx'); data=xlsread('附件1 近5年402家供应商的相关数据.xlsx',2); index=1:402; num2=[index' num]; num3=sortrows(num2,7);%对排名进行升序 wushi=num3(1:50,:); %找出卖A的 indexA=find(wushi(:,2)==1.2); A=wushi(indexA,:); %找出卖B的 indexB=find(wushi(:,2)==1.1); B=wushi(indexB,:); %找出卖C的 indexC=find(wushi(:,2)==1); C=wushi(indexC,:); for i=1:16 MaxA(i)=max(data(A(i,1),:)); end for i=1:14 MaxB(i)=max(data(B(i,1),:)); end for i=1:20 MaxC(i)=max(data(C(i,1),:)); end   F=[] Buy2=[]; R2=[]; Transport=[]; Shunshi2=[]; f=1000000000; count=1; X=100%模拟次数 for Xunhuan=1:X x=rand(1,3);y=sum(x);r=x/y; r2=sort(r,2)  %TA=28200*0.6*r2(3); %TB=28200*0.66*r2(2); %TC=28200*0.72*r2(1); if TC<1     TC=1 end changkuA=0; changkuB=0; changkuC=0;%表示出库材料存量 buyA=zeros(16,24); buyB=zeros(14,24); buyC=zeros(20,24); %% %购买材料A for i=1:16 buyA(i,1)=min(round(TC*0.5),randi(round([MaxA(i)*0.5,MaxA(i)]))); end if sum(buyA(:,1))>=TA     changkuA=sum(buyA(:,1))-TA;     ChangkuA(1)=changkuA; for j=2:24%星期     for i=1:16%供应商         fanwei=min(round(TC*0.5),randi(round([MaxA(i)*0.5,MaxA(i)])));         if fanwei==0;             fanwei=1         end         if sum(buyA(:,j))<=TA||changkuA<=2*TA             buyA(i,j)=randi(fanwei);             if sum(buyA(:,j))>=TA                 changkuA=changkuA+sum(buyA(:,j))-TA;                 if changkuA>=2*TA                     continue                 end             end         end     end     if sum(buyA(:,j))<=TA         changkuA=changkuA-(TA-sum(buyA(:,j)));     end     ChangkuA(j)=changkuA;%记录每周仓库的存储数量 end end  %% %购买材料B for i=1:14 buyB(i,1)=min(round(TB*0.5),randi(round([MaxB(i)*0.5,MaxB(i)]))); end if sum(buyB(:,1))>=TB     changkuB=sum(buyB(:,1))-TB;     ChangkuB(1)=changkuB; for j=2:24%星期     for i=1:14%供应商         fanwei=min(round(TC*0.5),randi(round([MaxB(i)*0.5,MaxB(i)])));         if fanwei==0;             fanwei=1;         end         if sum(buyB(:,j))<=TB||changkuB<=2*TB             buyB(i,j)=randi(fanwei);             if sum(buyB(:,j))>=TB                 changkuB=changkuB+sum(buyB(:,j))-TB;                 if changkuB>=2*TB                     continue                 end             end         end     end     if sum(buyB(:,j))<=TB         changkuB=changkuB-(TB-sum(buyB(:,j)));     end     ChangkuB(j)=changkuB;%记录每周仓库的存储数量 end end %% %购买材料C for i=1:20 buyC(i,1)=min(round(TC*0.5),randi(round([MaxC(i)*0.5,MaxC(i)]))); end if sum(buyC(:,1))>=TC     changkuC=sum(buyC(:,1))-TC;     ChangkuC(1)=changkuC; for j=2:24%星期     for i=1:20%供应商         fanwei=min(round(TC*0.5),randi(round([MaxC(i)*0.5,MaxC(i)])));         if fanwei==0             fanwei=1         end         if sum(buyC(:,j))<=TC||changkuC<=2*TC             buyC(i,j)=randi(fanwei);             if sum(buyC(:,j))>=TC                 changkuC=changkuC+sum(buyC(:,j))-TC;                 if changkuC>=2*TC                     continue                 end             end         end     end     if sum(buyC(:,j))<=TC         changkuC=changkuC-(TC-sum(buyC(:,j)));     end     ChangkuC(j)=changkuC;%记录每周仓库的存储数量 end end Buy=zeros(50,24); Buy(A(:,7),:)=buyA; Buy(B(:,7),:)=buyB; Buy(C(:,7),:)=buyC; %数据合并 buy=zeros(402,24)*nan; buy(A(:,1),:)=buyA; buy(B(:,1),:)=buyB; buy(C(:,1),:)=buyC; %xlswrite('订购方案2',buy); %% %模拟转运损耗 fujian2=xlsread('附件2 近5年8家转运商的相关数据.xlsx','B2:IG9'); yunshu=zeros(5,24); for j=1:8%第j个转运商 hang=fujian2(j,:); %sunhao=unique(hang); %[m,n]=size(sunhao); for i=1:24 xunzhe=randi(240); yunshu(j,i)=hang(xunzhe); end end  %% %实施转运 p=1:8;%运输排名 transport=zeros(50,24); for i=1:24%24周     a=1;%第a行     R=0;%运输量 yunshu2=[p' yunshu(:,i)]; yunshu2(find(yunshu2(:,2)==0),:)=[];%剔除不运输的转运商 [m,n]=size(yunshu2); yunshu3=sortrows(yunshu2,2);%进行降序 for j=1:50%50家供应商 indexT=yunshu3(a,1); if Buy(j,i)==0     continue end if R<=6000 R=R+Buy(j,i); transport(j,i)=indexT; else     a=a+1;     if a>m         a=m;     end     transport(j,i)=yunshu3(a,1);     R=0;     R=R+Buy(j,i); end end end  Transport2=ones(402,24)*nan; Transport2(wushi(:,1),:)=transport; %% %计算损失 Sunshi=zeros(50,24); for j=1:24%周     for i=1:50%供应商         if transport(i,j)==0             continue         end         Shunshi(i,j)=Buy(i,j)*yunshu(transport(i,j),j);     end end Shunshi2=ones(402,24)*nan; Shunshi2(wushi(:,1),:)=Shunshi;  if length(find(ChangkuA>=0))==24&&length(find(ChangkuB>=0))==24&&length(find(ChangkuC>=0))==24&&sum(sum(Buy))>406080     F(count)=sum(sum(buyA))*1.2+sum(sum(buyB))*1.1+sum(sum(buyC))+0.5*(sum(ChangkuA)+sum(ChangkuB)+sum(ChangkuC))+sum(sum(Buy))*0.5;     count=count+1;     if F(count-1)<f         f=F(count-1);         R2=r2;         Buy2=buy;         Transport3=Transport2;         Shunshi3=Shunshi2;         xlswrite('问题三订单',Buy2);         xlswrite('问题三转运',Transport3);         xlswrite('问题三损耗',Shunshi3);     end end Xunhuan end %结果画图 x=1:count-1; F2=sort(F,'descend') plot(x,F2,'-k*','linewidth',1) title('\fontname{宋体}有效模拟图'); xlabel('\fontname{宋体}有效模拟次数');; ylabel('\fontname{宋体}花费成本'); set(gca,'FontName','Times New Roman','fontsize',10.5) box on grid on xlim([1,count-1])  figure  plot(x,F,'-k*','linewidth',1) title('\fontname{宋体}有效模拟图'); xlabel('\fontname{宋体}有效模拟次数');; ylabel('\fontname{宋体}花费成本'); set(gca,'FontName','Times New Roman','fontsize',10.5) box on grid on xlim([1,count-1]) 

第四问

第四问和第三问类似,受到另外一篇文章启发,画图发现购买材料C越来越多,B相对平稳,A逐步下降,所以那个权重就变成了多买C,少买A,然后为了产能增加,我经过乱七八糟的计算,我直接取了个产量最多的年份的平均周产量,其实就是算年的之后除48(小声bb),然后取了40625,我就不负责的假设他为了有这么多生产能力

关于生产力提高这方面,我个人认为也能用预测模型预测,未来能达到的产量,预测好之后也能直接放到我这个代码,这个改起来很简单

第四问代码

杂项

这个代码被我删删改改,丢了一部分。。。。

%% %前五十供应商画图 clc;clear [num2,txt2,~]=xlsread('问题1评分排名.xlsx'); index=1:402; num3=[index' num2]; num4=sortrows(num3,7);%对排名进行升序 num5=num4(1:50,:); wushi=sortrows(num5,1); indexA=find(wushi(:,2)==1.2); A=wushi(indexA,:);  indexB=find(wushi(:,2)==1.1); B=wushi(indexB,:);  indexC=find(wushi(:,2)==1); C=wushi(indexC,:);  %平均每周 a1=A(:,3)./420 b1=B(:,3)./420 c1=C(:,3)./420 %平均每月 a2=A(:,3)./60 b2=B(:,3)./60 c2=C(:,3)./60  %每月画图 plot(A(:,1),a2,'-k*','linewidth',1) title('\fontname{宋体}材料A每月平均供应量'); xlabel('\fontname{宋体}供应商\fontname{Times New Roman}ID'); ylabel('\fontname{宋体}材料供应量'); set(gca,'FontName','Times New Roman','fontsize',10.5) box on grid on xlim([1,240]) figure  plot(B(:,1),b2,'-k*','linewidth',1) title('\fontname{宋体}材料B每月平均供应量'); xlabel('\fontname{宋体}供应商\fontname{Times New Roman}ID');; ylabel('\fontname{宋体}材料供应量'); set(gca,'FontName','Times New Roman','fontsize',10.5) box on grid on xlim([1,240]) figure  plot(C(:,1),c2,'-k*','linewidth',1) title('\fontname{宋体}材料C每月平均供应量'); xlabel('\fontname{宋体}供应商\fontname{Times New Roman}ID'); ylabel('\fontname{宋体}材料供应量');  set(gca,'FontName','Times New Roman','fontsize',10.5) box on grid on xlim([1,240])   %% %分离附件1订单材料ABC clc;clear [num,txt,~]=xlsread('附件1 近5年402家供应商的相关数据.xlsx',1); cailiao=ones(402,1); xunzhaoA = strcmp(txt(2:end,2),'A');%找出A,标记为1 mA=find(xunzhaoA==1);%找出字符串所在的索引 xunzhaoB = strcmp(txt(2:end,2),'B');%找出A,标记为1 mB=find(xunzhaoB==1);%找出字符串所在的索引 xunzhaoC = strcmp(txt(2:end,2),'C');%找出A,标记为1 mC=find(xunzhaoC==1);%找出字符串所在的索引  cailiao(mA)=1.2; cailiao(mB)=1.1; cailiao(mC)=1;  indexA=find(cailiao(:,1)==1.2); A=num(indexA,:);  indexB=find(cailiao(:,1)==1.1); B=num(indexB,:);  indexC=find(cailiao(:,1)==1); C=num(indexC,:);  shengchanli=sum(reshape(sum(A),48,5))./0.6+sum(reshape(sum(B),48,5))./0.66+sum(reshape(sum(C),48,5))./0.72; plot(shengchanli) 

模拟代码

%第四问模拟代码 %若出现某些错误,终止程序,可能是内存问题,重新运行即可 clc;clear; [num,txt,raw]=xlsread('问题1评分排名.xlsx'); data=xlsread('附件1 近5年402家供应商的相关数据.xlsx',2); index=1:402; num2=[index' num]; num3=sortrows(num2,7);%对排名进行升序 wushi=num3(1:50,:); %找出卖A的 indexA=find(wushi(:,2)==1.2); A=wushi(indexA,:); %找出卖B的 indexB=find(wushi(:,2)==1.1); B=wushi(indexB,:); %找出卖C的 indexC=find(wushi(:,2)==1); C=wushi(indexC,:); for i=1:16 MaxA(i)=max(data(A(i,1),:)); end for i=1:14 MaxB(i)=max(data(B(i,1),:)); end for i=1:20 MaxC(i)=max(data(C(i,1),:)); end   F=[] Buy2=[]; R2=[]; Transport=[]; Shunshi2=[]; f=1000000000; count=1; X=1000%模拟次数 for Xunhuan=1:X x=rand(1,3); y=sum(x); r=x/y; r2=sort(r,2)  TA=40625*0.6*r2(1); TB=40625*0.66*r2(2); TC=40625*0.72*r2(3); if TA<1     TA=1 end changkuA=0; changkuB=0; changkuC=0;%表示出库材料存量 buyA=zeros(16,24); buyB=zeros(14,24); buyC=zeros(20,24); %% %购买材料A for i=1:16 buyA(i,1)=min(round(TC*0.5),randi(round([MaxA(i)*0.5,MaxA(i)]))); end if sum(buyA(:,1))>=TA     changkuA=sum(buyA(:,1))-TA;     ChangkuA(1)=changkuA; for j=2:24%星期     for i=1:16%供应商         fanwei=min(round(TC*0.5),randi(round([MaxA(i)*0.5,MaxA(i)])));         if fanwei==0;             fanwei=1         end         if sum(buyA(:,j))<=TA||changkuA<=2*TA             buyA(i,j)=randi(fanwei);             if sum(buyA(:,j))>=TA                 changkuA=changkuA+sum(buyA(:,j))-TA;                 if changkuA>=2*TA                     continue                 end             end         end     end     if sum(buyA(:,j))<=TA         changkuA=changkuA-(TA-sum(buyA(:,j)));     end     ChangkuA(j)=changkuA;%记录每周仓库的存储数量 end end  %% %购买材料B for i=1:14 buyB(i,1)=min(round(TB*0.5),randi(round([MaxB(i)*0.5,MaxB(i)]))); end if sum(buyB(:,1))>=TB     changkuB=sum(buyB(:,1))-TB;     ChangkuB(1)=changkuB; for j=2:24%星期     for i=1:14%供应商         fanwei=min(round(TC*0.5),randi(round([MaxB(i)*0.5,MaxB(i)])));         if fanwei==0;             fanwei=1;         end         if sum(buyB(:,j))<=TB||changkuB<=2*TB             buyB(i,j)=randi(fanwei);             if sum(buyB(:,j))>=TB                 changkuB=changkuB+sum(buyB(:,j))-TB;                 if changkuB>=2*TB                     continue                 end             end         end     end     if sum(buyB(:,j))<=TB         changkuB=changkuB-(TB-sum(buyB(:,j)));     end     ChangkuB(j)=changkuB;%记录每周仓库的存储数量 end end %% %购买材料C for i=1:20 buyC(i,1)=min(round(TC*0.5),randi(round([MaxC(i)*0.5,MaxC(i)]))); end if sum(buyC(:,1))>=TC     changkuC=sum(buyC(:,1))-TC;     ChangkuC(1)=changkuC; for j=2:24%星期     for i=1:20%供应商         fanwei=min(round(TC*0.5),randi(round([MaxC(i)*0.5,MaxC(i)])));         if fanwei==0             fanwei=1         end         if sum(buyC(:,j))<=TC||changkuC<=2*TC             buyC(i,j)=randi(fanwei);             if sum(buyC(:,j))>=TC                 changkuC=changkuC+sum(buyC(:,j))-TC;                 if changkuC>=2*TC                     continue                 end             end         end     end     if sum(buyC(:,j))<=TC         changkuC=changkuC-(TC-sum(buyC(:,j)));     end     ChangkuC(j)=changkuC;%记录每周仓库的存储数量 end end Buy=zeros(50,24); Buy(A(:,7),:)=buyA; Buy(B(:,7),:)=buyB; Buy(C(:,7),:)=buyC; %数据合并 buy=zeros(402,24)*nan; buy(A(:,1),:)=buyA; buy(B(:,1),:)=buyB; buy(C(:,1),:)=buyC; %xlswrite('订购方案2',buy); %% %模拟转运损耗 fujian2=xlsread('附件2 近5年8家转运商的相关数据.xlsx','B2:IG9'); yunshu=zeros(5,24); for j=1:8%第j个转运商 hang=fujian2(j,:); %sunhao=unique(hang); %[m,n]=size(sunhao); for i=1:24 xunzhe=randi(240); yunshu(j,i)=hang(xunzhe); end end  %% %实施转运 p=1:8;%运输排名 transport=zeros(50,24); for i=1:24%24周     a=1;%第a行     R=0;%运输量 yunshu2=[p' yunshu(:,i)]; yunshu2(find(yunshu2(:,2)==0),:)=[];%剔除不运输的转运商 [m,n]=size(yunshu2); yunshu3=sortrows(yunshu2,2);%进行降序 for j=1:50%50家供应商 indexT=yunshu3(a,1); if Buy(j,i)==0     continue end if R<=6000 R=R+Buy(j,i); transport(j,i)=indexT; else     a=a+1;     if a>m         a=m;     end     transport(j,i)=yunshu3(a,1);     R=0;     R=R+Buy(j,i); end end end  Transport2=ones(402,24)*nan; Transport2(wushi(:,1),:)=transport; %% %计算损失 Sunshi=zeros(50,24); for j=1:24%周     for i=1:50%供应商         if transport(i,j)==0             continue         end         Shunshi(i,j)=Buy(i,j)*yunshu(transport(i,j),j);     end end Shunshi2=ones(402,24)*nan; Shunshi2(wushi(:,1),:)=Shunshi;  if length(find(ChangkuA>=0))==24&&length(find(ChangkuB>=0))==24&&length(find(ChangkuC>=0))==24&&sum(sum(Buy))>702000     F(count)=sum(sum(buyA))*1.2+sum(sum(buyB))*1.1+sum(sum(buyC))+0.5*(sum(ChangkuA)+sum(ChangkuB)+sum(ChangkuC))+sum(sum(Buy))*0.5;     count=count+1;     if F(count-1)<f         f=F(count-1);         R2=r2;         Buy2=buy;         Transport3=Transport2;         Shunshi3=Shunshi2;         xlswrite('问题四订单',Buy2);         xlswrite('问题四转运',Transport3);         xlswrite('问题四损耗',Shunshi3);     end end Xunhuan count end %结果画图 x=1:count-1; F2=sort(F,'descend') plot(x,F2,'-k*','linewidth',1) title('\fontname{宋体}问题四有效模拟图'); xlabel('\fontname{宋体}有效模拟次数');; ylabel('\fontname{宋体}花费成本'); set(gca,'FontName','Times New Roman','fontsize',10.5) box on grid on xlim([1,count-1]) figure plot(x,F,'-k*','linewidth',1) title('\fontname{宋体}问题四有效模拟图'); xlabel('\fontname{宋体}有效模拟次数');; ylabel('\fontname{宋体}花费成本'); set(gca,'FontName','Times New Roman','fontsize',10.5) box on grid on xlim([1,count-1]) 

其实还有一个模型检验的代码,不过太敷衍了,我就不放出来了

关于转运方案,改一改代码就能换成对应的表格了,我当时没注意它的表格格式,弄成了和订单一样的样子。。。

我这个用是蒙特卡洛模拟的思想

最后,欢迎大家和我交流

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!