用蒙特卡洛法实现

来源:互联网 发布:教育数据挖掘论文 编辑:程序博客网 时间:2024/06/06 02:49

蒙特卡洛(MonteCarlo)法,或称统计试验法、计算机随机模拟方法,源于美国在第一次世界大战进研制{MOD}的曼哈顿计划。该计划的主持人之一、数学家冯·诺伊曼用驰名世界的赌城摩纳哥的MonteCarlo—来命名这种方法,为它蒙上了一层神秘色彩。

一、蒙特卡洛法的基本思想及其应用

MonteCarlo方法是一种基于随机数,采用统计抽样方法,近似求解数学问题或物理问题的过程。把统计模拟法用于数值计算已有200多年的历史,最早是法国数学家蒲丰(17071788)。他进行了著名的“蒲丰投针实验”,早以此圆周率π的近似值。本世纪40年代,随着电子计算机的出现,特别是近年来高速电子计算机的出现,使得用数学方法在计算机上大量、快速地模拟这样的试验成为可能。

统计试验法通常用来研究概率过程,研究问题时常涉及下列一些与随机因素有关的概率,如各类概率等,一般来说,建立描述过程的复杂的概率模型是不成问题的,但用数学方法研究与分析这些模型是却很困难,问题的维数(即变量的个数)可能高达数百甚至数千。对这类问题,难度随维数的增加呈指数增长,这就是所谓的维数的灾难”(Course Dimensionality)传统的数值方法难以对付(即使使用速度最快的计算机),甚至达到了无法进行的地步。因此,唯一可取的研究方法是统计实验法。

统计模拟(蒙特卡洛法),在系统工程中的应用日益广泛,据国外有关文献报道其应用领域大致有:

1航空运输排队,机场设计等;

2港口设计,泊位研究等;

3消防车或救护车的布局和调派;

4城市公共汽车作业调度;

5出租汽车调度计划

6铁路货运调度计划

7加油站、停车场等设计;

8售票所布局;

9存储模拟,仓库布局等;

10设备维修计划;

11生产过程的安排;

12工厂的单件、小批生产的作业计划;

13销售预测;

二、排队或等待问题的分析

在日常生活中,我们每天都会遇到各种各样的排队。比如:银行中取款要排队,火车站买票要排队,超市、商场中购物付款要排队,预订旅馆或机票时也要排队,人们仿佛置身于一个排队的社会。

所谓排队,就是等候消费服务的顾客在进入点前排队(意大利佩里切利)。排队的类型一般来说有以下几种:

一个服务点。即只有一个点可供顾客选择等候服务,顾客只能按顺序一个接一个等侯,这样排队,顾客多的时候最混乱。现在这种排队类型已不多见。

多个服务点。即有两个或两个以上的点可供顾客选择,每一点都可排成一条队。多个服务点可减轻一个服务点的负担,增加顾客选择服务点的灵活性,同时顾客还可选择自己喜欢的服务员。

专门服务点。即专门为某些特殊的顾客开辟的服务点。专门的服务点可以保证某些特殊顾客的特殊权益,如:减少顾客等待服务时间,同时使一些顾客分离出来,减轻其服务口的压力

我们拿一个理发店的实际例子来分析:

某理发馆每天早晨开业后,顾客的到来总是陆续不断的,他们到来的间隔时间,给统计如下:

到达时间间隔(分

频率

4

0.07

5

0.10

6

0.52

7

0.20

8

0.11

顾客到来后,由管理人员引导至ABC三个服务椅(理发员固定在服务椅上),各服务椅的服务内容相同,但服务时间有多有少,根据以往统计资料,他们的服务时间分布如下:

A

B

C

时间(分)

频率

时间(分)

频率

时间(分)

频率

8

9

10

11

0.18

0.22

0.33

0.27

10

11

12

13

0.18

0.19

0.35

0.28

12

13

14

15

0.15

0.22

0.36

0.27

1.00

1.00

1.00

当有一个以上服务椅空闲时,管理人员按字母排列次序引导顾客至服务椅去进行服务。顾客中有10%特殊顾客理发很费时间,每人需多4分钟。现模拟10位顾客的到来和服务情况,并分析三个服务椅的忙闲情况。

顾客到达时间间隔

频率

累计频率

对应的

随机数

A服务时间

频率

累计频率

对应的随机数

4

0.07

0.07

0.00~0.06

8

0.18

0.18

0.00~0.17

5

0.10

0.17

0.07~0.16

9

0.22

0.40

0.18~0.39

6

0.52

0.69

0.17~0.68

10

0.33

0.73

0.40~0.72

7

0.20

0.89

0.69~0.88

11

0.27

1.00

1.00

8

0.11

1.00

0.89~1.00

B服务时间

频率

累计频率

对应的

随机数

C服务时间

频率

累计频率

对应的

随机数

10

0.18

0.18

0.00~0.17

12

0.15

0.15

0.00~0.14

11

0.19

0.37

0.18~0.36

13

0.22

0.37

0.15~0.36

12

0.35

0.72

0.37~0.71

14

0.36

0.73

0.37~0.72

13

0.28

1.00

0.72~1.00

15

0.37

1.00

0.72~1.00

我们采用二位随机数来模拟顾客到来间隔和服务情况,并选定碰到个位数是5就作为特殊顾(占10%)现从随机数中抽取随机数如下:

顾客

顾客到来时间间隔

服务随机数

随机数

时间间隔(分)

1

2

3

4

5

6

7

8

9

10

14

46

11

66

60

90

33

65

72

92

6

5

6

6

8

6

6

7

8

00

99

20

19

32

40

20

01(特殊顾客)

68

87

设第一个顾客到达的时间是在3点正,随机数是14,此数本不需要,主要是看一下是不是特殊顾客,现此数尾数不是5说明第一位不是特殊顾客。第一个顾客到达,按字母的顺序应到A服务椅,随机数00,表示要服务8分钟,第二个顾客到达与第一个顾客的间隔时间,按随机数46应为6分钟,即在8.06时,服务椅A正在使用,因此到服务椅B,服务随机数99,应服务13分钟,如此编成表如下:

顾客

到达时间

服务椅

等待时间

服务

时间

A

B

C

到达

离开

到达

离开

到达

离开

1

8:00

A

8

8:00

8:08

2

8:06

B

13

8:06

8:19

3

8:11

C

13

8:11

8:24

4

8:17

A

9

8:17

8:26

5

8:23

B

11

8:23

8:34

6

8:31

A

10

8:31

8:41

7

8:37

B

10

8:37

8:47

8

8:43

A

12

8:43

8:55

9

8:50

B

12

8:50

9:02

10

8:58

A

11

8:58

9:09

共计

109

*特殊顾客服务时间8(分)+4(分)=12(分)

10个顾客在系统内的总时间为63分。

分析:

110个顾客在系统(理发馆内)的总时间为69分,平均每人69/10=6.9

210个顾客总的服务时间为109分。平均每人服务109/10=10.9

3统内无等待时间,即没出现顾客排队等待问题.

4椅的服务情况如下:

A

B

C

服务顾客数

总的服务时间(分)

为每个顾客的平均服务时间

空闲时间

空闲占总的时间%

5

50

10

19

27.3%

4

46

11.5

23

33.3%

1

13

13

56

81.2%

三、排队等待问题的计算机模拟

在此基础上,可用计算机编写程序,在计算机上实现仿真模拟,用以模拟更多时间和更多顾客时的情况。以下我我组用Matlab编写的模拟程序的算法部分:

function [num,pass]=computing(tim0)

seat=[0 0 0];%服务员属性

pass=rand(1,4);%顾客信息:序号、到达时间、特殊要求时间、正常理发时间

pass(5)=0;%理发员

pass(6)=0;%离开时间

pass(7)=0;%等待时间

num=1;%服务人数初始化

tim=0;%时间计数器

temp=0;%

while tim<=tim0 

    pass(num,1)=num;  %装入序号

    pass(num,2)=rand;

    pass(num,3)=rand;

    pass(num,4)=rand;

    

    %计算顾客到达时间

    if pass(num,2)<=0.07

       temp=4;

    else if  pass(num,2)<=0.17

            temp=5;

        else if  pass(num,2)<=0.69

            temp=6;

            else if  pass(num,2)<=0.89

            temp=7;

                else temp=8;

                end

            end

        end

    end

    tim=tim+temp;   %装入顾客到达时间

    pass(num,2)=tim;

    if pass(num,3)<=0.1 

        pass(num,3)=4;  %装入需要特殊服务的时间

    else pass(num,3)=0;

    end

    num=num+1;

end

num=num-1;

for i=1:num

    

    %计算顾客的理发席位

    if seat(1)<=pass(i,2)+pass(i,7)

        pass(i,5)=1; %1号服务员理发

        temp1=timinge1(1,pass(i,4));

        seat(1)=pass(i,2)+pass(i,3)+temp1;

        pass(i,4)=temp1;  %装入正常理发所需时间

    else if seat(2)<=pass(i,2)+pass(i,7)

            pass(i,5)=2;  %2号服务员理发

            temp1=timinge1(2,pass(i,4));

            seat(2)=pass(i,2)+pass(i,3)+temp1;

            pass(i,4)=temp1; %装入正常理发所需时间

        else if seat(3)<=pass(i,2)+pass(i,7)

                pass(i,5)=3; %2号服务员理发

                temp1=timinge1(3,pass(i,4));

                seat(3)=pass(i,2)+pass(i,3)+temp1;

                pass(i,4)=temp1;                

            else               %计算等待时间

                x=seat(1);

                y=1; 

                if x>seat(2)

                    x=seat(2);

                    y=2;

                end

                if x>seat(3)

                    x=seat(3);

                    y=3;

                end

                pass(i,5)=y;

                temp1=timinge1(y,pass(i,4));

                pass(i,7)=seat(y)-pass(i,2);

                seat(y)=seat(y)+temp1+pass(i,3);

                pass(i,4)=temp1;

            end

        end

    end

pass(i,6)=pass(i,2)+pass(i,3)+pass(i,4);

end

function **x=timinge1(vect1,vect)

switch vect1

    case 1    

       if vect<=0.18

            **x=8;

        else if  vect<=0.4

               **x=9;

            else if vect<=0.77

                    **x=10;

                else **x=11;

                end

            end

       end

   case 2    

       if vect<=0.18

            **x=10;

        else if  vect<=0.37

               **x=11;

            else if vect<=0.72

                    **x=12;

                else **x=13;

                end

            end

       end

    otherwise  

        if vect<=0.15

            **x=12;

        else if  vect<=0.37

               **x=13;

            else if vect<=0.74

                    **x=14;

                else **x=15;

                end

            end

       end

end

下面是在一次模拟过程中产生的顾客用户信息表:

理发店顾客数据统计

序号

到达时间

正常理发所需时间

特殊要求时间

等待时间

离开时间

理发员

序号

到达时间

正常理发所需时 间

特殊要求时间

等待时间

离开时间

理发员

1

8

10

0

0

18

1

58

369

14

0

0

383

3

2

12

12

0

0

24

2

59

375

8

0

0

383

1

3

18

11

0

0

29

1

60

381

13

0

0

394

2

4

25

12

0

0

37

2

61

387

11

0

0

398

1

5

33

10

0

0

43

1

62

393

15

0

0

408

3

6

41

11

4

0

56

2

63

399

8

0

0

407

1

7

48

8

4

0

60

1

64

404

11

0

0

415

2

8

54

14

0

0

68

3

65

411

8

0

0

419

1

9

60

8

0

0

68

1

66

419

9

0

0

428

1

10

67

13

0

0

80

2

67

425

12

0

0

437

2

11

73

11

0

0

84

1

68

431

8

0

0

439

1

12

79

14

0

0

93

3

69

437

13

0

0

450

2

13

86

10

4

0

100

1

70

443

11

0

0

454

1

14

92

12

0

0

104

2

71

449

15

0

0

464

3

15

99

13

0

0

112

3

72

455

8

0

0

463

1

16

105

10

0

0

115

1

73

460

12

0

0

472

2

17

111

10

0

0

121

2

74

468

10

0

0

478

1

18

118

11

0

0

129

1

75

474

10

0

0

484

2

19

125

12

0

0

137

2

76

480

11

0

0

491

1

20

133

10

0

0

143

1

77

486

12

0

0

498

2

21

140

11

0

0

151

2

78

492

10

0

0

502

1

22

146

10

0

0

156

1

79

500

11

4

0

515

2

23

153

13

0

0

166

2

80

504

10

0

0

514

1

24

159

10

0

0

169

1

81

512

14

0

0

526

3

25

165

14

0

0

179

3

82

518

10

0

0

528

1

26

171

11

0

0

182

1

83

524

12

4

0

540

2

27

177

10

0

0

187

2

84

531

11

0

0

542

1

28

185

9

0

0

194

1

85

538

14

0

0

552

3

29

192

10

0

0

202

2

86

545

11

0

0

556

1

30

196

9

0

0

205

1

87

551

11

0

0

562

2

31

202

11

0

0

213

2

88

557

9

0

0

566

1

32

208

11

4

0

223

1

89

563

12

0

0

575

2

33

214

12

0

0

226

2

90

570

10

0

0

580

1

34

220

13

0

0

233

3

91

576

12

0

0

588

2

35

226

10

0

0

236

1

92

582

10

0

0

592

1

36

232

12

0

0

244

2

93

589

13

0

0

602

2

37

239

10

0

0

249

1

94

595

10

0

0

605

1

38

245

10

0

0

255

2

95

603

12

0

0

615

2

39

251

10

4

0

265

1

96

609

10

0

0

619

1

40

257

10

0

0

267

2

97

615

12

0

0

627

2

41

264

15

0

0

279

3

98

622

11

0

0

633

1

42

271

10

0

0

281

1

99

630

11

4

0

645

2

43

277

11

0

0

288

2

100

636

10

0

0

646

1

44

285

10

0

0

295

1

101

640

14

0

0

654

3

45

292

12

0

0

304

2

102

648

10

0

0

658

1

46

300

9

0

0

309

1

103

654

13

0

0

667

2

47

307

10

0

0

317

2

104

659

11

0

0

670

1

48

313

10

0

0

323

1

105

665

15

0

0

680

3

49

319

12

0

0

331

2

106

671

10

0

0

681

1

50

323

9

4

0

336

1

107

679

12

4

0

695

2

51

327

14

0

0

341

3

108

685

9

0

0

694

1

52

332

12

4

0

348

2

109

691

13

4

0

708

3

53

338

8

4

0

350

1

110

697

10

0

0

707

1

54

344

12

0

0

356

3

111

703

13

0

0

716

2

55

350

10

0

0

360

1

112

710

11

0

0

721

1

56

357

12

4

0

373

2

113

716

11

0

0

727

2

57

363

8

0

0

371

1

114

722

8

0

0

730

1

从该表中,我们可以做出如下分析:

1.共计114顾客中,在系统(理发馆内)的总时间为1319分,平均每人1319/114=11.6

2.系统内无等待时间,即没出现顾客排队等待问题.

3.理发员工作情况如下:

A

B

C

服务顾客数

总的服务时间(分)

为每个顾客的平均服务时间

空闲时间

空闲占总的时间%

55

565

10.3

155

21.5%

44

534

12.1

186

25.8%

15

211

14.1

509

70.7%

四、小结

在排队等待问题中,存在着在随机的某种不确定需要,而这种不确定的需要,还具备了一定的统计规律,用蒙特卡洛法对此类问题求解,可节省时间和劳力 ,大大提高工作效率 ,同时确保计算的高效、准确 ,对可靠性设计提供了保证。使我们对解决问题的决策有可能更加明智,从而能够有效地解决问题。缺点是为提高计算精度必须进行大量模拟计算 ,需要较长的计算时间和编程时间

附:用matlab 7.0 实现该项模拟的源程序:

clear all

close all

tim0=720;%需要模拟的时间

[num,pass]=computing(tim0);%计算模拟数据

%动画制作

envirment    %场景

title1= annotation('textbox', 'Position',[0.3377 0.8712 0.3348 0.07885],...

  'EdgeColor','none','FitHeightToText','off', 'FontName','Arial','FontSize',16,...

  'FontWeight','bold','String',{'理发店忙闲情况分析'});

time1 = annotation('textbox','Position',[0.02754 0.1019 0.0942 0.06538],...

  'EdgeColor','none','FitHeightToText','off','FontSize',14,'FontWeight','bold',...

  'String',{'时间'});

hour1 = annotation('textbox','Position',[0.1072 0.1038 0.07536 0.06346],...

  'EdgeColor','none','FitHeightToText','off','FontSize',14,...

  'FontWeight','bold','String',{'08'});

minute1 = annotation('textbox','Position',[0.1493 0.1038 0.07246 0.06538],...

  'EdgeColor','none','FitHeightToText','off', 'FontSize',14,...

  'FontWeight','bold','String',{'10'});

counter1=1;  %时间计数

temp1=1;     %顾客计数

counter=1;

counterxy=zeros(7,2);

tempa=0;

tempb=0;

tempc=0;

tempd=0;

tempe=0;

tempf=0;

tempg=0;

global man1 mana1 mana2 mana3 mana4 mana5

global manb1 manb2 manb3 manb4 manb5

global manc1 manc2 manc3 manc4 manc5

global mand1 mand2 mand3 mand4 mand5

global mane1 mane2 mane3 mane4 mane5

    man1(1,:)=[0.4625 0.1786 0.0982 0.1381];  %[x0 y0 x1 y1]

    man1(2,:)=[0.4875 0.2667 0.5018 0.2667];  %[x0 y0 x1 y1]

    man1(3,:)=[0.5214 0.2667 0.5339 0.2667];  %[x0 y0 x1 y1]

    man1(4,:)=[0.5125 0.25 0.5125 0.23];      %[x0 y0 x1 y1]

    man1(5,:)=[0.4968 0.2081 0.525 0.2072];   %[x0 y0 x1 y1]

axis off    

while  counter1<=10  %时间计数

    %显示时间

    minutex=rem(counter1,60);

    hourx=8+(counter1-minutex)/60;

    set(hour1,'String',{hourx})

    set(minute1,'String',{minutex})

  

    if temp1<=num

        

        %离开与删除人脸对象

        if tempa ~= 0

           if  pass(tempa,6) == counter1

               leaveflash(1,counterxy(1,1),counterxy(1,2))

               tempa=0;

               delete(mana1,mana2,mana3,mana4,mana5);

           end

        end

        if tempb~=0

           if  pass(tempb,6) == counter1

               leaveflash(2,counterxy(2,1),counterxy(2,2))

               tempb=0;

               delete(manb1,manb2,manb3,manb4,manb5);

           end

        end

        if tempc~=0            

           if  pass(tempc,6) == counter1

               leaveflash(3,counterxy(3,1),counterxy(3,2))

               tempc=0;

               delete(manc1,manc2,manc3,manc4,manc5);

           end

        end

        if tempd~=0

           if  pass(tempd,6) == counter1

               leaveflash(4,counterxy(4,1),counterxy(4,2))

               tempd=0;

               delete(mand1,mand2,mand3,mand4,mand5);

           end

        end

        if tempe~=0

           if  pass(tempe,6) == counter1

               leaveflash(5,counterxy(5,1),counterxy(5,2))

               tempe=0;

               delete(mane1,mane2,mane3,mane4,mane5);

           end

        end

        if tempf~=0

           if  pass(tempf,6) == counter1

               leaveflash(6,counterxy(6,1),counterxy(6,2))

               tempf=0;

               delete(manf1,manf2,manf3,manf4,manf5);

           end

        end

        if tempg~=0

           if  pass(tempg,6) == counter1

               leaveflash(7,counterxy(7,1),counterxy(7,2))

               tempg=0;

               delete(mang1,mang2,mang3,mang4,mang5);

           end

        end

         

        %产生人脸

        if pass(temp1,2)==counter1

            if tempa==0

               [mana1,mana2,mana3,mana4,mana5]=personcreat(pass(temp1,3))

               tempa=temp1;

               if pass(temp1,5)==1

                   counterxy(1,:)=[-0.027 0.055];

               end

               if pass(temp1,5)==2

                   counterxy(1,:)=[0 0.055];

               end

               if pass(temp1,5)==3

                   counterxy(1,:)=[0.027 0.055];

               end

               temp1=temp1+1;            

            else

                if tempb==0

                    [manb1,manb2,manb3,manb4,manb5]=personcreat(pass(temp1,3))

                    tempb=temp1;

                    if pass(temp1,5)==1

                        counterxy(2,:)=[-0.027 0.055];

                    end

                    if pass(temp1,5)==2

                        counterxy(2,:)=[0 0.055];

                    end

                    if pass(temp1,5)==3

                        counterxy(3,:)=[0.027 0.055];

                    end

                    temp1=temp1+1;

                else

                    if tempc==0

                        [manc1,manc2,manc3,manc4,manc5]=personcreat(pass(temp1,3))

                        tempc=temp1;

                        if pass(temp1,5)==1

                            counterxy(3,:)=[-0.027 0.055];

                        end

                        if pass(temp1,5)==2

                            counterxy(3,:)=[0 0.055];

                        end

                        if pass(temp1,5)==3

                            counterxy(3,:)=[0.027 0.055];

                        end

                        temp1=temp1+1;

                    else

                        if tempd==0

                            [mand1,mand2,mand3,mand4,mand5]=personcreat(pass(temp1,3))

                            tempd=temp1;

                            if pass(temp1,5)==1

                                counterxy(4,:)=[-0.027 0.055];

                            end

                            if pass(temp1,5)==2

                                counterxy(4,:)=[0 0.055];

                            end

                            if pass(temp1,5)==3

                                counterxy(4,:)=[0.027 0.055];

                            end

                            temp1=temp1+1;

                        else

                            if tempe==0

                                [mane1,mane2,mane3,mane4,mane5]=personcreat(pass(temp1,3))

                                tempe=temp1;  

                                if pass(temp1,5)==1

                                    counterxy(5,:)=[-0.027 0.055];

                                end

                                if pass(temp1,5)==2

                                    counterxy(5,:)=[0 0.055];

                                end

                                if pass(temp1,5)==3

                                    counterxy(5,:)=[0.027 0.055];

                                end

                                temp1=temp1+1;

                            else

                                if tempf==0

                                    [manf1,manf2,manf3,manf4,manf5]=personcreat(pass(temp1,3))

                                    tempf=temp1;  

                                    if pass(temp1,5)==1

                                        counterxy(6,:)=[-0.027 0.055];

                                    end

                                    if pass(temp1,5)==2

                                        counterxy(6,:)=[0 0.055];

                                    end

                                    if pass(temp1,5)==3

                                        counterxy(6,:)=[0.027 0.055];

                                    end

                                    temp1=temp1+1;

                                else

                                    if tempg==0

                                        [mang1,mang2,mang3,mang4,mang5]=personcreat(pass(temp1,3))

                                        tempg=temp1;  

                                        if pass(temp1,5)==1

                                            counterxy(7,:)=[-0.027 0.055];

                                        end

                                        if pass(temp1,5)==2

                                            counterxy(7,:)=[0 0.055];

                                        end

                                        if pass(temp1,5)==3

                                            counterxy(7,:)=[0.027 0.055];

                                        end

                                        temp1=temp1+1;

                                    end

                                end                                

                            end

                        end

                    end

                end

            end

        end

        

        %开始服务

         if tempa~=0

             if  pass(tempa,2)+pass(tempa,7) == counter1

                 serveflash(1,counterxy(1,1),counterxy(1,2))

             end

         end

         if tempb~=0

             if  pass(tempb,2)+pass(tempb,7) == counter1

                 serveflash(2,counterxy(2,1),counterxy(2,2))

             end

         end

         if tempc~=0

             if  pass(tempc,2)+pass(tempc,7) == counter1

                 serveflash(3,counterxy(3,1),counterxy(3,2))

             end

         end

         if tempd~=0

             if  pass(tempd,2)+pass(tempd,7) == counter1

                 serveflash(4,counterxy(4,1),counterxy(4,2))

             end

         end

         if tempe~=0

             if  pass(tempe,2)+pass(tempe,7) == counter1

                 serveflash(5,counterxy(5,1),counterxy(5,2))

             end

         end

         if tempf~=0

             if  pass(tempf,2)+pass(tempf,7) == counter1

                 serveflash(6,counterxy(6,1),counterxy(6,2))

             end

         end

         if tempg~=0

             if  pass(tempg,2)+pass(tempg,7) == counter1

                 serveflash(7,counterxy(7,1),counterxy(7,2))

             end

         end

    end   

    counter1=counter1+1;

end

iiii=1;

sumtime=0;

sumtimea=0;

sumtimeb=0;

sumtimec=0;

numa=0;

numb=0;

numc=0;

while iiii<num

    sumtime=sumtime+pass(iiii,3)+pass(iiii,4);

    if pass(iiii,5)==1

        sumtimea=sumtimea+pass(iiii,3)+pass(iiii,4);

        numa=numa+1;

    end

    if pass(iiii,5)==2

        sumtimeb=sumtimeb+pass(iiii,3)+pass(iiii,4);

        numb=numb+1;

    end

    if pass(iiii,5)==3

        sumtimec=sumtimec+pass(iiii,3)+pass(iiii,4);

        numc=numc+1;

    end

iiii=iiii+1;

end

num=num-1;

freetimea=720-sumtimea;

freetimeb=720-sumtimeb;

freetimec=720-sumtimec;

freetime=freetimea+freetimeb+freetimec;

avetimea=round(1000*sumtimea/720)/1000;

avetimeb=round(1000*sumtimeb/720)/1000;

avetimec=round(1000*sumtimec/720)/1000;

avetime=round(1000*sumtime/(3*720))/1000;

scaleaa=freetimea/720;

scaleab=freetimeb/720;

scaleac=freetimec/720;

dispdata(1,:)=[numa numb numc];

dispdata(2,:)=[sumtimea sumtimeb sumtimec];

dispdata(3,:)=[avetimea avetimeb avetimec];

dispdata(4,:)=[freetimea freetimeb freetimec];

dispdata(5,:)=[freetimea/720 freetimeb/720 freetimec/720];

figure(2)

subplot(2,2,1),pause

bar(dispdata(1,:))

title('服务顾客数')

subplot(2,2,2),pause

title('服务时间')

bar(dispdata(2,:))

subplot(2,2,3),pause

title('平均服务时间')

pie(dispdata(3,:))

subplot(2,2,4),pause

title('空闲比例')

pie(dispdata(5,:))

function [num,pass]=computing(tim0)

seat=[0 0 0];%服务员属性

pass=rand(1,4);%序号、到达时间、特殊要求时间、正常理发时间

pass(5)=0;%服务员

pass(6)=0;%离开时间

pass(7)=0;%等待时间

num=1;%服务人数

tim=0;%时间计数器

temp=0;%

while tim<=tim0 

    pass(num,1)=num;  %装入序号

    pass(num,2)=rand;

    pass(num,3)=rand;

    pass(num,4)=rand;

    

    %计算顾客到达时间

    if pass(num,2)<=0.07

       temp=4;

    else if  pass(num,2)<=0.17

            temp=5;

        else if  pass(num,2)<=0.69

            temp=6;

            else if  pass(num,2)<=0.89

            temp=7;

                else temp=8;

                end

            end

        end

    end

    tim=tim+temp;   %装入顾客到达时间

    pass(num,2)=tim;

    if pass(num,3)<=0.1 

        pass(num,3)=4;  %装入需要特殊服务的时间

    else pass(num,3)=0;

    end

    num=num+1;

end

num=num-1;

for i=1:num

    

    %计算顾客的理发席位

    if seat(1)<=pass(i,2)+pass(i,7)

        pass(i,5)=1; %1号服务员理发

        temp1=timinge1(1,pass(i,4));

        seat(1)=pass(i,2)+pass(i,3)+temp1;

        pass(i,4)=temp1;  %装入正常理发所需时间

    else if seat(2)<=pass(i,2)+pass(i,7)

            pass(i,5)=2;  %2号服务员理发

            temp1=timinge1(2,pass(i,4));

            seat(2)=pass(i,2)+pass(i,3)+temp1;

            pass(i,4)=temp1; %装入正常理发所需时间

        else if seat(3)<=pass(i,2)+pass(i,7)

                pass(i,5)=3; %2号服务员理发

                temp1=timinge1(3,pass(i,4));

                seat(3)=pass(i,2)+pass(i,3)+temp1;

                pass(i,4)=temp1;                

            else               %计算等待时间

                x=seat(1);

                y=1; 

                if x>seat(2)

                    x=seat(2);

                    y=2;

                end

                if x>seat(3)

                    x=seat(3);

                    y=3;

                end

                pass(i,5)=y;

                temp1=timinge1(y,pass(i,4));

                pass(i,7)=seat(y)-pass(i,2);

                seat(y)=seat(y)+temp1+pass(i,3);

                pass(i,4)=temp1;

            end

        end

    end

pass(i,6)=pass(i,2)+pass(i,3)+pass(i,4);

end

function envirment

%场景设置

    pict=figure('color',[0.75 0.75 0.75],'position',[50 50 690 520]);

    door = annotation('rectangle',[0.4214 0.1405 0.1946 0.03333],'FaceColor',[0 1 0],'EdgeColor',[0 1 0]);

    word1 = annotation('textbox','Position',[0.3393 0.1143 0.0875 0.07619],'EdgeColor','none','FitHeightToText','off','FontSize',12,'String',{'门口'});

    

    seata1= annotation('rectangle',[0.2054 0.8214 0.1107 0.02857],'FaceColor',[0.8471 0.1608 0],'EdgeColor',[0.8471 0.1608 0]);

    seata2 = annotation('rectangle',[0.2071 0.7476 0.01429 0.1024],'FaceColor',[0.8471 0.1608 0],'EdgeColor',[0.8471 0.1608 0]);

    seata3 = annotation('rectangle',[0.3021 0.7419 0.01429 0.1024],'FaceColor',[0.8471 0.1608 0],'EdgeColor',[0.8471 0.1608 0]);

    word2 = annotation('textbox','Position',[0.1607 0.65 0.1107 0.06667],'EdgeColor','none','FitHeightToText','off','String',{'Seat A'});

    

    seatb1 = annotation('rectangle',[0.4579 0.8171 0.1107 0.02857],'FaceColor',[0.8471 0.1608 0],'EdgeColor',[0.8471 0.1608 0]);

    seatb2 = annotation('rectangle',[0.4579 0.7386 0.01429 0.1024],'FaceColor',[0.8471 0.1608 0],'EdgeColor',[0.8471 0.1608 0]);

    seatb3 = annotation('rectangle',[0.5564 0.7448 0.01429 0.1024],'FaceColor',[0.8471 0.1608 0],'EdgeColor',[0.8471 0.1608 0]);

    word3 = annotation('textbox','Position',[0.3768 0.6381 0.1036 0.07381],'EdgeColor','none','FitHeightToText','off','String',{'Seat B'});

    

    seatc1 = annotation('rectangle',[0.6986 0.8157 0.1107 0.02857],'FaceColor',[0.8471 0.1608 0],'EdgeColor',[0.8471 0.1608 0]);

    seatc2 = annotation('rectangle',[0.6996 0.7343 0.01429 0.1024],'FaceColor',[0.8471 0.1608 0],'EdgeColor',[0.8471 0.1608 0]);

    seatc3 = annotation('rectangle',[0.7964 0.7333 0.01429 0.1024],'FaceColor',[0.8471 0.1608 0],'EdgeColor',[0.8471 0.1608 0]);

    word4 = annotation('textbox','Position',[0.6429 0.6286 0.1018 0.07619],'EdgeColor','none','FitHeightToText','off','String',{'Seat C'});

    

    watierplace = annotation('rectangle',[0.8214 0.1762 0.01964 0.3857],'FaceColor',[0 1 1],'EdgeColor',[0 1 1]);

    word5 = annotation('textbox','Position',[0.7107 0.2 0.1179 0.0619],'EdgeColor','none','FitHeightToText','off','String',{'等待席'});

function leaveflash(xx,yy,zz)

global man1 mana1 mana2 mana3 mana4 mana5

global manb1 manb2 manb3 manb4 manb5

global manc1 manc2 manc3 manc4 manc5

global mand1 mand2 mand3 mand4 mand5

global mane1 mane2 mane3 mane4 mane5

    

    switch xx

    case 1

          for ii = 10:-1:1

               face(:,1)=man1(:,1)+yy*(ii-1);

               face(:,3)=man1(:,3)+yy*(ii-1); 

               face(:,2)=man1(:,2)+zz*(ii-1);

               face(:,4)=man1(:,4)+zz*(ii-1);

               face(1,3)=0.0982;

               face(1,4)=0.1381;

               set(mana1,'position',[face(1,1) face(1,2)  face(1,3) face(1,4)]);

               set(mana2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)]);

               set(mana3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)]);

               set(mana4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)]);

               set(mana5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]);

               MM(ii)=getframe;

          end          

    case 2

           for ii = 10:-1:1

               face(:,1)=man1(:,1)+yy*(ii-1);

               face(:,3)=man1(:,3)+yy*(ii-1);

               face(:,2)=man1(:,2)+zz*(ii-1);

               face(:,4)=man1(:,4)+zz*(ii-1);

               face(1,3)=0.0982;

               face(1,4)=0.1381;

               set(manb1,'position',[face(1,1) face(1,2)  face(1,3) face(1,4)]);

               set(manb2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)]);

               set(manb3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)]);

               set(manb4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)]);

               set(manb5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]);

               MM(ii)=getframe;

            end

    case 3

           for ii = 10:-1:1

               face(:,1)=man1(:,1)+yy*(ii-1);

               face(:,3)=man1(:,3)+yy*(ii-1); 

               face(:,2)=man1(:,2)+zz*(ii-1);

               face(:,4)=man1(:,4)+zz*(ii-1);

               face(1,3)=0.0982;

               face(1,4)=0.1381;

               set(manc1,'position',[face(1,1) face(1,2)  face(1,3) face(1,4)]);

               set(manc2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)]);

               set(manc3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)]);

               set(manc4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)]);

               set(manc5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]);

               MM(ii)=getframe;

           end

    case 4

           for ii = 10:-1:1

               face(:,1)=man1(:,1)+yy*(ii-1);

               face(:,3)=man1(:,3)+yy*(ii-1); 

               face(:,2)=man1(:,2)+zz*(ii-1);

               face(:,4)=man1(:,4)+zz*(ii-1);

               face(1,3)=0.0982;

               face(1,4)=0.1381;

               set(mand1,'position',[face(1,1) face(1,2)  face(1,3) face(1,4)]);

               set(mand2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)]);

               set(mand3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)]);

               set(mand4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)]);

               set(mand5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]);

               MM(ii)=getframe;

           end

     case 5

           for ii = 10:-1:1

               face(:,1)=man1(:,1)+yy*(ii-1);

               face(:,3)=man1(:,3)+yy*(ii-1); 

               face(:,2)=man1(:,2)+zz*(ii-1);

               face(:,4)=man1(:,4)+zz*(ii-1);

               face(1,3)=0.0982;

               face(1,4)=0.1381;

               set(mane1,'position',[face(1,1) face(1,2)  face(1,3) face(1,4)]);

               set(mane2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)]);

               set(mane3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)]);

               set(mane4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)]);

               set(mane5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]);

               MM(ii)=getframe;

           end

     case 6

           for ii = 10:-1:1

               face(:,1)=man1(:,1)+yy*(ii-1);

               face(:,3)=man1(:,3)+yy*(ii-1); 

               face(:,2)=man1(:,2)+zz*(ii-1);

               face(:,4)=man1(:,4)+zz*(ii-1);

               face(1,3)=0.0982;

               face(1,4)=0.1381;

               set(manf1,'position',[face(1,1) face(1,2)  face(1,3) face(1,4)]);

               set(manf2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)]);

               set(manf3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)]);

               set(manf4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)]);

               set(manf5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]);

               MM(ii)=getframe;

           end

     case 7

           for ii = 10:-1:1

               face(:,1)=man1(:,1)+yy*(ii-1);

               face(:,3)=man1(:,3)+yy*(ii-1); 

               face(:,2)=man1(:,2)+zz*(ii-1);

               face(:,4)=man1(:,4)+zz*(ii-1);

               face(1,3)=0.0982;

               face(1,4)=0.1381;

               set(mang1,'position',[face(1,1) face(1,2)  face(1,3) face(1,4)]);

               set(mang2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)]);

               set(mang3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)]);

               set(mang4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)]);

               set(mang5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]);

               MM(ii)=getframe;

           end 

