免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2328 | 回复: 6
打印 上一主题 下一主题

一个小脚本求优化 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2017-03-31 10:00 |只看该作者 |倒序浏览
新手上路,写的非常的冗长,求优化。
1, 包括:A、B、C、D、E 五大组装车间,
2, 产品分为:100系列、200系列、300系列、400系列、500系列
3,A车间月产能为: 500系列 50台、 400系列60台、300系列70台、200系列80台、100系列90台 (注意:产能只满足其中一项,不是总和)
4,B车间月产能为: 500系列 90台、 400系列150台、300系列90台、200系列90台、100系列90台(注意:产能只满足其中一项,不是总和)
5,C车间月产能为: 500系列 30台、 400系列100台、300系列110台、200系列120台、100系列130台(注意:产能只满足其中一项,不是总和)
6,D车间月产能为: 500系列 70台、 400系列180台、300系列60台、200系列60台、100系列60台(注意:产能只满足其中一项,不是总和)

问题:
1, 写一个五大车间的排产算法,(可用任何语言)
2, 每月末输入总订单量后,会对下月生产计划进行自动排产。
3, 排产结果,要求排产结果为最优化状态

#!/usr/bin/perl

use strict;
use warnings;
use threads;  
use Getopt::Long;

my $Product = "300 200 100 400 500";
my $Number = "300 90 100 50 300";

my $Plsplit = undef;
my $Plant;
my $ProductType;

my $PlantA = undef;
my $PlantB = undef;
my $PlantC = undef;
my $PlantD = undef;
my $PlantE = undef;
my $totalNum = undef;
my $p100num = undef;
my $p200num = undef;
my $p300num = undef;
my $p400num = undef;
my $p500num = undef;

my @Plsplit = split(/\s/,"$Product");
my @Number = split(/\s/,"$Number");
#validateArguments
if (@Plsplit ne @Number){
        print ("Invalid Argument\n");
        exit;
}



my $len1=@Plsplit - 1;
for my $i (0..$len1){
        my $a; my $b; my $c ; my $d; my $e;
        if ($Plsplit[$i] ne "") {
        $ProductType = $Plsplit[$i];
        $totalNum = $Number[$i];
                SWITCH: {
                        $ProductType == 100 && do {
                                if (!$PlantE){
                                        ($a, $b,$c,$d,$e) = choosePlant100($ProductType, $totalNum);
                                        if (!$PlantA && $a){$PlantA = "100";$p100num = $a;}
                                        if (!$PlantB && $b){$PlantB = "100";$p200num = $b;}
                                        if (!$PlantC && $c){$PlantC = "100";$p300num = $c;}
                                        if (!$PlantD && $d){$PlantD = "100";$p400num = $d;}
                                        if (!$PlantE && $e){$PlantE = "100";$p500num = $e;}
                                }else{
                                        #do nothing;
                                }
                                last SWITCH;
                        };
                        $ProductType == 200 && do {
                                if (!$PlantE){
                                        ($a, $b,$c,$d,$e) = choosePlant200($ProductType, $totalNum);
                                        if (!$PlantA && $a){$PlantA = "200";$p100num = $a;}
                                        if (!$PlantB && $b){$PlantB = "200";$p200num = $b;}
                                        if (!$PlantC && $c){$PlantC = "200";$p300num = $c;}
                                        if (!$PlantD && $d){$PlantD = "200";$p400num = $d;}
                                        if (!$PlantE && $e){$PlantE = "200";$p500num = $e;}
                                }else{
                                        #do nothing;
                                }                       
                                last SWITCH;
                        };
                        $ProductType == 300 && do {
                                if (!$PlantE){
                                        ($a, $b,$c,$d,$e) = choosePlant300($ProductType, $totalNum);
                                        if (!$PlantA && $a){$PlantA = "300";$p100num = $a;}
                                        if (!$PlantB && $b){$PlantB = "300";$p200num = $b;}
                                        if (!$PlantC && $c){$PlantC = "300";$p300num = $c;}
                                        if (!$PlantD && $d){$PlantD = "300";$p400num = $d;}
                                        if (!$PlantE && $e){$PlantE = "300";$p500num = $e;}
                                }else{
                                        #do nothing;
                                }
                                last SWITCH;
                        };
                        $ProductType == 400 && do {
                                if (!$PlantD){
                                        ($a, $b,$c,$d,$e) = choosePlant400($ProductType, $totalNum);
                                        if (!$PlantA && $a){$PlantA = "400";$p100num = $a;}
                                        if (!$PlantB && $b){$PlantB = "400";$p200num = $b;}
                                        if (!$PlantC && $c){$PlantC = "400";$p300num = $c;}
                                        if (!$PlantD && $d){$PlantD = "400";$p400num = $d;}
                                        if (!$PlantE && $e){$PlantE = "400";$p500num = $e;}
                                }else{
                                        #do nothing;
                                }                       
                                last SWITCH;
                        };
                        $ProductType == 500 && do {
                                if (!$PlantB){
                                        ($a, $b,$c,$d,$e) = choosePlant500($ProductType, $totalNum);
                                        if (!$PlantA && $a){$PlantA = "500";$p100num = $a;}
                                        if (!$PlantB && $b){$PlantB = "500";$p200num = $b;}
                                        if (!$PlantC && $c){$PlantC = "500";$p300num = $c;}
                                        if (!$PlantD && $d){$PlantD = "500";$p400num = $d;}
                                        if (!$PlantE && $e){$PlantE = "500";$p500num = $e;}
                                }else{
                                        #do nothing;
                                }                       
                                last SWITCH;
                        };
                }
        }
        if (!$PlantA){$PlantA = "100";$p100num = $Number[0];}
        if (!$PlantB){$PlantB = "500";$p200num = $Number[1];}
        if (!$PlantC){$PlantC = "300";$p300num = $Number[2];}
        if (!$PlantD){$PlantD = "400";$p400num = $Number[3];}
        if (!$PlantE){$PlantE = "200";$p500num = $Number[4];}

}

