My question involves setting a global counter variable.
Sample Code:
globals [counter]
to go
;if counter / 6 is a whole number (as in is divisible by 6)
ask turtles
[
forward 1
]
set counter counter + 1
;otherwise
ask turtles
[
right 60
forward 1
]
end
Assume I've set up turtles and patches accordingly already. The idea is how to get my counter variable to test for true/false on being divisible by a number.
do you want the counter to increment each timestep? If so, use ticks instead as suggested by bergant instead of counter. Use counter if you are going to introduce some code later that means the counter only increments on some timesteps. I have also taken out your comment markers so the movement happens.
If using counter:
globals [counter]
to go
ask turtles
[ ifelse counter mod 6 = 0
[ forward 1 ]
[ right 60
forward 1 ]
]
set counter counter + 1
...
tick
end
If using ticks:
to go
ask turtles
[ ifelse ticks mod 6 = 0
[ forward 1 ]
[ right 60
forward 1 ]
]
tick
end
You can use ticks (reports the current value of the tick counter).
Use reset-ticks to set the counter to zero (this also updates all plots) - usually in your setup procedure.
Call tick to increment the ticks (go procedure)
And use mod operator for modulo.
Related
Trying to let a flood appear with every tick and make it disappear after every tick as well. Meanwhile, the tick counter should go on.
The flood appears this way:
to water_rise
ask patches [ ; saturates cell
if is-DEM < 800[
set cell-storage cell-storage + fill-rate
]
]
ask patches [
if any? neighbors4 with [ any? turtles-here ] [
set cell-storage cell-storage + fill-rate
]
]
ask patches [
if cell-storage > 0 [
if cell-storage > 5 [
set cell-storage 5
if not any? turtles-here [
sprout 1 [
set color blue
set size 10
set shape "circle"
]
]
]
set pcolor cell-storage + 82
]
]
end
Currently trying to figure out how to let the flood disappear after/or within this the tick, so that it can reoccur in the next one. I´m not aiming to reset the tick counter, it should reach 200.
Tried resetting the ticks, but only to manage resetting everything.
Any ideas ?
Thank you very much in adavance
Cheers
You can simply use the display primitive to update the view without waiting for the tick counter to advance.
I am not familiar with your whole model but here's a conceptual example that may help:
to water_rise
...
end
to go
repeat 100 [
water-rise
display
]
tick
end
This code would execute your water-rise procedure 100 times, update the view with new patch colors after each execution, and only increase the tick counter by 1 after the repeat loop is done.
Here is the link to NetLogo Dictionary entry about the display primitive:
http://ccl.northwestern.edu/netlogo/docs/dictionary.html#display
Note: due to its design, NetLogoWeb does not support the display primitive. So, you need to use the desktop version.
In NetLogo, I would like to have each round a likelihood of between 0 to 10 % turtles of the whole population to have a change of a variable. Within the chosen turtles, their variable can change between +1 and +4 by a certain likelihood.
breed [ humans human ]
humans-own [ var ]
to setup
create-humans(population) [ set var 0 ]
end
to go
ask humans [ var_change ]
end
to var_change [
let %draw (random-float 100)
let %strength 0 ;no eco loss
if (%draw < 50) [ set %strength (%strength + 1) ] ;1 little eco loss
if (%draw < 10) [ set %strength (%strength + 2) ] ;2 middle eco loss
if (%draw < 5) [ set %strength (%strength + 3) ] ;3 strong eco loss
if (%draw < 1) [ set %strength (%strength + 4) ] ;4 complete eco loss
[ ask one-of %strength patches [ set economic economic + 3 ]]; here I do not know how to continue(*)
]
end
*I do not know how to code that between 0 to 10 % of turtles can have with a certain probability have their variable var changed. How can I achieve that?
The way you have it set up, all turtles are sent to the var_change procedure. It would be easier to have the var_change procedure both select the turtles to change and assign the amount of change. Something like:
to go
var_change
end
to var_change
; choose proportion to change
let %draw-prop random-float 0.1
let n-changers round (%draw-prop * count turtles) + 1 ; +1 so at least one changes
; change by some amount
ask n-of n-changers turtles
[ let %draw-change random 100
set economic economic + 1 ;all get some eco loss
if (%draw-change < 10) [ set economic economic + 1 ] ;2 middle eco loss
if (%draw-change < 5) [ set economic economic + 1] ;3 strong eco loss
if (%draw-change < 1) [ set economic economic + 1 ] ;4 complete eco loss
]
end
Note that I changed all your + amounts to + 1. The way you had it written, drawing a number like 3 would have added 1 (as <50) then another 2 (as <10) then another 3 (as <5) for a total increase of 6. An if clause runs the code if the if condition is satisfied and skips over it if not satisfied. Either way, the next code always runs.
How can I give each tick a random amount of turtles a change in a binary variable (1 or 0), whereas no more than 5 % of the existing population at all times has a value of 0 in that variable?
In other words, I wish to have that the total amount of turtles having a variable value of 0 is between 0 % or 5 % of the total amount of turtles at every tick.
How can I achieve this?
My code is:
to setup
create-turtles 100
set var random 1 (only 5 % max shall have a 0 at start)
end
to start
change
end
to change
let %draw (random 1)
if (%draw < 0) … ; than I do not how to continue
end
The n-of primitive selects the specified number of agents. You want some number up to that, so you also need to randomly generate the number. Something like this:
to setup
create-turtles 100 [ set var 1 ] ; give them all value 1
ask n-of random 6 turtles [ set var 0 ] ; randomly selects 0 to 5 turtles, assigns value 0
end
I have a very simple model of 50 turtles moving away from a central point. I would like to be able to extract the spatial coordinates (xcor, ycor) of a subset of them every nth tick in behaviour space. Hope you can help!
The modulo operator mod is probably the simplest way to do this. It outputs the remainder from a division operation, so you can just use a logical flag such that the coordinates are only extracted when ticks divided by n is equal to 0. For example:
to setup
ca
crt 10
reset-ticks
end
to go
; set up lists for example output
let tlist []
let xlist []
let ylist []
ask turtles [
rt random 60 - 30
fd 1
]
tick
; If ticks is not zero, and the remainder of
; the number of ticks / 3 is zero, extract
; some info about the turtles and print it.
if ticks > 0 and ticks mod 3 = 0 [
ask turtles with [ xcor > 0 ] [
set tlist lput self tlist
set xlist lput xcor xlist
set ylist lput ycor ylist
]
print tlist
print xlist
print ylist
]
end
Run this several times and you'll see that on tick 3 (and 6, 9, 12, etc), the lists are printed out. Note that where you have your tick increment will affect when this output is actually extracted- in the example above, tick happens at the end of the go procedure but before the if statement is evaluated.
In my model I have some agents collecting from another agent who they bump into at random after which they move back to their base. As they move back they drop off some material as defined by the random function. Here's some sample code
to go
ask searchers
[ set energy energy - 1
fd 0.0125
if random-float 1 < (1 / 50)
[ ifelse random 2 = 0
[ rt 45 ]
[ lt 45 ]
]
search
]
end
to search
if any? depots in-radius vision with [color = yellow]
[spread
set energy 0] ;; makes them to back to base
end
to spread
if random 10000 = 1 [hatch-rubbish 1 [ set color white
set shape "circle"
set size 0.5]]
end
If I set the visual radius to something enormous so they can always see the depot the number of bits of rubbish works out.
However if I allow them to move around with a radius of 1, the count of rubbish is much higher than 1 in 10,000.
Why would that make a difference?
Thanks