Search code examples
anylogicagents

AnyLogic how to link one agent to another


Im a relative noob regarding anylogic but i got a task to do in my homework so here comes my question:

I created a population of agents who are all patients... these patients get ill with a probabilty of lets say 30%... i alrdy implemented this one but now my task is to add a medicine using a new agent for this problem to heal them... but how do I link this new agent with the already existing one? My first agent gets triggered by a message.. therefore we have to use an event sending this message to a first person who gets infected etc..

Can anyone help me how I can create a new agent and link it to the old one to heal the people?

Many thanks in advance!


Solution

  • ok so since this is what you need I will post it as an answer

    medicine has a state chart called SC with an initial state which is used and a second state notUsed that you get through a message transition from one to the other.

    when the patient gets to the state sick, it will need to find an agent Medicine which is in the state notUsed to be able to get healed, so you find it with the following code:

    Medicine med=findFirst(main.medicines,m->m.inState(m.notUsed));
    if(med!=null){//meaning that there is at least one not used medicine
        med.SC.fireEvent("use medicine");
        send("get better",this);
    }
    

    You will have to do the same probably when a new medicine is created you use the same method to find a guy in the state "sick".. I assume you are calling this function in the patient agent, but it may differ depending on when you are calling it.