print ("Product in PlantA is : $PlantA, Number is $p100num\n");
print ("Product in PlantB is : $PlantB, Number is $p200num\n");
print ("Product in PlantC is : $PlantC, Number is $p300num\n");
print ("Product in PlantD is : $PlantD, Number is $p400num\n");
print ("Product in PlantE is : $PlantE, Number is $p500num\n");

sub choosePlant100{
        my $ProductType = shift;
        my $totalNum = shift;
        my $a; my $b; my $c ; my $d; my $e;
       
        if ($totalNum >= "510"){
                $a = "90";        $b = "90";        $c = "130"; $d = "60"; $e = "140";
        }elsif ($totalNum < 510 && $totalNum >= 450){
                $a = "90";        $b = "90";        $c = "130";        $d = "";        $e = "140";
        }elsif ($totalNum < 450 && $totalNum >= 360){
                $a = "";        $b = "";        $c = "130";        $d = "";        $e = "140";
        }elsif ($totalNum < 360 && $totalNum >= 270){
                $a = "";        $b = "";        $c = "130";        $d = "";        $e = "140";
        }elsif ($totalNum < 270 && $totalNum >= 140){
                $a = "";        $b = "";        $c = "";        $d = "";        $e = "140";
        }elsif ($totalNum < 140 && $totalNum >= 130){
                $a = "";        $b = "";        $c = "";        $d = "";        $e = "140";
        }elsif ($totalNum < 130 && $totalNum >= 90){
                $a = "90";        $b = "";        $c = "";        $d = "";        $e = "";
        }elsif ($totalNum < 90 && $totalNum >= 60){
                $a = "";        $b = "";        $c = "";        $d = "60";        $e = "";
        }else{
                $a = "";        $b = "";        $c = "";        $d = "";        $e = "";
        }

        return ($a,$b,$c,$d,$e);
}

