djordon / queueing-tool

Simulator for queueing networks written in Python
http://queueing-tool.readthedocs.org/
MIT License
64 stars 9 forks source link

Starting simulation with already agents in the servers #87

Open arsiboo opened 1 year ago

arsiboo commented 1 year ago

Hi

Is there a possibility when starting the simulation there will be already some amount of agents in each server?

djordon commented 1 year ago

I believe the answer is "yes" but it is manual. You would create the network, then manually add agents to the queues you want to add agents to (with QueueServer.set_active and then QueueServer.simulate), and then initialize the network with QueueNetwork.initialize.

arsiboo commented 1 year ago

So what I understood is that I have to activate, intialize, and simulate each queue separately, right? Wouldn't in this case simulation begin from everywhere and generate agents on each queue?

djordon commented 1 year ago

So what I understood is that I have to activate, intialize, and simulate each queue separately, right?

Yeah, you need to activate and simulate each queue separately to get the queues in the initial state that you want them.

Wouldn't in this case simulation begin from everywhere and generate agents on each

So in this case, we are basically setting the state of the queues we are interested in independent of the network topology. After they are in the state you want them to be in, set them to inactive and then initialize the network (which may reactivate some queues). Then you could simulate the network normally, except that the queues would.


Actually, the more I think about it the more it seems like my proposal is a bad idea. Each agent keeps track of time independently of the network, other agents, and the queues. This means you'd have to manually create agents with a hand picked time and manually add it to the queues by calling the QueueServer._add_arrival function. So, queueing-tool doesn't really support this case (it doesn't make it easy). But you could get it to work with a bit of manual setup code.

arsiboo commented 1 year ago

Thank you for the help and information.

vcal-dedalus commented 1 year ago

I have the same problem as arsiboo. I need to have some agents in the queue, already waiting. I wrote some code that I think it could work, however, as I don't fully get your source code, I wonder if you see at first glance it won't work:

from heapq import heappush, heappop
from queueing_tool.queues.queue_servers import QueueServer

class InitializableQueueServer(QueueServer):
    def __init__(self,  initial_queue: int = 0, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.add_agents_at_same_time(num_agents=initial_queue)

    def add_agents_at_same_time(self, num_agents: int):
        if num_agents <= 0:
            return

        for agent_index in range(0, num_agents):
            self._num_total += 1
            new_agent = self.AgentFactory((self.edge[2], self._oArrivals))
            new_agent._time = self._next_ct
            heappush(self._arrivals, new_agent)
            self._oArrivals += 1

And in q_args, we would parametrize the QueueServer as follows.

q_args = {
    1: {
        'arrival_f': arrive_f,
        'service_f': service_f,
        'AgentFactory': Agent,
        'initial_queue': 10,
    },
...
}
djordon commented 1 year ago

@vcal-dedalus I think that would roughly work, but you should probably change the agent._time to be something other than self._next_ct (which I think is short for next_creation_time), since self._next_ct is zero there. Also, you should call self._update_time at the end of add_agents_at_same_time.

vcal-dedalus commented 1 year ago

In fact, I did it on purpouse. My approach was adding all the agents at the same point in time. For initializing the queue, I found more realistic that all the agents were in the queue at t=0 than assigning them a different t. Also, I thought that in real life it could happen that two or more people arrive to a queue at the same time.

Do you think it's theoretically incorrect or may involve a problem in the code to add agents at the same "t"?

djordon commented 1 year ago

There won't be any problems with any agent having the same initial _time as another agent, so you should be all good. And the initial agent service time distribution will be service_f(0), which I think is what you intended so I don't see any theoretical problems either.

vcal-dedalus commented 1 year ago

Great to know you don't foresee any problems! Thank you very much for your time and your explanations.

djordon commented 1 year ago

No problem at all, happy to help.