why plabel replace previous plabel in this netlogo model for displaying patch and turtle coordination? - netlogo

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

Related

how to calculate common friends of two nodes?

Suppose that the more common friends two nodes have, the more closely they are related. I want to find the closest link-neighbor of a node.
But it's difficult for me to find the common friends between one random nodes and its link-neighbors.
I wrote an example to see if my thought works:
turtles-own[CF] ;;common friends
to setup
ca
crt 10 [set size 1 set color gray]
ask turtle 1 [create-links-with other turtles]
ask turtle 2 [create-link-with turtle 3]
ask turtle 2 [create-link-with turtle 4]
layout-circle (sort turtles) 5
reset-ticks
end
to go
ask one-of turtles [
let communicator self
ask link-neighbors [;;setA
let i 0
ask link-neighbors [;;setB
if link-neighbor? communicator[
set i i + 1
]
]
set CF i
]
]
tick
end
In the interface, I set a reporter [CF] of turtle 2.If it works, the answer could be 2
But as it keeps going, the reporter shows sometimes 1 and sometimes 2. I don't know why and I hope sth more simple.
I haven't traced your code's operation, but your core structural problem is that "common friends" is a property of a PAIR of nodes, not one node .
I tweaked your code to set the common friends count ( CFL ) to the LINK between pairs of nodes. So (link 1 2) ends up with CFL = 2 and it's stable. It's not as efficient as it could be but then you can just run all the turtles, not one of the turtles in your go step.
Here's my solution:
turtles-own[CF] ;;common friends
links-own[CFL]
to setup
ca
crt 10 [set size 1 set color gray set shape "circle" set label who ]
ask turtle 1 [create-links-with other turtles set color green]
ask turtle 2 [create-link-with turtle 3 [set color orange set thickness 0.25] ]
ask turtle 2 [create-link-with turtle 4 [set color orange set thickness 0.25] ]
ask turtle 2 [ set color red ]
ask one-of links with [end1 = turtle 1 and end2 = turtle 3] [ set color green set thickness 0.25 ]
ask one-of links with [end1 = turtle 1 and end2 = turtle 4] [ set color green set thickness 0.25 ]
layout-circle (sort turtles) 5
reset-ticks
end
to go
let ln-direct ""
let ln-once-removed ""
ask turtles [
let communicator self
ask link-neighbors [;;setA
set ln-direct self
; print (word "for turtle " myself " one DIRECT link-neighbor is " ln-direct)
let i 0
ask link-neighbors [;;setB
set ln-once-removed self
; print ( word " one neighbor of that neighbor is " ln-once-removed )
if link-neighbor? communicator[
; print (word ",,,,,,,, BINGO - loops back to " communicator )
set i i + 1
]
]
ask one-of links with [
( end1 = communicator and end2 = ln-direct ) or
( end2 = communicator and end1 = ln-direct )
] [set CFL i set label CFL ]
]
]
tick
end

Agents on same patch

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

bathtub model sprout using decision rules

newbie in netlogo here. I'm simulating a flood model using cellular-automata. It's just a simple one - cell should be filled (change color) or sprout if water > elevation.
For a dummy code, I'm trying to do it this way:
to go
ask patches with [pcolor != blue] ;remove ocean
water_rise
tick
end
to water_rise ; saturates cell
if not any? turtles [
ask patch x-breach y-breach [ ;;; This will be the breach patch, will start to fill at first tick, a specific location in my map
set cell-storage elevation * fill-rate
]
]
ask patches [
;;; This has a patch check if any neighbors have sprouted.
;;; If any have, that patch starts to fill.
if any? neighbors4 with [ any? turtles-here ] [
set cell-storage elevation * fill-rate
let minv min [ cell-storage ] of patches
let maxv max [ cell-storage ] of patches
set pcolor scale-color green cell-storage 0 5 ;idea is to have a graduated color depending on fill stage
]
]
;;; Once all patches have had a chance this tick to fill,
;;; see if any are "full"
ask patches [
if cell-storage > elevation [
;; If the patch gets "full" and they have not already sprouted,
if not any? turtles-here [
sprout 1 [
set color yellow
set size 1
set shape "square"
]
]
]
]
end
Thanks in advance!
BTW, I'm working on a DEM re: elevation values.
I set fill-rate as a slider with 0.3 for now.
-Nands
I played around with this a little and it seems to me like you want your starting patch to fill up, and once it hits its maximum value, to start flooding into other cells that repeat the process. I think the main problem is that in your water-rise, you have are asking all patches with elevation greater than -9999 to first call the starting-point procedure and then also to sprout a turtle if they have any neighbors with elevation is less than cell-storage. It appears that all patches satisfy that condition, so all patches will sprout a turtle.
It may work better to rework your flow of logic, so that filling up your breach patch is independent of other patches. Something like:
to water_rise ; saturates cell
if not any? turtles [
ask patch 0 0 [ ;;; This would be your breach patch, will start to fill at first tick
set cell-storage cell-storage + fill-rate
]
]
ask patches [
;;; This has a patch check if any neighbors have sprouted.
;;; If any have, that patch starts to fill.
if any? neighbors4 with [ any? turtles-here ] [
set cell-storage cell-storage + fill-rate
]
]
;;; Once all patches have had a chance this tick to fill,
;;; see if any are "full"
ask patches [
if cell-storage > 0 [
if cell-storage > 5 [
set cell-storage 5
;; If the patch gets "full" and they have not already sprouted, sprout 1
if not any? turtles-here [
sprout 1 [
set color yellow
set size 0.5
set shape "circle"
]
]
]
set pcolor cell-storage + 82
]
]
end
Full toy model with variables and setup here.
Obviously, you would need to modify your starting patch (I used 0 0 for convenience and simplicity). Additionally, I included fill-rate as a way to slow the rate of filling as more and more patches begin getting filled up.