sub choosePlant200{
        my $ProductType = shift;
        my $totalNum = shift;
        my $a; my $b; my $c ; my $d; my $e;

        if ($totalNum >= "480"){
                $a = "80";        $b = "90";        $c = "120"; $d = "60"; $e = "130";
        }elsif ($totalNum < 480 && $totalNum >= 420){
                $a = "80";        $b = "90";        $c = "120"; $d = ""; $e = "130";
        }elsif ($totalNum < 420 && $totalNum >= 340){
                $a = "";        $b = "90";        $c = "120"; $d = ""; $e = "130";
        }elsif ($totalNum < 340 && $totalNum >= 250){
                $a = "";        $b = "";        $c = "120"; $d = ""; $e = "130";
        }elsif ($totalNum < 250 && $totalNum >= 130){
                $a = "";        $b = "";        $c = ""; $d = ""; $e = "130";
        }elsif ($totalNum < 130 && $totalNum >= 120){
                $a = "";        $b = "";        $c = "120"; $d = ""; $e = "";
        }elsif ($totalNum < 120 && $totalNum >= 90){
                $a = "";        $b = "90";        $c = ""; $d = ""; $e = "";
        }elsif ($totalNum < 90 && $totalNum >= 80){
                $a = "80";        $b = "";        $c = ""; $d = ""; $e = "";
        }elsif ($totalNum < 80 && $totalNum >= 60){
                $a = "";        $b = "";        $c = ""; $d = "60"; $e = "";
        }else{
                $a = "";        $b = "";        $c = ""; $d = ""; $e = "";
        }
        return ($a,$b,$c,$d,$e);
}

sub choosePlant300{
        my $ProductType = shift;
        my $totalNum = shift;
        my $a; my $b; my $c ; my $d; my $e;

        if ($totalNum >= "450"){
                $a = "70";        $b = "90";        $c = "110"; $d = "60"; $e = "120";
        }elsif ($totalNum < 450 && $totalNum >= 390){
                $a = "70";        $b = "90";        $c = "110"; $d = ""; $e = "120";
        }elsif ($totalNum < 390 && $totalNum >= 320){
                $a = "";        $b = "90";        $c = "110"; $d = ""; $e = "120";
        }elsif ($totalNum < 320 && $totalNum >= 230){
                $a = "";        $b = "";        $c = "110"; $d = ""; $e = "120";
        }elsif ($totalNum < 230 && $totalNum >= 120){
                $a = "";        $b = "";        $c = ""; $d = ""; $e = "120";
        }elsif ($totalNum < 120 && $totalNum >= 110){
                $a = "";        $b = "";        $c = "110"; $d = ""; $e = "";
        }elsif ($totalNum < 110 && $totalNum >= 90){
                $a = "";        $b = "90";        $c = ""; $d = ""; $e = "";
        }elsif ($totalNum < 90 && $totalNum >= 70){
                $a = "70";        $b = "";        $c = ""; $d = ""; $e = "";
        }elsif ($totalNum < 70 && $totalNum >= 60){
                $a = "";        $b = "";        $c = ""; $d = "60"; $e = "";
        }else{
                $a = "";        $b = "";        $c = "";        $d = "";        $e = "";
        }

        return ($a,$b,$c,$d,$e);
}

sub choosePlant400{
        my $ProductType = shift;
        my $totalNum = shift;
        my $a; my $b; my $c ; my $d; my $e;

        if ($totalNum >= "510"){
                $a = "60";        $b = "150";        $c = "100"; $d = "180"; $e = "20";
        }elsif ($totalNum < 510 && $totalNum >= 490){
                $a = "60";        $b = "150";        $c = "100"; $d = "180"; $e = "";
        }elsif ($totalNum < 490 && $totalNum >= 430){
                $a = "";        $b = "150";        $c = "100"; $d = "180"; $e = "";
        }elsif ($totalNum < 430 && $totalNum >= 330){
                $a = "";        $b = "150";        $c = ""; $d = "180"; $e = "";
        }elsif ($totalNum < 330 && $totalNum >= 180){
                $a = "";        $b = "";        $c = ""; $d = "180"; $e = "";
        }elsif ($totalNum < 180 && $totalNum >= 150){
                $a = "";        $b = "150";        $c = ""; $d = ""; $e = "";
        }elsif ($totalNum < 150 && $totalNum >= 100){
                $a = "";        $b = "";        $c = "100"; $d = ""; $e = "";
        }elsif ($totalNum < 100 && $totalNum >= 60){
                $a = "60";        $b = "";        $c = ""; $d = ""; $e = "";
        }elsif ($totalNum < 60 && $totalNum >= 20){
                $a = "";        $b = "";        $c = ""; $d = ""; $e = "20";
        }else{
                $a = "";        $b = "";        $c = "";        $d = "";        $e = "";
        }
        return ($a,$b,$c,$d,$e);
}