end

        

function [face1,face2,face3,face4,face5]=personcreat(vect)

if vect==0

     face1 = annotation('ellipse',[0.4625 0.1786 0.0982 0.1381],'FaceColor',[0.6824 0.4667 0],'EdgeColor',[0.6824 0.4667 0]);

else face1 = annotation('ellipse',[0.4625 0.1786 0.0982 0.1381],'FaceColor',[0.4 0.6 0],'EdgeColor',[0.6824 0.4667 0]);%[0.2096 0.6881 0.09821 0.1381]

end

    face2 = annotation('line',[0.4875 0.5018],[0.2667 0.2667],'LineWidth',3);

    face3 = annotation('line',[0.5214 0.5339],[0.2667 0.2667],'LineWidth',3);

    face4 = annotation('line',[0.5125 0.5125],[0.25 0.2309],'LineWidth',3);

    face5 = annotation('line',[0.4968 0.525],[0.2081 0.2072],'LineWidth',3);

function serveflash(xx,yy,zz)

global man1 mana1 mana2 mana3 mana4 mana5

global manb1 manb2 manb3 manb4 manb5

global manc1 manc2 manc3 manc4 manc5

global mand1 mand2 mand3 mand4 mand5

global mane1 mane2 mane3 mane4 mane5

        

switch xx

    case 1

            for ii = 1:10  %开始服务动画

                   face(:,1)=man1(:,1)+yy*(ii-1);

                   face(:,3)=man1(:,3)+yy*(ii-1); 

                   face(:,2)=man1(:,2)+zz*(ii-1);

                   face(:,4)=man1(:,4)+zz*(ii-1);

                   face(1,3)=0.0982;

                   face(1,4)=0.1381;

                   set(mana1,'position',[face(1,1) face(1,2)  face(1,3) face(1,4)])

                   set(mana2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)])

                   set(mana3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)])

                   set(mana4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)])

                   set(mana5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]) 

                   MM(ii)=getframe;

            end

    case 2

        for ii = 1:10  %开始服务动画

                   face(:,1)=man1(:,1)+yy*(ii-1);

                   face(:,3)=man1(:,3)+yy*(ii-1); 

                   face(:,2)=man1(:,2)+zz*(ii-1);

                   face(:,4)=man1(:,4)+zz*(ii-1);

                   face(1,3)=0.0982;

                   face(1,4)=0.1381;

                   set(manb1,'position',[face(1,1) face(1,2)  face(1,3) face(1,4)])

                   set(manb2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)])

                   set(manb3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)])

                   set(manb4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)])

                   set(manb5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]) 

                   MM(ii)=getframe;

        end

    case 3

        for ii = 1:10  %开始服务动画

                   face(:,1)=man1(:,1)+yy*(ii-1);

                   face(:,3)=man1(:,3)+yy*(ii-1); 

                   face(:,2)=man1(:,2)+zz*(ii-1);

                   face(:,4)=man1(:,4)+zz*(ii-1);

                   face(1,3)=0.0982;

                   face(1,4)=0.1381;

                   set(manc1,'position',[face(1,1) face(1,2)  face(1,3) face(1,4)])

                   set(manc2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)])

                   set(manc3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)])

                   set(manc4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)])

                   set(manc5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]) 

                   MM(ii)=getframe;

        end

    case 4

        for ii = 1:10  %开始服务动画

                   face(:,1)=man1(:,1)+yy*(ii-1);

                   face(:,3)=man1(:,3)+yy*(ii-1); 

                   face(:,2)=man1(:,2)+zz*(ii-1);

                   face(:,4)=man1(:,4)+zz*(ii-1);

                   face(1,3)=0.0982;

                   face(1,4)=0.1381;

                   set(mand1,'position',[face(1,1) face(1,2)  face(1,3) face(1,4)])

                   set(mand2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)])

                   set(mand3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)])

                   set(mand4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)])

                   set(mand5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]) 

                   MM(ii)=getframe;

        end

    case 5

        for ii = 1:10  %开始服务动画

                   face(:,1)=man1(:,1)+yy*(ii-1);

                   face(:,3)=man1(:,3)+yy*(ii-1); 

                   face(:,2)=man1(:,2)+zz*(ii-1);

                   face(:,4)=man1(:,4)+zz*(ii-1);

                   face(1,3)=0.0982;

                   face(1,4)=0.1381;

                   set(mane1,'position',[face(1,1) face(1,2)  face(1,3) face(1,4)])

                   set(mane2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)])

                   set(mane3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)])

                   set(mane4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)])

                   set(mane5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]) 

                   MM(ii)=getframe;

        end

    case 6

        for ii = 1:10  %开始服务动画

                   face(:,1)=man1(:,1)+yy*(ii-1);

                   face(:,3)=man1(:,3)+yy*(ii-1); 

                   face(:,2)=man1(:,2)+zz*(ii-1);

                   face(:,4)=man1(:,4)+zz*(ii-1);

                   face(1,3)=0.0982;

                   face(1,4)=0.1381;

                   set(manf1,'position',[face(1,1) face(1,2)  face(1,3) face(1,4)])

                   set(manf2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)])

                   set(manf3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)])

                   set(manf4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)])

                   set(manf5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]) 

                   MM(ii)=getframe;

        end

    case 7

        for ii = 1:10  %开始服务动画

                   face(:,1)=man1(:,1)+yy*(ii-1);

                   face(:,3)=man1(:,3)+yy*(ii-1); 

                   face(:,2)=man1(:,2)+zz*(ii-1);

                   face(:,4)=man1(:,4)+zz*(ii-1);

                   face(1,3)=0.0982;

                   face(1,4)=0.1381;

                   set(mang1,'position',[face(1,1) face(1,2)  face(1,3) face(1,4)])

                   set(mang2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)])

                   set(mang3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)])

                   set(mang4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)])

                   set(mang5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]) 

                   MM(ii)=getframe;

        end        

end

               

function **x=timinge1(vect1,vect)

switch vect1

    case 1    

       if vect<=0.18

            **x=8;

        else if  vect<=0.4

               **x=9;

            else if vect<=0.77

                    **x=10;

                else **x=11;

                end

            end

       end

   case 2    

       if vect<=0.18

            **x=10;

        else if  vect<=0.37

               **x=11;

            else if vect<=0.72

                    **x=12;

                else **x=13;

                end

            end

       end

    otherwise  

        if vect<=0.15

            **x=12;

        else if  vect<=0.37

               **x=13;

            else if vect<=0.74

                    **x=14;

                else **x=15;

                end

            end

       end

end

原创粉丝点击