离散事件模型
0x01 代码框架逻辑
模拟内容:
1.离散事件模拟,模拟银行营业时的排队情况
2.不考虑顾客中途离开,顾客到达事件随机,业务办理时间
3.长度随机,选择最短的队排队,不再换队
代码逻辑:
1.一个事件链表,四个窗口排队队列
2.事件驱动:每有一个新的顾客到达,将产生下一个新顾客到达的新事件按时间顺序从小到大(OccurTime)插入事件链表(EventList) (如果此时窗口队列只有 一个顾客,还将产生此顾客离开事件插入事件链表中)
每有一个顾客从某一队列首离开,将产生他的后一位顾客离开的新事件按时间顺序从小到大(OccurTime)插入事件链表(EventList)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | #include <stdio.h> #include <time.h> #include <stdlib.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 typedef int Status; typedef struct Event { //事件类型 int OccurTime; //事件发生时刻 int NType; //事件类型,0表示到达事件,1至4表示四个窗口的离开事件 struct Event *next; }Event, ElemType; typedef struct { //单向链表结构 ElemType *head; //头指针 ElemType *tail; //尾指针 int len; //长度 }LinkList; typedef LinkList EventList; //事件链表 typedef struct QElemType { //队列元素 int ArriveTime; //到达时间 int Duration; //办理业务所需时间 struct QElemType *next; }QElemType; typedef struct { //队列结构 QElemType *head; //头指针 QElemType *tail; //尾指针 }LinkQueue; Event NewEvent( int occurT, int nType); //根据OccurTime和NType值,创建新事件 Status InitList(LinkList *L); //初始化事件链表 Status OrderInsert(LinkList *L, Event e); //将事件e按发生时间顺序插入有序链表L中 Status ListEmpty(LinkList *L); //判断链表L是否为空,为空返回TRUE,否则返回FALSE Status DelFirst(LinkList *L, ElemType *e); //链表L不为空,删除其首结点,用e返回,并返回OK;否则返回ERROR Status ListTraverse(LinkList *L); //遍历链表 Status InitQueue(LinkQueue *Q); //初始化队列Q Status EmptyQueue(LinkQueue *Q); //若队列Q为空,返回TRUE,否则返回FALSE Status DelQueue(LinkQueue *Q, QElemType *e); //若队列Q不为空,首结点出队,用e返回,并返回OK;否则返回ERROR Status EnQueue(LinkQueue *Q, QElemType e); //结点e入队Q int QueueLength(LinkQueue Q); //返回队列Q的长度,即元素个数 Status GetHead(LinkQueue *Q, QElemType *e); //若队列Q不为空,用e返回其首结点,并返回OK,否则返回ERROR Status QueueTraverse(LinkQueue *Q); //遍历队列Q //------------------// int Min( int a[], int n); //返回长度为n的数组a第一个最小值的下标,从1开始 int ShortestQueue(); //获取最短队列的编号 void OpenForDay(); //初始化操作 void CustomerArrived(); //顾客达到事件 void CustomerDepature(); //顾客离开事件 void Bank_Simulation(); //银行排队模拟 void PrintEventList(); //输出事件队列 void PrintQueue(); //打印当前队列 //----全局变量-----// EventList ev; Event en; LinkQueue q[5]; QElemType customer; int TotalTime, CustomerNum; int CloseTime = 50; //关闭时间,即营业时间长度 //--------------main()------------------// int main() { Bank_Simulation(); return 0; } //--------------模拟排队----------------// void OpenForDay() { //初始化操作 int i; TotalTime = 0; CustomerNum = 0; InitList(&ev); //初始化事件链表 en.OccurTime = 0; en.NType = 0; OrderInsert(&ev, en); for (i = 1; i <= 4; i++) InitQueue(&q[i]); //初始化四个窗口队列 } //OpenForDay void CustomerArrived() { //顾客达到事件 int durtime, intertime, i, t; QElemType e; ++CustomerNum; intertime = rand () % 5 + 1; //间隔时间在5分钟内 durtime = rand () % 30 + 1; //办理业务时间在30分钟内 t = en.OccurTime + intertime; if (t<CloseTime) { //银行尚未关门 printf ( "A new customer will arrive at:%d\n" , en.OccurTime); //下一位顾客达到时间 OrderInsert(&ev, NewEvent(t, 0)); i = ShortestQueue(); //最短队列 e.ArriveTime = en.OccurTime; e.Duration = durtime; EnQueue(&q[i], e); if (QueueLength(q[i]) == 1) OrderInsert(&ev, NewEvent(en.OccurTime + durtime, i)); } } void CustomerDepature() { //顾客离开事件 int i = en.NType; DelQueue(&q[i], &customer); printf ( "A customer leaves at:%d\n" , en.OccurTime); //输出顾客离开时间 TotalTime += en.OccurTime - customer.ArriveTime; if (!EmptyQueue(&q[i])) { GetHead(&q[i], &customer); OrderInsert(&ev, NewEvent(en.OccurTime + customer.Duration, i)); } } void Bank_Simulation() { //银行排队模拟 OpenForDay(); srand ((unsigned) time (NULL)); while (!ListEmpty(&ev)) { DelFirst(&ev, &en); //事件驱动 if (en.NType == 0) CustomerArrived(); else CustomerDepature(); //PrintEventList(); PrintQueue(); } printf ( "\nTotal time is: %d min,average time is: %g min.\n" , TotalTime, ( float )TotalTime / CustomerNum); } void PrintQueue() { //打印当前队列 int i; for (i = 1; i <= 4; i++) { printf ( "Queue %d have %d customer(s):" , i, QueueLength(q[i])); QueueTraverse(&q[i]); } printf ( "\n" ); } void PrintEventList() { //输出事件队列 printf ( "Current Eventlist is:\n" ); ListTraverse(&ev); } int Min( int a[], int n) { //返回长度为n的数组a第一个最小值的下标,从0开始 int i, tmp, ind = 0; tmp = a[0]; for (i = 1; i<n; i++) { if (a[i]<tmp) { tmp = a[i]; ind = i; } } return ind; } int ShortestQueue() { //获取最短队列的编号 int i, a[4]; for (i = 1; i <= 4; i++) { a[i - 1] = QueueLength(q[i]); //printf("队%d的长度为%d\n",i,QueueLength(q[i])); } return Min(a, 4) + 1; //队列从1开始编号 } //-----------队和链表操作--------------// Event NewEvent( int occurT, int nType) { //根据OccurTime和NType值,创建新事件 Event e; e.OccurTime = occurT; e.NType = nType; return e; } Status InitList(LinkList *L) { //初始化事件链表 L->head = L->tail = (ElemType *) malloc ( sizeof (ElemType)); if (!L->head) { printf ( "Apply for memory error.LinkList initialize failed.\n" ); exit (0); } L->head->next = NULL; return OK; } Status OrderInsert(LinkList *L, Event e) { //将事件e按发生时间顺序插入有序链表L中 ElemType *p, *q, *newptr; newptr = (ElemType *) malloc ( sizeof (ElemType)); if (!newptr) { printf ( "Apply for memory error,new node can't insert intot the Eventlist.\n" ); exit (0); } *newptr = e; if (TRUE == ListEmpty(L)) { //链表为空 L->head->next = newptr; L->tail = newptr; L->tail->next = NULL; return OK; } q = L->head; p = L->head->next; while (p) { //遍历整个链表 if (p->OccurTime >= newptr->OccurTime) break ; q = p; p = p->next; } q->next = newptr; newptr->next = p; if (!p) //插入位置为链表尾部 L->tail = newptr; return OK; } Status ListEmpty(LinkList *L) { //判断链表L是否为空,为空返回TRUE,否则返回FALSE if ((L->head == L->tail) && (L->head != NULL)) return TRUE; else return FALSE; } Status DelFirst(LinkList *L, ElemType *e) { //链表L不为空,删除其首结点,用e返回,并返回OK;否则返回ERROR ElemType *p = L->head->next; if (!p) return ERROR; L->head->next = p->next; *e = *p; free (p); if (L->head->next == NULL) L->tail = L->head; return OK; } Status ListTraverse(LinkList *L) { //遍历链表 Event *p = L->head->next; if (!p) { printf ( "List is empty.\n" ); return ERROR; } while (p != NULL) { printf ( "OccurTime:%d,Event Type:%d\n" , p->OccurTime, p->NType); p = p->next; } printf ( "\n" ); return OK; } Status InitQueue(LinkQueue *Q) { //初始化队列Q Q->head = Q->tail = (QElemType *) malloc ( sizeof (QElemType)); if (!Q->head) { printf ( "Apply for memory error.LinkQueue initialize failed.\n" ); exit (0); } Q->head->next = NULL; return OK; } Status EmptyQueue(LinkQueue *Q) { //若队列Q为空,返回TRUE,否则返回FALSE if (Q->head == Q->tail&&Q->head != NULL) return TRUE; else return FALSE; } Status DelQueue(LinkQueue *Q, QElemType *e) { //若队列Q不为空,首结点出队,用e返回,并返回OK;否则返回ERROR QElemType *p = Q->head->next; if (!p) return ERROR; *e = *p; Q->head->next = p->next; //修正队首指针 free (p); if (!Q->head->next) //队空 Q->tail = Q->head; return OK; } Status EnQueue(LinkQueue *Q, QElemType e) { //结点e入队Q QElemType *p = (QElemType *) malloc ( sizeof (QElemType)); if (!p) { printf ( "Apply for memory error,new element can't enqueue.\n" ); exit (0); } *p = e; p->next = NULL; Q->tail->next = p; //插入队尾 Q->tail = p; //修改队尾指针 return OK; } int QueueLength(LinkQueue Q) { //返回队列Q的长度,即元素个数 int count = 0; QElemType *p = Q.head->next; while (p) { p = p->next; count++; } return count; } Status GetHead(LinkQueue *Q, QElemType *e) { //若队列Q不为空,用e返回其首结点,并返回OK,否则返回ERROR if (EmptyQueue(Q)) return ERROR; *e = *(Q->head->next); return OK; } Status QueueTraverse(LinkQueue *Q) { //遍历队列Q QElemType *p = Q->head->next; if (!p) { printf ( "--Is empty.\n" ); return ERROR; } while (p) { printf ( "(%d,%d) " , p->ArriveTime, p->Duration); p = p->next; } printf ( "\n" ); return OK; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