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
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 am trying to code for a reverse reaction in biology - I have already done that by taking inspiration from a sample code pasted below. What I don't understand is how is the association rate constant (Kb in the code) and dissociation constant (Ku in the code) are affecting the forward and backward reactions in this code. Are they affecting the speed of movement of the products after they are formed? Are they affecting the speed with which they are formed? I want the Ku to affect affinity for substrates to form a product, and Ku to affect time to dissociate for products. The product is a complex formation of two substrates.
Also, why do they multiple Kb by 2? To slow the product complex down or speed it up? It should slow down due to increase in mass.
breed [reactants reactant] ;; reactants are green, products are red breed [products product]
to setup clear-all set-default-shape reactants "molecule1" set-default-shape products "molecule2" create-reactants number
[ set color green
setxy random-xcor random-ycor ] reset-ticks end
to go ask turtles
[ rt random-float 10 - random-float 10 ;; wander around randomly
fd 1 ] ask turtles
[ ifelse (breed = reactants)
[ react-forward ] ; reactants
[ react-backward ] ; products
] tick end
to react-forward if (any? other reactants-here) and
;; multiply Kb by 2 because 2 molecules are involved
random-float 1000 < (Kb * 2)
[ ask one-of other reactants-here
[ die ]
set breed products
set color red ] end
to react-backward if (random-float 1000) < Ku
[ set breed reactants ;; change back to reactant
set color green
;; then split into two reactants
hatch 1 [ set heading random 360 ] ] end
I am trying to simulate a robotic lawn mower on Netlogo. My first goal was it finds its way home to recharge itself when the battery is low.
This is now done (thanks Luke !) but I can't figure how to make the lawnmower stop when it reaches the house (it keeps moving 2 patches around). Therefore my energy slider goes infinitely below zero.
I first thought of adding an event "recharge" and placing it after "check death" in to go with a if instance. But :
How do I say if car goes on specific patch : stop and get energy ? Should I use a special patch color for the house and then code if car goes on blue patch..... ?
Could adding the recharge event at the end of the to go elements would possibly create a conflict because recharge only goes when on low battery ? Would putting it in check death be a solution ?
Then I would like it to go back to work after instant recharge.
Any ideas ?
Here's the code :
breed [cars car]
cars-own [target]
breed [houses house]
to setup
clear-all
setup-patches
setup-cars ;;represent lawn mower
setup-house
reset-ticks
end
to setup-patches
ask patches [set pcolor green] ;; Setup grass patches
ask patches with [
pxcor = max-pxcor or
pxcor = min-pxcor or
pycor = max-pycor or
pycor = min-pycor ] [
set pcolor red ;; Setup the borders of the garden
]
end
to setup-cars
create-cars 1 [
setxy 8 8
set target one-of houses
]
end
to setup-house
set-default-shape houses "house"
ask patch 7 8 [sprout-houses 1]
end
to place-walls
if mouse-down? [
ask patch mouse-xcor mouse-ycor [ set pcolor red ]
display
]
end
to go
move-cars
cut-grass
check-death ;; Check % battery.
tick
end
to move-cars
ask cars
[
ifelse [pcolor] of patch-ahead 1 = red
[ lt random-float 360 ] ;; see red patch ahead turn left.
[ fd 1 ] ;; otherwise it is ok to go.
set energy energy - 1
]
tick
end
to cut-grass
ask cars [
if pcolor = green [
set pcolor gray
]
]
end
to check-death ;; when low energy lawn mower will go back to house
ask cars [
ifelse energy >= 150
[ set label "Energy ok" ]
[ set label "Low Energy, returning to base"
set target min-one-of houses [distance myself]
face target
ifelse distance target < 1
[ move-to target ]
[ fd 1 ]
]
]
end
You can use a logical flag (eg charging?) in conjunction with a counter (eg charge-time) to do this. Try modifying your cars-own definitions like this:
cars-own [target charging? charge-time]
and setup-cars like this:
to setup-cars
create-cars 1 [
setxy 8 8
set target one-of houses
set charging? false
]
end
Then you can have the mower do different things based on whether charging? is true or false. Try modifying your move-cars and check-death to accommodate these changes (side note- you've got a tick in both go and move-cars):
to move-cars
ask cars [
ifelse charging? [
set charge-time charge-time + 1
if charge-time > 14 [
set energy 200
set charging? false
set charge-time 0
]
] [
ifelse [pcolor] of patch-ahead 1 = red
[ lt random-float 360 ] ;; see red patch ahead turn left.
[ fd 1 ] ;; otherwise it is ok to go.
set energy energy - 1
]
]
end
to check-death ;; when low energy lawn mower will go back to house
ask cars [
ifelse energy >= 150
[ set label "Energy ok" ]
[ set label "Low Energy, returning to base"
set target min-one-of houses [distance myself]
face target
ifelse distance target < 1
[ move-to target
set charging? true
]
[ fd 1 ]
]
]
end
I suggest you change energy a turtle variable like car-energy so that the turtles values are independent of your slider. That would mean you could have multiple mowers and they could reset their energy level to the slider setting once they are charged! You would just include a line like
set car-energy energy
in your setup-cars (and then modify so that energy in your car procedures is instead car-energy).
I'm modeling territory selection in NetLogo, where turtles pick a territory center ("start-patch") and then build a territory out from there based on value of patches. Turtles always return to the start-patch after claiming a new patch, then choose, move to, and claim the next most valuable patch. After choosing a territory, the turtle knows which patches it owns, and patches know their owner.
Ultimately, the start-patch of a territory may not actually end up being the true geographic center. After a turtle has selected its territory, how might I ask it to evaluate the territory, identify the geographic center, and calculate proximity of the start-patch to the true center of the territory? (Note: I don't want to force turtles to keep the start-patch in the geographic center--they are free to choose any patches they want. But I may force turtles to re-select a territory if there isn't a close match--these territories are not very efficient otherwise.)
Here's an example of how territory start-patches (black stars) do not equal geographic centers. Some example code is below. Any suggestions? Thanks in advance!
patches-own [
benefit ;; ranges 0.1-1 and represents food available in the patch
owner ] ;; patches are owned once selected for a territory
turtles-own [
start-patch ;; the selected center of the territory
sum-value ;; sum of values of patches in my territory
territory-list ;; list of patches I've selected
territory ;; agentset of patches I've selected
established ] ;; true/false, true when has settled in a final territory after assessing geographic center
globals [threshold = 25]
to setup
ask patches [ set owner nobody ]
end
to go
ask turtles [
pick-center
build-territory]
tick
end
to pick-center
if start-patch = 0
[move-to best-center ;; (calculated by a reporter elsewhere as moving windows for a cluster of high-benefit patches)
set start-patch patch-here
set owner self
set territory-list (list patch-here)
set territory (patches with [owner = myself])
]
to build-territory
ifelse sum-value < threshold
[ pick-patch ] ;; keeps picking patches for territory until I've met my threshold
[ assess-geographic-center] ;; once met threshold, assess real center of patch-set
end
to pick-patch
let _destination highest-value ;; (this is calculated by reporters elsewhere based on benefit / travel costs to a patch)
face _destination forward 1
if patch-here = _destination
[ claim-patch _destination ]
end
to claim-patch [_patch]
ask _patch [set owner myself]
set sum-value sum-value + (benefit / (distance start-patch))
set territory-list lput patch-here territory-list
set territory (patch-set territory _patch)
move-to start-patch
end
to assess-geographic-center
;; Once the territory is built, the turtle should identify the actual
;; geographic center of the patch-set it has selected...how to do this?
;; Once it knows the center, the turtle should compare the distance to the original start-patch.
;; If >10 patches away, it will move to that start-patch and start over with selecting a territory....
end
Could you get away with just the mean pxcor and pycor of all patches? Something like:
to setup
ca
reset-ticks
let n 70
ask one-of patches [
set pcolor red
]
while [ ( count patches with [pcolor = red ] ) < n ] [
ask one-of patches with [ pcolor = red ] [
ask one-of neighbors4 [set pcolor red ]
]
]
let xmean mean [pxcor] of patches with [ pcolor = red ]
print xmean
let ymean mean [pycor] of patches with [ pcolor = red ]
print ymean
ask patch xmean ymean [ set pcolor blue
]
end
Following the previous answer, here is what I came up with. This goes in the last procedure in my original code:
to assess-geographic-center
let xmean mean [pxcor] of patches with [ owner = myself ]
let ymean mean [pycor] of patches with [ owner = myself ]
ask patch xmean ymean [ set pcolor black ] ;; make the geographic center visible
let geographic-center patch xmean ymean
let distance-away distance geographic-center ;; the turtle is on its start-patch when assessing this distance
ifelse distance-away <= 5
[ set established true ] ;; turtle is happy if start-patch and geographic-center are approximately equal, territory "established"
[ move-to geographic-center ;; otherwise, turtle moves to geographic-center,
reposition ] ;; and follows a procedure "reposition" to makes this the new start-patch and repick the territory
end
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.