Agents on same patch - netlogo

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

Related

How can I establish a decreasing relationship between variables?

I am a teacher of Primary Education and Early Childhood Education and I am trying to generate a simulator through NetLogo on how fertilization and pesticides are decimating the butterfly population. However, despite having read the manual, I am not managing to program the code to make it work.
My problem is that although I set the turtles I can't establish the following relationship between the variables/buttons:
If butterflies randomly touch a plant (which is fertilized with pesticide) its pollinating capacity is reduced by a certain percentage (depends on the amount of pesticide)
My problem is that I can't get the pollination capacity of the butterfly to be set to 100% initially and that the greater the amount of pesticide, the lower its pollination capacity is when touching a flower. Currently, although the amount of pesticide is the highest, there are peaks where its pollination capacity increases instead of being reduced.
breed [butterflies butterfly]
breed [flowers flower]
globals
[
butterfliesless-neighborhoods ;; how many patches have no butterflies in any neighboring patches?
pollinating-capacity ;; measures how well-bivouaced the butterflies are
]
patches-own
[
butterflies-nearby ;; how many butterflies in neighboring patches?
]
flowers-own
[
carried-butterflies ;; the butterflies I'm carrying (or nobody if I'm not carrying in)
found-bivouac? ;; becomes true when I find a bivouac to drop it in
]
to setup
clear-all
set-default-shape butterflies "butterflies"
set-default-shape flowers "flower"
ask patches
[ set pcolor green + (random-float 0.8) - 0.4] ;; varying the green just makes it look nicer
create-butterflies num-butterflies
[ set color white
set size 1.5 ;; easier to see
setxy random-xcor random-ycor ]
create-flowers num-flowers
[ set color brown
set size 1.5 ;; easier to see
set carried-butterflies nobody
set found-bivouac? false
setxy random-xcor random-ycor ]
reset-ticks
end
to update-butterflies-counts
ask patches
[ set butterflies-nearby (sum [count butterflies-here] of neighbors) ]
set butterfliesless-neighborhoods (count patches with [butterflies-nearby = 0])
end
to calculate-pollinating-capacity
set pollinating-capacity (butterfliesless-neighborhoods / (count patches with [not any? butterflies-here])) * 100
end
to go
ask flowers
[ ifelse carried-butterflies = nobody
[ search-for-butterflies ] ;; find a butterflies and pick it up
[ ifelse found-bivouac?
[ find-empty-spot ] ;; find an empty spot to drop the butterflies
[ find-new-bivouac ] ] ;; find a bivouac to drop the butterflies in
wiggle
fd 1
if carried-butterflies != nobody
;; bring my butterflies to where I just moved to
[ ask carried-butterflies [ move-to myself ] ] ]
ask butterflies with [not hidden?]
[ wiggle
fd pesticide-amount ]
tick
end
to wiggle ;; turtle procedure
rt random 50 - random 50
end
to search-for-butterflies ;; flowers procedure
set carried-butterflies one-of butterflies-here with [not hidden?]
if (carried-butterflies != nobody)
[ ask carried-butterflies
[ hide-turtle ] ;; make the butterflies invisible to other flowers
set color blue ;; turn flower blue while carrying butterflies
fd 1 ]
end
to find-new-bivouac ;; flowers procedure
if any? butterflies-here with [not hidden?]
[ set found-bivouac? true ]
end
to find-empty-spot ;; flowers procedure
if all? butterflies-here [hidden?]
[ ask carried-butterflies
[ show-turtle ] ;; make the butterflies visible again
set color brown ;; set my own color back to brown
set carried-butterflies nobody
set found-bivouac? false
rt random 360
fd 20 ]
end
Defined Buttons
Your screenshot is a nice start with sliders, buttons and plots. In the code tab consider making two breeds of turtles: butterflies and plants.
breed [butterflies butterfly]
butterflies-own [pollinatingCapacity]
breed [plants plant]
plants-own [pesticideAmount]
And then think about how you might decrease pollinatingCapacity of a butterfly when it is on the same patch as a plant. A butterfly can see if any plants are on the same patch using plants-here

NetLogo - Have an agent identify one of two other types of agent closest to it and perform actions conditional on which agent type is identified

