I am trying to build a model where turtle move one patch per tick with random movement. I am looking for a solution to increase the number of turtle per tick based on the percentage. For ex. In the beginning there are 7 turtles, by each tick they should increase by following percentage:
10.72%
10.83%
10.93%
11.03%
11.11%
11.19%
11.27%
11.33%
11.39%
11.45%
Not sure if this is possible? If needed this can be round up to whole number.
If this is not possible, how can I increase the turtle number by 11% each tick for 10 ticks then after 12% each tick for another 10 ticks and so on?
Below is the code that I am using.
to setup
clear-all
setup-turtles
setup-patches
reset-ticks
end
to setup-patches
ask patches [ set pcolor green ]
end
to setup-turtles
create-turtles tourists [setxy random-xcor random-ycor ]
ask turtles [ set shape "person" set size 2 ]
end
to go
if ticks >= 130 [ stop ]
move-turtles
eat
tick
end
to move-turtles
ask turtles [ right random 360 forward 1]
end
to eat
ask turtles [ if pcolor = green [ set pcolor black ] ]
end
Thank you for your support.
Avi
I think your model is very similar to 'Rabbits Grass Weeds' in the Models Library so you might want to have a look at that to get some ideas. Focusing just on the revised question of moving to a green patch, you need with, which will restrict choices. Note that the code below will break if there are no neighbouring patches that are green.
to setup
clear-all
setup-turtles
setup-patches
reset-ticks
end
to setup-patches
ask patches [ set pcolor green ]
end
to setup-turtles
create-turtles 7 [setxy random-xcor random-ycor ]
ask turtles [ set shape "person" set size 2 ]
end
to go
if ticks >= 130 [ stop ]
move-turtles
increase-turtles
tick
end
to move-turtles
ask turtles [ move-to one-of neighbors with [pcolor = green] ]
end
to increase-turtles
ask one-of turtles [ hatch 1 ]
end
I used hatch in this code, which gets one turtle to create another turtle at the same place with the same colour etc. If you want to just create a new turtle in exactly the same way as your original turtles, then you want something more like this.
to setup
clear-all
make-turtles 7
setup-patches
reset-ticks
end
to setup-patches
ask patches [ set pcolor green ]
end
to make-turtles [ num ]
create-turtles num
[ setxy random-xcor random-ycor
set shape "person"
set size 2
]
end
to go
if ticks >= 130 [ stop ]
move-turtles
increase-turtles
tick
end
to move-turtles
ask turtles [ move-to one-of neighbors with [pcolor = green] ]
end
to increase-turtles
make-turtles 1
end
In this case, I have made a new procedure (called make-turtles) that creates as many turtles as you specify, randomly locates them etc. In setup, I call it to make 7 turtles, and then later on just make 1 each time.
Related
Hi, this is a continuation of my previous question. Basically I'm
trying to differentiate the patches by its income level and once I
have established this I will set a monitor to count them. However,
when I run the model the patches are not updating. Where am I going
wrong? Appreciate your assistance.
turtles-own [wealth]
patches-own [income]
to setup
ca
setup-turtles
setup-patches
reset-ticks
end
to setup-turtles
create-turtles num-turtles
ask turtles
[
set shape "person"
set size 1
setxy random-xcor random-ycor
set wealth 100
]
end
to setup-patches
ask n-of 2000 patches [ set pcolor green ] ;; to identify patches that will accumulate income from turtles
end
to go
if not any? turtles with [wealth > 0] [stop]
move-turtles
spend
tick
end
to move-turtles ;; turtles to stop once they are spend all their wealth
ask turtles [
ifelse wealth > 0
[rt random 360 forward 1]
[stop]
]
end
to color-patches ;; patch colors to change based on their income level
ask patches [
ifelse income > 0 and income < 100 [
set pcolor 15
] [
ifelse income > 100 and income < 200 [
set pcolor 45
] [
ifelse income > 200 [
set pcolor 64
] [
set pcolor green
]
]
]
]
end
to spend
ask turtles with [wealth > 0] [
if pcolor = green [
set wealth wealth - 1
set income income + 1
]
]
end
The short answer is that you created the procedure to update patch colours but you never told NetLogo to run it. You probably want to add a line to your go procedure, I think this order:
to go
if not any? turtles with [wealth > 0] [stop]
move-turtles
spend
color-patches ; this is what you are missing
tick
end
This is not relevant to your question, but I also noticed that you have this code for moving:
to move-turtles ;; turtles to stop once they are spend all their wealth
ask turtles [
ifelse wealth > 0
[rt random 360 forward 1]
[stop]
]
end
You don't need to ask every turtle and then use stop to exit for some of them, instead it is generally easier to filter the turtles using with. So that code could be replaced with:
to move-turtles
ask turtles with [wealth > 0] [
rt random 360
forward 1
]
end
I'm trying to get a turtle to stop if they run out of energy as if they have died. when they have eaten the grass, however keep getting an error when attempting to use an if statement.
turtles-own [energy]
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to setup-patches
ask patches [ set pcolor green ]
end
to setup-turtles
create-turtles 100
ask turtles [ setxy random-xcor random-ycor ]
set energy energy + 10
end
to go
move-turtles
eat-grass
tick
end
to move-turtles
ask turtles [
if energy energy = 0 [ stop ]
right random 360
forward 1
set energy energy - 1
]
end
to eat-grass
ask turtles [
if pcolor = green [
set pcolor black
set energy energy + 10
]
]
end
you have repeated the variable name energy, just replace if energy energy = 0 with if energy = 0
I am building a model population within a bioreactor, building on a basic tutorial telling how to eat, reproduce, and die, etc. While tinkering, my turtles stopped walking. I suspect it has to do with how to ask the different breeds to do things?
Edit: For some reason my contaminants aren't "wheels" either
turtles-own [ energy ]
patches-own [ nutrition ]
breed [ Xanthomonas Xanthomona ]
breed [ contaminants contaminant ]
globals
[ xanthan biomass ]
to setup
clear-all
setup-patches
set-default-shape Xanthomonas "bug"
set-default-shape contaminants "wheel"
crt num-Xanthomonas
[set color yellow
set energy 10
setxy random-xcor random-ycor
]
foul
determine-biomass
reset-ticks
;; begins defining a procedure named "setup"
;; resets the world to an initial, empty state
;; creates 100 turtles, they start out standing at the origin 0,0
;; set default shape so I don't have to tell it every time
;; A turtle's color variable is random until specified
;; setxy command with next two numbers as inputs
;; chooses random reporters for allowable x and y coordinates
End
to setup-patches
ask patches
[ set pcolor green
set nutrition 50
]
;; every patch starts with 50 nutrition, the color indicates it for us
end
to foul
set-default-shape contaminants "wheel"
if Contamination?
[ crt num-contaminants
[ set color red
setxy random-xcor random-ycor
]
compete ]
end
to go
if ticks >= 2000 [ stop ]
if count turtles > 2000 [stop]
if count turtles = 0 [stop]
feed
move-turtles
ask turtles
[eat-glucose]
ask Xanthomonas
[reproduce]
check-death
tick
end
to determine-biomass
ifelse Contamination?
[set biomass num-Xanthomonas + num-contaminants
]
[set biomass num-Xanthomonas ]
end
to move-turtles
;; says that each turtle should run the commands in the brackets
;; random doesn't include the number you give it as a possible result
;; uses a reporter, each turtle picks a random whole number between 0 and 359
;; makes the turtle move forward one step
;;specify what you're defining will lose 1 energy per step
ask Xanthomonas
[ right random 360
forward 1
set energy energy - 1 ]
end
to Feed
if Continuous-feed?
[ ask patches
[if random 100 < 3
[set pcolor green
set nutrition nutrition + 50
] ] ]
end
to eat-glucose
ask Xanthomonas
[ if pcolor = green
[ set energy energy + 10
set nutrition nutrition - 50
set pcolor gray
set biomass biomass + 1
] ]
ifelse show-energy?
[ set label energy ]
[ set label "" ]
;;ask turtles before "if"
;;if when true, and only then will the turtle run the commands inside the brackets (otherwise it skips them)
;;true/false questions, if true, will do first set of brackets
;;false means turtle runs commands in second set of bracket
;;energy (elements) will default to zero
end
to reproduce
ask Xanthomonas
[ if energy > 50
[set energy energy - 50
set xanthan xanthan + 1
hatch-Xanthomonas 1
[set biomass biomass + 1
rt random-float 360 fd 1
] ] ]
end
to check-death
ask Xanthomonas
[ if energy < 0
[ die ]
]
end
to reinoculate
ask patches [
if random 100 < 10
[ set pcolor green
]
]
end
to Contaminate
crt num-contaminants
[ set color red
setxy random-xcor random-ycor
]
end
to compete
ask contaminants
[ if energy > 50
[set energy energy - 50
set xanthan xanthan + 1
hatch-contaminants 1
[ set color red
set biomass biomass + 1
rt random-float 360 fd 1
] ] ]
end
okay, your basic problem is the crt command. This is short for create-turtles. You have two breeds here and you have defined them. However, when you create the turtles, you are not telling NetLogo which breed to create.
What this means is that you need to make a couple of minor changes to specify the breed. If you look in the dictionary, you will see the command is create-<breed>. Where you have crt num-Xanthomonas, change it to create-Xanthomonas num-Xanthomonas and similarly where you create the contaminants.
turtles-own [wages]
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to go
move-turtles
get-employed
tick
end
to move-turtles
ask turtles [
right random 360
forward 1
]
end
to get-employed
ask turtles [
if pcolor = blue [
set color green
set wages wages + 10
]
ifelse show-wages?
[ set label wages ]
[ set label " " ]
]
end
to setup-patches
ask patches [set pcolor pink ]
patch 0 0 [ set pcolor blue ]
end
to setup-turtles
create-turtles 80
ask turtles [ setxy random-xcor random-ycor ]
ask turtles [ set color red]
end
I want to add the code to select the first 20 percent of the 80 turtles who comes in contact with the patch having color blue.
In your question is not clear how you will use the first 20% of turtles who get the blue patch so i assume you just want to store them in order to use this information later.
I would add a turtles-own called is-first-20-percent? set to false for every turtle.
Then, at the end of the go procedure, before the tick i would execute the check-20 procedure as following:
to check-20
if count turtles with [color = green] = (count turtles * 20 / 100) [
ask turtles with [color = green] [set is-first-20-percent? true]
]
end
In every moment you can retrieve the first 20% of turtles who reached the blu zone with the command:
ask turtles with [is-first-20-percent?] [ ... do something ... ]
This code is working because 20% of 80 is an integer number (16) but if you plan to modify the starting number of turtles i suggest to modify the check-20 procedure as following:
to check-20
if (count turtles with [color = green] >= (count turtles * 20 / 100)
and count turtles with [is-first-20-percent?] = 0) [
ask turtles with [color = green] [set is-first-20-percent? true]
]
end
I created 1000 turtles in NetLogo which move randomly, but I want only thirty percent of them to move forward one step and the other seventy percent to move forward five steps.
to setup
ca
setup-turtles
setup-patches
reset-ticks
end
to go
move-turtles
tick
end
to setup-patches
ask patches [set pcolor brown]
end
to setup-turtles
create-turtles number
ask turtles [set shape "person" set size 1]
ask turtles [setxy random-xcor random-ycor] ;; posicionar las personas en un punto al azar
end
to move-turtles
ask turtles [
right random 360]
end
One simple and fast way to approach this would be to do it probabilistically:
ask turtles [ fd ifelse-value (random 100 <= 30) [ 1 ] [ 5 ] ]
...so that each turtle would move one step with a probability of 30% and five steps with a probability of 70%. But it wouldn't guarantee that exactly 30% of your turtles move one step. It would just average to 30% in the long run.
If you want exact numbers, one way to do it would be:
let small-movers n-of (count turtles * 0.3) turtles
let big-movers turtles with [ not member? self small-movers ]
ask small-movers [ fd 1 ]
ask big-movers [ fd 5 ]
But this will be slowish because of the member? check.
A much faster way would be to use a turtle variable. Assuming you have:
turtles-own [ step-size ]
Then you can do:
ask turtles [ set step-size 5 ]
ask n-of (count turtles * 0.3) turtles [ set step-size 1 ]
ask turtles [ fd step-size ]