/* * * * * * * * * * * * * * * * * * * * * * * * * *\ * CS2310 Term Project * * Heuristic Time Scheduling Algorithm * * Yue Zhang * * zysxqn@cs.pitt.edu * \* * * * * * * * * * * * * * * * * * * * * * * * * */ /* cpp file:HTSA.cpp */ /* implementation of heuristic time scheduling algorithm */ /*head file*/ #include "HTSA.h" #include #include #include #include #include char * STOP='\0'; /*define*/ #define DAY_START_TIME 800 #define DAY_END_TIME 1800 #define SPEED 2 // KM/MINUTE /*global varient*/ ifstream info; int patient_no; PATIENT *patient; int nurse_no; NURSE *nurse; int max_no; //max number of patients a nurse can serve within 1 day int served_no=0; /*function*/ int TravelTime(LOCATION l1, LOCATION l2) { return (int)sqrt((l1.x-l2.x)*(l1.x-l2.x)+(l1.y-l2.y)*(l1.y-l2.y))/SPEED; } int Plus(int t1, int t2) { int hour; int min; int c; hour=t1/100; min=t1%100; c=(min+t2)/60; min=(min+t2)%60; hour+=c; return hour*100+min; } int CanServe(NURSE n, PATIENT p) { if(p.served)return 0; //check whether it's morning or afternoon if(n.current_timen.init_schedule.FindSlot(REST).start_time) { if(Plus(n.init_schedule.FindSlot(REST).end_time,TravelTime(n.current_location,p.location))>p.schedule.FindSlot(FREE).start_time)return 0; } //check whether can serve else { if(Plus(n.current_time,TravelTime(n.current_location,p.location))>p.schedule.FindSlot(FREE).start_time)return 0; } } else if(n.current_timep.schedule.FindSlot(FREE).start_time)return 0; } else //afternoon { if(Plus(n.current_time,TravelTime(n.current_location,p.location))>p.schedule.FindSlot(FREE).start_time)return 0; } return 1; } int H(NURSE n, PATIENT p) { return p.schedule.FindSlot(FREE).end_time; } void UpdateSchedule(NURSE &n, PATIENT &p) { //check travel time if(n.current_timen.init_schedule.FindSlot(REST).start_time) { //travel after lunch rest //fill rest slot n.final_schedule.slot[n.final_schedule.slot_no].FillSlot(REST,n.current_time,n.init_schedule.FindSlot(REST).end_time,n.current_location,n.current_location); n.final_schedule.slot_no++; //fill travel slot n.final_schedule.slot[n.final_schedule.slot_no].FillSlot(TRAVEL,n.init_schedule.FindSlot(REST).end_time,Plus(n.init_schedule.FindSlot(REST).end_time,TravelTime(n.current_location,p.location)),n.current_location,p.location); n.final_schedule.slot_no++; //fill rest slot n.final_schedule.slot[n.final_schedule.slot_no].FillSlot(REST,Plus(n.init_schedule.FindSlot(REST).end_time,TravelTime(n.current_location,p.location)),p.schedule.FindSlot(FREE).start_time,p.location,p.location); n.final_schedule.slot_no++; //fill serve slot n.final_schedule.slot[n.final_schedule.slot_no].FillSlot(SERVE,p.schedule.FindSlot(FREE).start_time,p.schedule.FindSlot(FREE).end_time,p.location,p.location); n.final_schedule.slot_no++; return; } } //travel, then rest, then serve //fill travel slot n.final_schedule.slot[n.final_schedule.slot_no].FillSlot(TRAVEL,n.current_time,Plus(n.current_time,TravelTime(n.current_location,p.location)),n.current_location,p.location); n.final_schedule.slot_no++; //fill rest slot n.final_schedule.slot[n.final_schedule.slot_no].FillSlot(REST,Plus(n.current_time,TravelTime(n.current_location,p.location)),p.schedule.FindSlot(FREE).start_time,p.location,p.location); n.final_schedule.slot_no++; //fill serve slot n.final_schedule.slot[n.final_schedule.slot_no].FillSlot(SERVE,p.schedule.FindSlot(FREE).start_time,p.schedule.FindSlot(FREE).end_time,p.location,p.location); n.final_schedule.slot_no++; return; } void Initiation(char * filename) //read information nessarary for the algorithm { //read information file info.open(filename); int i=0; int j=0; char line[100]; char temp[10]; //read patient number info.getline(line,100); while(line[i]==' ')i++; while(line[i]!='\0')temp[j++]=line[i++]; temp[j]='\0'; patient_no=strtol(temp,&STOP,10); patient=new PATIENT[patient_no]; //read each patient for(int k=0;k=0) { //select one patient patient[select].served=1; nurse[i].serve_no++; UpdateSchedule(nurse[i],patient[select]); nurse[i].current_time=minH; nurse[i].current_location.x=patient[select].location.x; nurse[i].current_location.y=patient[select].location.y; } else { //change another nurse break; } } nurse[i].final_schedule.slot[nurse[i].final_schedule.slot_no].FillSlot(REST,nurse[i].current_time,DAY_END_TIME,nurse[i].current_location,nurse[i].home); nurse[i].final_schedule.slot_no++; } } void Output(void) { for(int i=0;i