计算机仿真排队系统实验报告(附代码).doc

上传人:李医生 文档编号:5735531 上传时间:2020-07-25 格式:DOC 页数:11 大小:61.50KB
返回 下载 相关 举报
计算机仿真排队系统实验报告(附代码).doc_第1页
第1页 / 共11页
计算机仿真排队系统实验报告(附代码).doc_第2页
第2页 / 共11页
计算机仿真排队系统实验报告(附代码).doc_第3页
第3页 / 共11页
计算机仿真排队系统实验报告(附代码).doc_第4页
第4页 / 共11页
计算机仿真排队系统实验报告(附代码).doc_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《计算机仿真排队系统实验报告(附代码).doc》由会员分享,可在线阅读,更多相关《计算机仿真排队系统实验报告(附代码).doc(11页珍藏版)》请在三一文库上搜索。

1、计算机仿真实验报告第一题1. 作业内容应用排队系统流程图,用C语言编制仿真程序,求解以下问题。修理店只有一个修理工,来修理的顾客到达次数服从泊松分布,平均4人/h;修理时间服从指数分布,平均需6min。试求(随机数发生器采用float lcgrand(int stream) ,种子stream 为自己学号的最后两位。): 修理店空闲的概率; 店内有三个顾客的概率; 店内至少有一个顾客的概率; 在店内顾客的平均数; 顾客在店内的平均逗留时间; 顾客必须在店内消耗15分钟以上的概率。统计量实现算法: 修理店空闲的概率; p1=1-area_server_status/sim_time area_s

2、erver_status:总服务时间(即修理工在这段仿真时间里非空闲时间) sim_time:总仿真时间 用1减去非空闲概率,即为空闲概率。 店内有三个顾客的概率; p2=Three_people_time/sim_time 增加变量Three_people_time,即有三个顾客在店内的时间。 三个顾客在店里,也就是说一个顾客在理发,两个人在排队,此时,无论是来一个新的客人或者离开一个客人,都会破坏这种三个人的状态,所以我们每次要统计的,就是这种三个人的状态持续的时间。因此,用到的是time_since_last_event这个变量,该变量用于统计两种状态(事件,包括离开和到来)之间的事件。

3、因此,在每次计算完time_since_last_event之后,考察队伍中的人数是否为2,若为2,则把该段time_since_last_event加到Three_people_time中。仿真结束时,用Three_people_time/总仿真时间,即为店内有三个顾客的概率。 店内至少有一个顾客的概率; p3=p3=1-idle_time/sim_time 增加变量idle_time,即店里空闲的概率(没有顾客),用1减去一个顾客都没有的概率,就是至少有一个顾客的概率。 判断店里空闲的方法是,如果当前有一个顾客来了,他不需要排队,就意味着这之前店里是空闲着的。这里仍用到time_since

4、_last_event,在这个客人来之前的这段时间店里空闲,因此把这个时间段加给idle_time,以此累加,最后得到总的空闲时间。 在店内顾客的平均数; mean_customer=1.0*num_custs_delayed/sim_time 在这里我计算的是店里平均每分钟的顾客数(包括等待的和正在理发的)。num_cust_delayed是在这段仿真时间里总共出现的顾客,除以仿真时间,就是店内顾客平均数。 顾客在店内的平均逗留时间; mean_stay_time=(area_num_in_q+area_server_status)/(1.0*num_custs_delayed) 对于任意一

5、位顾客,他在店里逗留的时间排队时间理发时间。area_num_in_q统计的是队列中的顾客等待的总时间,而area_server_status统计的是所有顾客理发的总时间,相加就是所有顾客在店里逗留的时间,除以顾客的人数,就是顾客平均逗留时间。 顾客必须在店内消耗15分钟以上的概率。 p6=1.0*count15/num_custs_delayed 增加变量count15,用于统计在店内消耗15分钟以上的顾客人数,再增加变量single_cust_time,用于统计单个顾客在店内消耗的时间。 这里有两种情况。 (1)不用排队。那么该顾客在店里消耗的时间就是理发时间,即single_cust_t

6、ime=time_next_event2-sim_time。time_next_event2=sim_time+expon(mean_service)。 (2)要排队。那么single_cust_time=delay+time_next_event2-sim_time。delay是当前有一位顾客离开时,得到的下一位将要理发的顾客(即队首顾客)的等待时间。每一位顾客到店里时都会有一个time_arrival将其抵达时刻记录下来,因此,轮到他时,用sim_time减去time_arrival就得到了他等待的时间。等待时间加上理发时间,就是这位顾客在店里消耗的时间。 以上可计算出单个顾客在店里消耗的

7、时间,与15分钟比较,若超过,加入count15中,count15除以总人数,就是所求概率。对原程序的修改点: 去掉了文件操作,用提示输入的方式输入参数,可多次输入。增加变量count15,Three_people_time,idle_time,single_cust_time。作用见上面六小题具体算法。代码:#include #include #include /*#include lcgrand.h Header file for random-number generator. */#define Q_LIMIT 100 /* Limit on queue length. */#defi

8、ne BUSY 1 /* Mnemonics for servers being busy */#define IDLE 0 /* and idle. */int next_event_type, num_custs_delayed, num_delays_required, num_events,num_in_q, server_status,count15;float area_num_in_q, area_server_status, mean_interarrival, mean_service,sim_time, time_arrivalQ_LIMIT + 1, time_last_

9、event, time_next_event3,total_of_delays,Three_people_time,idle_time,single_cust_time,time_since_last_event;/* The following 3 declarations are for use of the random-number generator lcgrand and the associated functions lcgrandst and lcgrandgt for seed management. This file (named lcgrand.h) should b

10、e included in any program using these functions by executing #include lcgrand.h before referencing the functions. */float lcgrand(int stream);void lcgrandst(long zset, int stream);long lcgrandgt(int stream);void initialize(void);void timing(void);void arrive(void);void depart(void);void report(void)

11、;void update_time_avg_stats(void);float expon(float mean);int main() /* Main function. */ /* Specify the number of events for the timing function. */ char choice; num_events = 2; while(1) printf(Start to simulate?(Y/N); getchar(); scanf(%c,&choice); if(choice=N) exit(0); printf(nnPlease enter the in

12、terarrival time(min):); scanf(%f,&mean_interarrival); printf(nPlease enter the serving time(min):); scanf(%f,&mean_service); printf(nPlease enter the number of customer:); scanf(%d,&num_delays_required); /* Initialize the simulation. */ initialize(); /* Run the simulation while more delays are still

13、 needed. */ while (num_custs_delayed num_delays_required) /* Determine the next event. */ timing(); /* Update time-average statistical accumulators. */ update_time_avg_stats(); /* Invoke the appropriate event function. */ switch (next_event_type) case 1: arrive(); break; case 2: depart(); break; /*

14、Invoke the report generator and end the simulation. */ report(); return 0;void initialize(void) /* Initialization function. */ /* Initialize the simulation clock. */ sim_time = 0.0; /* Initialize the state variables. */ server_status = IDLE; num_in_q = 0; time_last_event = 0.0; /* Initialize the sta

15、tistical counters. */ num_custs_delayed = 0; total_of_delays = 0.0; area_num_in_q = 0.0; area_server_status = 0.0;/ Initialize the variables I addedThree_people_time = 0.0;idle_time = 0.0;count15 = 0; /* Initialize event list. Since no customers are present, the departure (service completion) event

16、is eliminated from consideration. */ time_next_event1 = sim_time + expon(mean_interarrival); time_next_event2 = 1.0e+30;void timing(void) /* Timing function. */ int i; float min_time_next_event = 1.0e+29; next_event_type = 0; /* Determine the event type of the next event to occur. */ for (i = 1; i =

17、 num_events; +i) if (time_next_eventi Q_LIMIT) /* The queue has overflowed, so stop the simulation. */ printf(nOverflow of the array time_arrival at); printf( time %f, sim_time); exit(2); /* There is still room in the queue, so store the time of arrival of the arriving customer at the (new) end of t

18、ime_arrival. */ time_arrivalnum_in_q = sim_time; /record the time when this person arrived else /* Server is idle, so arriving customer has a delay of zero. (The following two statements are for program clarity and do not affect the results of the simulation.) */ delay = 0.0; /no need to wait total_

19、of_delays += delay;idle_time+=time_since_last_event; /* Increment the number of customers delayed, and make server busy. */ +num_custs_delayed; server_status = BUSY; /* Schedule a departure (service completion). */ time_next_event2 = sim_time + expon(mean_service); /compute the serve time of this pe

20、rson(to know when he will leave)single_cust_time = time_next_event2 - sim_time;if(single_cust_time=15)count15+; void depart(void) /* Departure event function. */ int i; float delay; /* Check to see whether the queue is empty. */ if (num_in_q = 0) /* The queue is empty so make the server idle and eli

21、minate the departure (service completion) event from consideration. */ server_status = IDLE; time_next_event2 = 1.0e+30; else /* The queue is nonempty, so decrement the number of customers in queue. */ -num_in_q; /* Compute the delay of the customer who is beginning service and update the total dela

22、y accumulator. */ delay = sim_time - time_arrival1; /how long does this person wait in the queue total_of_delays += delay; /the total waiting-time of people in the queue /* Increment the number of customers delayed, and schedule departure. */ +num_custs_delayed; time_next_event2 = sim_time + expon(m

23、ean_service); /compute the serve time of this personsingle_cust_time = delay + time_next_event2 - sim_time; if(single_cust_time=15)count15+; /* Move each customer in queue (if any) up one place. */ for (i = 1; i 16) * MULT1 + (lowprd 16); zi = (lowprd & 65535) - MODLUS) + (hi31 & 32767) 15); if (zi 0) zi += MODLUS;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1