void run() { init(); while (events.Count != 0) { currentEvent = (H_Event)events[0]; if (currentEvent.EventType == -1) //事件类型为-1,处理救护车病人到达事件 { carPatientArrive(hospital); } else if (currentEvent.EventType == -2) //事件类型为-2,处理自行病人到达事件 { selfPatientArrive(hospital); } else { patientDeparture(hospital); //事件类型非-1或2,处理病人离开事件 } events.RemoveAt(0); } end(); avg_line_car = avg_line_car / car_line_time; avg_line_self = avg_line_self / self_line_time; avg_line_total = avg_line_total / total_line_time; patient_time_sum = total_stay_time / total_patients; }
void selfPatientArrive(int hospital) //自行病人到达函数 { total_patients++; //病人数目++ //-------------------------------生成下一名病人的到达事件---------------------------------------// double intertime = RandExp(hosInfo[hospital, 0]); //下一名自行病人到达的时间间隔 double time = currentEvent.occur_time + intertime; //下一名自行病人的到达时间 H_Event temp_event = new H_Event(time, -2); //生成下一名病人的到达事件 //判断实验终止条件,如果已经到达最大服务时间,则不将事件加入事件列表。 if (time < total_service_time) { addEvent(events, temp_event); } //------------------------------根据到达事件生成当前病人----------------------------------------// Patient inPatient = new Patient(currentEvent.occur_time); //将病人加入病人列表 SelfPatientQueue.Add(inPatient); //--------------------------------处理目前队列中的病人-----------------------------------------// int idleIndex = GetIdleBed(); //寻找空闲的床位 if (idleIndex >= 0) { if (returnQueueStatus() != QueueStatus.CarBusy) //只有当没有救护车病人的时候自行病人才可以进入 { Patient outPatient = (Patient)SelfPatientQueue[0]; time = currentEvent.occur_time + RandExp(hosInfo[hospital, 1]); SelfPatientQueue.RemoveAt(0); //将病人从队列中取出 beds[idleIndex].servePatient(outPatient); //将病人安排至床位 beds[idleIndex].setBusy(); //将床位状态设置为“繁忙” temp_event = new H_Event(time, idleIndex); //病人被安排至床位后,生成对应的离开事件 addEvent(events, temp_event); } } else { avg_line_self += (SelfPatientQueue.Count - 1) * (currentEvent.occur_time - self_line_time); self_line_time = currentEvent.occur_time; avg_line_total += (CarPatientQueue.Count - 1 + SelfPatientQueue.Count) * (currentEvent.occur_time - total_line_time); total_line_time = currentEvent.occur_time; if (SelfPatientQueue.Count > max_line_self) { max_line_self = SelfPatientQueue.Count; } if (CarPatientQueue.Count + SelfPatientQueue.Count > max_line_total) { max_line_total = CarPatientQueue.Count + SelfPatientQueue.Count; } } }
void carPatientArrive(int hospital) //救护车病人到达函数 { total_patients++; //病人数目++ //-------------------------------生成下一名病人的到达事件---------------------------------------// double intertime = RandExp((double)adjustLambda[hospital]); //下一名救护车病人到达的时间间隔 double time = currentEvent.occur_time + intertime; //下一名救护车病人的到达时间 H_Event temp_event = new H_Event(time, -1); //生成下一名病人的到达事件 //判断实验终止条件,如果已经到达最大服务时间,则不将事件加入事件列表。 if (time < total_service_time) { addEvent(events, temp_event); } //------------------------------根据到达事件生成当前病人----------------------------------------// Patient inPatient = new Patient(currentEvent.occur_time); //将病人加入排队 CarPatientQueue.Add(inPatient); //--------------------------------处理目前队列中的病人-----------------------------------------// int idleIndex = GetIdleBed(); //寻找空闲的床位 if (idleIndex >= 0) { //救护车病人有优先排队权,因此无需判定救护车队伍是否为空 Patient outPatient = (Patient)CarPatientQueue[0]; time = currentEvent.occur_time + RandExp(hosInfo[hospital, 1]); CarPatientQueue.RemoveAt(0); //将病人从队列中取出 beds[idleIndex].servePatient(outPatient); //将病人安排至床位 beds[idleIndex].setBusy(); //将床位状态设置为“繁忙” temp_event = new H_Event(time, idleIndex); //病人被安排至床位后,产生对应的离开事件 addEvent(events, temp_event); } else { avg_line_car += (CarPatientQueue.Count - 1) * (currentEvent.occur_time - car_line_time); car_line_time = currentEvent.occur_time; avg_line_total += (CarPatientQueue.Count - 1 + SelfPatientQueue.Count) * (currentEvent.occur_time - total_line_time); total_line_time = currentEvent.occur_time; if (CarPatientQueue.Count > max_line_car) { max_line_car = CarPatientQueue.Count; } if (CarPatientQueue.Count + SelfPatientQueue.Count > max_line_total) { max_line_total = CarPatientQueue.Count + SelfPatientQueue.Count; } } }
void addEvent(ArrayList list, H_Event @event) { if (@event.occur_time > ((H_Event)list[list.Count - 1]).occur_time) { list.Add(@event); } else { for (int i = 0; i != list.Count; i++) { if (((H_Event)list[i]).occur_time > @event.occur_time) { list.Insert(i, @event); break; } } } }
void patientDeparture(int hospital) //病人离开函数 { //判断实验终止条件,如果已经到达最大服务时间,则不进行处理 if (currentEvent.occur_time < total_service_time) { //计算病人总逗留时间 double stay_time = (currentEvent.occur_time - beds[currentEvent.EventType].getArriveTime()); total_stay_time += stay_time; if (stay_time > max_stay_time) { max_stay_time = stay_time; } //如果队列中还有病人,则立刻对下一位病人进行服务,并生成下一位病人的离开事件 //优先处理救护车病人 if (CarPatientQueue.Count > 0) { avg_line_car += CarPatientQueue.Count * (currentEvent.occur_time - car_line_time); car_line_time = currentEvent.occur_time; avg_line_total += (CarPatientQueue.Count + SelfPatientQueue.Count) * (currentEvent.occur_time - total_line_time); total_line_time = currentEvent.occur_time; if (CarPatientQueue.Count + SelfPatientQueue.Count > max_line_total) { max_line_total = CarPatientQueue.Count + SelfPatientQueue.Count; } if (CarPatientQueue.Count > max_line_car) { max_line_car = CarPatientQueue.Count; } Patient patient = (Patient)CarPatientQueue[0]; CarPatientQueue.RemoveAt(0); beds[currentEvent.EventType].servePatient(patient); //从队列中取出下一位病人并分配至当前床位 double InterTime = RandExp(hosInfo[hospital, 1]); //计算当前病人的治疗时间 double time = currentEvent.occur_time + InterTime; //计算下一位病人离开事件的发生时间 H_Event temp_event = new H_Event(time, currentEvent.EventType); addEvent(events, temp_event); } else if (SelfPatientQueue.Count > 0) { avg_line_self += SelfPatientQueue.Count * (currentEvent.occur_time - self_line_time); self_line_time = currentEvent.occur_time; avg_line_total += (CarPatientQueue.Count + SelfPatientQueue.Count) * (currentEvent.occur_time - total_line_time); total_line_time = currentEvent.occur_time; if (CarPatientQueue.Count + SelfPatientQueue.Count > max_line_total) { max_line_total = CarPatientQueue.Count + SelfPatientQueue.Count; } if (SelfPatientQueue.Count > max_line_self) { max_line_self = SelfPatientQueue.Count; } Patient patient = (Patient)SelfPatientQueue[0]; SelfPatientQueue.RemoveAt(0); beds[currentEvent.EventType].servePatient(patient); //从队列中取出下一位病人并分配至当前床位 double InterTime = RandExp(hosInfo[hospital, 1]); //计算当前病人的治疗时间 double time = currentEvent.occur_time + InterTime; //计算下一位病人离开事件的发生时间 H_Event temp_event = new H_Event(time, currentEvent.EventType); addEvent(events, temp_event); } else { beds[currentEvent.EventType].setIdle(); } } }