# Modeling With Agents: Multiline Equations [Systems thinking & modelling series]

This is part 69 of a series of articles featuring the book Beyond Connecting the Dots, Modeling for Meaningful Results.

So far the equations we have looked at have generally been straightforward mathematical formulae. We have introduced some more advanced concepts, such as vectors, but for the most part our equations have been relatively simple one-liners. When doing Agent Based Modeling however, at some point you will find these one-line equations to be limiting. When you begin to run into these limitations with your own models, you may need to start using multiline equations to achieve certain agent behavior.

Almost everyplace in Insight Maker where you can write a mathematical expression, you can also write a multiline equation. It turns out that Insight Maker’s language for specifying equations is actually a complete computer programming language. You can exploit the strength of this programming language by writing your equations over several lines instead of using a single line mathematical formula.

We delayed introducing these capabilities until now, as they can sometimes be a distraction from focusing on understanding a system. However, when you build complex Agent Based Models, these capabilities can be necessary to express the model logic you wish. Given this need, we will provide a brief introduction to the programming features that can be used as part of Insight Maker equations. You do not need to delve deeply into these capabilities now, but be aware that they are available when you need them in your own models.

## Variables

Variables are temporary slots to store values to be reused within your equations. Variables are created using the ‘<-’ symbol meaning assignment. For instance:

a <- 2 # The variable ‘a’ holds the value 2
b <- a + 2 # The variable ‘b’ holds the value 4
a <- b^2 # a=16, b=4

Variable names can contain any number of letters and numbers and must always start with a letter.

## If-Then-Else

You should be familiar with the IfThenElse() function. A multiline alternative to it exists. The following is equivalent to IfThenElse([Height] > 2, 1, 2).

If [Height] > 10 Then
1
Else
2
End If

One of the benefits of these multiline equations is that they can be more legible than the single line functions. This is especially true if you are trying to do nested if statements. Compare IfThenElse([Height] > 2, 1, IfThenElse([Height] < 1, -1, 2)) to:

If [Height] > 2 Then
1
Else If [Height] < 1 Then
-1
Else
2
End If

The second one is much more legible. This makes it easier to maintain and more resilient to potential typographical errors.

## Loops

Loops are a programming construct that repeat some code multiple times. There are several different types of loops. One important loop is the for loop which repeats a command a specified number of times. Here is an example of it being used:

sum <- 0
For i From 1 To 3
sum <- sum + i
End Loop
sum

The inner part of the loop is run three times here. The first time the variable i is assigned the value of 1, the next time 2, and the last time 3. So this sums the values of 1, 2, and 3, resulting in a value of 6.

Another variant of the for loop is the for-in loop. This uses a vector to assign the values of the iterations. The following code sums the numbers 1, 5, and 10 to get a value of 16.

sum <- 0
For i In {1, 5, 10}
sum <- sum + i
End Loop
sum

For-in loops can be very useful to iterate through a vector of agents. Another useful loop is called the while loop. It does not repeat a predefined number of times; it repeats until a condition becomes true. Here is an example:

total <- 2
While total < 100
total <- total^2
End Loop
total

This code keeps squaring the total variable until the total is greater than 100. In this case, the result is 256.

## Functions

Functions allow you to reuse code in multiple places in your model. For instance, imagine you had a model that dealt with temperatures in both degrees Fahrenheit and Celsius. If you could not use the built-in unit conversion functionality, every time you wanted to convert from one form to the other you would have to include the standard conversion formula in your equations. Not only would this be tedious, it would also be error prone, as the more times you type an equation, the higher the chance of making a mistake.

You can define functions in two ways. One is a short one-liner:

FtoC(f) <- 5/9*(f+32)

And another is a multiline form allowing you to incorporate multiline logic in your functions:

Function FtoC(f)
5/9*(f+32)
End Function

A great place to include your functions is in the Macros section of your model. You can enter macros by clicking the button in the section of the toolbar. The functions you define here will be accessible in any equation in any part of your model.

Exercise 10-12
Write a function to return the range of a vector. The range is the largest element of the vector minus the smallest element.

Exercise 10-13
Write a function to calculate the nth Fibonacci number. The Fibonacci sequence goes 1, 1, 2, 3, 5, 8, 13, … After the first two, each number is the sum of the two proceeding numbers in the sequence.

What is the 15th Fibonacci number?

Next edition: Modeling With Agents: Integrating System Dynamics modeling and Agent Based Modeling.

Article sources: Beyond Connecting the Dots, Insight Maker. Reproduced by permission.

Rate this post

### Scott Fortmann-Roe and Gene Bellinger

Scott Fortmann-Roe, creator of Insight Maker, and Gene Bellinger, creator of SystemsWiki, have written the innovative interactive book "Beyond Connecting the Dots" to demystify systems thinking and modelling.

### Related Articles

This site uses Akismet to reduce spam. Learn how your comment data is processed.