Is there a possibility to have an ifelse statement with two conditions (ticks mod and probability)?

Viewed 189

I'm fairly new to Netlogo but I want to build a model where an agent (a car driver) will leave his home at a certain hour with a certain probability. Let's say he leaves Monday morning at 1 am (if have linked the ticks to the time so one tick is one hour).

I tried to work with ifelse-statements combined with a second statement which has to be verified in order for the entire statement to become true. In the example below the car / agent should leave with a probability of 7.7% its house and drive to a patch called underway-patches. Since one week has 168 hours, I tried to link the hour via the mod ticks (hence, mod ticks = 1 is equal to 1 am on a Monday morning).

This alone works:

ifelse ticks mod 168 = 1 and random-float 100.0 < 7.7
  [ ask turtles [ move-to one-of underway-patches ] ]
  [ ask turtles [ move-to one-of home-patches] ]

This works fine. So I have always about 7 out of 100 turtles moving to the underway-patch.

But If I now add the second hour, so 2 am, the first function does not work anymore (there are no turtles moving at all at 1 am - only at 2 am). I expect about 7 out of 100 turtles to move at 1 am to the underway-patch and then I expect about 5 out of 100 turtles to move at 2 am to the underway-patch (and the other 7 of the first hour should go back to the home-patches).

This does not work anymore:

; Monday, 1 am
ifelse ticks mod 168 = 1 and random-float 100.0 < 7.7
  [ ask turtles [ move-to one-of underway-patches ] ]
  [ ask turtles [ move-to one-of home-patches] ]

; Monday, 2 am
ifelse ticks mod 168 = 2 and random-float 100.0 < 5.1
  [ ask turtles [ move-to one-of underway-patches] ]
  [ ask turtles [move-to one-of home-patches] ]

I appreciate every help! Thanks in advance.

1 Answers

First, congratulations on an extremely clear question despite your newness to the site.

The problem is not that you have multiple ifelse statements, that is fine. The issue is that your ifelse statement is applying a single test to all turtles. Just look at the first example with one statement:

ifelse ticks mod 168 = 1 and random-float 100.0 < 7.7
  [ ask turtles [ move-to one-of underway-patches ] ]
  [ ask turtles [ move-to one-of home-patches] ]

Imagine that it is tick number 1. The computer runs the random number generator and gets 2. Great, the condition is true so the first block gets run. That will have ALL turtles move to the underway-patches. Similarly, if the random number generator returns 10, then the condition is false and ALL turtles move to home-patches.

You probably want something more like (you don't have to do the brackets on multiple lines, I did it so you can see the logical blocks of the structure):

ifelse ticks mod 168 = 1
  [ ask turtles-on home-patches
    [ if random-float 100.0 < 7.7
      [ move-to one-of underway-patches
      ]
    ]
  ]
  [ ask turtles-on underway-patches [ move-to one-of home-patches] ]

Or if you want exactly the correct proportion of turtles to move:

ifelse ticks mod 168 = 1
  [ let num-to-move 0.077 * count turtles-on home-patches
    ask n-of num-to-move turtles-on home-patches
    [ move-to one-of underway-patches
    ]
  ]
  [ ask turtles-on underway-patches [ move-to one-of home-patches] ]

Just a general observation, if you are going to code this for every possible starting time, you are going to have a lot of code that is identical except for the tick and the proportion. You state that you are new to NetLogo, so I don't want to jump too quickly to more advanced concepts, but come back when you're a little further along with having thought through your model and we can probably help you create a procedure that reduces the need to duplicate code.

UPDATE: One approach to reusing the code

This isn't quite right because I'm not really clear what sort of movement you want, but here is a complete model where the proportions are stored in a list and ticks is used to identify the correct item in the list. That proportion is then passed to a piece of code that moves turtles.

globals
[ home-patches
  underway-patches
  proportions
]

to setup
  clear-all
  set proportions [0 0.077 0.05 0.15]
  set home-patches patches with [abs pxcor <= 3 and abs pycor <= 3]
  ask home-patches [ set pcolor white ]
  set underway-patches patches with [not member? self home-patches]
  ask underway-patches [ set pcolor yellow ]
  create-turtles 100
  [ set color red
  ]
  reset-ticks
end

to go
  move-turtles item (1 + ticks mod 3) proportions
  tick
end

to move-turtles [#prop]
  print #prop
  ask turtles
  [ ifelse member? patch-here home-patches
    [ if random-float 1 < #prop
      [ move-to one-of underway-patches
      ]
    ]
    [ move-to one-of home-patches
    ]
  ]
end
Related