Suppose I have 2 breeds of agents, sharks (stars and dots) and fish. The stars (large sharks) can eat dots (smaller sharks) and fish that are in-radius 0.5 of a star. A star should eat what is closest to it, either a dot or fish. For simplicity purposes, agents cannot move, and whether or not a star can feed depends on its spatial proximity to dots and fish during setup.
But i'm struggling to get the code to work. Specifically,
I am encountering an "expected reporter" error message in my code and i cannot figure out how to solve this, although I don't know if fixing the code will achieve the aim. Any help is greatly appreciated.
Below is my code:
; Create the agents
breed [sharks shark]
breed [fishes fish]
sharks-own
[
energy
]
fishes-own
[
x0 ; Starting x-cor
y0 ; Starting y-cor
]
to setup
; Always start with this
clear-all
reset-ticks
ask patches [set pcolor gray]
create-sharks 666
[
set color blue
set energy 100
ifelse who >= 15
[
set shape "dot" set size 2.5
]
[
set shape "star" set size 3.5
]
setxy random-xcor random-ycor
]
create-fishes 300
[
setxy random-xcor random-ycor
set x0 xcor
set y0 ycor
set shape "fish"
set size 2.5
set color white
]
; this procedure gives an "expected reporter' error
if shape = "star"
[
ifelse min-one-of turtles with [shape = "dot"]
[
ifelse any? sharks with [shape = "dot"] in-radius 0.5 ; namely here
[
fd 0
]
[
set energy (energy + 100)
set color green
] ; End of 2nd ifelse
]
[
if min-one-of turtles with [shape = "fish"]
[
ifelse any? fishes with [shape = "fish"] in-radius 0.5
[
fd 0
]
[
set energy (energy + 1)
set color yellow
]
]
] ; End of 1st ifelse
] ; End of 1st if
end
You core issue is you are using ifelse min-one-of turtles with [shape = "dot"] as though min-one-of will give a true/false, but it will report a turtle. The error message NetLogo is giving is not great in this case. What I think you want to use any? in those cases (there are two of them that I see).
After those are resolved you have a context error, where you are checking if shape = "star" , but you aren't inside an ask [...] block where that check would have a turtle context to be valid. Maybe that's just a copy/paste issue in getting this question code ready, but I thought I'd note that, too.
Ok after many hours of blood, sweat and tears I finally arrived at the solution. In case the issue/objective is of interest to anyone else, the working code is as follows:
; Create the agents
breed [sharks shark]
breed [fishes fish]
sharks-own
[
energy
]
fishes-own
[
x0 ; Starting x-cor
y0 ; Starting y-cor
]
to setup
; Always start with this
clear-all
reset-ticks
ask patches [set pcolor gray]
create-sharks 666
[
set color blue
set energy 100
ifelse who >= 10
[
set shape "dot" set size 2.5
]
[
set shape "star" set size 3.5
]
setxy random-xcor random-ycor
]
create-fishes 300
[
setxy random-xcor random-ycor
set x0 xcor
set y0 ycor
set shape "fish"
set size 2.5
set color white
]
ask turtles [
if shape = "star"
[
let turtleID min-one-of other turtles [distance myself] ; Create a variable that identifies nearest turtle to apex
ifelse [shape] of turtleID = "dot" ; Evaluate whether the nearest turtle is a small meso
[
ifelse distance turtleID < 0.5 ; check if prey is still close enough
[
; do this if within radius 0.5
set color green
]
[
; do this if not within radius 0.5
set color yellow
]
]
[
if [shape] of turtleID = "fish" ; Evaluate whether the nearest turtle is a fish
[
ifelse distance turtleID < 0.5
[
; do this if within radius 0.5
set color red
]
[
; otehrwise do this if not within radius 0.5
set color orange
]
]
]
]
]
end

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]

set a demand and supply curve for the model tragedy of the commons in the case of an overfished pond

