排队系统仿真 包含C_第1页
排队系统仿真 包含C_第2页
排队系统仿真 包含C_第3页
排队系统仿真 包含C_第4页
排队系统仿真 包含C_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、排队系统仿真实验1. 实验目的离散事件系统大量存在于现实生活中。离散事件系统往往是随机的,具有复杂的变化关系,难以用常规的微分方程、差分方程等模型来描述,计算机仿真技术是解决这类问题的有效手段。排队系统是一种非常重要的离散事件系统,也是最早研究的离散事件系统。本实验通过设计一种最简单的单服务台排队系统仿真程序,深入理解排队系统的建模与仿真方法,掌握排队系统仿真的基本步骤和程序设计技术,了解离散事件系统仿真的一般原理。2. 排队系统仿真程序功能模拟一个单服务台单队列排队系统的运行过程,完成一定数量活动实体的服务过程,输出排队系统的常用统计指标。仿真运行时间(可由活动实体数确定)、活动实体到达平均

2、时间间隔、平均服务时间作为参数在程序运行时输入。3. 关键技术(1)随机数发生器乘同余法递推公式:程序中采用:(2)随机变量的产生变换抽样法指数分布随机变量的产生:其中,为到达(或服务)速率,1/到达时间平均间隔(或平均服务时间),u为随机数。(3)排队、到达、服务模式排队规则:先到先服务(FIFO);到达模式:泊松到达,即相邻两个顾客到达的时间间隔服从指数分布;服务模式:服务台为活动实体提供服务的时间是随机的,服从指数分布。(4)常用统计性能指标计算平均延误时间:其中,Di为第i个活动实体在队列中耽误的时间。平均滞留时间:其中,Wi为第i个活动实体在系统中滞留的时间,Si为第i个活动实体接受

3、服务台服务的时间。平均队长:其中,Q(t)为t时刻系统中队列的长度。平均实体数:其中,L(t)为t时刻系统中的活动实体数,Q(t)为t时刻队列的长度,S(t)为t时刻接受服务台服务的活动实体数。4. 程序架构(1)基本模块void initialize(void); /*变量初始化*/void timing(void);/*时间调度*/void arrive(void);/*到达事件处理*/void depart(void);/*离开事件处理*/void report(void);/*统计量输出*/void update_time_avg_stats(void);/*统计量的更新*/float

4、 expon(float mean);/*指数分布随机变量的变换抽样法*/(2)程序流程(main()函数)(3)到达事件处理流程(arrive()函数)(4)离开事件处理流程(depart()函数)5. 程序设计(1)定义#include <stdio.h>#include <math.h>/*#include "lcgrand.h" Header file for random-number generator. */#define Q_LIMIT 100 /* Limit on queue length. */#define BUSY 1 /*

5、 Mnemonics for server's being busy */#define IDLE 0 /* and idle. */int next_event_type, num_custs_delayed, num_delays_required, num_events, num_in_q, server_status;float area_num_in_q, area_server_status, mean_interarrival, mean_service, sim_time, time_arrivalQ_LIMIT + 1, time_last_event, time_n

6、ext_event3, total_of_delays;FILE *infile, *outfile;float lcgrand(int stream);void initialize(void);void timing(void);void arrive(void);void depart(void);void report(void);void update_time_avg_stats(void);float expon(float mean);(2)主程序main() /* Main function. */ /* Open input and output files. */ inf

7、ile = fopen("mm1.in", "r"); outfile = fopen("mm1.out", "w"); /* Specify the number of events for the timing function. */ num_events = 2; /* Read input parameters. */ fscanf(infile, "%f %f %d", &mean_interarrival, &mean_service, &num_delay

8、s_required); /* Write report heading and input parameters. */ fprintf(outfile, "Single-server queueing systemnn"); fprintf(outfile, "Mean interarrival time%11.3f minutesnn", mean_interarrival); fprintf(outfile, "Mean service time%16.3f minutesnn", mean_service); fprintf

9、(outfile, "Number of customers%14dnn", num_delays_required); /* Initialize the simulation. */ initialize(); /* Run the simulation while more delays are still needed. */ while (num_custs_delayed < num_delays_required) /* Determine the next event. */ timing(); /* Update time-average stati

10、stical accumulators. */ update_time_avg_stats(); /* Invoke the appropriate event function. */ switch (next_event_type) case 1: arrive(); break; case 2: depart(); break; /* Invoke the report generator and end the simulation. */ report(); fclose(infile); fclose(outfile); return 0;(3)初始化函数void initiali

11、ze(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 statistical counters. */ num_custs_delayed = 0; total_of_delays = 0.0; area_num_in_q = 0.0

12、; area_server_status = 0.0; /* Initialize event list. Since no customers are present, the departure (service completion) event is eliminated from consideration. */ time_next_event1 = sim_time + expon(mean_interarrival); time_next_event2 = 1.0e+30;(4)时间调度函数void timing(void) /* Timing function. */ int

13、 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 <= num_events; +i) if (time_next_eventi < min_time_next_event) min_time_next_event = time_next_eventi; next_event_type = i; /* Check to see whether the event l

14、ist is empty. */ if (next_event_type = 0) /* The event list is empty, so stop the simulation. */ fprintf(outfile, "nEvent list empty at time %f", sim_time); exit(1); /* The event list is not empty, so advance the simulation clock. */ sim_time = min_time_next_event;(5)到达事件处理函数void arrive(vo

15、id) /* Arrival event function. */ float delay; /* Schedule next arrival. */ time_next_event1 = sim_time + expon(mean_interarrival); /* Check to see whether server is busy. */ if (server_status = BUSY) /* Server is busy, so increment number of customers in queue. */ +num_in_q; /* Check to see whether

16、 an overflow condition exists. */ if (num_in_q > Q_LIMIT) /* The queue has overflowed, so stop the simulation. */ fprintf(outfile, "nOverflow of the array time_arrival at"); fprintf(outfile, " time %f", sim_time); exit(2); /* There is still room in the queue, so store the time

17、 of arrival of the arriving customer at the (new) end of time_arrival. */ time_arrivalnum_in_q = sim_time; 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; total_o

18、f_delays += delay; /* 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); (6)离开事件处理函数void depart(void) /* Departure event function. */ int i;

19、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 eliminate the departure (service completion) event from consideration. */ server_status = IDLE; time_next_event2 = 1.0e+30; else /* The queue is nonempty, so decrement the

20、number of customers in queue. */ -num_in_q; /* Compute the delay of the customer who is beginning service and update the total delay accumulator. */ delay = sim_time - time_arrival1; total_of_delays += delay; /* Increment the number of customers delayed, and schedule departure. */ +num_custs_delayed

21、; time_next_event2 = sim_time + expon(mean_service); /* Move each customer in queue (if any) up one place. */ for (i = 1; i <= num_in_q; +i) time_arrivali = time_arrivali + 1; (7)输出函数void report(void) /* Report generator function. */ /* Compute and write estimates of desired measures of performan

22、ce. */ fprintf(outfile, "nnAverage delay in queue%11.3f minutesnn", total_of_delays / num_custs_delayed); fprintf(outfile, "Average number in queue%10.3fnn", area_num_in_q / sim_time); fprintf(outfile, "Server utilization%15.3fnn", area_server_status / sim_time); fprint

23、f(outfile, "Time simulation ended%12.3f minutes", sim_time);(8)统计量更新函数void update_time_avg_stats(void) /* Update area accumulators for time-average statistics. */ float time_since_last_event; /* Compute time since last event, and update last-event-time marker. */ time_since_last_event = si

24、m_time - time_last_event; time_last_event = sim_time; /* Update area under number-in-queue function. */ area_num_in_q += num_in_q * time_since_last_event; /* Update area under server-busy indicator function. */ area_server_status += server_status * time_since_last_event;(9)随机数及随机变量产生函数float expon(fl

25、oat mean) /* Exponential variate generation function. */ /* Return an exponential random variate with mean "mean". */ return -mean * log(lcgrand(1);#define MODLUS 2147483647#define MULT1 24112#define MULT2 26143/* Set the default seeds for all 100 streams. */static long zrng = 1, 197327291

26、2, 281629770, 20006270,1280689831,2096730329,1933576050, 913566091, 246780520,1363774876, 604901985,1511192140,1259851944, 824064364, 150493284, 242708531, 75253171,1964472944,1202299975, 233217322,1911216000, 726370533, 403498145, 993232223,1103205531, 762430696,1922803170,1385516923, 76271663, 413

27、682397, 726466604, 336157058,1432650381,1120463904, 595778810, 877722890,1046574445, 68911991,2088367019, 748545416, 622401386,2122378830, 640690903, 1774806513,2132545692,2079249579, 78130110, 852776735,1187867272, 1351423507,1645973084,1997049139, 922510944,2045512870, 898585771, 243649545,1004818771, 773686062, 403188473, 372279877,1901633463, 498067494,2087759558, 493157915, 597104727,1530940798,1814496276, 536444882,1663153658, 855503735, 67784357,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论