AnyLogic, custom resource choice among resource sets

1.6k Views Asked by At

within an AnyLogic project, in the 'seize' block I need to make a custom choice of resources from resource sets. Having in the properties tab of the 'seize' block the field "Resource sets" with the value { {ResourcePool_A, ResourcePool_B} } and the flag "customize resource choice" checked. In the "resource choice condition" code section, I need to make a choice like:

    if (unit isfrom ResourcePool_A)
    {
        if (unit.param_a == value) 
            do something
            ....
    }
    else if (unit isfrom ResourcePool_B)
    {
        if (unit.param_b == value) 
            do something
            ....
    }

How can I check if a resource unit is from a given pool or not and then discriminate resources accordingly with their features? Thank you. Best regards.

2

There are 2 best solutions below

1
On BEST ANSWER

I solved the issue writing a an Anylogic function that returns a boolean and I used it in the resource choice condition. I implemented "isfrom" to discriminate from which pools the resource is picked up as shown in following code:

`

// cast pool object to the prorper type
ResourcePool t_pool = (ResourcePool)pool;

// resource selection condition
if ( (t_pool == ResourcePool_A && ((Resource_A)unit).param == agent.param_bar) ||
     (t_pool == ResourcePool_B && ((Resource_B)unit).param == agent.param_bar) ) {
    return true;
}
else {
    return false;
}

`

In the Anylogic documentation is not explained that in the resource choice condition of the seize block you have access also to the pool object (this is bad...).

3
On

from your question it seems that you don't need to choose a specific resource, but rather do a specific set of actions on the resource once it has been seized. which is why i've added two answers.

1.
If you just want to do a specific set of action. You should just copy your code to the "On seize unit" action in the seize object.

2.
If you want to select a specific resource. the easiest way to do that is to create an Anylogic function resource_selector()that returns a boolean.

if(unit isfrom ResourcePool_A && unit.param_foo == agent.param_bar)
    ...
    your own code 
    ...
    return true;
else
    return false;

and then in the Resource choice condition write:

resource_selector(unit, agent);