#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include"FEL.h"
#include"Event.h"
#include <fstream>
using namespace std;
#define MAX 60
int TotalSimTime;
int CustomerIndex;
int SimTime;
int Stop=INT_MAX;
int PVQueue1;
int PVQueue2;
int PVQueue3;
double CustomerArriveMean;
double ServiceMean;
FEL Fel;
FEL FelRecord;
///stastic variable
int sys_num;//系统人数
double mean_sys_num;//系统平均人数
int sys_queue;//系统排队人数
double mean_sys_queue;//系统平均排队人数
int LastSimTime;//上次事件点时间
int sts_CustomerIndex;//统计时使用,用户ID
int level_1_wait;
int level_2_wait;
int level_3_wait;
int sys_stay;
int waitnum_1;
int waitnum_2;
int waitnum_3;
int level_1_use_num;
int level_2_use_num;
int level_3_use_num;
int Exponential(int mean);
void Initial(void);
void Simulate(void);
void Arrive(Event * PtrEvent);
void Finish(Event * PtrEvent);
void Report(void);
void Record();
int main(void)
{
cout<<"输出重定向到record.txt,就在当前程序目录:"<<endl;
ofstream outfile("record.txt");
streambuf *streams=cout.rdbuf();
cout.rdbuf(outfile.rdbuf());
Initial();
Simulate();
Report();
//Fel.DelAll();
cout.rdbuf(streams);
return 1;
}//end of main
int Exponential(int mean)
{
int result=1-(int)(mean*log(1.0*rand()/RAND_MAX));
return result<MAX?result:MAX;
}
/////////////////////////////****************************************///////////////////////
void Initial()
{
PVQueue1=-3;
PVQueue2=-2;
PVQueue3=-1;
TotalSimTime=INT_MAX;//default
CustomerArriveMean=8;//default
ServiceMean=8;//default
SimTime=0;
Stop=5000;
//initial stastic variable
sys_num=0;
sys_queue=0;
LastSimTime=0;
level_1_wait=0;
level_2_wait=0;
level_3_wait=0;
sys_stay=0;
waitnum_1=0;
waitnum_2=0;
waitnum_3=0;
level_1_use_num=0;
level_2_use_num=0;
level_3_use_num=0;
CustomerIndex=1;
srand( (unsigned)time( NULL ) );
Event el;
el.CustomerIndex=CustomerIndex;
// el.Time=(int)(-CustomerArriveMean*log((1.0*rand()/RAND_MAX)))+1;
el.Time=Exponential(CustomerArriveMean);
el.Type=EVENT_ARRIVE_1;
el.NextEventPtr=NULL;
el.IsExecutable=EXECUTABLE;
//el.QueueLength=PVQueue1;
Fel.Add(Fel.FEL_Tail,el);//add first event in to FEL
FelRecord.Add(FelRecord.FEL_Tail,el);
}//end of function of Initial()
/////////////////////////////****************************************///////////////////////
/////////////////////////////****************************************///////////////////////
void Simulate()
{
cout<<"仿真开始:"<<endl;
while(SimTime<TotalSimTime)
{
Event * PtrTemp;
PtrTemp=Fel.FindNextExecutableEvent();
if(NULL==PtrTemp)
{
/*cout<<"error: no executable FEL"<<endl;
cout<<endl;
cout<<"***************************************"<<endl;
cout<<"stack in Fel for "<<endl;
Event * pEvent;
while(pEvent=Fel.FindNextEvent())
{
if(pEvent->Time<TotalSimTime)
{
cout<<"customer="<<pEvent->CustomerIndex<<",at time="<<pEvent->Time<<" "<<pEvent->name<<endl;
}
Fel.Del(pEvent);
}
cout<<"***************************************"<<endl;
cout<<endl;*/
break;
}
else//find event in FEL
{
SimTime=PtrTemp->Time;
switch((PtrTemp->Type))
{
case EVENT_ARRIVE_1:
case EVENT_ARRIVE_2:
case EVENT_ARRIVE_3:
{
Arrive(PtrTemp);
break;
}
case EVENT_LEVEL_1_FINISH:
case EVENT_LEVEL_2_FINISH:
case EVENT_LEVEL_3_FINISH:
{
Finish(PtrTemp);
break;
}
}//end of switch
//FelRecord.Add(FelRecord.FEL_Tail,*PtrTemp);
Fel.Del(PtrTemp);
Record();
}//end of else
}//end of while(SimTime<TotalSimTime)
cout<<"仿真结束:"<<endl;
}//end of function of Simulate()
////////////////**********//////////////////
void Arrive(Event * PtrEvent)
{
int * pPVQueue;
switch(PtrEvent->Type)
{
case EVENT_ARRIVE_1:pPVQueue=&PVQueue1;
break;
case EVENT_ARRIVE_2:pPVQueue=&PVQueue2;
break;
case EVENT_ARRIVE_3:pPVQueue=&PVQueue3;
break;
}//end of switch
(*pPVQueue)++;
if((*pPVQueue)>0)// can not server now
{
Event Wait;
Wait.CustomerIndex=PtrEvent->CustomerIndex;
Wait.IsExecutable=UNEXECUTABLE;
Wait.NextEventPtr=NULL;
Wait.Time=PtrEvent->Time;
Wait.QueueLength=* pPVQueue;
switch(PtrEvent->Type)
{
case EVENT_ARRIVE_1:Wait.Type=EVENT_WAIT_1;
break;
case EVENT_ARRIVE_2:Wait.Type=EVENT_WAIT_2;
break;
case EVENT_ARRIVE_3:Wait.Type=EVENT_WAIT_3;
break;
}//end of switch
Fel.Add(Fel.FEL_Tail,Wait);
//add record
FelRecord.Add(FelRecord.FEL_Tail,Wait);
}//end of if
else
{
Event Finish;
Finish.CustomerIndex=PtrEvent->CustomerIndex;
Finish.NextEventPtr=NULL;
Finish.IsExecutable=EXECUTABLE;
//Finish.Time=(int)PtrEvent->Time-ServiceMean*log((1.0*rand()/RAND_MAX))+1;
Finish.Time=PtrEvent->Time+Exponential(ServiceMean);
switch(PtrEvent->Type)
{
case EVENT_ARRIVE_1:Finish.Type=EVENT_LEVEL_1_FINISH;
break;
case EVENT_ARRIVE_2:Finish.Type=EVENT_LEVEL_2_FINISH;
break;
case EVENT_ARRIVE_3:Finish.Type=EVENT_LEVEL_3_FINISH;
break;
}//end of switch
Fel.Add(Fel.FEL_Tail,Finish);
//add record
FelRecord.Add(FelRecord.FEL_Tail,Finish);
}//end of if(*pQueueLen>0)
if(PtrEvent->Type==EVENT_ARRIVE_1&&SimTime<=Stop)//add an EVENT_ARRIVE_1 event into FEL
{
Event ArriveEl;
//ArriveEl.Time=(int)PtrEvent->Time-CustomerArriveMean*log((1.0*rand()/RAND_MAX))+1;
ArriveEl.Time=PtrEvent->Time+Exponential(CustomerArriveMean);
ArriveEl.CustomerIndex=++CustomerIndex;;
ArriveEl.Type=EVENT_ARRIVE_1;
ArriveEl.NextEventPtr=NULL;
ArriveEl.IsExecutable=EXECUTABLE;
Fel.Add(Fel.FEL_Tail,ArriveEl);
//add record
FelRecord.Add(FelRecord.FEL_Tail,ArriveEl);
}
}//end of Arrive()
////////////////**********//////////////////
void Finish(Event * PtrEvent)
{
int * pPVQueue;
switch(PtrEvent->Type)
{
case EVENT_LEVEL_1_FINISH:pPVQueue=&PVQueue1;
break;
case EVENT_LEVEL_2_FINISH:pPVQueue=&PVQueue2;
break;
case EVENT_LEVEL_3_FINISH:pPVQueue=&PVQueue3;
break;
}//end of switch
(*pPVQueue)--;
if(PtrEvent->Type==EVENT_LEVEL_1_FINISH||PtrEvent->Type==EVENT_LEVEL_2_FINISH)//add arrive event into FEL
{
Event Arrive;
Arrive.CustomerIndex=PtrEvent->CustomerIndex;
Arrive.IsExecutable=EXECUTABLE;
Arrive.NextEventPtr=NULL;
Arrive.Time=PtrEvent->Time;
switch(PtrEvent->Type)
{
case EVENT_LEVEL_1_FINISH:Arrive.Type=EVENT_ARRIVE_2;
break;
case EVENT_LEVEL_2_FINISH:Arrive.Type=EVENT_ARRIVE_3;
break;
}//end of switch
Fel.Add(Fel.FEL_Tail,Arrive);
//add record
FelRecord.Add(FelRecord.FEL_Tail,Arrive);
}
if((*pPVQueue)>=0)
{
int WaitType;
switch(PtrEvent->Type)
{
case EVENT_LEVEL_1_FINISH:WaitType=EVENT_WAIT_1;
break;
case EVENT_LEVEL_2_FINISH:WaitType=EVENT_WAIT_2;
break;
case EVENT_LEVEL_3_FINISH:WaitType=EVENT_WAIT_3;
break;
}//end of switch
Event * pNextEvent=Fel.FindNextCertainType(WaitType);
if(pNextEvent)
{
Event Finish;
Finish.CustomerIndex=pNextEvent->CustomerIndex;
Finish.IsExecutable=EXECUTABLE;
Finish.NextEventPtr=NULL;
//Finish.Time=(int)PtrEvent->Time-ServiceMean*log((1.0*rand()/RAND_MAX))+1;
Finish.Time=PtrEvent->Time+Exponential(ServiceMean);
Finish.EndOfWaitTime=SimTime;
Finish.Type=PtrEvent->Type;
Fel.Add(Fel.FEL_Tail,Finish);
//add record
FelRecord.Add(FelRecord.FEL_Tail,Finish);
}
else
{
//error("error in indNextCertainType");
fprintf( stderr, "error in indNextCertainType");
}
Fel.Del(pNextEvent);
}//end of if(*pPVQueue>0)
}//end of Finish()
/////////////////////////////****************************************//
- 1
- 2
前往页