📄 eventsim.cpp
字号:
#include "stdafx.h"
#include "Network.h"
#include "EventSim.h"
#include "Routing.h"
/* global variables */
extern NODE_TYPE nodeT[4000];
extern NETWORK_TYPE net;
//run an event of a probability of successrate and see if it successful
//it is a binomial distirbution of course
bool RunEvent(double successrate)
{
if ((successrate<0)||(successrate>1))
assert(false);
double test = ((double) rand() /(double) RAND_MAX);
if (test <successrate)
return true;
else
return false;
}
//simulate a signal event
bool SignalEvent2(int member, int receiver)
{
double test;
test = linkquality(member, receiver)*linkquality(receiver,member);
if (RunEvent(test)==true)
return true;
else
return false;
}
bool SignalEvent(int member, int receiver)
{
double test;
test = linkquality(member, receiver)*linkquality(receiver,member)*linkquality(member,receiver);
if (RunEvent(test)==true)
return true;
else
return false;
}
double SignalOnceCost(int member, int receiver)
{
double test = 0;
if (RunEvent(linkquality(member,receiver))==true)
test+=net.LAMBDA;
else
{test+=net.LAMBDA; return test;}
if (RunEvent(linkquality(receiver, member))==true)
test+=net.LAMBDA;
else
{test+=net.LAMBDA; return test;}
test+=1;
return test;
}
double SignalOnceCostForOptimized(int member, int receiver)
{
double test = 0;
if (RunEvent(linkquality(member,receiver))==true)
test+=net.LAMBDA;
else
{test+=net.LAMBDA; return test;}
{test+=net.LAMBDA; return test;}
}
void CostCliqueSendNative(int current, int nexthop, int numofmember, int* members, double *cost, double *sendtime)
{
int currentindex;
double accmulatetime = 0;
double accmulatecost = 0;
double oldaccmulatetime;
//Check if the node R itself has received the packet earlier
while (1)
{
oldaccmulatetime = accmulatetime;
if (RunEvent(linkquality(current,nexthop))==true)
{
*cost = 1+accmulatecost;
*sendtime = 1+accmulatetime;
return;
}
//if the node R has not received the packet, check if
//some members of R has received the packet. The cost and time should be incremented
//for time 1 is 1ms for cost is one data packet
accmulatetime+=1;
accmulatecost+=1;
//check all neighbors
for (currentindex = 0; currentindex < numofmember; currentindex ++)
{
if (RunEvent(linkquality(current,members[currentindex]))==true)
{
if (SignalEvent(members[currentindex],nexthop)==true)
{
accmulatecost+=net.LAMBDA*2+1;
accmulatetime+=net.LAMBDA*2+1;
*cost = accmulatecost;
*sendtime = accmulatetime;
return;
}
else
{
accmulatecost+=SignalOnceCost(members[currentindex],nexthop);
accmulatetime+= net.LAMBDA*2;
}
}
else{
accmulatetime+=net.LAMBDA*2;
}
}
if (oldaccmulatetime+SEND_TIMEOUT>accmulatetime)
accmulatetime = oldaccmulatetime+SEND_TIMEOUT;
}
return;
}
void CostCliqueSendOptimized(int current, int nexthop, int numofmember, int *members, int numofbetter, int *betternodes, double *cost, double *sendtime)
{
int currentindex;
double accmulatetime = 0;
double accmulatecost = 0;
double oldaccmulatetime;
//Check if the node R itself has received the packet earlier
while (1)
{
oldaccmulatetime = accmulatetime;
accmulatecost+=1;
accmulatetime+=1;
for (currentindex = 0; currentindex < numofbetter; currentindex ++)
{
if (RunEvent(linkquality(current,betternodes[currentindex]))==true)
{
if (SignalEvent2(betternodes[currentindex],nexthop)==true)
{
accmulatecost+=net.LAMBDA*2;
accmulatetime+=net.LAMBDA*2;
*cost = accmulatecost;
*sendtime = accmulatetime;
nodeT[current].optimizednexthop = betternodes[currentindex];
return;
}
else
{
accmulatecost+=SignalOnceCostForOptimized(betternodes[currentindex],nexthop);
accmulatetime+=net.LAMBDA*2;
}
}
else
{
accmulatetime+=net.LAMBDA*2;
}
}
if (RunEvent(linkquality(current,nexthop))==true)
{
*cost = accmulatecost;
*sendtime = accmulatetime;
return;
}
//if the node R has not received the packet, check if
//some members of R has received the packet. The cost and time should be incremented
//for time 1 is 1ms for cost is one data packet
//check all neighbors
for (currentindex = 0; currentindex < numofmember; currentindex ++)
{
if (RunEvent(linkquality(current,members[currentindex]))==true)
{
if (SignalEvent(members[currentindex],nexthop)==true)
{
accmulatecost+=net.LAMBDA*2+1;
accmulatetime+=net.LAMBDA*2+1;
*cost = accmulatecost;
*sendtime = accmulatetime;
return;
}
else
{
accmulatecost+=SignalOnceCost(members[currentindex],nexthop);
accmulatetime+= net.LAMBDA*2;
}
}
else
accmulatetime+=net.LAMBDA *2;
}
if (oldaccmulatetime+SEND_TIMEOUT>accmulatetime)
accmulatetime = oldaccmulatetime+SEND_TIMEOUT;
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -