排队论经典程序MM1代码.docx
《排队论经典程序MM1代码.docx》由会员分享,可在线阅读,更多相关《排队论经典程序MM1代码.docx(18页珍藏版)》请在冰豆网上搜索。
排队论经典程序MM1代码
修理店仿真报告
一.问题:
1修理店空闲的概率;
2店有三个顾客的概率;
3店至少有一个顾客的概率;
4在店顾客的平均数;
5顾客在店的平均逗留时间;
6顾客必须在店消耗15分钟以上的概率。
二.求解问题的方法:
①修理店空闲的概率:
(sim_time-area_server_status)/sim_time);
②店有三个顾客的概率:
area_3_in_q/sim_time);
③店至少有一个顾客的概率:
abv_1/sim_time);
④在店顾客的平均数:
area_num_in_h/sim_time);
⑤顾客在店的平均逗留时间:
(total_of_delays+total_of_server)/num_custs_delayed);
⑥顾客必须在店消耗15分钟以上概率:
abv_15/num_custs_delayed);
三。
求解过程中计算统计量的方法:
1 area_server_status+=server_status*time_since_last_event;
2 //店有三个顾客的概率
if(server_status==BUSY)
//服务台忙,则有队列中有两个顾客
if(num_in_q==2)
area_3_in_q+=time_since_last_event;
3 //店至少有一个顾客的概率
if(server_status==BUSY)//服务台忙,则店至少有一个顾客
abv_1+=time_since_last_event;
4 //在店顾客的平均数
if(server_status==BUSY)//服务台忙,总的顾客数为排队顾客数加一
area_num_in_h+=(num_in_q+1)*time_since_last_event;
5 total_of_server+=time_next_event[2]-sim_time;//总的服务时间加一个服务时间为新的服务总时间
delay=sim_time-time_arrival[1];//排队时间=当前时间-这个人来的时间
total_of_delays+=delay;
6 //离开时总的消耗时间大于15,必须在店消耗15分钟以上的顾客数加一
if((delay+time_next_event[2]-sim_time)>15)
abv_15++;
//到达时总的服务时间大于15,必须在店消耗15分钟以上的顾客数加一
if((time_next_event[2]-sim_time)>15)
abv_15++;
程序代码:
/*Externaldefinitionsforsingle-serverqueueingsystem.*/
#include
#include
/*#include"lcgrand.h"Headerfileforrandom-numbergenerator.*/
#defineQ_LIMIT100/*Limitonqueuelength.队伍最长100人*/
#defineBUSY1/*Mnemonicsforserver'sbeingbusy忙碌状态*/
#defineIDLE0/*andidle.空闲状态*/
intnext_event_type,//下一个事件类型
num_custs_delayed,//已模拟的顾客数
num_delays_required,//模拟的顾客数
num_events,//事件数
num_in_q,//队列中的顾客数
server_status;//服务状态
floatarea_num_in_q,//有顾客的时间
area_server_status,//总的服务时间
mean_interarrival,//平均顾客到达时间间隔
mean_service,//平均服务时间
sim_time,//模拟时间
time_arrival[Q_LIMIT+1],//到来的时间
time_last_event,//上一个事件的时间
time_next_event[3],//下一个事件的时间
total_of_delays;//总的排队时间
////////////////////////////////////////////////////////////////////////////////////
//添加的变量
floatabv_15,//15分钟以上的顾客数量
total_of_server,//所有顾客的总的服务时间
area_3_in_q,//有3个顾客的时间
abv_1,//至少有一个顾客的时间
area_num_in_h;//顾客总数
////////////////////////////////////////////////////////////////////////////////////
FILE*infile,*outfile;
/*Thefollowing3declarationsareforuseoftherandom-numbergenerator
lcgrandandtheassociatedfunctionslcgrandstandlcgrandgtforseed
management.Thisfile(namedlcgrand.h)shouldbeincludedinanyprogram
usingthesefunctionsbyexecuting
#include"lcgrand.h"
beforereferencingthefunctions.*/
floatlcgrand(intstream);
voidlcgrandst(longzset,intstream);
longlcgrandgt(intstream);
voidinitialize(void);
voidtiming(void);
voidarrive(void);
voiddepart(void);
voidreport(void);
voidupdate_time_avg_stats(void);
floatexpon(floatmean);
main()/*Mainfunction.*/
{
/*Openinputandoutputfiles.*/
infile=fopen("mm1.in","r");
outfile=fopen("mm1.out","w");
/*Specifythenumberofeventsforthetimingfunction.*/
num_events=2;//两种事件
/*Readinputparameters.*/
fscanf(infile,"%f%f%d",&mean_interarrival,&mean_service,
&num_delays_required);
/*Writereportheadingandinputparameters.输出*/
fprintf(outfile,"Single-serverqueueingsystem\n\n");
fprintf(outfile,"Meaninterarrivaltime%11.3fminutes\n\n",
mean_interarrival);
fprintf(outfile,"Meantime%16.3fminutes\n\n",mean_service);
fprintf(outfile,"Numberofcustomers%14d\n\n",num_delays_required);
/*Initializethesimulation.初始化仿真*/
initialize();//初始化
/*Runthesimulationwhilemoredelaysarestillneeded.没服务完,仿真继续*/
while(num_custs_delayed/*Determinethenextevent.确定下一事件*/
timing();
/*Updatetime-averagestatisticalaccumulators.时间记录更新*/
update_time_avg_stats();
/*Invoketheappropriateeventfunction.根据事件的不同,调用不同的函数*/
switch(next_event_type){
case1:
arrive();//到达
break;
case2:
depart();//离开
break;
}
}
/*Invokethereportgeneratorandendthesimulation.*/
report();
fclose(infile);
fclose(outfile);
return0;
}
voidinitialize(void)/*Initializationfunction.*/
{
/*Initializethesimulationclock.仿真时间置为0*/
sim_time=0.0;
/*Initializethestatevariables.最开始状态初始化*/
server_status=IDLE;//服务空闲
num_in_q=0;//队伍里无人排队
time_last_event=0.0;//上一个事件的时间,最开始肯定是0开始
/*Initializethestatisticalcounters.*/
num_custs_delayed=0;//已经服务的人数
total_of_delays=0.0;//总的排队时间
area_num_in_q=0.0;//有顾客的时间
area_server_status=0.0;//总的服务时间
/////////////////////////////////////////////////////////////////////////////////
//添加的变量的初始化
area_3_in_q=0.0;//有3个顾客的时间
abv_1=0.0;//有顾客的时间
area_num_in_h=0.0;//顾客的总数
total_of_server=0.0;//所有顾客的所有的服务的时间
abv_15=0.0;//消耗15分钟以上的顾客数
/////////////////////////////////////////////////////////////////////////////////
/*Initializeeventlist.初始化事件列表Sincenocustomersarepresent,thedeparture
(servicecompletion)eventiseliminatedfromconsideration.无顾客存在和离开*/
time_next_event[1]=sim_time+expon(mean_interarrival);//下一事件是来的时间
time_next_event[2]=1.0e+30;//下一事件是离开的时间
}
voidtiming(void)/*Timingfunction.*/
{
inti;
floatmin_time_next_event=1.0e+29;//像指针一样的对于当前服务的人来说下一个事件的时间
next_event_type=0;
/*Determinetheeventtypeofthenexteventtooccur.接下来将要发生的事件的类型*/
for(i=1;i<=num_events;++i)
if(time_next_event[i]min_time_next_event=time_next_event[i];
next_event_type=i;
}
/*Checktoseewhethertheeventlistisempty.*/
if(next_event_type==0){
/*Theeventlistisempty,sostopthesimulation.无事件,停止仿真过程*/
fprintf(outfile,"\nEventlistemptyattime%f",sim_time);
exit
(1);
}
/*Theeventlistisnotempty,soadvancethesimulationclock.有事件,进行仿真过程*/
sim_time=min_time_next_event;//仿真的时间就是当前事件的时间
}
voidarrive(void)/*Arrivaleventfunction.*/
{
floatdelay;
/*Schedulenextarrival.计划下一次的到来*/
time_next_event[1]=sim_time+expon(mean_interarrival);
/*Checktoseewhetherserverisbusy.检测是否在服务状态*/
if(server_status==BUSY){
/*Serverisbusy,soincrementnumberofcustomersinqueue.在服务则排队多一人*/
++num_in_q;
/*Checktoseewhetheranoverflowconditionexists.检测人数是否超出*/
if(num_in_q>Q_LIMIT){
/*Thequeuehasoverflowed,sostopthesimulation.*/
fprintf(outfile,"\nOverflowofthearraytime_arrivalat");
fprintf(outfile,"time%f",sim_time);
exit
(2);
}
/*Thereisstillroominthequeue,sostorethetimeofarrivalofthe
arrivingcustomeratthe(new)endoftime_arrival.队列中仍有空间时,记录新到达的时间*/
time_arrival[num_in_q]=sim_time;//在这个时间的时候有这么多的排队人数,用于计算3顾客的问题
}
else{//服务空闲的状况
/*Serverisidle,soarrivingcustomerhasadelayofzero.(The
followingtwostatementsareforprogramclarityanddonotaffect
theresultsofthesimulation.)*/
delay=0.0;
total_of_delays+=delay;//总的排队时间
/*Incrementthenumberofcustomersdelayed,andmakeserverbusy.*/
++num_custs_delayed;//已经模拟的顾客数加1
server_status=BUSY;//人到来,服务开始
/*Scheduleadeparture(servicecompletion).服务完成*/
time_next_event[2]=sim_time+expon(mean_service);//这个人离开的时间为现在时间+服务时间
/////////////////////////////////////////////////////////////////////////////////
//总的服务时间加上当前服务时间,更新总的服务时间
total_of_server+=time_next_event[2]-sim_time;//总的服务时间加一个服务时间为新的服务总时间
//总的服务时间大于15,必须在店消耗15分钟以上的顾客数加一
if((time_next_event[2]-sim_time)>15)//如果这个人服务时间超过15分钟则耗费15分钟人数加1
abv_15++;
/////////////////////////////////////////////////////////////////////////////////
}
}
voiddepart(void)/*Departureeventfunction.讨论离开事件*/
{
inti;
floatdelay;
/*Checktoseewhetherthequeueisempty.检测队列是否为空*/
if(num_in_q==0){
/*Thequeueisemptysomaketheserveridleandeliminatethe
departure(servicecompletion)eventfromconsideration.队列空,服务空闲*/
server_status=IDLE;
time_next_event[2]=1.0e+30;//离开的时间无限大(无人离开)
}
else{
/*Thequeueisnonempty,sodecrementthenumberofcustomersin
queue.有人离开,队列人数减少*/
--num_in_q;
/*Computethedelayofthecustomerwhoisbeginningandupdate
thetotaldelayaccumulator.*/
delay=sim_time-time_arrival[1];//排队时间=当前时间-这个人来的时间
total_of_delays+=delay;
/*Incrementthenumberofcustomersdelayed,andscheduledeparture.已经服务人数+1*/
++num_custs_delayed;//服务人数加1
time_next_event[2]=sim_time+expon(mean_service);//当前接受服务的人的离开时间
/////////////////////////////////////////////////////////////////////////////////
//总的服务时间加上当前服务时间,更新总的服务时间
total_of_server+=time_next_event[2]-sim_time;
//总的消耗时间大于15,必须在店消耗15分钟以上的顾客数加一
if((delay+time_next_event[2]-sim_time)>15)
abv_15++;
/////////////////////////////////////////////////////////////////////////////////
/*Moveeachcustomerinqueue(ifany)uponeplace.有人离开,队列前移*/
for(i=1;i<=num_in_q;++i)
time_arrival[i]=time_arrival[i+1];//人的到达时间也前移
}
}
voidreport(void)/*Reportgeneratorfunction.*/
{
/*Computeandwriteestimatesofdesiredmeasuresofperformance.*/
fprintf(outfile,"\n\nAveragedelayinqueue%11.3fminutes\n\n",
total_of_delays/num_custs_delayed);
fprintf(outfile,"Averagenumberinqueue%10.3f\n\n",
area_num_in_q/sim_time);
fprintf(outfile,"Serverutilization%15.3f\n\n",
area_server_status/sim_time);
fprintf(outfile,"Timesimulationended%12.3fminutes",sim_time);
printf("统计量:
\n");
////////////////////////////////////////////////////////////////////////
//总时间减去服务台忙的时间除以总时间,得到服务台空闲的概率
printf("①修理店空闲的概率:
%24.3f\n",
(sim_time-area_server_status)/sim_time);
printf("②店有三个顾客的概率:
%20.3f\n",
area_3_in_q/sim_time);
printf("③店至少有一个顾客的概率:
%16.3f\n",
abv_1/sim_time);
printf("④在店顾客的平均数:
%22.3f\n",
area_num_in_h