I want to change turtle's behavior.
first 6 ticks, turtles using foraging map to move.
after foraging, turtles using resting map to rest in 6 ticks.
I tried to use count-down, but it failed it.
this is the code I used
to go
move-turtles
tick
if ticks >= 4320
[stop]
end
to move-turtles
ask turtles
[
foraging
resting
]
end
to foraging
uphill food
if food >= [food] of one-of neighbors
[move-to one-of neighbors]
set count-down count-down - 1
set label count-down
set label-color red
if count-down = 0
[resting
set label "6"
reset-count-down
]
end
to resting
uphill rplace
if rplace >= [rplace] of one-of neighbors
[move-to one-of neighbors]
set count-down count-down - 1
set label count-down
set label-color blue
if count-down = 0
[foraging
set label "6"
reset-count-down
]
end
to reset-count-down
set count-down 6
end
You need to add a turtle attribute that tracks what state a turtle is in. For example, you could add a resting? attribute, which you set to true when the foraging count down hits 0, and set to false when the resting countdown hits 0. You will then condition behavior (foraging or resting) on this attribute.
Btw, usually you should name command procedures with a verb (here, forage and rest).
Related
In this model the boat moves diagonally across the world (which does not wrap, 00 bottom left, max-pycor & max-pxcor 49) and 20 fish move in the opposite diagonal. The model is set up so the boat counts the fish it encounters, and as a double-check each fish counts if it met the boat. It does not matter if the boat does not meet all the fish.
My problem is that on some (10-15%) of runs, although both agents appear to be on the same patch, the relevant record does not increase, i.e. times that f-count does not increase and other times the b-count might not increase. I have spent time watching the agents closely, and they appear to be on the same patch, but the record does not increase. As a beginner with NetLogo, is there something in the coding of "boats-here", "fish-here" or "neighbors" that I am overlooking that is related to the xcor/ycor of the agents? Or is the sequence of commands the source of the problem? Thanks for considering my question.
breed [boats boat]
breed [fish a-fish]
boats-own [b-count fuel]
fish-own [f-count]
to setup
clear-all
reset-ticks
create-boats 1
[apply-boat-properties]
create-fish 20
[apply-fish-properties]
end
to apply-boat-properties
ask boats
[
set color red
set size 0.3
set shape "star"
setxy min-pxcor max-pycor
set fuel 710
]
end
to apply-fish-properties
ask fish
[
set color blue
set size 0.3
set shape "star"
setxy random-xcor max-pycor
]
end
to go
if (any? boats with [fuel <= 0 ])
[ stop ]
ask boats
[
follow-route1
survey
]
ask fish
[
check-if-boat
move
]
tick
end
to follow-route1
ask boats
[
facexy 49 0
fd 0.05
pen-down
set fuel fuel - 1
]
end
to survey
ifelse any? fish-here ;; if boat on the same patch as a fish
[ask fish-here [ ;; ask the fish on this patch
if shape = "star"[ ;; with a "star" shape
set shape "circle" ;; change shape to "circle" shape
ask boats-here ;; ask boat on same patch to increase b-
count by 1
[set b-count b-count + 1]]
]
]
;; otherwise
[ask fish-on neighbors [ ;; ask fish on neighbouring 8 patches
if shape = "circle"[ ;; if the fish has a "circle" shape
set shape "star" ] ;; change that shape to "star" shape
]]
end
to move
ask fish
[
set heading 225
fd 0.0025
]
end
to check-if-boat
ifelse any? boats-here ;; if boats on same patch as the
fish
[
ask fish-here with [color = blue] ;; ask the blue fish to change
colour to yellow
[ set color yellow
set f-count f-count + 1] ;; and increase f-count by 1
]
[ask fish-here with [color = yellow] ;; otherwise ask fish with yellow
colour to change color to blue
[set color blue
] ]
end
to-report boat-b-count ;;Report count of boats with b-
count more than 1
report sum [b-count] of boats with [b-count > 0]
end
to-report fish-f-count ;;Report count of fish with f-
count more than 1
report sum [f-count] of fish with [f-count > 0]
end
to-report %fish-counted ;;Report count of fish with f-
count more than 1
report (sum [f-count] of fish / count fish) * 100
end
I write a line of code which is this
ask turtles [if count other turtles in-radius 1 > 5 [set color white]]
now when turtle has changed color to white, but after the certain amount of time this property is not true for that same turtle, it should change its color to default color?
how can I fix it?
I think you're after a turtles-own counter that decreases whenever the condition is not satisfied. With this setup:
turtles-own [ default-color countdown ]
to setup
ca
crt 150 [
setxy random-xcor random-ycor
set default-color blue
set color default-color
]
reset-ticks
end
Now you can get your turtles to wander around and change their countdown variable whenever they change color. When that condition is not met, they can decrease the counter until it hits zero, at which point they can revert to their default color. More detail in comments:
to go
ask turtles [
rt random 60 - 30
fd 0.25
; if there are more than 5 turtles in radius 3,
; turn white and set countdown to 5
ifelse count other turtles in-radius 3 > 5 [
set color white
set countdown 5
] [
; If not, and counter is greater than 0,
; decrease the counter.
if countdown > 0 [
set countdown countdown - 1
; If counter gets down to 0,
; set color back to the default.
if countdown = 0 [
set color default-color
]
]
]
]
tick
end
I am teaching myself how to create ABMs in Netlogo using the book of Railsback & Grimm 2012. I am having trouble with one book exercise which is on butterflies following "virtual" corridors. Basic idea is that butterflies go uphill for mating using the differences in height as guide. I need to calculate the width of the corridors dividing the number of patches used by the butterflies over the average distance the butterflies fly from the start patch to the end patch. I am
struggling with plotting this corridor width, which I am coding like this:
to-report corridor-width
let patches-visited count patches with [used?]
let mean-distance mean [distance start-patch] of turtles
report patches-visited / mean-distance
I then created a plot in the interface with the command:
plot corridor-width
The error message I get reads:
Division by zero. error while observer running / called by procedure
CORRIDOR-WIDTH called by plot 'Corridor width' pen 'default' update
code called by procedure SETUP called by Button 'setup'
I believe there is something wrong with the way I am coding distance start-patch but I have surfed the web and looked at several codes and I cannot spot my mistake. My whole code looks like this:
globals [ q ] ;; q is the probability that butterfly moves directly to highest patch
turtles-own [ start-patch ]
patches-own [ elevation used? ] ;; patches property of elevation and whether the patch has been used by butterfly or not.
to setup
ca
;; Let's create patches and asign them an elevation and color by using ask patches statement
ask patches
[
;; Elevation decreases linearly with distance from the center of hills. Hills are at (30,30) and
;; (120,120) coordinates. The first hill is 100 units high whereas the second one is 50
let elev1 100 - distancexy 30 30
let elev2 50 - distancexy 120 100
ifelse elev1 > elev2
[ set elevation elev1 ]
[ set elevation elev2 ]
set pcolor scale-color green elevation 0 100
set used? false
]
;; Create 50 butterflies
crt 50
ask turtles [
set size 6
;; set their initial location as their initial patch
setxy random-pxcor random-pycor
set start-patch patch-here
;; have the butterfly draw its path with the pen-down statement
pen-down
]
reset-ticks
;; Initialize the q parameter
set q 0.4
end
;; The master schedule
to go
ask turtles [ move ]
plot corridor-width
tick
if ticks >= 1000
[
let final-corridor-width corridor-width
write "Corridor width: " print final-corridor-width
;export-plot "Corridor width" (word "Corridor-width-output-for-q-" q ".csv")
stop
]
end
;; let's code the butterfly procedure of movement
to move
if elevation >=
[ elevation ] of max-one-of neighbors [ elevation ]
[ stop ]
ifelse random-float 1 < q ;; Decide whether to move to the highest sorrounding
;; patch with p=q
[ uphill elevation ] ;; move deterministically uphill
[ move-to one-of neighbors ] ;; or move randomly
set used? true
end
to-report corridor-width
let patches-visited count patches with [used?]
let mean-distance mean [distance start-patch] of turtles
report patches-visited / mean-distance
end
What happens when the mean-distance is 0?
let mean-distance mean [distance start-patch] of turtles
Essentially, in your setup, you set all the turtle's start-patch to their current patch. So, if you ask all the turtles how far away they are from their start patch, they will all tell you 0 units away.
So, [distance start-patch] of turtles is filled with a list of all 0's.
Thus, a mean of a list of all 0s is 0 causing your divide by 0 error.
Maybe in this situation, you want to report 0 instead...so
ifelse mean-distance = 0
[ report 0]
[report patches-visited / mean-distance]
with the help of #JenB I wrote a code to help understand forward 1, patch and turtle coordination. The code is attached at the end.
However, I noticed an error: I built two buttons setup and test, after click setup and then click test a few times, we will notice some of turtle 1's plabels will replace previous ones, which is not at all intended and should not happen; whereas, turtle 0 seems to have no such problem.
to setup
clear-all
create-ordered-turtles 2
[
ifelse (who mod 2) = 0
[
set heading heading + 15
]
[
set heading 135
]
pen-down
; forward 1
]
end
to test
ask turtles [ ;; for each turtle
fd 1
;; get patch corrdination
let patch-cor (word "px" pxcor ":" "py" pycor)
;; get turtle corrdination
let turtle-cor (word "x" (precision xcor 2) ":" "y" (precision ycor 2))
;; get distance between current and previous turtle corrdinations
;; get distance between current and origin turtle corrdinations
let distanceTurtle precision (distancexy 0 0) 1
let patch-color color ;; get turtle's color
set label patch-cor ;; let this turtle's label show current patch-corrdination
set label-color patch-color
ask patch-at -1 0 ;; focus on patch 1 unit left to where this turtle is on
[
set plabel patch-cor ;; let this patch label show turtle's patch corrdination
set plabel-color patch-color
]
ask patch-at 4 0 ;; focus on patch 4 unit right to where this turtle is on
[
set plabel turtle-cor ;; let this patch label show turtle's own corrdination
set plabel-color patch-color
]
ask patch-at 5 0 ;; focus on patch 5 unit right to where this turtle is on
[
set plabel distanceTurtle ;; let this patch label show distance between current turtle and origin
set plabel-color patch-color
]
]
end
i am making a model for a class in netlogo, but i have run into a problem, whenever a turtle asks the patch that it is on what color it is, it thinks that it is black, even when it is colored white
globals [var inside outside var1 ratio]
turtles-own [randomX randomY]
to setup
reset-ticks
ask patches [set pcolor black]
set var 0
set var1 0
while [var <= 360] [ask patch 0 0 [sprout 1 [set color white set heading var pd fd 100]]set var var + 0.15]
ask turtles [die]
tick
end
to go
ask patch 0 0 [sprout 1[]]
check-location
ask turtles [set randomX random 2000000 / 10000 - 100
set randomY random 2000000 / 10000 - 100
set xcor randomX
set ycor randomY]
tick
reset-variables
end
to check-location
ask turtles [ ask patch-here[if pcolor != black [set inside inside + 1]]]
ask turtles [ ask patch-here[if pcolor != white [set outside outside + 1]]]
end
to reset-variables
set outside 0
set inside 0
end
I have a setup button, a go button with forever checked, a monitor that shows the variable inside, another monitor that shows the variable outside, and a monitor that shows count turtles. the outside monitor always shows the same number as the total monitor. any help would be greatly appreciated.
Putting the turtles pen down colors over the patches. It does not change the color of the patches. (You just cannot see that anymore.) To changes the patch color, always use pcolor.