sub choosePlant500{
        my $ProductType = shift;
        my $totalNum = shift;
        my $a; my $b; my $c ; my $d; my $e;

        if ($totalNum >= "250"){
                $a = "50";        $b = "90";        $c = "30"; $d = "70"; $e = "10";
        }elsif ($totalNum < 250 && $totalNum >= 240){
                $a = "50";        $b = "90";        $c = "30"; $d = "70"; $e = "";
        }elsif ($totalNum < 240 && $totalNum >= 210){
                $a = "50";        $b = "90";        $c = ""; $d = "70"; $e = "";
        }elsif ($totalNum < 210 && $totalNum >= 160){
                $a = "";        $b = "90";        $c = ""; $d = "70"; $e = "";
        }elsif ($totalNum < 160 && $totalNum >= 90){
                $a = "";        $b = "90";        $c = ""; $d = ""; $e = "";
        }elsif ($totalNum < 90 && $totalNum >= 70){
                $a = "";        $b = "";        $c = ""; $d = "70"; $e = "";
        }elsif ($totalNum < 70 && $totalNum >= 50){
                $a = "50";        $b = "";        $c = ""; $d = ""; $e = "";
        }elsif ($totalNum < 50 && $totalNum >= 30){
                $a = "";        $b = "";        $c = "30"; $d = ""; $e = "";
        }elsif ($totalNum < 30 && $totalNum >= 10){
                $a = "";        $b = "";        $c = ""; $d = ""; $e = "10";
        }else{
                $a = "";        $b = "";        $c = "";        $d = "";        $e = "";
        }
        return ($a,$b,$c,$d,$e);
}


论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
2 [报告]
发表于 2017-03-31 11:37 |只看该作者
本帖最后由 sunzhiguolu 于 2017-03-31 11:43 编辑
Product in PlantA is : 100, Number is 300
Product in PlantB is : 500, Number is 90
Product in PlantC is : 300, Number is 110
Product in PlantD is : 400, Number is 50
Product in PlantE is : 300, Number is 120


能对你的输出结果进行解释一下吗?
1.> 产品类型与数量之间 以及产品总数之间一个怎样的关系
2.> 这个排产到底是个啥意思

论坛徽章:
0
3 [报告]
发表于 2017-03-31 12:08 |只看该作者
我这个脚本有问题,正在查

论坛徽章:
0
4 [报告]
发表于 2017-03-31 12:38 |只看该作者
my $Product = "300 200 100 400 500";
my $Number = "300 90 100 50 300";

my $PlantA = undef;
my $PlantB = undef;
my $PlantC = undef;
my $PlantD = undef;
my $PlantE = undef;

至少这两项如果用HASH表达,会方便很多,代码也会简洁一些!

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
5 [报告]
发表于 2017-04-01 01:26 |只看该作者
本帖最后由 rubyish 于 2017-04-02 03:45 编辑