Netlogo: measure mean distance between start and end patches

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]

Netlogo , How to display number of ticks

Hi I am writing simulation which uses ticks to represent time in simulation environment . Is it possible to display tick counter in monitor GUI? Also I have tried to input the code but it is not working.
My sample code:
if [agents count = 0]
show "count ticks"
This should show tick exactly value when agent unit value is zero. For example if agent = 0 at tick 200 it should display 200 on monitor even the whole simulation run on 500 ticks.
whole code is
patches-own[
nest?
nest-scent
food
food-source-number
chemical
]
breed [agents agent] ;agent is ant
breed [antiagents a-antiagent] ;antiagent spider
antiagents-own[energy]
agents-own[venom]
;;;;;;;;;;;;;;;;;;;;;;;;;; Setup ;;;;;;;;;;;;;;;;;;;
to setup
clear-all
set-default-shape agents "ant"
create-agents initial-ants ;; create the ants, then initialize their variables
[
set color orange
set size 2 ;; easier to see
set venom (ant-venom-strength)
]
set-default-shape antiagents "spider"
create-antiagents initial-spiders ;; create the spider, then initialize their variables
[
set color yellow
set size 3 ;; easier to see
set energy (spider-health)
setxy random-xcor random-ycor
]
setup-patch
display-labels
reset-ticks
end
;;;;; nest food patch;;;
to setup-patch
ask patches
[ setup-nest
setup-food
recolor-patch ]
end
;;;;; setup nest
to setup-nest ;; patch procedure
;; set nest? variable to true inside the nest, false elsewhere
set nest? (distancexy 0 0) < 5
;; spread a nest-scent over the whole world -- stronger near the nest
set nest-scent 200 - distancexy 0 0
end
to setup-food ;; patch procedure
;; setup food source one on the right
if (distancexy (0.6 * max-pxcor) 0) < 5
[ set food-source-number 1 ]
;; setup food source two on the lower-left
if (distancexy (-0.6 * max-pxcor) (-0.6 * max-pycor)) < 5
[ set food-source-number 2 ]
;; setup food source three on the upper-left
if (distancexy (-0.8 * max-pxcor) (0.8 * max-pycor)) < 5
[ set food-source-number 3 ]
;; set "food" at sources to either 1 or 2, randomly
if food-source-number > 0
[ set food one-of [1 2] ]
end
to recolor-patch ;; patch procedure
;; give color to nest and food sources
ifelse nest?
[ set pcolor violet ]
[ ifelse food > 0
[ if food-source-number = 1 [ set pcolor cyan ]
if food-source-number = 2 [ set pcolor sky ]
if food-source-number = 3 [ set pcolor blue ]
if food-source-number = 4 [ set pcolor brown ] ]
;; scale color to show chemical concentration
[ set pcolor scale-color green chemical 0.1 5 ] ]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;; GO ;;;;;;;;;;;;;;;;;;;;;;
to go ;; forever button
;;;;;;;;;;;;Spider ;;;;;;;;
ask antiagents [
setup-spider-movemonet ; option for user to select spider movement
;move-spider
catch-ant
spider-death
]
;;;;;;;;;; Ant ;;;;;;;;;;;;
ask agents
[ if who >= ticks [ stop ] ;; delay initial departure
ifelse color = orange
[ look-for-food ] ;; not carrying food? look for it
[ return-to-nest ] ;; carrying food? take it back to nest
wiggle
fd 1 ]
diffuse chemical (diffusion-rate / 100)
ask patches
[ set chemical chemical * (100 - evaporation-rate) / 100 ;; slowly evaporate chemical
recolor-patch ]
tick
display-labels
;;;;;;;;;;; Stop function ;;;;;;;;;;
if count agents = 0 [stop]
if count patches with [pcolor = blue] = 0 [stop]
end
;;;;;;;;;;;;;;;;;;;;;;;;;; function in go ;;;;;;;;;;;;;;;
to look-for-food ;; turtle procedure
if food > 0
[ set color green + 1 ;; pick up food
set food food - 1 ;; and reduce the food source
rt 180 ;; and turn around
stop ]
;; go in the direction where the chemical smell is strongest
if (chemical >= 0.05) and (chemical < 2)
[ uphill-chemical ]
end
;;;;;;;;;;; ant function ;;;;;;;;;;
;; sniff left and right, and go where the strongest smell is
to uphill-chemical ;; turtle procedure
let scent-ahead chemical-scent-at-angle 0
let scent-right chemical-scent-at-angle 45
let scent-left chemical-scent-at-angle -45
if (scent-right > scent-ahead) or (scent-left > scent-ahead)
[ ifelse scent-right > scent-left
[ rt 45 ]
[ lt 45 ] ]
end
to return-to-nest
ifelse nest?
[ ;; drop food and head out again
set color orange
rt 180 ]
[ set chemical chemical + 60 ;; drop some chemical
uphill-nest-scent ] ;; head toward the greatest value of nest-scent
end
to wiggle ;; turtle procedure
rt random 40
lt random 40
if not can-move? 1 [ rt 180 ]
end
to uphill-nest-scent
let scent-ahead nest-scent-at-angle 0
let scent-right nest-scent-at-angle 45
let scent-left nest-scent-at-angle -45
if (scent-right > scent-ahead) or (scent-left > scent-ahead)
[ ifelse scent-right > scent-left
[ rt 45 ]
[ lt 45 ] ]
end
to-report chemical-scent-at-angle [angle]
let p patch-right-and-ahead angle 1
if p = nobody [ report 0 ]
report [chemical] of p
end
to-report nest-scent-at-angle [angle]
let p patch-right-and-ahead angle 1
if p = nobody [ report 0 ]
report [nest-scent] of p
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;; Spider-Function ;;;;;;;;;;;;;;;;;;;;;;
to move-spider
rt random 360
lt random 360
fd 3
end
to spider-death ;; turtle procedure
if energy <= 0 [ask antiagents-here [die]]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to catch-ant
let prey one-of agents-here
if prey != nobody
[ ask prey [ die ]
set energy (energy - ant-venom-strength)]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; GUI Function ;;;;
to display-labels
ask antiagents [ set label "" ]
if show-spider-health [
ask antiagents [ set label round energy ]
]
end
;;;;;;; Spider movement GUI ;;;;;;
to setup-spider-movemonet
if setup-spider-movement
[
ask antiagents [ move-spider ]
]
end
To make a monitor that displays the tick count, the complete code to put in the monitor is:
ticks
That's all.
If you want the monitor to show the tick count when there are no agents, but be blank otherwise, you would put this code in:
ifelse-value any? agents
[ "" ]
[ ticks ]
Your syntax is incorrect. You could however include in an observer procedure (e.g., your go procedure) the following: if (count agents = 0) [show ticks]. This would display in the area above the command line.
However it sounds like you are looking for user-message, rather than show. E.g., if (count agents = 0) [user-message (word ticks)]. http://ccl.northwestern.edu/netlogo/docs/dict/user-message.html
If you want to update the monitor each tick but only when there are agents, you need to create a separate global variable (called counter below) and update that instead. The ticks internal variable will always increase so monitoring it will always increment. This is because the ticks variable is the simulation clock, it reports the passage of time.
globals [counter]
to setup
...
set counter 0 ; note, not required as initialise to 0 but helps readability
reset-ticks
end
to go
...
if any? turtles [ set counter counter + 1 ]
...
tick
end
Then create a monitor with just counter (and 0 decimal places etc)