Beispiel #1
0
/*
 * TIME NEXT EVENT -- Exponential
 * Given an intensity "totalIntensity" it gives the time deltaT of the next exponential event.
 */
double timeNextEvent(double totalIntensity)
{
    //The time for the next event to occur is
    double deltaT = exponentialGenerator(totalIntensity);
    
    if(deltaT < 0)
    {
        printf("\nNEGATIVE TIME \n");
        printf("deltaT = %f \n", deltaT);
        printf("totalIntensity = %f \n", totalIntensity);
        exit(0);
    }
    
    if(deltaT != deltaT)
    {
        printf("\nYour are dividing with respect to 0! \n");
        printf("deltaT = %f \n", deltaT);
        printf("totalIntensity = %f \n", totalIntensity);
        exit(0);
    }
    return deltaT;
}
Beispiel #2
0
/*
* TEST EXPONENTIAL GENERATOR FUNCTION
*/
void exponentialGeneratorTest(double lambda){
   	printf("\nExponential Generator Test.\n");
    FILE *exponentialTest;
    
    int maxTest = 10000;
    double test[maxTest];
	// INIT OUTPUT
    exponentialTest = fopen("exponentialTest.dat", "w+");
	fclose(exponentialTest);
	// END INIT OUTPUT
	double mean=0;
    
	// Seed initialization
	srand((unsigned int) time(NULL));
	for (int i=0; i<maxTest; i++)
    {
		test[i] = exponentialGenerator(lambda);
		mean += test[i];
		exponentialTest = fopen("exponentialTest.dat", "a+");
		fprintf(exponentialTest, "%f\n", test[i]);
		fclose(exponentialTest);
    }
	mean = mean/ maxTest;
	printf("The mean is %f\n", mean);
	double variance=0;
	for (int i = 0; i<maxTest; i++)
	{
		variance+=pow(test[i]-mean,2);
	}
	variance = variance/ maxTest;
	printf("The variance is %f\n", variance);

    printf("###Exponential -- Theoretical Values###\n");
    double varianceTheo = 0, meanTheo = 0;
    meanTheo = 1 / lambda;
    varianceTheo = 1 / (pow(lambda,2.));
    printf("meanTheo = %f \tvarianceTheo = %f \n", meanTheo, varianceTheo);

}
/// Tests for Exponential variate generator.
TEST_F(ExponentialTrafficGeneratorTest, ExponentialVariateGeneratorTest) {
	std::shared_ptr<Message> source(new Message("This is a dummy entity for source."));
	std::shared_ptr<Message> destination(new Message("This is a dummy entity for destination."));
	std::shared_ptr<Message> tokenContents(new Message("This is a dummy Token contents."));
	unsigned int seed = 1;
	double tau = 1.0;
	std::shared_ptr<Token> token(nullptr);

	// Set the fixed seed.
	simulatorGlobals.seedRandomNumberGenerator(seed);
	ExponentialTrafficGenerator exponentialGenerator(simulatorGlobals, scheduler, EventType::TRAFFIC_GENERATOR_ARRIVAL, tokenContents, source, destination, 1, tau);
	// Keep generator off, attempt to generate one instance of traffic. Member token is null at this point.
	EXPECT_EQ(token, exponentialGenerator.createInstanceTrafficEvent());
	// Now turn generator On, generate one instance of traffic. Test outcomes.
	exponentialGenerator.turnOn();
	token = exponentialGenerator.createInstanceTrafficEvent();
	EXPECT_EQ(1, token->id);
	EXPECT_EQ(1, token->priority);
	EXPECT_EQ(tokenContents, token->associatedEntity);
	EXPECT_EQ(source, token->source);
	EXPECT_EQ(destination, token->destination);
	// Get the event that was scheduled.
	Event event = scheduler.cause();
	// Check the event time and see whether it is always the same value per the seed.
	EXPECT_DOUBLE_EQ(0.539605826511863, event.occurAfterTime);
	
	// Create another exponential generator and see whether it begins with a different random sequence.
	// It should; however, these tests prove that all exponential generators follow *one* unique pseudorandom sequence, determined by
	// simulatorGlobals random engine and its seed. That is the desired behavior.
	ExponentialTrafficGenerator exponentialGeneratorSecond(simulatorGlobals, scheduler, EventType::TRAFFIC_GENERATOR_ARRIVAL, tokenContents, source, destination, 1, tau);
	exponentialGeneratorSecond.turnOn();
	token = exponentialGeneratorSecond.createInstanceTrafficEvent();
	EXPECT_EQ(2, token->id);
	EXPECT_EQ(1, token->priority);
	EXPECT_EQ(tokenContents, token->associatedEntity);
	EXPECT_EQ(source, token->source);
	EXPECT_EQ(destination, token->destination);
	// Get the event that was scheduled.
	event = scheduler.cause();
	// Check the event time and see whether it always the same value per the seed, but different than another instance of the expo. generator.
	EXPECT_DOUBLE_EQ(5.8727248609813936, event.occurAfterTime); // 5.82 for a tau of 1, isn't it too large?

	// Get two more values, and then change the seed to another value and test two generations. The new values after seeding should be different than the next two here.
	token = exponentialGenerator.createInstanceTrafficEvent();
	EXPECT_EQ(3, token->id);
	// Get the event that was scheduled.
	event = scheduler.cause();
	// Check the event time and see whether it always the same value per the seed, but different than another instance of the expo. generator.
	EXPECT_DOUBLE_EQ(1.2741252381599264, event.occurAfterTime);
	token = exponentialGenerator.createInstanceTrafficEvent();
	EXPECT_EQ(4, token->id);
	// Get the event that was scheduled.
	event = scheduler.cause();
	// Check the event time and see whether it always the same value per the seed, but different than another instance of the expo. generator.
	EXPECT_DOUBLE_EQ(2.6964778379468424, event.occurAfterTime);
	// Change global seed, generate two tokens, compare with values generated previously, in another test, for seed = 2.
	simulatorGlobals.seedRandomNumberGenerator(2);
	token = exponentialGenerator.createInstanceTrafficEvent();
	EXPECT_EQ(5, token->id);
	// Get the event that was scheduled.
	event = scheduler.cause();
	// Check the event time and see whether it always the same value per the seed, but different than another instance of the expo. generator.
	EXPECT_DOUBLE_EQ(0.57269198962718915, event.occurAfterTime);
	token = exponentialGenerator.createInstanceTrafficEvent();
	EXPECT_EQ(6, token->id);
	// Get the event that was scheduled.
	event = scheduler.cause();
	// Check the event time and see whether it always the same value per the seed, but different than another instance of the expo. generator.
	EXPECT_DOUBLE_EQ(0.20466788427640734, event.occurAfterTime);
	// Generate one more, just to get the third value on the sequence of seed 2.
	token = exponentialGenerator.createInstanceTrafficEvent();
	EXPECT_EQ(7, token->id);
	// Get the event that was scheduled.
	event = scheduler.cause();
	// Check the event time and see whether it always the same value per the seed, but different than another instance of the expo. generator.
	EXPECT_DOUBLE_EQ(0.026268236846611673, event.occurAfterTime);
	
	// Seed again, generate two tokens, compare with previous values. Does reseeding "reset" the same sequence?
	simulatorGlobals.seedRandomNumberGenerator(2);
	token = exponentialGenerator.createInstanceTrafficEvent();
	EXPECT_EQ(8, token->id);
	// Get the event that was scheduled.
	event = scheduler.cause();
	// Event time should be the same as first prior event after seeding.
	EXPECT_DOUBLE_EQ(0.57269198962718915, event.occurAfterTime);
	token = exponentialGenerator.createInstanceTrafficEvent();
	EXPECT_EQ(9, token->id);
	// Get the event that was scheduled.
	event = scheduler.cause();
	// Event time should be the same as second prior event after seeding.
	EXPECT_DOUBLE_EQ(0.20466788427640734, event.occurAfterTime);

	// Now let's reseed with 2, generate one token with one generator, another with second generator, another with first generator.
	// The first value from the first generator should match the previous value just after seeding, but the subsequent values should be different, if the random number generator is really globally unique.
	simulatorGlobals.seedRandomNumberGenerator(2);
	token = exponentialGenerator.createInstanceTrafficEvent();
	EXPECT_EQ(10, token->id);
	// Get the event that was scheduled.
	event = scheduler.cause();
	// Event time should be the same as first prior event after seeding.
	EXPECT_DOUBLE_EQ(0.57269198962718915, event.occurAfterTime);
	token = exponentialGeneratorSecond.createInstanceTrafficEvent();
	EXPECT_EQ(11, token->id);
	// Get the event that was scheduled.
	event = scheduler.cause();
	// Event time *should* be the same as second prior event after seeding.
	EXPECT_DOUBLE_EQ(0.20466788427640734, event.occurAfterTime);
	token = exponentialGenerator.createInstanceTrafficEvent();
	EXPECT_EQ(12, token->id);
	// Get the event that was scheduled.
	event = scheduler.cause();
	// Event time *should* be the same as third event from seed 2. That proves that the same pseudorandom sequence is unique per simulation.
	EXPECT_DOUBLE_EQ(0.026268236846611673, event.occurAfterTime);

	// Now test generator's token counters.
	EXPECT_EQ(10, exponentialGenerator.getTokensGeneratedCount());
	EXPECT_EQ(2, exponentialGeneratorSecond.getTokensGeneratedCount());

}