I am new at net logo and I want to write a model based on tragedy of the commons in the case of an overfished pond. The purpose is to find an equilibrium between fishers and fishes based on an economic model with demand and supply. If there are less fishers, more fishes will be in the pond, then after a certain time (ticks) the number of fishers increases and less fishes will be in the pond. Maybe set like a number of fishes per day that can be fished. Thus, the solution is to find a convenient number of fishers as the fishes can still reproduce. I want to have a box in the interface where I can type in a number and see what happens with the number of fishes.
I have no idea how to set this up. I hope to hear from you :)
I started with this code:
breed [fishers fisher]
breed [fishes fish]
to setup
clear-all
reset-ticks
ask patches [set pcolor blue ] ;; lake/pond in form of a rectangle in color
ask patches [ if pxcor > 8 [ set pcolor green ]]
ask patches [ if pycor > 8 [ set pcolor green ]]
ask patches [ if pycor < -8 [ set pcolor green ]]
ask patches [ if pxcor < -8 [ set pcolor green ]]
ask one-of patches with [ pcolor = blue ] [ sprout 20 [set shape "fish" set color pink set size 1.5 ]] ;; creates fishes
ask one-of patches with [ pcolor = green ] [ sprout 2 [set shape "person" set color black set size 3 ] ] ;; creates fishers
end
to go
tick
;;fishes
ask turtles with [ shape = "fish" and color = pink ]
[ right random 360 forward 1
if [pcolor] of patch-ahead 1 = green [ right 180 fd 1 ]]
;; fishers
ask turtles with [ shape = "person" and color = black]
[;right random 360 forward 1
if any? patches with [pcolor = blue]
[set heading towards one-of patches with [pcolor = blue] forward 1]
if [pcolor] of patch-ahead 1 = blue [ right 180 fd 2 ]]
ask turtles with [shape = "person" and color = black]
[if any? turtles with [shape = "fish" and color = pink] in-radius 2
[ask turtles with [shape = "fish" and color = pink] in-radius 2 [die]]]
end
Firstly, I suggest you look through existing models in the Netlogo library (Wolf-sheep-predation model may help). You roughly have the right idea in your current code, but you should look at other models to improve. You've already set your different breeds of turtles, but you should also set up their respective shapes under 'setup'. This would help you a great deal later - instead of calling for
ask turtles with [ shape = "fish"...]
you can simply
ask fishes [do sth...]
For that 'box at the interface', you can have a slider at the interface determining the number of fishers you want your run to start with. With another slider, you can set the fishing pressure in your simulated run (i.e. how many fish each fisher will catch) and I suppose you can also consider how this changes when population of fish decreases.
Finally, for a model like yours, you can observe the supply and demand trend by plotting the curves of no. of fishers over time and no. of fishes over time. Again, look at the wolf-sheep-predation model to have an idea of how to do this.
I can't give you more than this I'm afraid since I'm no pro myself but hope this helps a little. Hope someone else would be able to give you a clearer idea.

BehaviorSpace freezes on step #0

Sorry if this is obvious, I have searched everything I can think of and asked a colleague and we are both stuck.
I have some code (below - a slight twist on wolf sheep predation) that I want to run behaviorspace (input also below). However, it never runs to completion. It always seems to get stuck on step #0 of a run (the run # it gets stuck on varies). The runs that it sticks on work fine if they are run in isolation (so all the variables seem fine). It just seems to freeze (producing no error messages, nor does it crash).
Any ideas what is causing it?
Thanks,
Simon
Code:
globals [grass] ;; keep track of how much grass there is
;; Sheep and wolves are both breeds of turtle.
breed [sheep a-sheep] ;; sheep is its own plural, so we use "a-sheep" as the singular.
breed [wolves wolf]
turtles-own [energy] ;; both wolves and sheep have energy
patches-own [countdown]
to setup
clear-all
ask patches [ set pcolor green ]
;; check GRASS? switch.
;; if it is true, then grass grows and the sheep eat it
;; if it false, then the sheep don't need to eat
if grass? [
ask patches [
set countdown random grass-regrowth-time ;; initialize grass grow clocks randomly
set pcolor one-of [green brown]
]
]
if fences?[ ; this code adds in blue fences to create patches of various size (if fences are turned on). Turtles cannot pass over fences
ask patches with [pxcor mod connectivity2 = 0]
[ set pcolor blue ]
ask patches with [pycor mod connectivity = 0]
[ set pcolor blue ]
]
set-default-shape sheep "sheep"
create-sheep ((count patches - (count patches with [pcolor = blue])) / 25) ;; create the sheep, then initialize their variables. The starting density always remains constant despite a varying world size
[
set color white
set size 1.5 ;; easier to see
set label-color blue - 2
set energy random (2 * sheep-gain-from-food)
setxy random-xcor random-ycor
]
set-default-shape wolves "wolf"
create-wolves ((count patches - (count patches with [pcolor = blue])) / 50) ;; create the wolves, then initialize their variables. The starting density always remains constant despite a varying world size
[
set color black
set size 2 ;; easier to see
set energy random (2 * wolf-gain-from-food)
setxy random-xcor random-ycor
]
display-labels
set grass count patches with [pcolor = green]
reset-ticks
end
to go
if count wolves = 0 and count sheep = 0 and ((count patches with [pcolor = green]) = (count patches - (count patches with [pcolor = blue]))) [ stop ] ; stop model when the whole world has flipped to grass
ask sheep [
move
if grass? [
set energy energy - 1 ;; deduct energy for sheep only if grass? switch is on
eat-grass
]
death
reproduce-sheep
]
ask wolves [
move
set energy energy - 1 ;; wolves lose energy as they move
catch-sheep
death
reproduce-wolves
]
if grass? [ ask patches [ grow-grass ] ]
set grass count patches with [pcolor = green]
tick
display-labels
end
to move ;; turtle procedure
rt random 50
lt random 50
ifelse [pcolor] of patch-ahead 1 = blue
[ move] ;; If there is a blue patch ahead of you, choose another random direction
[ fd 1 ] ;; Otherwise, it is safe to move forward.
end
to eat-grass ;; sheep procedure
;; sheep eat grass, turn the patch brown
if pcolor = green [
set pcolor brown
set energy energy + sheep-gain-from-food ;; sheep gain energy by eating
]
end
to reproduce-sheep ;; sheep procedure
if random-float 100 < sheep-reproduce [ ;; throw "dice" to see if you will reproduce
set energy (energy / 2) ;; divide energy between parent and offspring
hatch 1 [ ifelse [pcolor] of patch-ahead 1 = blue ;; hatch an offspring
[ move] ;; If there is a blue patch ahead of the offspring, it chooses another random direction
[ fd 1 ] ;;If not, offspring move forward 1 step
]]
end
to reproduce-wolves ;; wolf procedure
if random-float 100 < wolf-reproduce [ ;; throw "dice" to see if you will reproduce
set energy (energy / 2) ;; divide energy between parent and offspring
hatch 1 [ ifelse [pcolor] of patch-ahead 1 = blue ;; hatch an offspring
[ move ] ;; If there is a blue patch ahead of the offspring, it chooses another random direction
[ fd 1 ] ;;If not, offspring move forward 1 step
] ]
end
to catch-sheep ;; wolf procedure
let prey one-of sheep-here ;; grab a random sheep
if prey != nobody ;; did we get one? if so,
[ ask prey [ die ] ;; kill it
set energy energy + wolf-gain-from-food ] ;; get energy from eating
end
to death ;; turtle procedure
;; when energy dips below zero, die
if energy < 0 [ die ]
end
to grow-grass ;; patch procedure
;; countdown on brown patches: if reach 0, grow some grass
if pcolor = brown [
ifelse countdown <= 0
[ set pcolor green
set countdown grass-regrowth-time ]
[ set countdown countdown - 1 ]
]
end
to display-labels
ask turtles [ set label "" ]
if show-energy? [
ask wolves [ set label round energy ]
if grass? [ ask sheep [ set label round energy ] ]
]
end
; Copyright 1997 Uri Wilensky.
; See Info tab for full copyright and license.
behaviorspace input:
variables:
["world-width" 51]
["fences?" true]
["wolf-gain-from-food" 20]
["sheep-reproduce" 7]
["show-energy?" false]
["grass?" true]
["connectivity" 10 25 50]
["wolf-reproduce" 5]
["grass-regrowth-time" [1 1 100]]
["sheep-gain-from-food" 4]
repetitions: 1
reporters:
count wolves
count sheep
count patches with [pcolor = green]
measure runs at each step
setup: setup
Go: go
Stop (I have tried it with and without this): count wolves = 0 and count sheep = 0 and ((count patches with [pcolor = green]) = (count patches - (count patches with [pcolor = blue])))
Time limit: 5000
It's difficult for me to imagine this could be caused by anything other than running low on memory. Running very low on memory can cause the JVM to slow to a crawl.
What do the memory usage stats in the System tab of "About NetLogo" say? You can verify there that you're actually getting the heap size you asked for.