1

New to AnyLogic and trying to create conditional, if-then equations within flow elements.

For example, I would like the flow of ‘police_graduates’ to be 10, on the condition that the stock of ‘Police’ is less than 7500; if the stock surpasses 7500, I would like the flow of ‘police_graduates’ to be 5.

I entered the following equation into the equation field of the flow properties:

if( Police<7500 )
  10
  else 
  5

See here

But the model does not run and I get the following error message:

Error during model creation:
ERROR during variable generation:
Couldn't parse expression for police_graduates - Flow: syntax error.
Please check expressions of this variable.
java.lang.RuntimeException: ERROR during variable generation:
Couldn't parse expression for police_graduates - Flow: syntax error.
Please check expressions of this variable.
    at model2.Main.<init>(Main.java:162)
    at model2.Simulation.createRoot(Simulation.java:158)
    at model2.Simulation.createRoot(Simulation.java:1)

Is there something wrong with the syntax in which I’ve written the equation?

Alex
  • 33
  • 4

2 Answers2

1

Use conditional operators, you can only write 1 line of code in those fields:

Police<7500 ? 10 : 5

Benjamin
  • 10,603
  • 3
  • 16
  • 28
  • sure, please upvote answers that were helpful, see https://stackoverflow.com/help/why-vote – Benjamin Jun 14 '23 at 09:58
  • 1
    Hi Benjamin - tried to upvote it but I'm also brand new to posting on stackoverflow, so I still lack the 'reputation' to be able to cast a vote – Alex Jun 15 '23 at 23:15
1

you have to use the following format:

Police<7500 ? 10 : 5

or you can use a function that returns a double

function(Police)

and the function would have the following

if(Police<7500) return 10.0;
else return 5.0;
Felipe
  • 8,311
  • 2
  • 15
  • 31