shibushi?

  1. #!/usr/bin/perl -w
  2. use 5.010;

  3. my @PLANT = qw[plantA plantB plantC plantD plantE];
  4. my @plant = (
  5.     { 100 => 90,  500 => 50, 200 => 80,  300 => 70,  400 => 60 },
  6.     { 300 => 90,  100 => 90, 500 => 90,  200 => 90,  400 => 150 },
  7.     { 300 => 110, 500 => 30, 200 => 120, 100 => 130, 400 => 100 },
  8.     { 100 => 60,  200 => 60, 500 => 70,  300 => 60,  400 => 180 },
  9.     { 100 => 70,  200 => 65, 500 => 50,  300 => 65,  400 => 150 },
  10. );

  11. my $next = {
  12.     300 => 100,    # 200,
  13.     200 => 90,
  14.     100 => 110,
  15.     400 => 50,
  16.     500 => 80,     # 300
  17. };

  18. my @pro = sort { $b->[1][0][1] <=> $a->[1][0][1] }
  19.   map {
  20.     my ( $k, $v ) = ( $_, $next->{$_} );
  21.     my @time =
  22.       sort { $a->[1] <=> $b->[1] }
  23.       map { [ $_, $v / $plant[$_]{$k} ] } 0 .. $#plant;
  24.     [ $_, \@time ]
  25.   } keys %$next;

  26. my ( $min, $best );
  27. $min += $_->[1][-1][1] for @pro;

  28. sub go {
  29.     my ( $I, $ok, $MIN ) = @_;
  30.     if ( $I == @pro ) {
  31.         ( $min, $best ) = ( $MIN, $ok );
  32.         return;
  33.     }

  34.     my %has = map @$_, @$ok;
  35.     for ( @{ $pro[$I][1] } ) {
  36.         last if $MIN + $_->[1] >= $min;
  37.         go( $I + 1, [ @$ok, [ $_->[0], $pro[$I][0] ] ], $MIN + $_->[1] )
  38.           if !$has{ $_->[0] };
  39.     }

  40. }

  41. go( 0, [], 0 );

  42. for my $b ( sort { $a->[0] <=> $b->[0] } @$best ) {
  43.     my ( $i, $pro ) = @$b;
  44.     print "$PLANT[$i]: product is $pro, ";
  45.     say "num = $next->{$pro}";
  46. }


  47. __DATA__
  48. $_

  49. plantA: product is 200, num = 90
  50. plantB: product is 300, num = 100
  51. plantC: product is 100, num = 110
  52. plantD: product is 500, num = 80
  53. plantE: product is 400, num = 50

复制代码

论坛徽章:
0
6 [报告]
发表于 2017-04-02 10:58 |只看该作者
回复 5# rubyish

厉害~~大赞。之前问过别人,说用权重的概念比较简单,但我一直没弄出来

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
7 [报告]
发表于 2017-04-06 05:26 |只看该作者
回复 6# zhjwlgh01

maybe~



@pro:

  1. [ 300, [ [ 2, 0.91 ], [ 1, 1.11 ], [ 0, 1.43 ], [ 4, 1.54 ], [ 3, 1.67 ] ] ]
  2. [ 500, [ [ 1, 0.89 ], [ 3, 1.14 ], [ 0, 1.60 ], [ 4, 1.60 ], [ 2, 2.67 ] ] ]
  3. [ 100, [ [ 2, 0.85 ], [ 0, 1.22 ], [ 1, 1.22 ], [ 4, 1.57 ], [ 3, 1.83 ] ] ]
  4. [ 200, [ [ 2, 0.75 ], [ 1, 1.00 ], [ 0, 1.12 ], [ 4, 1.38 ], [ 3, 1.50 ] ] ]
  5. [ 400, [ [ 3, 0.28 ], [ 1, 0.33 ], [ 4, 0.33 ], [ 2, 0.50 ], [ 0, 0.83 ] ] ]
复制代码



go:

  1. 8.5
  2. 8.5|4.68 [ [ 2, 300 ], [ 1, 500 ], [ 0, 100 ], [ 4, 200 ], [ 3, 400 ] ]
  3. 4.68|4.6 [ [ 2, 300 ], [ 3, 500 ], [ 0, 100 ], [ 1, 200 ], [ 4, 400 ] ]
  4. 4.6|4.55 [ [ 1, 300 ], [ 3, 500 ], [ 2, 100 ], [ 0, 200 ], [ 4, 400 ] ]
  5. 4.55|4.55 [ [ 1, 300 ], [ 3, 500 ], [ 0, 100 ], [ 2, 200 ], [ 4, 400 ] ]
复制代码


您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP