?? gen.h
字號:
#ifndef GEN_H
#define GEN_H
#include <stdlib.h>
#include <math.h>
#include "link.h"
#include "event.h"
#include "stcs.h"
#include "para.h"
#define UN_SEED_1 10203
#define UN_SEED_2 12345
class Gen
{
public:
Gen(double lda);
~Gen();
double getTim();
int genEvt(); //create an event and sendMes
void sendMes();
int shortQue();
int strQue(int nsev);
double rnd();
double rnd1();
double rnd2();
list<Mess>* sch;
list<Event>* que[MAX_SEV];
Stcs** sts;
#ifdef _DEBUG_
int cc[MAX_SEV];
#endif
private:
double Tim;
double lambda;
double r_seed;
double r_seed_1;
double r_seed_2;
};
Gen::Gen(double lda)
{
lambda=lda;
Tim=0;
sch=NULL;
sts=NULL;
for(int i=0;i<MAX_SEV;i++)
que[i]=NULL;
srand((unsigned)SEV_SEED);
r_seed=(double)GEN_SEED;
r_seed_1=(double)UN_SEED_1;
r_seed_2=(double)UN_SEED_2;
#ifdef _DEBUG_
for(int v=0;v<MAX_SEV;v++)
cc[v]=0;
#endif
}
Gen::~Gen()
{
sch=NULL;
for(int i=0;i<MAX_SEV;i++)
que[i]=NULL;
}
double Gen::getTim(){return Tim;}
int Gen::shortQue()
{
assert(que);
int tem;
int tid;
int NUM_SEV;
//check if the normal server is more than one
if((MAX_SEV-EXP_SEV)<0)
{
cout<<"error..."<<endl;
}
//decide if arriving event is express
int strq=-1;
while(1)
{
if(rnd1()>EXP_P)
{
NUM_SEV=MAX_SEV-EXP_SEV;
strq=strQue(NUM_SEV);
}
else
{
NUM_SEV=MAX_SEV;
strq=(MAX_SEV-EXP_SEV)+strQue(EXP_SEV);
}
if(NUM_SEV!=0)
break;
}
//int strq=strQue(NUM_SEV);
if(strq==-1)
exit(0);
#ifdef _DEBUG_
// cc[strq]++;
#endif
int id=strq;
int min=que[id]->getLen();
for(int i=0;i<NUM_SEV;i++)
{
tid=(strq+i)%NUM_SEV;
tem=que[tid]->getLen();
//tem=que[i]->getLen();
if(tem<min)
{
min=tem;
//id=i;
id=tid;
}
}
#ifdef _DEBUG_
cc[id]++;
#endif
return id;
}
int Gen::genEvt()
{
Event* evt=new Event();
int str=shortQue();
evt->setStrStp(Tim);
////////////////////////////////////
que[str]->addTail(evt);
////////////////////////////////////
sendMes();
////////////////////////////////////
return str;
}
void Gen::sendMes()
{
double p;
double delt;
while(1)
{
p=rnd();
if(p!=0)
break;
}
delt=-log(p)/lambda;
Tim=Tim+delt;
Mess* ms=new Mess(GEN,Tim);
assert(sch);
sch->addPri(ms);
}
int Gen::strQue(int nsev)
{
double p;
while(1)
{
p=rnd2();
if(p!=1)
return (int)floor(nsev*p);
}
}
double Gen::rnd()
{
r_seed=fmod(A*r_seed,M);
return (r_seed*4.656612875e-10);
}
double Gen::rnd1()
{
r_seed_1=fmod(A*r_seed_1,M);
return (r_seed_1*4.656612875e-10);
}
double Gen::rnd2()
{
r_seed_2=fmod(A*r_seed_2,M);
return (r_seed_2*4.656612875e-10);
}
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -