Initial proportion of three different patches with only two sliders - netlogo

i am trying to create a predation model in netlogo with foxes and rabbits. foxes eat rabbits and waste (left from people) und rabbits eat grass in a special pleasure ground.
the initial proportion of patches fixed in the setup should be: 65% green patches (grass), 35 % brown patches (eaten grass), 10 % margenta patches (waste from people). they can be distributed randomly.
at the moment it looks like that :
globals [grass waste]
breed [rabbits rabbit]
breed [foxes fox]
turtles-own [energy]
patches-own [countdown]
to setup
clear-all
ask patches [ set pcolor green ]
if grass? [
ask patches [
set pcolor one-of [green brown]
if-else pcolor = green
[ set countdown grass-regrowth-time ]
[ set countdown random grass-regrowth-time ]
]
]
ask patches [ set pcolor magenta ]
if waste? [
ask patches [
set pcolor one-of [ green brown magenta ]
if-else pcolor = [ green brown ]
[ set countdown waste-regeneration-time ]
[ set countdown random waste-regeneration-time ]
]
]
set-default-shape rabbits "rabbit"
create-rabbits initial-number-rabbits
[
set color white
set size 2.2
set label-color gray + 1
set energy random ( 2 * rabbit-gain-from-food)
setxy random-xcor random-ycor
]
set-default-shape foxes "fox"
create-foxes initial-number-foxes
[
set color red
set label-color gray + 1
set size 2.2
set energy random ( fox-gain-from-food )
setxy random-xcor random-ycor
]
display-labels
set grass count patches with [pcolor = green]
set waste count patches with [pcolor = magenta]
reset-ticks
end

Related

NETLOGO: Count patches outside of certain radius

I want to count patches of a specific color starting at a patch radius end and outward. There is the in-radius command, but it considers the space between a patch center and its radius, but I want this space to be excluded and start counting where the radius ends. Any help is appreciated.
Javier
How about a reporter that uses member? to return only a patch-set consisting only of patches that are not in-radius?
to setup
ca
ask n-of 10 patches [ set pcolor white ]
crt 1 [
setxy random-pxcor random-pycor
set color red
set size 2
]
reset-ticks
end
to go
ask turtles [
ask patches in-radius 5 with [ pcolor = black ] [ set pcolor black + 2 ]
print count ( patches-outside-radius 8 ) with [ pcolor = white ]
]
end
to-report patches-outside-radius [ radius ]
let inrad patches in-radius radius
report patches with [ not member? self inrad ]
end

Netlogo code problems, day/night cycle not working

I'm trying to crate a complex environment, where three types of trees co-exits and you can modify the numbers of each type using a slider. What I'm having problems implementing is a "day/night" cycle. When it is night the colors should turn darker, but with the code I have the colors get darker and never return to a brighter color. I used as a base the Algae model.
Here is my code:
to setup
clear-all
setup-world
reset-ticks
end
to setup-world
ask n-of synchronic-tree-density patches [
set pcolor blue
]
ask n-of asynchronious-tree-density patches [
set pcolor yellow
]
ask n-of tree-patches patches [
set pcolor green
]
recolor-world true
end
to recolor-world
ask patches [
if pcolor = blue [
ifelse setting-up? or daytime? [
set pcolor blue
] [
set pcolor blue - 3
]
]
if pcolor = yellow [
ifelse setting-up? or daytime? [
set pcolor yellow
] [
set pcolor yellow - 3
]
]
if pcolor = green [
ifelse setting-up? or daytime? [
set pcolor green
] [
set pcolor green - 3
]
]
]
end
to go
recolor-world false
tick-advance 1
end
to-report daytime?
report ticks mod 24 < day-length
end
Luke's answer addresses your issue. But you might additionally want to simplify the code somewhat to only assess the state of daytime? once each tick. For example:
to recolor-world
if-else daytime? [
ask patches with [ tree-type = 1 ] [ set pcolor blue ]
ask patches with [ tree-type = 2 ] [ set pcolor yellow ]
ask patches with [ tree-type = 3 ] [ set pcolor green]
] [
ask patches with [ tree-type = 1 ] [ set pcolor blue - 3 ]
ask patches with [ tree-type = 2 ] [ set pcolor yellow - 3 ]
ask patches with [ tree-type = 3 ] [ set pcolor green - 3 ]
]
end
welcome to Stack Overflow. Please have a look at the MCVE guidelines for some asking tips. Ideally your question code is pared down to just what is necessary for other users to run your program- the goal being that they can just copy your code as is. Right now, I can't run your program without a fair bit of modification- I'm not sure if my solution will apply to your setup. You are more likely to get useful answers if you simplify your code.
That said, I'm pretty sure your issue comes from that fact that all your if statements in your recolor-world procedure. Consider this one:
if pcolor = green [
ifelse daytime? [
set pcolor green
] [
set pcolor green - 3
]
]
So on the first go-around, you do have some green patches since they were set that way in the setup procedure. However, once daytime? becomes false, those patches run the command set pcolor green - 3 and so they no longer evaluate if pcolor = green as true- they will never run that code block again. I think the easiest fix is to use a grouping variable other than color for filtering:
patches-own [ tree-type ]
to setup
clear-all
reset-ticks
setup-world
end
to setup-world
ask n-of 50 patches [
set pcolor blue
set tree-type 1
]
ask n-of 50 patches [
set pcolor yellow
set tree-type 2
]
ask n-of 50 patches [
set pcolor green
set tree-type 3
]
end
to recolor-world
ask patches with [ tree-type = 1 ] [
ifelse daytime? [
set pcolor blue
] [
set pcolor blue - 3
]
]
ask patches with [ tree-type = 2 ] [
ifelse daytime? [
set pcolor yellow
] [
set pcolor yellow - 3
]
]
ask patches with [ tree-type = 3 ] [
ifelse daytime? [
set pcolor green
] [
set pcolor green - 3
]
]
end
to go
recolor-world
tick
end
to-report daytime?
report ticks mod 24 < 12
end
Edit
See #JenB's further improvement in her answer for a more efficient implementation.

How to stop turtles when all patches have been colored

When the turtles have covered the world in patches, I would like the turtles to stop on the last one so that I can record the amount of ticks it took.
Here is my code so far:
globals [marked-patches angle nextangle]
to setup ca ask patches [ set pcolor black ] crt turtle_amount
[set color red
set size 1
setxy (random 20) (random 20)] reset-ticks
end
to go ask turtles [
fd 1
rt random trt_ang
lt random trt_ang
if pcolor = black [set pcolor yellow] ]
tick end
In go, specifically in the turtle command, you can add:
to go
ask turtles [
fd 1
rt random trt_ang
lt random trt_ang
if pcolor = black [
set pcolor yellow
if count patches with [pcolor = black] = 0 [
stop
]
]
]
tick
end

Why won't any water squares spawn in on my netlogo simulation?

globals [grass]
;; 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 water? [
if grass? [
ask patches [
set pcolor one-of [green brown blue]
if-else pcolor =[green blue]
[ set countdown grass-regrowth-time ]
[ set countdown random grass-regrowth-time ] ;; initialize grass grow clocks randomly for brown patches
]
]
if grass? [
ask patches [
set pcolor one-of [green brown]
if-else pcolor = green
[ set countdown grass-regrowth-time ]
[ set countdown random grass-regrowth-time ] ;; initialize grass grow clocks randomly for brown patches
]
]
]
set-default-shape sheep "sheep"
create-sheep initial-number-sheep ;; create the sheep, then initialize their variables
[
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 initial-number-wolves ;; create the wolves, then initialize their variables
[
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]
;;set water count patches with [pcolor = blue]
reset-ticks
end
to go
if not any? turtles [ stop ]
ask sheep [
move
if water? [
if pcolor = blue [
set energy energy = 0
death ;;sheep drowns
]
]
if grass? [
set energy energy - 1 ;; deduct energy for sheep only if grass? switch is on
eat-grass
]
death
reproduce-sheep
]
ask wolves [
move
if water? [
if pcolor = blue [
set energy energy = 0
death
]
]
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
fd 1
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 [ rt random-float 360 fd 1 ] ;; hatch an offspring and move it 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 [ rt random-float 360 fd 1 ] ;; hatch an offspring and move it 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
but when I run this ( you have to place a on off switch for water? in the wolf sheep predation) it runs as if I made no changes at all. Why is this? How can I fix this?
anything related to water was added to the code by me.
You have two if statements inside the first if water? (in setup), and both respond to grass? So you do turn some patches blue, but then you execute the send if and turn all either brown or green.
Whenever you get confused, make smaller procedures. E.g.,
to color-patches
if grass? [
let patch-colors [green brown]
if water? [set patch-colors [green brown blue]]
ask patches [set pcolor one-of patch-colors]
]
end
One other problem, is that you don't want if pcolor = [green blue], which (i) will always be false and (ii) is trying to set a regrowth countdown on water patches.

How to setup a slider for pen size into the code in Netlogo

this is my code , i need to fit the slider so i edit the pen size my global variable is turtle-pen-size
to setup
clear-all
ask patches [ set pcolor sky ]
setup-turtles
end
to setup-turtles
create-turtles turtles-to-create
[ set color lime setxy random-xcor random-ycor set size size-of-turtle]
set-default-shape turtles "circle"
end
to go
ask turtles[
ifelse pen-down? [ pen-down ] [ pen-up ]
fd 1
]
end
You could set pen-size where you are asking each turtle to pen-down
I am not exactly sure what you want to do in your code, the pen-down? is not defined in your code, I assume you have a turtle property which defines a pen-down? to one of values of true and false, if you define it a global value I believe all of your turtles end up with same value, for pen-size you can use following code
set pen-size turtle-pen-size
This is your completed code:
turtles-own[pen-down?]
to setup
clear-all
reset-ticks
ask patches [ set pcolor sky ]
setup-turtles
end
to setup-turtles
create-turtles turtles-to-create
[
set color lime
setxy random-xcor random-ycor
set size size-of-turtle
set pen-size turtle-pen-size
set pen-down? one-of [true false]
]
set-default-shape turtles "circle"
end
to go
ask turtles[
ifelse pen-down?
[ pen-down ]
[ pen-up ]
fd 1
]
tick
end