Using varying lambda with interarrival time while receiving same arrivals across models - simulation

I have learned from my previous question “Get the same arrival rate across models” that for two models with the same arrival rate input, I can get the exact same arrivals generated in both models by setting the source block in both to “Interarrival time” and in the code, use exponential(lambda, 0, rand) where rand is a user-defined random number generator (RNG), for example, Random rand = new Random(1234). If prior to setting the source block to “Interarrival time” I had it set to “Rate Schedule” in which the schedule is of type rate that is provided from the database and in my case the rate (lambda) is not constant, it depends on the time of the day, how can I overcome this when setting the source block to “Interarrival time”?

Since it is not possible to set a seed for the arrival rate, you'll need to use interarrival times instead in your source.
However, since you also need to vary the interarrival time based on the time of day, you should write a function that returns different exponential random variables depending on the time of day and use that as your interarrival times.
Here is how I did that in AnyLogic:
And I made a quick plot of how many injections were occurring per hour just to verify that the rates are actually changing.

Perhaps I'm understanding the question wrong, but interarrival times can these not be transformed to arrival rate, so for example if interarrival time = 2minutes, the arrival rate is 0.5 per minute. If this is the case for your model, you can use in the source block arrivals defined by: a rate schedule (as shown in the figure below.)
After that in the schedule you can change the different arrival rates, so for example between 00:00 and 01:00 the arrival rate should be 1 as shown in the figure below:

Related

Anylogic - Substantial variances in identical arrival rate schedule outputs

I am currently completing some verification checks on an Anylogic DES simulation model, and I have two source blocks with identical hourly arrival rate schedules, broken down into 24 x 1h blocks.
The issue I am encountering is significant differences in the number of agents generated by one block compared with another. I understand that the arrival rate is based on the poisson distribution, so there is some level of randomness in the instants of agent generation, but I would expect that the overall number generated by these two blocks should be similar, if not identical. For example, in one operating scenario one block is generating 78 agents, whilst the other is only generating 67 over the 24h period. This seems to be a common issue across all operating scenarios.
Is there a potential explanation regarding idiosyncrasies within Anylogic that might explain this?
Any pointers would be welcomed.
I think it occurs because it follows a poisson distribution. To solve this, you could use the interarrival time function of the source block. In that case you would have the same number of arrivals for different source blocks. However, I'm not sure whether this fits a schedule. If not, you could use the getHourOfDay() function together with a parameter representing the interarrival time. You then have to write the code below for every hour of the day:
if(getHourOfDay()==14) parameter =5;
using sources with poisson distributions will definitely not produce same results... That's the magic of stocastic models.
An alternative to solve this problem is the following:
sources will generate using the inject function
use dynamic events that will be in charge to do source.inject();
let's imagine you have R trains coming per day, and this is a fixed value you want to use, you can then distribute the trains accross the day by doing this:
for(int i=0;i<R;i++){
create_DynamicEvent1(uniform(0,1),DAY); //for source1
create_DynamicEvent2(uniform(0,1),DAY); //for source2
}
This doesn't follow a poisson distribution, but generates a predefined number of arrivals of trains throughout the day, and you can use another distribution of your choice if the uniform is not good enough for you.
run this for every day

Get the same arrival rate across models

I have a model that I have duplicated and made some adjustments to it. When I run both models with the same fixed seed I don’t get the same results, which I understand because I have other sources of randomness in the model. Regardless, in both models, I am using a source block, such that the arrivals are defined by a rate schedule, the schedule is of type rate, and is provided from the database. Now, I know the following pieces of information:
Generally, I can use my own random number generator (RNG) in distributions, for example triangular(5, 10, 25, myRNG), such that Random myRNG = new Random (2)
By default, a schedule with “type” rate follows a passion distribution that utilizes the default RNG.
At anytime in the model, I can substitute the default RNG with my own by calling setDefaultRandomGenerator(Random r).
The question is: Is it possible to use a fixed seed for the arrival rate to make sure I am getting the same exact input in both models?
In anylogic, rates are always equivalent to a poisson distribution with lambda equal to the rate you set,
Intearrival times don't follow any distribution, but using exponential(lambda) in the field is equivalent to using a arrival by rate with a rate of lambda.
But poisson and exponential are closely related, which is why if you use poisson(1.0/lambda) in the intearrival time, you have the same average arrivals as if you use exponential(lambda).
It is not possible to set a seed for the arrival rate, and that's why you need to use intearrival times instead in your source
But you need to create a variable first, let's call it rand, of type Random with initial value new Random(seed)
where seed is any integer you want (long to be more exact)
then in the intearrival time you need to do:
exponential(lambda, 0,rand)
This will lead to unique simulation runs, no matter what configuration you have in the AnyLogic experiment
Sure, simply set both Source blocks to "Interarrival time" in the "arrivals defined by".
Then, use the same code poisson(1, myRNG) in the field, making sure that the myRNG RNG uses the same initial seed (i.e.new Random(1234)
(the "Rate" setting is the same as using poission(1) for interarrival-time)

Source is producing too many agents

My arrival rate for my source is in hours, and I am using events to set it's rate to a distribution at different times, sourceShoppers.set_rate(triangular(1, 5, 2));. However, the source is producing roughly 3 per second, as opposed to an average of 2 an hour.
do this:
self.set_rate(triangular(1,5,2), PER_HOUR);
Nevertheless, when you do that, notice that you will get a random sample for the triangular distribution, which will set the rate to be for instance 1.2 per hour in which case the arrivals will follow a poisson distribution with an average of 1.2 per hour always unless you change the rate again...
If you want some advice, you have to say what you want to achieve...

Pedestrian arrival rate of 5 per h only 3 showing for 1 h during the simulation. Any reason why?

I'm trying to simulate a pedestrian flow in the entrance of an hospital.
We are installing check-in platforms and I want to know how many platforms we should get according to the patient flow.
I'm using Anylogic personal learning edition and when I put an arrival rate of 5 per hour during the simulation only 3 appears.
I'm trying to understand how anylogic works and distribute the pedestrians according to the rate we put.
For the personnal learning edition 1h equal 1min in real.
enter image description here
if you choose rate=5, the pedSource block will generate pedestrians with an exponentially distributed interarrival time with mean = 1/rate = 1/5.
Which means that the average of arrivals on the long term will be 5, but you won't get 5 every hour since it's a stochastic variable.
If you change the seed, you will have different arrivals... click on Simulation: Main and you can change the seed or use a random seed:
Now if you really want exactly 5 per hour in a deterministic way, you need to change the arrival from rate to inject function:
Then you can create an event that runs cyclically 5 times per hour.. or 1 time every 12 minutes:
and you do pedSource.inject(1);

Simulation: send packets according to exponential distribution

I am trying to build a network simulation (aloha like) where n nodes decide at any instant whether they have to send or not according to an exponential distribution (exponentially distributed arrival times).
What I have done so far is: I set a master clock in a for loop which ticks and any node will start sending at this instant (tick) only if a sample I draw from a uniform [0,1] for this instant is greater than 0.99999; i.e. at any time instant a node has 0.00001 probability of sending (very close to zero as the exponential distribution requires).
Can these arrival times be considered exponentially distributed at each node and if yes with what parameter?
What you're doing is called a time-step simulation, and can be terribly inefficient. Each tick in your master clock for loop represents a delta-t increment in time, and in each tick you have a laundry list of "did this happen?" possible updates. The larger the time ticks are, the lower the resolution of your model will be. Small time ticks will give better resolution, but really bog down the execution.
To answer your direct questions, you're actually generating a geometric distribution. That will provide a discrete time approximation to the exponential distribution. The expected value of a geometric (in terms of number of ticks) is 1/p, while the expected value of an exponential with rate lambda is 1/lambda, so effectively p corresponds to the exponential's rate per whatever unit of time a tick corresponds to. For instance, with your stated value p = 0.00001, if a tick is a millisecond then you're approximating an exponential with a rate of 1 occurrence per 100 seconds, or a mean of 100 seconds between occurrences.
You'd probably do much better to adopt a discrete-event modeling viewpoint. If the time between network sends follows the exponential distribution, once a send event occurs you can schedule when the next one will occur. You maintain a priority queue of pending events, and after handling the logic of the current event you poll the priority queue to see what happens next. Pull the event notice off the queue, update the simulation clock to the time of that event, and dispatch control to a method/function corresponding to the state update logic of that event. Since nothing happens between events, you can skip over large swatches of time. That makes the discrete-event paradigm much more efficient than the time step approach unless the model state needs updating in pretty much every time step. If you want more information about how to implement such models, check out this tutorial paper.