Search code examples
artificial-intelligenceanylogic

On Car source components, I can't define 2 initial speed values respectively to vehicles being in or not in emergency state


In AnyLogic I'm making a college project for my course's AI subject. It involves creating a population of an agent that I called "Veiculo" (it's in Portuguese, in English it means "Vehicle").

Inside the agent "Veiculo", there's 1 parameter, 1 variable, 1 oval shape and 1 function relevant for my question, which are, respectively, "velocidade" (means "speed"), "modoEmergencia" (means "emergencyMode"), luzEmergencia (means "emergencyLight") and velocidadeEmergency (means "emergencySpeed"), and they have the following details:

The components inside the agent "Veiculo"

Parameter "velocidade"

Type: double
Default value: 50

Parameter "velocity"

Variable "modoEmergencia"

Type: boolean
Initial value: uniform(0, 1) < 0.10;

Variable "modoEmergencia"

Oval shape "luzEmergencia"

Visible: modoEmergencia

Oval shape "luzEmergencia". It's visibility is defined by the function "modoEmergencia"

Function "velocidadeEmergencia"

-Returns nothing

Function body:

if (modoEmergencia) {
    velocidade = 100;
} else {
    velocidade = 50;
}

Function "velocidadeEmergencia"

The problem

In the Main agent, I have a highway network, and the veiculos (means vehicles) from the population of the agent "Veiculo" enter through 8 Car sources. The problem I have is that, when I try to define the "Initial speed" of a Car source to be:
veiculos.velocidade
Deatails of a Car source from the highway network

This error appears:

Description: velocidade cannot be resolved or is not a field.

Description of the error that appears

I also tried defining defining the "Initial speed" of a Car source to veiculos.modoEmergencia ? 100 : 50
or
Veiculo.modoEmergencia ? 100 : 50
or
agent.modoEmergencia ? 100 : 50
but it didn't work.

What I want in my project, is that the vechicles in emergency mode travel at 100 km/h and the ones not in emergency mode travel at 50 km/h on the highway network.


Solution

  • If your agents are initialized correctly, there is just one point you are missing. On the CarSource block, when you go into Initial Speed you will see a light bulb on the left side of the text control. This light bulb shows you any special variables available in that context.

    In this case, you have self (which points on the block itself) and car, which points to the instance of car being created. If you want to use this information, you could set the initial speed for example to car.velocidadeEmergencia() - which will call the function on each instance.

    Again, it is very important in this case that the agents (cars) are initialized correctly. In this case, it is correct since the modoEmergencia variable has an initial value.